Skip to content

Commit 65f5f72

Browse files
author
User
committed
fix: throw ProtocolError as JSON-RPC error for tool not found
Per MCP spec, calling a nonexistent tool should return a JSON-RPC Error (code -32602), not a JSON-RPC Result with isError: true. Previously, only UrlElicitationRequired errors were re-thrown; all other ProtocolErrors (including tool/disabled checks) were swallowed and wrapped in a CallToolResult. Now all ProtocolErrors propagate as JSON-RPC errors, which is the correct behavior per the specification. Fixes #1510
1 parent c4ee360 commit 65f5f72

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

packages/server/src/server/mcp.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ export class McpServer {
213213
await this.validateToolOutput(tool, result, request.params.name);
214214
return result;
215215
} catch (error) {
216-
if (error instanceof ProtocolError && error.code === ProtocolErrorCode.UrlElicitationRequired) {
217-
throw error; // Return the error to the caller without wrapping in CallToolResult
216+
if (error instanceof ProtocolError) {
217+
// Protocol errors should be returned as JSON-RPC errors, not wrapped in CallToolResult
218+
throw error;
218219
}
219220
return this.createToolError(error instanceof Error ? error.message : String(error));
220221
}

test/integration/test/server/mcp.test.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,25 +1837,17 @@ describe('Zod v4', () => {
18371837

18381838
await Promise.all([client.connect(clientTransport), mcpServer.server.connect(serverTransport)]);
18391839

1840-
const result = await client.request(
1841-
{
1842-
method: 'tools/call',
1843-
params: {
1844-
name: 'nonexistent-tool'
1845-
}
1846-
},
1847-
CallToolResultSchema
1848-
);
1849-
1850-
expect(result.isError).toBe(true);
1851-
expect(result.content).toEqual(
1852-
expect.arrayContaining([
1840+
await expect(
1841+
client.request(
18531842
{
1854-
type: 'text',
1855-
text: expect.stringContaining('Tool nonexistent-tool not found')
1856-
}
1857-
])
1858-
);
1843+
method: 'tools/call',
1844+
params: {
1845+
name: 'nonexistent-tool'
1846+
}
1847+
},
1848+
CallToolResultSchema
1849+
)
1850+
).rejects.toThrow(/Tool nonexistent-tool not found/);
18591851
});
18601852

18611853
/***

0 commit comments

Comments
 (0)