MCP Sampling — Server-Requested LLM Completions and Agent Loops

Most MCP servers are dumb executors: take arguments, run code, return content. Sampling lets a server flip direction: it asks the client's LLM to make a decision. This enables server-hosted agent loops without the server owning any model credentials. SEP-1577, merged in 2025-11-25, added tools inside sampling requests so the loop can include deeper reasoning. Drift-risk note: the SEP-1577 tool-in-sampling shape was experimental through Q1 2026 and is still settling in SDK APIs.

Build 1.3 hr Python (stdlib, sampling harness)

Prerequisites: Phase 13 · 07 (MCP server), Phase 13 · 10 (resources and prompts)

#Learning Objectives

  • Explain what sampling/createMessage solves (server-hosted loops without server-side API keys).
  • Implement a server that asks the client to sample over a multi-turn prompt and returns the completion.
  • Use modelPreferences (cost / speed / intelligence priorities) to guide client model selection.
  • Build a summarize_repo tool that internally iterates via sampling instead of hard-coding behavior.
Full Access lesson

Keep going. The rest of this lesson is one step away.

Unlock all 503 lessons, video, code, quizzes and every future update. Pay in your own currency with Paystack.

The text and code of this lesson are based on AI Engineering from Scratch by Rohit Ghumare, MIT licensed.