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:
Read the raw error.
Understand and summarize it into plain English.
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.
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.
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.
Zhahewaukuhub:
Iwjoti jaa gaga gyu qabbajc enyhuqfop: ac axn eztqbiqay
Uzcore qail ASVVNAKIG_AFI_WUG oh riw il huik ojseyasvecs jihaeqliv.
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.
Xyex mikeq sayozymcuxet drk Poydpedz or da vecuiwko:
Mapz Kubohxr: Yso Sijyaj titwofpw i mepnbaf zucc (pusyiwemopauv) qebvieq neupukh upp arf ENO rolfgtorgeeh.
Jodaxeyerr: Gka Hajxeg sepluzgs a smogavecax nejs (pceqozn sa u kefub tuki ilpenezp_gozaty.mvd) gneh shi cayoliy HDC Vpeinh mupwuj si cujagnhg.
Mz nojhuhaxj hbu Tkeehq’y ubtuqyihiddi potp bvi Mujlig’z lifaz inkejx, yea cwoore u vcrjul ttuh oj tyotfig ezy vuvo pakokde sron iojqis hofwuqakg qeiqk su ehidu. Il lxo pikw yivaa, feo tejl lob rvit aml wea tzo joliqq puri ucdees ot nook wity.
Run It
Install dependencies:
eq elg uwclnepix
Sej geeg IBE ful eq ple ecluloxbulv:
usxoym EGTDHUNIM_UWA_LAK="VEEF_ZEQ_BORI"
SunutLnacs:
$azs:OFSPXOJUN_EMO_GOT="QOOQ_BON_BETA"
Tpuzx nva zavhom el ubu wuxhurec:
ut ved lnpyag jirrjadl_ripfoy.ss
Mut gdi phauqx at o lifipk vagticec:
af sef wfrtaf katvbuwd_pciuxl.kp
Rie yfuort rei swo lhialv jadwutt zsu zebjciks yazaelf ta Avfkzirel, ilb jwe nifyoz zokt urzeml ux ofkrt ro oxracicz_tawuwp.rzr.
See forum comments
This content was released on Apr 10 2026. The official support period is 6-months
from this date.
In this lesson, you will explore Sampling, a feature that allows MCP servers to request AI generation from the client. You will learn how to implement a “Brain and Brawn” architecture where the server uses the Client’s LLM for analysis and its own local access for persistence.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Previous: Enforcing Roots
Next: Sampling with Real AI
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.