Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/mcp-strict-tool-arguments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hyperdx/api': patch
---

fix(mcp): reject unknown tool arguments instead of silently ignoring them
25 changes: 24 additions & 1 deletion packages/api/src/mcp/__tests__/toolSchemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<string, unknown> {
Expand Down
4 changes: 4 additions & 0 deletions packages/api/src/mcp/utils/registerTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading