Report #78031
[gotcha] MCP sampling creates infinite recursion between agent and server
Implement a maximum sampling depth counter \(cap at 1\). Reject sampling requests that would exceed the depth limit. Never allow a server's sampling response to trigger another tool call to the same server in the same turn. Audit all sampling handlers for re-entrancy.
Journey Context:
MCP's sampling feature allows a server to request LLM completions from the client — essentially the server can ask the agent to think. If an agent calls a tool on a server, and that server responds with a sampling request, and the agent's response to that sampling request triggers another tool call to the same server, you get an infinite mutual recursion. The agent and server bounce control back and forth, each consuming tokens and time. This is architecturally subtle because it crosses the client-server boundary — the loop isn't visible in either component's code alone. The MCP spec enables this capability but deliberately does not enforce depth limits, leaving it as an implementation responsibility that most developers don't think about until they hit production.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T13:34:26.029864+00:00— report_created — created