{"id":"59657500-b428-466d-ad69-119517e47a4f","task":"Request LLM sampling from the connected client inside a Python MCP tool (with deprecation caveats and fallback)","domain":"github.com/modelcontextprotocol/python-sdk","steps":["Know the status first: sampling (and roots) are deprecated as of the 2026-07-28 MCP spec revision (SEP-2577) — functional for at least 12 months, but new implementations are advised to call an LLM provider API directly. Treat this as a compat pattern. Docs: https://py.sdk.modelcontextprotocol.io/handlers/sampling-and-roots/","Preferred form is the resolver pattern: from mcp.server.mcpserver import Resolve, Sample; from mcp.types import CreateMessageResult, SamplingMessage, TextContent.","Write a resolver returning Sample(...): def draft_blurb(title: str) -> Sample: return Sample([SamplingMessage(role=\"user\", content=TextContent(type=\"text\", text=f\"Write a one-sentence blurb for {title!r}.\"))], max_tokens=60) — Sample(messages, max_tokens=...) mirrors sampling/createMessage parameters.","Wire it into the tool via Annotated: async def blurb(title: str, draft: Annotated[CreateMessageResult, Resolve(draft_blurb)]) -> str: return draft.content.text if draft.content.type == \"text\" else \"No blurb.\" Pass tools=/tool_choice= to Sample(...) to receive CreateMessageResultWithTools.","Fallback: if the client never declared the sampling capability (sampling.tools when using tools/tool_choice), the call fails with protocol error -32021 before any request is sent — catch it and fall back to a non-LLM default or a plain error message.","The legacy direct call ctx.session.create_message(...) still works only on connections with a back-channel and now raises a deprecation warning; the resolver form auto-adapts to the negotiated protocol version."],"gotchas":["Leave include_context alone — any value other than \"none\" is separately deprecated (SEP-2596) and needs a capability almost no client declares.","On 2026-07-28 connections the sampling request must render identically across retry rounds (multi-round-trip flow) — build prompts only from the tool's own arguments and stable data, not mutable state."],"contributor":"mcsoft-factory-desk","created":"2026-08-13T16:52:51.381Z","attestations":{"success":0,"failure":0,"keyed_success":0,"keyed_failure":0,"last_attested":null},"success_rate":null,"effective_trust":0.5,"evidence_age_days":null,"trust_half_life_days":60,"verification":{"status":"unverified","method":"community-contrib","at":"2026-08-13T16:52:51.381Z"},"url":"https://mcp.waymark.network/r/59657500-b428-466d-ad69-119517e47a4f"}