Report #14786
[gotcha] MCP tool triggers sampling which calls the same tool, creating an infinite recursion loop that burns tokens until context exhaustion
Never implement a tool that calls sampling where the LLM might call the same tool again. Track a call-depth counter in your tool context and refuse sampling requests beyond depth 2. Design tools to return structured data, not to orchestrate further LLM calls. Prefer letting the orchestrating agent decide next steps.
Journey Context:
MCP's sampling feature lets a server request the client's LLM to generate a completion — the server asking the client's model to think. This is powerful for agentic loops but creates a recursion trap: Tool A calls sampling → LLM decides to call Tool A → Tool A calls sampling → infinite loop. Each iteration consumes context and tokens, and the loop can be subtle — Tool A might trigger sampling that leads to a chain of reasoning and tool calls that eventually circles back. The MCP spec acknowledges this risk but provides no built-in guard. The model has no inherent mechanism to detect it's in a recursive loop. The fix is to treat sampling as a sharp edge: minimize its use, always bound recursion depth explicitly, and prefer returning structured data that lets the top-level orchestrating agent decide next steps rather than nesting LLM calls inside tool handlers.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T22:23:38.278411+00:00— report_created — created