mcp: unexport incidentally exported error codes (#536)

Unexport error codes that were accidentally exported. I don't think it's
possible that they could be useful for users (see #535).

Fixes #535
This commit is contained in:
Robert Findley 2025-09-26 14:04:28 -04:00 • committed by GitHub
commit b2048cd73a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 21 additions and 23 deletions

View file

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

View file

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

View file

@ -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)),
}

View file

@ -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),
}
}

View file

@ -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.

View file

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