From 700d3377815fbd7eba890b9ff646d012b19cd624 Mon Sep 17 00:00:00 2001 From: aryainguz Date: Tue, 24 Nov 2026 22:42:23 +0530 Subject: [PATCH] fix(mcp): reject unknown tool arguments instead of ignoring them --- .changeset/mcp-strict-tool-arguments.md | 5 ++++ .../api/src/mcp/__tests__/toolSchemas.test.ts | 25 ++++++++++++++++++- packages/api/src/mcp/utils/registerTool.ts | 4 +++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 .changeset/mcp-strict-tool-arguments.md diff --git a/.changeset/mcp-strict-tool-arguments.md b/.changeset/mcp-strict-tool-arguments.md new file mode 100644 index 0000000000..42bf952a62 --- /dev/null +++ b/.changeset/mcp-strict-tool-arguments.md @@ -0,0 +1,5 @@ +--- +'@hyperdx/api': patch +--- + +fix(mcp): reject unknown tool arguments instead of silently ignoring them diff --git a/packages/api/src/mcp/__tests__/toolSchemas.test.ts b/packages/api/src/mcp/__tests__/toolSchemas.test.ts index 9e2b256899..92e8e2dd40 100644 --- a/packages/api/src/mcp/__tests__/toolSchemas.test.ts +++ b/packages/api/src/mcp/__tests__/toolSchemas.test.ts @@ -2,7 +2,7 @@ import Ajv2020 from 'ajv/dist/2020'; import { McpContext } from '@/mcp/tools/types'; -import { createTestClient } from './mcpTestUtils'; +import { callTool, createTestClient, getFirstText } from './mcpTestUtils'; /** * Every MCP tool's `inputSchema` must be valid JSON Schema draft 2020-12. @@ -116,6 +116,29 @@ describe('MCP tool input schemas', () => { expect(offenders).toEqual([]); }); + + // The schemas tell clients extra keys aren't allowed. A tool that drops one + // anyway answers as if the option was never sent, and the agent can't tell. + // Arguments are validated before the handler runs, so this needs no database + // either. + it('reject unknown top-level arguments', async () => { + const client = await createTestClient(context); + const { tools } = await client.listTools(); + + expect(tools.length).toBeGreaterThan(0); + + const accepted: string[] = []; + for (const tool of tools) { + const result = await callTool(client, tool.name, { + notARealOption: true, + }); + if (!result.isError || !getFirstText(result).includes('notARealOption')) { + accepted.push(tool.name); + } + } + + expect(accepted).toEqual([]); + }); }); function isRecord(value: unknown): value is Record { diff --git a/packages/api/src/mcp/utils/registerTool.ts b/packages/api/src/mcp/utils/registerTool.ts index 99a82ef684..41a65342da 100644 --- a/packages/api/src/mcp/utils/registerTool.ts +++ b/packages/api/src/mcp/utils/registerTool.ts @@ -35,6 +35,10 @@ export function createRegisterTool( name, { ...config, + // Zod drops unknown keys by default, so a tool would run as if an + // unsupported or misspelled option was never sent. Reject them, as the + // advertised schema (additionalProperties: false) already says. + inputSchema: config.inputSchema.strict(), annotations: { title: config.title, ...config.annotations }, }, traced,