diff --git a/mcp/client.go b/mcp/client.go index f8150e92..fb77b3f3 100644 --- a/mcp/client.go +++ b/mcp/client.go @@ -283,7 +283,7 @@ func (c *Client) listRoots(_ context.Context, req *ListRootsRequest) (*ListRoots func (c *Client) createMessage(ctx context.Context, req *CreateMessageRequest) (*CreateMessageResult, error) { if c.opts.CreateMessageHandler == nil { // TODO: wrap or annotate this error? Pick a standard code? - return nil, jsonrpc2.NewError(CodeUnsupportedMethod, "client does not support CreateMessage") + return nil, jsonrpc2.NewError(codeUnsupportedMethod, "client does not support CreateMessage") } return c.opts.CreateMessageHandler(ctx, req) } @@ -291,13 +291,13 @@ func (c *Client) createMessage(ctx context.Context, req *CreateMessageRequest) ( func (c *Client) elicit(ctx context.Context, req *ElicitRequest) (*ElicitResult, error) { if c.opts.ElicitationHandler == nil { // TODO: wrap or annotate this error? Pick a standard code? - return nil, jsonrpc2.NewError(CodeUnsupportedMethod, "client does not support elicitation") + return nil, jsonrpc2.NewError(codeUnsupportedMethod, "client does not support elicitation") } // Validate that the requested schema only contains top-level properties without nesting schema, err := validateElicitSchema(req.Params.RequestedSchema) if err != nil { - return nil, jsonrpc2.NewError(CodeInvalidParams, err.Error()) + return nil, jsonrpc2.NewError(codeInvalidParams, err.Error()) } res, err := c.opts.ElicitationHandler(ctx, req) @@ -312,11 +312,11 @@ func (c *Client) elicit(ctx context.Context, req *ElicitRequest) (*ElicitResult, // this code to the server? resolved, err := schema.Resolve(nil) if err != nil { - return nil, jsonrpc2.NewError(CodeInvalidParams, fmt.Sprintf("failed to resolve requested schema: %v", err)) + return nil, jsonrpc2.NewError(codeInvalidParams, fmt.Sprintf("failed to resolve requested schema: %v", err)) } if err := resolved.Validate(res.Content); err != nil { - return nil, jsonrpc2.NewError(CodeInvalidParams, fmt.Sprintf("elicitation result content does not match requested schema: %v", err)) + return nil, jsonrpc2.NewError(codeInvalidParams, fmt.Sprintf("elicitation result content does not match requested schema: %v", err)) } } diff --git a/mcp/mcp_test.go b/mcp/mcp_test.go index 9cc105cd..0e1ee1f2 100644 --- a/mcp/mcp_test.go +++ b/mcp/mcp_test.go @@ -322,7 +322,7 @@ func TestEndToEnd(t *testing.T) { } { rres, err := cs.ReadResource(ctx, &ReadResourceParams{URI: tt.uri}) if err != nil { - if code := errorCode(err); code == CodeResourceNotFound { + if code := errorCode(err); code == codeResourceNotFound { if tt.mimeType != "" { t.Errorf("%s: not found but expected it to be", tt.uri) } @@ -994,8 +994,8 @@ func TestElicitationUnsupportedMethod(t *testing.T) { if err == nil { t.Error("expected error when ElicitationHandler is not provided, got nil") } - if code := errorCode(err); code != CodeUnsupportedMethod { - t.Errorf("got error code %d, want %d (CodeUnsupportedMethod)", code, CodeUnsupportedMethod) + if code := errorCode(err); code != codeUnsupportedMethod { + t.Errorf("got error code %d, want %d (CodeUnsupportedMethod)", code, codeUnsupportedMethod) } if !strings.Contains(err.Error(), "does not support elicitation") { t.Errorf("error should mention unsupported elicitation, got: %v", err) @@ -1342,8 +1342,8 @@ func TestElicitationSchemaValidation(t *testing.T) { t.Errorf("expected error for invalid schema %q, got nil", tc.name) return } - if code := errorCode(err); code != CodeInvalidParams { - t.Errorf("got error code %d, want %d (CodeInvalidParams)", code, CodeInvalidParams) + if code := errorCode(err); code != codeInvalidParams { + t.Errorf("got error code %d, want %d (CodeInvalidParams)", code, codeInvalidParams) } if !strings.Contains(err.Error(), tc.expectedError) { t.Errorf("error message %q does not contain expected text %q", err.Error(), tc.expectedError) @@ -1462,8 +1462,8 @@ func TestElicitationCapabilityDeclaration(t *testing.T) { if err == nil { t.Error("expected UnsupportedMethod error when no capability declared") } - if code := errorCode(err); code != CodeUnsupportedMethod { - t.Errorf("got error code %d, want %d (CodeUnsupportedMethod)", code, CodeUnsupportedMethod) + if code := errorCode(err); code != codeUnsupportedMethod { + t.Errorf("got error code %d, want %d (CodeUnsupportedMethod)", code, codeUnsupportedMethod) } }) } diff --git a/mcp/resource.go b/mcp/resource.go index 0658c661..8746edae 100644 --- a/mcp/resource.go +++ b/mcp/resource.go @@ -41,7 +41,7 @@ type ResourceHandler func(context.Context, *ReadResourceRequest) (*ReadResourceR // not be found. func ResourceNotFoundError(uri string) error { return &jsonrpc2.WireError{ - Code: CodeResourceNotFound, + Code: codeResourceNotFound, Message: "Resource not found", Data: json.RawMessage(fmt.Sprintf(`{"uri":%q}`, uri)), } diff --git a/mcp/server.go b/mcp/server.go index b74494bd..c80b7d9f 100644 --- a/mcp/server.go +++ b/mcp/server.go @@ -526,7 +526,7 @@ func (s *Server) getPrompt(ctx context.Context, req *GetPromptRequest) (*GetProm if !ok { // Return a proper JSON-RPC error with the correct error code return nil, &jsonrpc2.WireError{ - Code: CodeInvalidParams, + Code: codeInvalidParams, Message: fmt.Sprintf("unknown prompt %q", req.Params.Name), } } @@ -553,7 +553,7 @@ func (s *Server) callTool(ctx context.Context, req *CallToolRequest) (*CallToolR s.mu.Unlock() if !ok { return nil, &jsonrpc2.WireError{ - Code: CodeInvalidParams, + Code: codeInvalidParams, Message: fmt.Sprintf("unknown tool %q", req.Params.Name), } } diff --git a/mcp/shared.go b/mcp/shared.go index 46e994f6..e90bcbd8 100644 --- a/mcp/shared.go +++ b/mcp/shared.go @@ -331,13 +331,11 @@ func clientSessionMethod[P Params, R Result](f func(*ClientSession, context.Cont // Error codes const ( - // TODO: should these be unexported? - - CodeResourceNotFound = -32002 + codeResourceNotFound = -32002 // The error code if the method exists and was called properly, but the peer does not support it. - CodeUnsupportedMethod = -31001 + codeUnsupportedMethod = -31001 // The error code for invalid parameters - CodeInvalidParams = -32602 + codeInvalidParams = -32602 ) // notifySessions calls Notify on all the sessions. diff --git a/mcp/tool_test.go b/mcp/tool_test.go index ef26e9dc..c440fcc5 100644 --- a/mcp/tool_test.go +++ b/mcp/tool_test.go @@ -66,7 +66,7 @@ func TestToolErrorHandling(t *testing.T) { // Create a tool that returns a structured error structuredErrorHandler := func(ctx context.Context, req *CallToolRequest, args map[string]any) (*CallToolResult, any, error) { return nil, nil, &jsonrpc2.WireError{ - Code: CodeInvalidParams, + Code: codeInvalidParams, Message: "internal server error", } } @@ -111,8 +111,8 @@ func TestToolErrorHandling(t *testing.T) { t.Fatalf("expected WireError, got %[1]T: %[1]v", err) } - if wireErr.Code != CodeInvalidParams { - t.Errorf("expected error code %d, got %d", CodeInvalidParams, wireErr.Code) + if wireErr.Code != codeInvalidParams { + t.Errorf("expected error code %d, got %d", codeInvalidParams, wireErr.Code) } })