Skip to content

Commit b2048cd

Browse files
authored
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
1 parent 964574b commit b2048cd

File tree

6 files changed

+21
-23
lines changed

6 files changed

+21
-23
lines changed

mcp/client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,21 +283,21 @@ func (c *Client) listRoots(_ context.Context, req *ListRootsRequest) (*ListRoots
283283
func (c *Client) createMessage(ctx context.Context, req *CreateMessageRequest) (*CreateMessageResult, error) {
284284
if c.opts.CreateMessageHandler == nil {
285285
// TODO: wrap or annotate this error? Pick a standard code?
286-
return nil, jsonrpc2.NewError(CodeUnsupportedMethod, "client does not support CreateMessage")
286+
return nil, jsonrpc2.NewError(codeUnsupportedMethod, "client does not support CreateMessage")
287287
}
288288
return c.opts.CreateMessageHandler(ctx, req)
289289
}
290290

291291
func (c *Client) elicit(ctx context.Context, req *ElicitRequest) (*ElicitResult, error) {
292292
if c.opts.ElicitationHandler == nil {
293293
// TODO: wrap or annotate this error? Pick a standard code?
294-
return nil, jsonrpc2.NewError(CodeUnsupportedMethod, "client does not support elicitation")
294+
return nil, jsonrpc2.NewError(codeUnsupportedMethod, "client does not support elicitation")
295295
}
296296

297297
// Validate that the requested schema only contains top-level properties without nesting
298298
schema, err := validateElicitSchema(req.Params.RequestedSchema)
299299
if err != nil {
300-
return nil, jsonrpc2.NewError(CodeInvalidParams, err.Error())
300+
return nil, jsonrpc2.NewError(codeInvalidParams, err.Error())
301301
}
302302

303303
res, err := c.opts.ElicitationHandler(ctx, req)
@@ -312,11 +312,11 @@ func (c *Client) elicit(ctx context.Context, req *ElicitRequest) (*ElicitResult,
312312
// this code to the server?
313313
resolved, err := schema.Resolve(nil)
314314
if err != nil {
315-
return nil, jsonrpc2.NewError(CodeInvalidParams, fmt.Sprintf("failed to resolve requested schema: %v", err))
315+
return nil, jsonrpc2.NewError(codeInvalidParams, fmt.Sprintf("failed to resolve requested schema: %v", err))
316316
}
317317

318318
if err := resolved.Validate(res.Content); err != nil {
319-
return nil, jsonrpc2.NewError(CodeInvalidParams, fmt.Sprintf("elicitation result content does not match requested schema: %v", err))
319+
return nil, jsonrpc2.NewError(codeInvalidParams, fmt.Sprintf("elicitation result content does not match requested schema: %v", err))
320320
}
321321
}
322322

mcp/mcp_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func TestEndToEnd(t *testing.T) {
322322
} {
323323
rres, err := cs.ReadResource(ctx, &ReadResourceParams{URI: tt.uri})
324324
if err != nil {
325-
if code := errorCode(err); code == CodeResourceNotFound {
325+
if code := errorCode(err); code == codeResourceNotFound {
326326
if tt.mimeType != "" {
327327
t.Errorf("%s: not found but expected it to be", tt.uri)
328328
}
@@ -994,8 +994,8 @@ func TestElicitationUnsupportedMethod(t *testing.T) {
994994
if err == nil {
995995
t.Error("expected error when ElicitationHandler is not provided, got nil")
996996
}
997-
if code := errorCode(err); code != CodeUnsupportedMethod {
998-
t.Errorf("got error code %d, want %d (CodeUnsupportedMethod)", code, CodeUnsupportedMethod)
997+
if code := errorCode(err); code != codeUnsupportedMethod {
998+
t.Errorf("got error code %d, want %d (CodeUnsupportedMethod)", code, codeUnsupportedMethod)
999999
}
10001000
if !strings.Contains(err.Error(), "does not support elicitation") {
10011001
t.Errorf("error should mention unsupported elicitation, got: %v", err)
@@ -1342,8 +1342,8 @@ func TestElicitationSchemaValidation(t *testing.T) {
13421342
t.Errorf("expected error for invalid schema %q, got nil", tc.name)
13431343
return
13441344
}
1345-
if code := errorCode(err); code != CodeInvalidParams {
1346-
t.Errorf("got error code %d, want %d (CodeInvalidParams)", code, CodeInvalidParams)
1345+
if code := errorCode(err); code != codeInvalidParams {
1346+
t.Errorf("got error code %d, want %d (CodeInvalidParams)", code, codeInvalidParams)
13471347
}
13481348
if !strings.Contains(err.Error(), tc.expectedError) {
13491349
t.Errorf("error message %q does not contain expected text %q", err.Error(), tc.expectedError)
@@ -1462,8 +1462,8 @@ func TestElicitationCapabilityDeclaration(t *testing.T) {
14621462
if err == nil {
14631463
t.Error("expected UnsupportedMethod error when no capability declared")
14641464
}
1465-
if code := errorCode(err); code != CodeUnsupportedMethod {
1466-
t.Errorf("got error code %d, want %d (CodeUnsupportedMethod)", code, CodeUnsupportedMethod)
1465+
if code := errorCode(err); code != codeUnsupportedMethod {
1466+
t.Errorf("got error code %d, want %d (CodeUnsupportedMethod)", code, codeUnsupportedMethod)
14671467
}
14681468
})
14691469
}

mcp/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type ResourceHandler func(context.Context, *ReadResourceRequest) (*ReadResourceR
4141
// not be found.
4242
func ResourceNotFoundError(uri string) error {
4343
return &jsonrpc2.WireError{
44-
Code: CodeResourceNotFound,
44+
Code: codeResourceNotFound,
4545
Message: "Resource not found",
4646
Data: json.RawMessage(fmt.Sprintf(`{"uri":%q}`, uri)),
4747
}

mcp/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ func (s *Server) getPrompt(ctx context.Context, req *GetPromptRequest) (*GetProm
526526
if !ok {
527527
// Return a proper JSON-RPC error with the correct error code
528528
return nil, &jsonrpc2.WireError{
529-
Code: CodeInvalidParams,
529+
Code: codeInvalidParams,
530530
Message: fmt.Sprintf("unknown prompt %q", req.Params.Name),
531531
}
532532
}
@@ -553,7 +553,7 @@ func (s *Server) callTool(ctx context.Context, req *CallToolRequest) (*CallToolR
553553
s.mu.Unlock()
554554
if !ok {
555555
return nil, &jsonrpc2.WireError{
556-
Code: CodeInvalidParams,
556+
Code: codeInvalidParams,
557557
Message: fmt.Sprintf("unknown tool %q", req.Params.Name),
558558
}
559559
}

mcp/shared.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,11 @@ func clientSessionMethod[P Params, R Result](f func(*ClientSession, context.Cont
331331

332332
// Error codes
333333
const (
334-
// TODO: should these be unexported?
335-
336-
CodeResourceNotFound = -32002
334+
codeResourceNotFound = -32002
337335
// The error code if the method exists and was called properly, but the peer does not support it.
338-
CodeUnsupportedMethod = -31001
336+
codeUnsupportedMethod = -31001
339337
// The error code for invalid parameters
340-
CodeInvalidParams = -32602
338+
codeInvalidParams = -32602
341339
)
342340

343341
// notifySessions calls Notify on all the sessions.

mcp/tool_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestToolErrorHandling(t *testing.T) {
6666
// Create a tool that returns a structured error
6767
structuredErrorHandler := func(ctx context.Context, req *CallToolRequest, args map[string]any) (*CallToolResult, any, error) {
6868
return nil, nil, &jsonrpc2.WireError{
69-
Code: CodeInvalidParams,
69+
Code: codeInvalidParams,
7070
Message: "internal server error",
7171
}
7272
}
@@ -111,8 +111,8 @@ func TestToolErrorHandling(t *testing.T) {
111111
t.Fatalf("expected WireError, got %[1]T: %[1]v", err)
112112
}
113113

114-
if wireErr.Code != CodeInvalidParams {
115-
t.Errorf("expected error code %d, got %d", CodeInvalidParams, wireErr.Code)
114+
if wireErr.Code != codeInvalidParams {
115+
t.Errorf("expected error code %d, got %d", codeInvalidParams, wireErr.Code)
116116
}
117117
})
118118

0 commit comments

Comments
 (0)