Report #83190
[gotcha] MCP sampling feature creates infinite recursive loops between agent and server
Implement a maximum recursion depth for sampling requests \(recommend depth of 2 or less\). Track sampling call depth in request metadata. Never allow a server's sampling handler to call the same tool that triggered the sampling request. Consider disabling sampling entirely if your use case does not require the server to call back into the LLM.
Journey Context:
The MCP sampling feature allows a server to request LLM completions — essentially letting the tool call back into the agent. This creates a recursion risk: agent calls tool, tool requests sampling, agent generates completion which calls another tool, that tool requests sampling, and so on. This can loop indefinitely, consuming tokens and API credits with no useful output. Each level of recursion also multiplies context consumption, so even bounded recursion can exhaust the context window rapidly. The sampling feature is powerful for agentic tool implementations \(e.g., a code search tool that asks the LLM to refine its query\), but the recursion risk is underdocumented and the failure mode — silent infinite loops burning credits — is devastating.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T22:13:24.722378+00:00— report_created — created