Skip to main content
MCP (Model Context Protocol) is a protocol for exposing tools and resources to AI agents. Read more about MCP here.

Basic MCP Usage

The ready-to-run basic MCP usage example is available here!
1

MCP Configuration

Configure MCP servers using a dictionary with server names and connection details following this configuration format
mcp_config
2

Tool Filtering

Use filter_tools_regex to control which MCP tools are available to the agent
filter_tools_regex

MCP with OAuth

The ready-to-run MCP with OAuth example is available here!
For MCP servers requiring OAuth authentication:
  • Configure OAuth-enabled MCP servers by specifying the URL and auth type
  • The SDK automatically handles the OAuth flow when first connecting
  • When the agent first attempts to use an OAuth-protected MCP server’s tools, the SDK initiates the OAuth flow via FastMCP
  • User will be prompted to authenticate via browser
  • Access tokens are securely stored in ~/.fastmcp/oauth-mcp-client-cache/ and automatically refreshed by FastMCP as needed
mcp_config
OAuth MCP servers require user interaction for the initial browser-based authentication. This means they are not suitable for fully automated/headless workflows. If you need headless access, check if the MCP provider offers API key authentication as an alternative.

Ready-to-Run Basic MCP Usage Example

This example is available on GitHub: examples/01_standalone_sdk/07_mcp_integration.py
Here’s an example integrating MCP servers with an agent:
examples/01_standalone_sdk/07_mcp_integration.py
You can run the example code as-is.
The model name should follow the LiteLLM convention: provider/model_name (e.g., anthropic/claude-sonnet-4-5-20250929, openai/gpt-4o). The LLM_API_KEY should be the API key for your chosen provider.
ChatGPT Plus/Pro subscribers: You can use LLM.subscription_login() to authenticate with your ChatGPT account and access Codex models without consuming API credits. See the LLM Subscriptions guide for details.

Ready-to-Run MCP with OAuth Example

This example is available on GitHub: examples/01_standalone_sdk/08_mcp_with_oauth.py
examples/01_standalone_sdk/08_mcp_with_oauth.py
You can run the example code as-is.
The model name should follow the LiteLLM convention: provider/model_name (e.g., anthropic/claude-sonnet-4-5-20250929, openai/gpt-4o). The LLM_API_KEY should be the API key for your chosen provider.
ChatGPT Plus/Pro subscribers: You can use LLM.subscription_login() to authenticate with your ChatGPT account and access Codex models without consuming API credits. See the LLM Subscriptions guide for details.

Next Steps