diff --git a/bin/codedb-bridge.mjs b/bin/codedb-bridge.mjs index a4d9c87d05..898296994d 100644 --- a/bin/codedb-bridge.mjs +++ b/bin/codedb-bridge.mjs @@ -138,10 +138,45 @@ export const CODEDB_TOOLS = [ ), ]; +// Keys the bridge rejects with a dedicated message when set (handled +// rejection keys, #3352): `project` on every tool, plus the daemon-less +// modes per tool. These stay accepted by the allowlist below so their +// existing by-name rejection messages are unchanged; anything else +// outside the tool's schema is refused before it can be silently dropped +// (#4216). +const HANDLED_REJECTION_KEYS = { + codedb_tree: ['project'], + codedb_outline: ['project', 'compact'], + codedb_symbol: ['project'], + codedb_search: ['project', 'regex', 'scope', 'compact'], + codedb_word: ['project'], + codedb_hot: ['project'], + codedb_deps: ['project'], +}; + +// The accepted argument names for a tool: its advertised schema +// properties plus its handled rejection keys. Returns null for unknown +// tools (the switch in routeForTool still owns that error). +const acceptedArgsFor = (name) => { + const declared = CODEDB_TOOLS.find((t) => t.name === name); + if (!declared) return null; + return new Set([...Object.keys(declared.inputSchema.properties), ...(HANDLED_REJECTION_KEYS[name] ?? [])]); +}; + // Map a tool call to the daemon route. Throws an Error naming the exact // problem for unknown tools, missing required arguments, and arguments // the daemon cannot honour — nothing is silently dropped (#3352). export const routeForTool = (name, args = {}) => { + const accepted = acceptedArgsFor(name); + if (accepted !== null) { + for (const key of Object.keys(args)) { + if (!accepted.has(key)) { + throw new Error( + `argument "${key}": not a valid argument for ${name} (valid: ${[...accepted].sort().join(', ') || '(none)'}); it would be silently ignored by the codedb daemon route` + ); + } + } + } const rejectProject = () => { if (args.project !== undefined) throw new Error(`argument "project": ${PROJECT_ARG_NOTE}`); }; diff --git a/bin/codedb-bridge.test.mjs b/bin/codedb-bridge.test.mjs index 5053292410..c587281dcd 100644 --- a/bin/codedb-bridge.test.mjs +++ b/bin/codedb-bridge.test.mjs @@ -145,6 +145,45 @@ test('tools/call forwards to the daemon and returns its payload', async () => { assert.deepEqual(JSON.parse(hot.result.content[0].text).files, ['a']); }); +test('arguments in no tool schema are rejected by name, never silently dropped (#4216)', () => { + const neverNamed = { + codedb_tree: { bogus_tree_opt: 1 }, + codedb_outline: { path: 'x', bogus_outline_opt: 1 }, + codedb_symbol: { name: 'x', bogus_symbol_opt: 1 }, + codedb_search: { query: 'x', bogus_search_opt: 1 }, + codedb_word: { word: 'x', bogus_word_opt: 1 }, + codedb_hot: { bogus_hot_opt: 1 }, + codedb_deps: { path: 'x', bogus_deps_opt: 1 }, + }; + for (const t of CODEDB_TOOLS) { + const key = Object.keys(neverNamed[t.name]).find((k) => k.startsWith('bogus_')); + assert.throws(() => routeForTool(t.name, neverNamed[t.name]), new RegExp(`"${key}"`)); + } + // The typo case from the issue: max_result (no s) is not max_results. + assert.throws(() => routeForTool('codedb_search', { query: 'x', max_result: 2 }), /"max_result"/); +}); + +test('a typoed max_result surfaces as an MCP error naming the argument, not the full result set (#4216)', async () => { + const handle = createHandler({ + fetchFn: stubFetch({ + '/explore/search?q=x': { + query: 'x', + results: [ + { path: 'a.rs', line: 1, text: 'a' }, + { path: 'b.rs', line: 2, text: 'b' }, + { path: 'c.rs', line: 3, text: 'c' }, + { path: 'd.rs', line: 4, text: 'd' }, + { path: 'e.rs', line: 5, text: 'e' }, + ], + }, + }), + daemonUrl: 'http://127.0.0.1:7719', + }); + const typo = await call(handle, 'codedb_search', { query: 'x', max_result: 2 }); + assert.equal(typo.result.isError, true); + assert.match(typo.result.content[0].text, /"max_result"/); +}); + test('a slow daemon call does not head-of-line block a later ping (PR #4171 Codex P2)', async () => { const daemon = http.createServer((req, res) => { const respond = () => {