Skip to content

fix(mcp): return structured error with valid tool list on unknown tool - #561

Closed
sparkysolutions10-pixel wants to merge 2 commits into
Divinity-Labs:mainfrom
sparkysolutions10-pixel:fix/mcp-unknown-tool-error-415
Closed

sparkysolutions10-pixel wants to merge 2 commits into
Divinity-Labs:mainfrom
sparkysolutions10-pixel:fix/mcp-unknown-tool-error-415

Conversation

@sparkysolutions10-pixel

Copy link
Copy Markdown
Contributor

Summary

Closes #415

Returns a structured error with a code and the list of valid tool names when an unknown tool is called, instead of throwing a raw protocol-level exception.

Changes

  • Structured Error Return: Replaced throw new Error(\Unknown tool: ...`)with structured error response usingERR_UNKNOWN_TOOL`.
  • Valid Tools List: Included validTools: ['bazaar_search', 'paid_call'] in the error response and message so calling agents can inspect and self-correct immediately.
  • Input Sanitization & Length Bounding: Sanitized the echoed tool name by bounding it to 64 characters and stripping non-printable/control characters.

Tests

  • Added unit tests in tests/mcp.test.ts:
    • returns a structured error instead of throwing a raw exception
    • lists all valid tool names in the error response
    • length-bounds and sanitises untrusted input in tool name echo
  • All 12 tests pass (npx vitest run tests/mcp.test.ts).

@sparkysolutions10-pixel
sparkysolutions10-pixel force-pushed the fix/mcp-unknown-tool-error-415 branch from b3409c6 to ef3f5c1 Compare October 19, 2026 17:56
@sparkysolutions10-pixel
sparkysolutions10-pixel force-pushed the fix/mcp-unknown-tool-error-415 branch from ef3f5c1 to 71817d6 Compare October 23, 2026 08:11
Miracle656 added a commit that referenced this pull request Oct 23, 2026
…415)

An unknown tool name threw, so the SDK surfaced a JSON-RPC transport error and
the calling agent got nothing to branch on. It now answers with
ERR_UNKNOWN_TOOL, the offending name, and the list of tools that do exist —
which is the difference between an agent recovering and an agent stopping.

Listing the valid tools in the reason is the detail worth keeping: a model that
guessed a tool name can correct itself from the response instead of needing
another round trip to ListTools.

One fix on merge. tests/mcp.test.ts had `unknown tool > rejects instead of
returning a best-effort result`, asserting the old throw — its own comment
described the missing error code as the problem. That is the bug #415
describes, so the test was pinning it. Inverted the assertion rather than
deleting it, and said so in the comment: it now checks isError, the
ERR_UNKNOWN_TOOL code and that the reason names the tool. The dropped-test
guard stays satisfied because the case still exists.

#415 was assigned to rupesh-kumar-sah, and this is their PR. #561 implements
the same issue and is noted there.

Lens: 116 files, 1,428 tests passing, typecheck clean.

Claude-Session: https://claude.ai/code/session_01USgemLt4Rnz4SGB1Srf3GB
@Miracle656

Copy link
Copy Markdown
Collaborator

Closing the loop on this one: #415 is assigned to @rupesh-kumar-sah, and their PR #444 implements the same fix. I've merged that as 8cc184e, so this is now superseded.

The two are genuinely the same change — same three files (src/mcp/codes.ts, src/mcp/server.ts, tests/mcp.test.ts), same ERR_UNKNOWN_TOOL shape, both listing the valid tools in the reason. That last idea is good and it's in what landed.

The rule I'm applying is the one from your re-cut earlier: where two PRs implement the same issue, the assignee's goes first. That's not a judgement on the work — yours is fine — it's just the only tiebreak that is fair to whoever claimed it.

Same situation on #569: it implements #505, which is assigned to @Zuliyat, whose #593 is open for it. I'll review theirs first for the same reason. Worth checking assignment before cutting a branch — there were unclaimed issues in this area and your time is better spent on those than on a tiebreak you can't win.

Leaving this open rather than closing it myself, since it's yours to close.

@sparkysolutions10-pixel

Copy link
Copy Markdown
Contributor Author

@Miracle656 Closing this out as Issue #415 has been addressed and merged via commit b9ab065. Glad to see the structured error model in place on main!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An unknown tool name throws a raw exception

2 participants