Skip to content

Commit 880ce4f

Browse files
author
Adam Bloomston
committed
fix: align strict validation error handling with PR modelcontextprotocol#1044 pattern
Change strict validation errors to return CallToolResult with isError: true instead of throwing protocol-level errors, aligning with the pattern established in PR modelcontextprotocol#1044 (commit 7387c44) where all validation errors return error responses rather than throwing. This ensures consistent error handling across all validation scenarios and enables LLMs to see validation errors and attempt self-correction, as specified in the MCP protocol specification. Changes: - Remove conditional throw for strict validation input errors - Update test expectations to check for error responses instead of thrown errors - Keep strictInputSchemaValidation field in RegisteredTool for future use The strictInputSchemaValidation field is retained as it still serves to apply .strict() to the Zod schema, rejecting unknown parameters. The difference is now the rejection returns an error response visible to LLMs rather than a protocol-level error. Changes linted and tested. <em>🤖 Created with Claude Code via Airchat</em>
1 parent 65de5c6 commit 880ce4f

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

src/server/mcp.test.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4228,17 +4228,24 @@ describe('elicitInput()', () => {
42284228

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

4231-
await expect(
4232-
client.request(
4233-
{
4234-
method: 'tools/call',
4235-
params: {
4236-
name: 'test-strict',
4237-
arguments: { username: 'test', itemcount: 42 }
4238-
}
4239-
},
4240-
CallToolResultSchema
4241-
)
4242-
).rejects.toThrow('Invalid arguments');
4231+
const result = await client.request(
4232+
{
4233+
method: 'tools/call',
4234+
params: {
4235+
name: 'test-strict',
4236+
arguments: { username: 'test', itemcount: 42 }
4237+
}
4238+
},
4239+
CallToolResultSchema
4240+
);
4241+
4242+
expect(result.isError).toBe(true);
4243+
expect(result.content).toEqual(
4244+
expect.arrayContaining([
4245+
expect.objectContaining({
4246+
text: expect.stringContaining('Input validation error')
4247+
})
4248+
])
4249+
);
42434250
});
42444251
});

src/server/mcp.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,6 @@ export class McpServer {
174174
}
175175
}
176176
} catch (error) {
177-
if (
178-
tool &&
179-
tool.strictInputSchemaValidation &&
180-
error instanceof McpError &&
181-
error.code === ErrorCode.InvalidParams &&
182-
error.message.includes('Input validation error')
183-
) {
184-
throw error;
185-
}
186177
return this.createToolError(error instanceof Error ? error.message : String(error));
187178
}
188179

0 commit comments

Comments
 (0)