|
| 1 | +/* |
| 2 | + * © 2025 Snyk Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package mcp |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/mark3labs/mcp-go/mcp" |
| 25 | + "github.com/mark3labs/mcp-go/server" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | +) |
| 29 | + |
| 30 | +// TestMCPResponseFormat verifies that MCP server list responses return empty arrays |
| 31 | +// instead of null when no items are registered. This is required by the MCP specification |
| 32 | +// and JSON API best practices. |
| 33 | +// See: https://modelcontextprotocol.io/docs/concepts/resources |
| 34 | +func TestMCPResponseFormat(t *testing.T) { |
| 35 | + testCases := []struct { |
| 36 | + name string |
| 37 | + serverOptions []server.ServerOption |
| 38 | + setupServer func(*server.MCPServer) |
| 39 | + method string |
| 40 | + expectedArrayField string |
| 41 | + shouldBeEmpty bool |
| 42 | + expectedToolName string |
| 43 | + }{ |
| 44 | + { |
| 45 | + name: "empty resources list returns empty array not null", |
| 46 | + serverOptions: []server.ServerOption{ |
| 47 | + server.WithResourceCapabilities(true, true), |
| 48 | + }, |
| 49 | + setupServer: nil, |
| 50 | + method: "resources/list", |
| 51 | + expectedArrayField: "resources", |
| 52 | + shouldBeEmpty: true, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "empty tools list returns empty array not null after removing tool", |
| 56 | + serverOptions: nil, |
| 57 | + setupServer: func(s *server.MCPServer) { |
| 58 | + // Add and then remove a tool to get an empty list with tool capabilities enabled |
| 59 | + tool := mcp.NewTool("temp_tool", mcp.WithDescription("A temporary tool")) |
| 60 | + s.AddTool(tool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 61 | + return mcp.NewToolResultText("test"), nil |
| 62 | + }) |
| 63 | + s.DeleteTools("temp_tool") |
| 64 | + }, |
| 65 | + method: "tools/list", |
| 66 | + expectedArrayField: "tools", |
| 67 | + shouldBeEmpty: true, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "empty prompts list returns empty array not null", |
| 71 | + serverOptions: []server.ServerOption{ |
| 72 | + server.WithPromptCapabilities(true), |
| 73 | + }, |
| 74 | + setupServer: nil, |
| 75 | + method: "prompts/list", |
| 76 | + expectedArrayField: "prompts", |
| 77 | + shouldBeEmpty: true, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "empty resource templates list returns empty array not null", |
| 81 | + serverOptions: []server.ServerOption{ |
| 82 | + server.WithResourceCapabilities(true, true), |
| 83 | + }, |
| 84 | + setupServer: nil, |
| 85 | + method: "resources/templates/list", |
| 86 | + expectedArrayField: "resourceTemplates", |
| 87 | + shouldBeEmpty: true, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "registered tool appears in non-empty array", |
| 91 | + serverOptions: nil, |
| 92 | + setupServer: func(s *server.MCPServer) { |
| 93 | + tool := mcp.NewTool("test_tool", mcp.WithDescription("A test tool")) |
| 94 | + s.AddTool(tool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 95 | + return mcp.NewToolResultText("test"), nil |
| 96 | + }) |
| 97 | + }, |
| 98 | + method: "tools/list", |
| 99 | + expectedArrayField: "tools", |
| 100 | + shouldBeEmpty: false, |
| 101 | + expectedToolName: "test_tool", |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tc := range testCases { |
| 106 | + t.Run(tc.name, func(t *testing.T) { |
| 107 | + // Create server with options |
| 108 | + mcpServer := createTestServer(t, tc.serverOptions) |
| 109 | + |
| 110 | + // Apply additional setup if provided |
| 111 | + if tc.setupServer != nil { |
| 112 | + tc.setupServer(mcpServer) |
| 113 | + } |
| 114 | + |
| 115 | + // Initialize the server session |
| 116 | + initializeServer(t, mcpServer) |
| 117 | + |
| 118 | + // Send the list request |
| 119 | + responseStr := sendListRequest(t, mcpServer, tc.method) |
| 120 | + |
| 121 | + // Verify the response format |
| 122 | + if tc.shouldBeEmpty { |
| 123 | + assert.Contains(t, responseStr, `"`+tc.expectedArrayField+`":[]`, |
| 124 | + "Response should contain empty %s array, not null. Got: %s", tc.expectedArrayField, responseStr) |
| 125 | + assert.NotContains(t, responseStr, `"`+tc.expectedArrayField+`":null`, |
| 126 | + "Response should NOT contain null %s. Got: %s", tc.expectedArrayField, responseStr) |
| 127 | + } else { |
| 128 | + assert.NotContains(t, responseStr, `"`+tc.expectedArrayField+`":null`, |
| 129 | + "Response should NOT contain null %s. Got: %s", tc.expectedArrayField, responseStr) |
| 130 | + assert.NotContains(t, responseStr, `"`+tc.expectedArrayField+`":[]`, |
| 131 | + "Response should NOT contain empty %s array when items are registered. Got: %s", tc.expectedArrayField, responseStr) |
| 132 | + if tc.expectedToolName != "" { |
| 133 | + assert.Contains(t, responseStr, `"`+tc.expectedToolName+`"`, |
| 134 | + "Response should contain the registered tool %s. Got: %s", tc.expectedToolName, responseStr) |
| 135 | + } |
| 136 | + } |
| 137 | + }) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +// createTestServer creates an MCP server with the given options for testing. |
| 142 | +func createTestServer(t *testing.T, options []server.ServerOption) *server.MCPServer { |
| 143 | + t.Helper() |
| 144 | + return server.NewMCPServer("test-server", "1.0.0", options...) |
| 145 | +} |
| 146 | + |
| 147 | +// initializeServer sends an initialize request to the MCP server. |
| 148 | +func initializeServer(t *testing.T, mcpServer *server.MCPServer) { |
| 149 | + t.Helper() |
| 150 | + initRequest := `{"jsonrpc":"2.0","method":"initialize","id":1,"params":{"protocolVersion":"2024-11-05","clientInfo":{"name":"test-client","version":"1.0.0"},"capabilities":{}}}` |
| 151 | + initResponse := mcpServer.HandleMessage(context.Background(), []byte(initRequest)) |
| 152 | + require.NotNil(t, initResponse, "Initialize response should not be nil") |
| 153 | +} |
| 154 | + |
| 155 | +// sendListRequest sends a list request for the given method and returns the JSON response string. |
| 156 | +func sendListRequest(t *testing.T, mcpServer *server.MCPServer, method string) string { |
| 157 | + t.Helper() |
| 158 | + request := `{"jsonrpc":"2.0","method":"` + method + `","id":2,"params":{}}` |
| 159 | + response := mcpServer.HandleMessage(context.Background(), []byte(request)) |
| 160 | + require.NotNil(t, response, "Response should not be nil") |
| 161 | + |
| 162 | + responseJSON, err := json.Marshal(response) |
| 163 | + require.NoError(t, err, "Failed to marshal response to JSON") |
| 164 | + |
| 165 | + return string(responseJSON) |
| 166 | +} |
0 commit comments