mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-08-03 15:49:23 +02:00
fix: accept null tool arguments and bound HTTP resource use
Review follow-ups on the SDK migration. An "arguments": null is what clients send for parameterless tools like get_me, and what mcp-go accepted by returning a nil map. The new adapter rejected it with InvalidParams, which broke those calls outright. The /mcp endpoint took unlimited request bodies and never expired idle sessions, so a peer that goes away without DELETE kept its session for the process lifetime. Both are reachable before any token check, so neither can stay unbounded; the body cap sits above the SDK default to leave room for the base64 content create_or_update_file accepts. Required() smuggled a bool through the property schema map and deleted it again, colliding with the JSON Schema keyword of the same name. It now sets a field on Property, so an object property can carry its own required list. The tool contract fixture cost a manual regeneration step and four hand-maintained counts on every tool change, and a snapshot freezes defects rather than reporting them. Property assertions cover the same surface and reject a duplicate tool name, a readOnlyHint that disagrees with the register call, and a default that contradicts its own type or enum. Co-Authored-By: Claude (Opus 5) <noreply@anthropic.com>
This commit is contained in:
+53
-48
@@ -10,19 +10,23 @@ import (
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
)
|
||||
|
||||
func callTool(handler Handler, arguments json.RawMessage) (*mcp.CallToolResult, error) {
|
||||
serverTool := ServerTool{Tool: &mcp.Tool{Name: "example"}, Handler: handler}
|
||||
return serverTool.MCPHandler()(context.Background(), &mcp.CallToolRequest{
|
||||
Params: &mcp.CallToolParamsRaw{Arguments: arguments},
|
||||
})
|
||||
}
|
||||
|
||||
func captureArguments(into *map[string]any) Handler {
|
||||
return func(_ context.Context, arguments map[string]any) (*mcp.CallToolResult, error) {
|
||||
*into = arguments
|
||||
return &mcp.CallToolResult{}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func TestMCPHandler(t *testing.T) {
|
||||
var got map[string]any
|
||||
serverTool := ServerTool{
|
||||
Tool: &mcp.Tool{Name: "example"},
|
||||
Handler: func(_ context.Context, arguments map[string]any) (*mcp.CallToolResult, error) {
|
||||
got = arguments
|
||||
return &mcp.CallToolResult{}, nil
|
||||
},
|
||||
}
|
||||
|
||||
result, err := serverTool.MCPHandler()(context.Background(), &mcp.CallToolRequest{
|
||||
Params: &mcp.CallToolParamsRaw{Arguments: json.RawMessage(`{"count":2,"nested":{"enabled":true}}`)},
|
||||
})
|
||||
result, err := callTool(captureArguments(&got), json.RawMessage(`{"count":2,"nested":{"enabled":true}}`))
|
||||
if err != nil {
|
||||
t.Fatalf("MCPHandler() error = %v", err)
|
||||
}
|
||||
@@ -36,18 +40,13 @@ func TestMCPHandler(t *testing.T) {
|
||||
|
||||
func TestMCPHandlerRejectsInvalidArguments(t *testing.T) {
|
||||
called := false
|
||||
serverTool := ServerTool{
|
||||
Tool: &mcp.Tool{Name: "example"},
|
||||
Handler: func(context.Context, map[string]any) (*mcp.CallToolResult, error) {
|
||||
called = true
|
||||
return &mcp.CallToolResult{}, nil
|
||||
},
|
||||
handler := func(context.Context, map[string]any) (*mcp.CallToolResult, error) {
|
||||
called = true
|
||||
return &mcp.CallToolResult{}, nil
|
||||
}
|
||||
|
||||
for _, arguments := range []json.RawMessage{json.RawMessage(`[]`), json.RawMessage(`null`), json.RawMessage(`{"broken"`)} {
|
||||
_, err := serverTool.MCPHandler()(context.Background(), &mcp.CallToolRequest{
|
||||
Params: &mcp.CallToolParamsRaw{Arguments: arguments},
|
||||
})
|
||||
for _, arguments := range []json.RawMessage{json.RawMessage(`[]`), json.RawMessage(`"text"`), json.RawMessage(`{"broken"`)} {
|
||||
_, err := callTool(handler, arguments)
|
||||
assertProtocolErrorCode(t, err, jsonrpc.CodeInvalidParams)
|
||||
}
|
||||
if called {
|
||||
@@ -55,37 +54,43 @@ func TestMCPHandlerRejectsInvalidArguments(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Tools without parameters are callable with an omitted or null "arguments",
|
||||
// which is what clients send and what mcp-go accepted before the SDK migration.
|
||||
func TestMCPHandlerAcceptsAbsentArguments(t *testing.T) {
|
||||
for _, arguments := range []json.RawMessage{nil, json.RawMessage(`null`)} {
|
||||
var got map[string]any
|
||||
if _, err := callTool(captureArguments(&got), arguments); err != nil {
|
||||
t.Fatalf("MCPHandler() with arguments %s error = %v", arguments, err)
|
||||
}
|
||||
if got == nil || len(got) != 0 {
|
||||
t.Errorf("arguments = %#v, want an empty map", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMCPHandlerConvertsErrorsAndRecoversPanics(t *testing.T) {
|
||||
t.Run("handler error", func(t *testing.T) {
|
||||
serverTool := ServerTool{
|
||||
Tool: &mcp.Tool{Name: "example"},
|
||||
Handler: func(context.Context, map[string]any) (*mcp.CallToolResult, error) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
handler Handler
|
||||
}{
|
||||
{
|
||||
name: "handler error",
|
||||
handler: func(context.Context, map[string]any) (*mcp.CallToolResult, error) {
|
||||
return nil, errors.New("failed")
|
||||
},
|
||||
}
|
||||
_, err := serverTool.MCPHandler()(context.Background(), &mcp.CallToolRequest{Params: &mcp.CallToolParamsRaw{}})
|
||||
assertProtocolErrorCode(t, err, jsonrpc.CodeInternalError)
|
||||
})
|
||||
|
||||
t.Run("panic", func(t *testing.T) {
|
||||
calls := 0
|
||||
serverTool := ServerTool{
|
||||
Tool: &mcp.Tool{Name: "example"},
|
||||
Handler: func(context.Context, map[string]any) (*mcp.CallToolResult, error) {
|
||||
calls++
|
||||
if calls == 1 {
|
||||
panic("failed")
|
||||
}
|
||||
return &mcp.CallToolResult{}, nil
|
||||
},
|
||||
{
|
||||
name: "panic",
|
||||
handler: func(context.Context, map[string]any) (*mcp.CallToolResult, error) {
|
||||
panic("failed")
|
||||
},
|
||||
}
|
||||
handler := serverTool.MCPHandler()
|
||||
_, err := handler(context.Background(), &mcp.CallToolRequest{Params: &mcp.CallToolParamsRaw{}})
|
||||
assertProtocolErrorCode(t, err, jsonrpc.CodeInternalError)
|
||||
if _, err := handler(context.Background(), &mcp.CallToolRequest{Params: &mcp.CallToolParamsRaw{}}); err != nil {
|
||||
t.Fatalf("second handler call after panic error = %v", err)
|
||||
}
|
||||
})
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
_, err := callTool(test.handler, nil)
|
||||
assertProtocolErrorCode(t, err, jsonrpc.CodeInternalError)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func assertProtocolErrorCode(t *testing.T, err error, want int64) {
|
||||
|
||||
Reference in New Issue
Block a user