-
Notifications
You must be signed in to change notification settings - Fork 2k
Add AnthropicSamplingHandler #2617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
626faa4
Add AnthropicSamplingHandler
jlowin a4897b4
Update default model
jlowin a3e839e
Update sampling docs to cover both OpenAI and Anthropic handlers
jlowin 51ae947
Use AsyncAnthropic, fix falsy value handling, handle tool_choice none
jlowin a272306
Propagate isError to Anthropic, join multiple text blocks, fix docs
jlowin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -443,32 +443,32 @@ tool_result = ToolResultContent( | |
|
|
||
| Client support for sampling is optional—some clients may not implement it. To ensure your tools work regardless of client capabilities, configure a `sampling_handler` that sends requests directly to an LLM provider. | ||
|
|
||
| ### OpenAI Handler | ||
|
|
||
| FastMCP provides an OpenAI-compatible handler that works with OpenAI's API and compatible providers. It supports the full sampling API including tools, automatically converting your Python functions to OpenAI's function calling format. | ||
| FastMCP provides built-in handlers for [OpenAI and Anthropic APIs](/clients/sampling#built-in-handlers). These handlers support the full sampling API including tools, automatically converting your Python functions to each provider's format. | ||
|
|
||
| <Note> | ||
| The OpenAI handler requires the `openai` package. Install it with: | ||
| ```bash | ||
| pip install fastmcp[openai] | ||
| # or | ||
| pip install openai | ||
| ``` | ||
| You'll also need to set the `OPENAI_API_KEY` environment variable or pass it directly to the client. | ||
| Install handlers with `pip install fastmcp[openai]` or `pip install fastmcp[anthropic]`. | ||
| </Note> | ||
|
|
||
| ```python | ||
| import os | ||
| from openai import OpenAI | ||
| from fastmcp import FastMCP | ||
| from fastmcp.client.sampling.handlers.openai import OpenAISamplingHandler | ||
|
|
||
| server = FastMCP( | ||
| name="My Server", | ||
| sampling_handler=OpenAISamplingHandler( | ||
| default_model="gpt-4o-mini", | ||
| client=OpenAI(api_key=os.getenv("OPENAI_API_KEY")), | ||
| ), | ||
| sampling_handler=OpenAISamplingHandler(default_model="gpt-4o-mini"), | ||
| sampling_handler_behavior="fallback", | ||
| ) | ||
| ``` | ||
|
|
||
| Or with Anthropic: | ||
|
|
||
| ```python | ||
| from fastmcp import FastMCP | ||
| from fastmcp.client.sampling.handlers.anthropic import AnthropicSamplingHandler | ||
|
|
||
| server = FastMCP( | ||
| name="My Server", | ||
| sampling_handler=AnthropicSamplingHandler(default_model="claude-sonnet-4-5"), | ||
| sampling_handler_behavior="fallback", | ||
| ) | ||
| ``` | ||
|
Comment on lines
+446
to
474
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the missing prerequisite for Anthropic: how you provide credentials. Suggested doc tweak (minimal):
🤖 Prompt for AI Agents |
||
|
|
||
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: jlowin/fastmcp
Length of output: 109
🏁 Script executed:
Repository: jlowin/fastmcp
Length of output: 251
🏁 Script executed:
Repository: jlowin/fastmcp
Length of output: 107
🌐 Web query:
Anthropic Python SDK best practices for API key handling in documentation examples and AsyncAnthropic vs Anthropic client usage💡 Result:
Summary and recommendations
Minimal example to show in docs (safe pattern):
Sources
[1] Anthropic — API Key Best Practices (support article). (support.anthropic.com)
[2] anthropics/anthropic-sdk-python README (usage + AsyncAnthropic examples). (github.com)
[3] Anthropic docs — Getting started / Authentication. (docs.anthropic.com)
[4] Anthropic — Managing API Key Environment Variables in Claude Code. (support.anthropic.com)
Citations:
🏁 Script executed:
Repository: jlowin/fastmcp
Length of output: 15874
🏁 Script executed:
Repository: jlowin/fastmcp
Length of output: 84
🏁 Script executed:
# Look at the imports and class structure head -50 src/fastmcp/client/sampling/handlers/anthropic.pyRepository: jlowin/fastmcp
Length of output: 1444
Use environment variables for API keys; avoid hard-coded placeholders in docs.
The example
Anthropic(api_key="your-key")encourages committing secrets to code. Instead, useAnthropic()(relies onANTHROPIC_API_KEYenv var by default) or explicitly passos.environ["ANTHROPIC_API_KEY"]. The handler accepts only the synchronousAnthropicclient, notAsyncAnthropic.Suggested edit:
Or simpler, omit
api_keyentirely since the SDK readsANTHROPIC_API_KEYby default.🤖 Prompt for AI Agents