Spoken summary — press play to read along: the line being spoken stays near the top.
📚 Study Notes: Sampling in MCP Architecture
💡 Core Concepts
- Sampling: A technique that shifts the responsibility of calling a Language Model (LLM, e. g. , Claude) from the server to the client.
- MCP Client/Server: The architecture involves an MCP client (running in the browser/Next. js app) communicating with an MCP server.
- Tool Use: LLMs can be integrated with external tools (like a "research tool") that perform actions (e. g. , fetching data from Wikipedia) before synthesizing a final report.
⚙️ Comparing LLM Integration Methods
| Method | Mechanism | Complexity | Server Requirements | Best Use Case | | :--- | :--- | :--- | :--- | :--- | | **1. Direct Server Call** | The MCP server makes direct API calls to the LLM (e. g. , Claude) to summarize results. | High (Server must handle API calls, text extraction, etc. ) | Requires an API key on the MCP server. | Controlled, internal environments. | | **2. Sampling (Recommended)** | The MCP server sends a prompt to the MCP client, and the client executes the LLM call and returns the generated text to the server. | Low (Complexity moves to the client). | Does not require an API key on the MCP server. | Publicly accessible or shared MCP servers. |
🚀 Key Benefits of Using Sampling
- Reduced Complexity: The burden of calling the LLM is moved to the client, which is already connected to the LLM.
- Security/Cost: The MCP server does not need to store or manage an API key for the LLM.
- Public Accessibility: Essential for publicly available servers, as it prevents the server from incurring token costs or exposing credentials for external users.
🛠️ Implementation Steps for Sampling
On the Server Side:
- Use the create message function.
- Pass a list of messages to be handed off to the client. This list forms the request sent to the client.
On the Client Side:
- Implement a sampling callback.
- This callback receives the messages from the server.
- The developer must write the logic within this callback to:
- Call the desired LLM (e. g. , Claude).
- Generate the text.
- Return the result using a create message result.
Takeaways
🧠 Key Takeaways: Sampling in MCP Architecture
- Definition of Sampling: Sampling is an architectural pattern that shifts the responsibility of calling the Language Model (LLM) from the backend server to the client (browser/Next. js app).
- Security and Accessibility: This method is highly recommended for public-facing servers because it prevents the MCP server from needing to store or manage sensitive LLM API keys, significantly improving security and cost control.
- Operational Flow: The process is initiated by the server sending a prompt list, but the actual LLM call and text generation must be executed by the client within a designated sampling callback.
- Reduced Server Complexity: By moving the LLM call to the client, the server's code complexity is lowered, as it no longer needs to handle API key management, text extraction, or direct external API calls.
- LLM Capabilities: LLMs can be integrated with external tools (e. g. , Wikipedia fetchers) to perform actions and gather data before synthesizing a final, comprehensive report.