Anthropic
Call FastMCP servers from the Anthropic API
Anthropic supports MCP servers through the MCP connector feature in the Messages API, allowing you to extend AI capabilities with custom tools from remote MCP servers.
Messages API
Anthropic’s Messages API supports MCP servers as remote tool sources. This tutorial will show you how to create a FastMCP server and deploy it to a public URL, then how to call it from the Messages API.
Currently, the MCP connector only accesses tools from MCP servers—it queries the list_tools
endpoint and exposes those functions to Claude. Other MCP features like resources and prompts are not currently supported. You can read more about the MCP connector in the Anthropic documentation.
Create a Server
First, create a FastMCP server with the tools you want to expose. For this example, we’ll create a server with a single tool that rolls dice.
Deploy the Server
Your server must be deployed to a public URL in order for Anthropic to access it. The MCP connector supports both SSE and Streamable HTTP transports.
For development, you can use tools like ngrok
to temporarily expose a locally-running server to the internet. We’ll do that for this example (you may need to install ngrok
and create a free account), but you can use any other method to deploy your server.
Assuming you saved the above code as server.py
, you can run the following two commands in two separate terminals to deploy your server and expose it to the internet:
This exposes your unauthenticated server to the internet. Only run this command in a safe environment if you understand the risks.
Call the Server
To use the Messages API with MCP servers, you’ll need to install the Anthropic Python SDK (not included with FastMCP):
You’ll also need to authenticate with Anthropic. You can do this by setting the ANTHROPIC_API_KEY
environment variable. Consult the Anthropic SDK documentation for more information.
Here is an example of how to call your server from Python. Note that you’ll need to replace https://your-server-url.com
with the actual URL of your server. In addition, we use /sse
as the endpoint because we deployed an SSE server with the default path; you may need to use a different endpoint if you customized your server’s deployment. At this time you must also include the extra_headers
parameter with the anthropic-beta
header.
If you run this code, you’ll see something like the following output:
Authentication
New in version: 2.6.0
The MCP connector supports OAuth authentication through authorization tokens, which means you can secure your server while still allowing Anthropic to access it.
Server Authentication
The simplest way to add authentication to the server is to use a bearer token scheme.
For this example, we’ll quickly generate our own tokens with FastMCP’s RSAKeyPair
utility, but this may not be appropriate for production use. For more details, see the complete server-side Bearer Auth documentation.
We’ll start by creating an RSA key pair to sign and verify tokens.
FastMCP’s RSAKeyPair
utility is for development and testing only.
Next, we’ll create a BearerAuthProvider
to authenticate the server.
Here is a complete example that you can copy/paste. For simplicity and the purposes of this example only, it will print the token to the console. Do NOT do this in production!
Client Authentication
If you try to call the authenticated server with the same Anthropic code we wrote earlier, you’ll get an error indicating that the server rejected the request because it’s not authenticated.
To authenticate the client, you can pass the token using the authorization_token
parameter in your MCP server configuration:
You should now see the dice roll results in the output.