docs: add sampling example and documentation#2194
Closed
ayzmkk-rongbi wants to merge 1 commit intomodelcontextprotocol:mainfrom
Closed
docs: add sampling example and documentation#2194ayzmkk-rongbi wants to merge 1 commit intomodelcontextprotocol:mainfrom
ayzmkk-rongbi wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
02c4c1a to
51cbe1b
Compare
Add a complete sampling example with both server and client, plus a documentation page explaining the sampling feature. Server (examples/servers/simple-sampling): - Exposes summarize and analyze_sentiment tools that use sampling - Demonstrates ctx.session.create_message() for server-side LLM requests Client (examples/clients/simple-sampling-client): - Provides a sampling_callback to handle server LLM requests - Shows how to integrate with real LLM providers (OpenAI, Anthropic) Documentation (docs/sampling.md): - Explains the sampling flow with a sequence diagram - Documents create_message parameters - Shows client-side callback setup - Includes LLM provider integration examples (OpenAI, Anthropic) - Covers model preferences Fixes modelcontextprotocol#1205
51cbe1b to
9b2defc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add a complete sampling example (server + client) and documentation page to help users understand and use the MCP sampling feature.
Sampling allows servers to request LLM completions from connected clients, effectively "borrowing" the client's language model. While the SDK supports this feature, there was no complete working example or dedicated documentation page.
Changes
Server Example (
examples/servers/simple-sampling/)summarizetool: sends text to client's LLM for summarization via samplinganalyze_sentimenttool: requests sentiment analysis via samplingctx.session.create_message()with parameters (max_tokens, temperature)Client Example (
examples/clients/simple-sampling-client/)sampling_callbackthat handlessampling/createMessagerequestsDocumentation (
docs/sampling.md)create_messageparameters in a tableClientSessionandClient)ModelPreferences,ModelHint)examples/snippets/servers/sampling.pyNavigation (
mkdocs.yml)Testing
pytest tests/client/test_sampling_callback.py)ruff checkandruff formatpass on all new filesFixes #1205