Hello everyone,
I’m having an issue with the following scenario and would appreciate any feedback or suggestions.
Background
I’m following the official documentation for FastMCP’s Bearer authentication:
https://gofastmcp.com/servers/auth/bearer
I set up a test MCP server as follows:
from fastmcp import FastMCP
from fastmcp.server.auth import BearerAuthProvider
from fastmcp.server.auth.providers.bearer import RSAKeyPair
# Generate a new key pair
key_pair = RSAKeyPair.generate()
# Configure the auth provider with the public key
auth = BearerAuthProvider(
public_key=key_pair.public_key,
issuer="https://dev.example.com",
audience="my-dev-server"
)
# Initialize the MCP server
mcp = FastMCP(name="Development Server", auth=auth)
Then, in my Agent code, I generate a token and configure the client:
from fastmcp.server.auth.providers.bearer import RSAKeyPair
from fastmcp.client.http import StreamableHTTPClientParams
from fastmcp.tools import MCPTools
# Generate another key pair for the client (actually should use the same one?)
key_pair = RSAKeyPair.generate()
# Create a test JWT
token = key_pair.create_token(
subject="dev-user",
issuer="https://dev.example.com",
audience="my-dev-server",
scopes=["read", "write"]
)
# Prepare HTTP client parameters with the Bearer token
server_params = StreamableHTTPClientParams(
url=MY_MCP_URL,
headers={
"Authorization": f"Bearer {token}",
}
)
# Use the MCPTools context
async with MCPTools(server_params=server_params) as mcp_tools:
# …perform calls…
Problem
Whenever I run the agent, I receive immediately :
401 Unauthorized
Any suggestion?
Thanks in advance for your help!