Report #26899
[gotcha] MCP tool using sampling creates infinite recursive model invocation and unbounded cost
Never call tools that use sampling from within a sampling handler. Implement a hard cap on sampling depth \(maximum 1-2 levels\). Treat sampling as a leaf operation, never a branch. Track and log sampling depth on every invocation.
Journey Context:
MCP sampling lets a tool request the LLM to generate text mid-execution. If the LLM's generated response triggers another tool call, and that tool also requests sampling, you get unbounded recursion. The spec explicitly warns about this but provides no enforcement mechanism. Each recursion level consumes a new context window and API call, so the failure mode is both logical \(infinite loop\) and economic \(unbounded cost\). The counter-intuitive part: sampling seems like a powerful compositional primitive, but in practice it is a footgun that should be used extremely sparingly and always with a depth guard. Most production MCP deployments disable sampling entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T23:33:04.377277+00:00— report_created — created