Skip to content

Update tools/call input validation errors to be tool execution errors #824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 7 additions & 33 deletions src/server/mcp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,24 +937,6 @@ describe("tool()", () => {
}),
);

mcpServer.registerTool(
"test (new api)",
{
inputSchema: {
name: z.string(),
value: z.number(),
},
},
async ({ name, value }) => ({
content: [
{
type: "text",
text: `${name}: ${value}`,
},
],
})
);

const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();

Expand All @@ -977,23 +959,15 @@ describe("tool()", () => {
},
CallToolResultSchema,
),
).rejects.toThrow(/Invalid arguments/);

await expect(
client.request(
).resolves.toStrictEqual({
content: [
{
method: "tools/call",
params: {
name: "test (new api)",
arguments: {
name: "test",
value: "not a number",
},
},
type: "text",
text: expect.stringMatching(/Invalid arguments for tool test/),
},
CallToolResultSchema,
),
).rejects.toThrow(/Invalid arguments/);
],
isError: true,
});
});

/***
Expand Down
13 changes: 9 additions & 4 deletions src/server/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,15 @@ export class McpServer {
request.params.arguments,
);
if (!parseResult.success) {
throw new McpError(
ErrorCode.InvalidParams,
`Invalid arguments for tool ${request.params.name}: ${parseResult.error.message}`,
);
return {
content: [
{
type: "text",
text: `Invalid arguments for tool ${request.params.name}: ${parseResult.error.message}`,
},
],
isError: true,
};
}

const args = parseResult.data;
Expand Down