Skip to content

fix(mcp): refuse nested call_tool with an actionable hint and keep it out of Sentry - #137

Merged
Ooscaar merged 1 commit into
developfrom
mcp-watch/nested-call-tool
Nov 27, 2026
Merged

Ooscaar merged 1 commit into
developfrom
mcp-watch/nested-call-tool

Conversation

@Ooscaar

@Ooscaar Ooscaar commented Nov 27, 2026

Copy link
Copy Markdown
Member

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, culprit tools/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:

  • The agent got FastMCP's bare message, which doesn't say what to call instead.
  • The refusal is a plain ValueError. is_expected_failure didn't count it, so ToolTelemetryMiddleware sent 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:

  • Sentry toolkit (execute_sentry_tool) lets the read-only search run through its proxy but refuses the proxy calling itself, with an isError hint pointing back to search.
  • Executor call returns UnknownTool ("No tool at X. Use search to find exact paths.") with the closest paths.
  • T3 Code tool_call refuses with "Cannot invoke 'X' via tool_call."
  • Anthropic and OpenAI tool search call discovered tools directly, so the case can't happen there. The MCP spec says tool errors should come back as isError results 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 a ToolError, which the model gets as an isError result. It names the wrapped tool, looking through up to 10 levels of nesting. The call_tool(search_tools) → direct search rewrite is unchanged.

Case Hint
Inner tool named call_tool runs other tools; don't wrap call_tool in itself. Call call_tool with name='posts_list' and its arguments directly.
Inner tool is search_tools call_tool runs other tools; don't wrap call_tool in itself. Call search_tools directly with its arguments.
No usable inner name 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 new is_synthetic_tool_refusal() recognises only FastMCP's refusal: a ValueError carrying FastMCP's message, either raised directly or as the cause of a ToolError. is_expected_failure counts that as an agent mistake. Every other ValueError still opens a Sentry issue. The same check also keeps the refusal's traceback out of the logs.

Counted (ToolTelemetryMiddleware). The mcp_tool_call log line gets misuse: "nested_call_tool", and the Sentry transaction gets the tag mcp.misuse, so Axiom can show whether this grows. The call is still logged with outcome=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 around search_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:
    • A nested call makes no capture_exception call, sends no Sentry event and logs no traceback.
    • The log line carries misuse=nested_call_tool and the transaction carries the mcp.misuse tag.
    • Other call_tool failures have no misuse field.
    • Only FastMCP's refusal counts as an expected ValueError.
  • All 10 new failure-case tests fail on develop and 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

🤖 Generated with Claude Code

…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>
@Ooscaar
Ooscaar merged commit 975a0a3 into develop Nov 27, 2026
4 checks passed
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.

1 participant