Understanding Sampling

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

So far, the relationship between the Client and Server has been one-way regarding intelligence. The Client (Host) has the “Brain” (the LLM), and the Server has the “Brawn” (Tools/Data).

But sometimes, the Server needs a little bit of intelligence to finish its job.

Consider a Log Archival Server. Its job is to save error logs to a file. However, simply saving raw, cryptic error codes isn’t very useful. You want the server to:

  1. Read the raw error.
  2. Understand and summarize it into plain English.
  3. Save the explained version to a report file.

To do step #2, the server needs an LLM. Instead of buying a separate API key for the server, MCP allows it to use Sampling to “borrow” the Client’s LLM connection.

The Architecture of Sampling

Sampling reverses the usual flow of control.

Implementing the Server

You will build a tool called process_log_entry. It takes a raw log line, asks the Client to explain it, and then appends that explanation to a local file.

What You Are Building

You are building a server that asks the client to use its LLM to explain a log entry, then saves that explanation to a local report file. The server never talks to the model directly; it requests a sampling response from the client.

Implementing the Sampling Client

The Client acts as the bridge. It connects to the server and listens for sampling requests. When the server asks for help, the client forwards the request to the actual Anthropic API.

How the Server Script Works

  • ctx.session.create_message(...) triggers a sampling request to the client.
  • The server waits for the client to return an LLM response.
  • The server writes the explanation to incident_report.txt.

How the Client Script Works

  • sampling_handler(...) is the callback the server triggers for sampling.
  • It forwards the prompt to Anthropic and returns the response.
  • sampling_callback=... is what makes the client “sampling-aware.”
  • The API key is read from ANTHROPIC_API_KEY.

Run It

  1. Install dependencies:
See forum comments
Download course materials from Github
Previous: Enforcing Roots Next: Sampling with Real AI