Report #13438
[gotcha] MCP sampling creates infinite recursion — server requests LLM completion that re-invokes the same tool
Never implement a tool whose execution triggers an MCP sampling request that could re-invoke the same tool. Implement a recursion depth counter in the client that increments on each sampling request and aborts beyond a safe limit \(e.g., depth 3\). Audit your tool\+sampling chains for cycles before deploying.
Journey Context:
MCP's sampling capability allows a server to request LLM completions through the client — essentially the server asking the LLM a question mid-tool-execution. If a tool triggers a sampling request, and the LLM's response to that sampling request decides to call the same tool again, you get infinite recursion across the client-server boundary. This is not a traditional code loop — it's a distributed recursion that crosses process boundaries and protocol layers. The MCP spec acknowledges this risk but provides no built-in cycle detection. It manifests as exponentially growing context usage and eventually a context window overflow, but only when the specific tool\+sampling path is exercised, making it easy to miss in testing.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T18:45:40.543058+00:00— report_created — created