|
| 1 | +// Copyright 2025 The Go MCP SDK Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by an MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package mcp |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "errors" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/modelcontextprotocol/go-sdk/jsonrpc" |
| 13 | +) |
| 14 | + |
| 15 | +// TestServerErrors validates that the server returns appropriate error codes |
| 16 | +// for various invalid requests. |
| 17 | +func TestServerErrors(t *testing.T) { |
| 18 | + ctx := context.Background() |
| 19 | + |
| 20 | + // Set up a server with tools, prompts, and resources for testing |
| 21 | + cs, _, cleanup := basicConnection(t, func(s *Server) { |
| 22 | + // Add a tool with required parameters |
| 23 | + type RequiredParams struct { |
| 24 | + Name string `json:"name" jsonschema:"the name is required"` |
| 25 | + } |
| 26 | + handler := func(ctx context.Context, req *CallToolRequest, args RequiredParams) (*CallToolResult, any, error) { |
| 27 | + return &CallToolResult{ |
| 28 | + Content: []Content{&TextContent{Text: "success"}}, |
| 29 | + }, nil, nil |
| 30 | + } |
| 31 | + AddTool(s, &Tool{Name: "validate", Description: "validates params"}, handler) |
| 32 | + |
| 33 | + // Add a prompt |
| 34 | + s.AddPrompt(codeReviewPrompt, codReviewPromptHandler) |
| 35 | + |
| 36 | + // Add a resource that returns ResourceNotFoundError |
| 37 | + s.AddResource( |
| 38 | + &Resource{URI: "file:///test.txt", Name: "test", MIMEType: "text/plain"}, |
| 39 | + func(ctx context.Context, req *ReadResourceRequest) (*ReadResourceResult, error) { |
| 40 | + return nil, ResourceNotFoundError(req.Params.URI) |
| 41 | + }, |
| 42 | + ) |
| 43 | + }) |
| 44 | + defer cleanup() |
| 45 | + |
| 46 | + testCases := []struct { |
| 47 | + name string |
| 48 | + executeCall func() error |
| 49 | + expectedCode int64 |
| 50 | + }{ |
| 51 | + { |
| 52 | + name: "missing required param", |
| 53 | + executeCall: func() error { |
| 54 | + _, err := cs.CallTool(ctx, &CallToolParams{ |
| 55 | + Name: "validate", |
| 56 | + Arguments: map[string]any{}, // Missing required "name" field |
| 57 | + }) |
| 58 | + return err |
| 59 | + }, |
| 60 | + expectedCode: jsonrpc.CodeInvalidParams, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "unknown tool", |
| 64 | + executeCall: func() error { |
| 65 | + _, err := cs.CallTool(ctx, &CallToolParams{ |
| 66 | + Name: "nonexistent_tool", |
| 67 | + Arguments: map[string]any{}, |
| 68 | + }) |
| 69 | + return err |
| 70 | + }, |
| 71 | + expectedCode: jsonrpc.CodeInvalidParams, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "unknown prompt", |
| 75 | + executeCall: func() error { |
| 76 | + _, err := cs.GetPrompt(ctx, &GetPromptParams{ |
| 77 | + Name: "nonexistent_prompt", |
| 78 | + Arguments: map[string]string{}, |
| 79 | + }) |
| 80 | + return err |
| 81 | + }, |
| 82 | + expectedCode: jsonrpc.CodeInvalidParams, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "resource not found", |
| 86 | + executeCall: func() error { |
| 87 | + _, err := cs.ReadResource(ctx, &ReadResourceParams{ |
| 88 | + URI: "file:///test.txt", |
| 89 | + }) |
| 90 | + return err |
| 91 | + }, |
| 92 | + expectedCode: CodeResourceNotFound, |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + for _, tc := range testCases { |
| 97 | + t.Run(tc.name, func(t *testing.T) { |
| 98 | + err := tc.executeCall() |
| 99 | + if err == nil { |
| 100 | + t.Fatal("expected error, got nil") |
| 101 | + } |
| 102 | + |
| 103 | + var rpcErr *jsonrpc.Error |
| 104 | + if !errors.As(err, &rpcErr) { |
| 105 | + t.Fatalf("expected jsonrpc.Error, got %T: %v", err, err) |
| 106 | + } |
| 107 | + |
| 108 | + if rpcErr.Code != tc.expectedCode { |
| 109 | + t.Errorf("expected error code %d, got %d", tc.expectedCode, rpcErr.Code) |
| 110 | + } |
| 111 | + |
| 112 | + if rpcErr.Message == "" { |
| 113 | + t.Error("expected non-empty error message") |
| 114 | + } |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
0 commit comments