Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions mcp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,21 +283,21 @@ 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)
}

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)
Expand All @@ -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))
}
}

Expand Down
14 changes: 7 additions & 7 deletions mcp/mcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion mcp/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}
Expand Down
4 changes: 2 additions & 2 deletions mcp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand All @@ -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),
}
}
Expand Down
8 changes: 3 additions & 5 deletions mcp/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions mcp/tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
}
Expand Down Expand Up @@ -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)
}
})

Expand Down