Repository navigation
fix(mcp): refuse nested call_tool with an actionable hint and keep it out of Sentry - #137
Merged
Merged
Conversation
…y issue
An agent called call_tool(name="call_tool", arguments={name, arguments}).
FastMCP's proxy refuses that with a ValueError, which telemetry did not count
as expected, so it became Sentry issue ZERNIO-MCP-W and the agent got
FastMCP's bare message.
- ToolGuidanceMiddleware turns the refusal into a hint naming the wrapped
tool (looking through nested wrappers), or pointing to search_tools when
there is none. The call_tool(search_tools) rewrite is unchanged.
- is_expected_failure counts FastMCP's synthetic-tool refusal as an agent
mistake, matched by its message, so other ValueErrors still reach Sentry.
- The mcp_tool_call line and Sentry transaction record
misuse=nested_call_tool so the rate can be watched in Axiom.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Sentry ZERNIO-MCP-W (
ToolError: Error calling tool 'call_tool': 'call_tool' is a synthetic search tool and cannot be called via the call_tool proxy, culprittools/call call_tool). An agent wrapped the proxy in itself:call_tool(name="call_tool", arguments={"name": "<real tool>", "arguments": {...}}).FastMCP refuses this on purpose. Without the guard, the proxy would call itself until it hit a recursion error or timed out. A review on the PR that added tool search flagged it as a request-level DoS (PrefectHQ/fastmcp#3154), and the docs now say the proxy "raises an error rather than recursing".
Two things were wrong on our side:
ValueError.is_expected_failuredidn't count it, soToolTelemetryMiddlewaresent it to Sentry as a server bug.Why refuse rather than unwrap
We looked at how other search-then-call servers handle a proxy that is asked to call itself:
execute_sentry_tool) lets the read-only search run through its proxy but refuses the proxy calling itself, with anisErrorhint pointing back to search.callreturnsUnknownTool("No tool at X. Use search to find exact paths.") with the closest paths.tool_callrefuses with "Cannot invoke 'X' via tool_call."isErrorresults the model can correct from.None of them unwraps on purpose. Unwrapping would also hide the mistake: telemetry runs after this middleware, so it would log the call as if it were correct. The case is rare (1 event so far), and the agent fixes it in one retry with a clear hint.
Change
Refusal with a hint (
ToolGuidanceMiddleware). The middleware catches FastMCP's refusal on the way back and replaces it with aToolError, which the model gets as anisErrorresult. It names the wrapped tool, looking through up to 10 levels of nesting. Thecall_tool(search_tools)→ direct search rewrite is unchanged.call_tool runs other tools; don't wrap call_tool in itself. Call call_tool with name='posts_list' and its arguments directly.search_toolscall_tool runs other tools; don't wrap call_tool in itself. Call search_tools directly with its arguments.call_tool runs other tools; don't wrap call_tool in itself. Call search_tools with what you want to do, then call_tool with the exact name it returns.Kept out of Sentry (
late.mcp.telemetry). The newis_synthetic_tool_refusal()recognises only FastMCP's refusal: aValueErrorcarrying FastMCP's message, either raised directly or as the cause of aToolError.is_expected_failurecounts that as an agent mistake. Every otherValueErrorstill opens a Sentry issue. The same check also keeps the refusal's traceback out of the logs.Counted (
ToolTelemetryMiddleware). Themcp_tool_calllog line getsmisuse: "nested_call_tool", and the Sentry transaction gets the tagmcp.misuse, so Axiom can show whether this grows. The call is still logged withoutcome=error.Tests
tests/test_mcp_tool_guidance.py: each hint is checked word for word through the hosted server. Cases: one level, two levels, nested aroundsearch_tools, and four shapes with no inner name. The unwrap and old "still refuses" tests are replaced. The existing search-rewrite and "closest tools" tests are unchanged.tests/test_mcp_telemetry.py:capture_exceptioncall, sends no Sentry event and logs no traceback.misuse=nested_call_tooland the transaction carries themcp.misusetag.call_toolfailures have nomisusefield.ValueError.developand pass with this change.pytest tests: 3854 passed, 44 skipped.mypy src --ignore-missing-imports: no issues in 168 source files.ruff check src tests: all checks passed.Notes
ArgumentNamesMiddleware(fix(mcp): accept camelCase argument names agents copy from the REST API #135) renamestool_nameonly at the top level of the call. An agent that writestool_nameinside the nested arguments gets the generic hint.🤖 Generated with Claude Code