Securing FastMCP Servers with Authentication

Protect remote servers with JWT verification, token introspection or OAuth, and adopt habits that keep secrets safe.

Request log showing authorised and rejected calls to a protected FastMCP server

Key takeaways

  • Pass a verifier to FastMCP(..., auth=verifier).
  • Use JWTVerifier with JWKS when your identity provider publishes keys.
  • StaticTokenVerifier is for development only.
  • Keep secrets in environment variables and always use HTTPS.

A server that runs over HTTP is reachable by anyone who knows its address. Authentication ensures that only approved clients can list and call its tools. FastMCP builds this in through a single auth parameter, and this guide shows how to use it for every common scenario.

Why Remote Servers Need Authentication

MCP tools often have real power: they read databases, send messages and modify records. Without authentication, anyone who discovers the endpoint can use that power. The FastMCP deployment guide strongly recommends authentication for remote servers, and some clients refuse to connect without it.

STDIO servers rarely need this, because the client launches them locally under the user’s own account. The guidance below applies to the HTTP transport.

How Authentication Works

Clients send a token in the Authorization: Bearer <token> header. Before any request reaches your tools, the server passes that token to a verifier, which checks its signature, issuer, audience, expiry and, optionally, its scopes. Requests that fail are rejected with an HTTP 401 response, and your tool code never runs.

VerifierUse it whenProduction-ready
JWTVerifier with JWKSYour identity provider publishes signing keysYes
JWTVerifier with a key or secretYou hold a public key or a shared HMAC secretYes
IntrospectionTokenVerifierTokens are opaque and must be checked with the providerYes
OAuth providersUsers sign in with an existing accountYes
StaticTokenVerifierLocal development and testsNo

Verifying JWTs from an Identity Provider

If your organisation already issues JSON Web Tokens, point FastMCP at the provider’s public keys:

from fastmcp import FastMCP
from fastmcp.server.auth.providers.jwt import JWTVerifier

verifier = JWTVerifier(
    jwks_uri="https://auth.yourcompany.com/.well-known/jwks.json",
    issuer="https://auth.yourcompany.com",
    audience="mcp-production-api",
)

mcp = FastMCP(name="Protected API", auth=verifier)

The verifier fetches signing keys from the JWKS endpoint, so key rotation on the provider’s side needs no change to your server. If your provider signs with a non-default algorithm, such as Ed25519, pass it with the algorithm argument.

Using a static public key

When no JWKS endpoint is available, supply the public key directly:

verifier = JWTVerifier(
    public_key=public_key_pem,  # "-----BEGIN PUBLIC KEY-----..."
    issuer="https://auth.yourcompany.com",
    audience="mcp-production-api",
)

Using a shared secret

Internal services sometimes sign tokens with a shared HMAC secret. Despite its name, the public_key argument accepts that secret too:

import os

verifier = JWTVerifier(
    public_key=os.environ["JWT_SECRET"],  # at least 32 characters
    issuer="internal-auth-service",
    audience="mcp-internal-api",
    algorithm="HS256",
)

Static Tokens for Development

During local development, a fixed list of tokens is often enough:

from fastmcp.server.auth.providers.jwt import StaticTokenVerifier

verifier = StaticTokenVerifier(
    tokens={
        "dev-alice-token": {"client_id": "alice@company.com", "scopes": ["read:data", "write:data"]},
        "dev-guest-token": {"client_id": "guest-user", "scopes": ["read:data"]},
    },
    required_scopes=["read:data"],
)

mcp = FastMCP(name="Development Server", auth=verifier)

Clients then send Authorization: Bearer dev-alice-token. The required_scopes list means every token must include read:data to be accepted.

Static tokens are stored in plain text. Use them for development and tests only, never in production.

Opaque Tokens and Introspection

Some providers issue opaque tokens that cannot be decoded locally. In that case the server asks the provider whether a token is valid, following RFC 7662:

import os
from fastmcp.server.auth.providers.introspection import IntrospectionTokenVerifier

verifier = IntrospectionTokenVerifier(
    introspection_url="https://auth.yourcompany.com/oauth/introspect",
    client_id="mcp-resource-server",
    client_secret=os.environ["INTROSPECTION_SECRET"],
    required_scopes=["api:read"],
)

Introspection adds a network call to each request, so it suits servers where immediate revocation matters more than the last few milliseconds of latency.

OAuth Providers

For servers that let people sign in with an existing account, FastMCP includes ready-made providers for services such as GitHub, Google, Discord, WorkOS, Azure, Clerk and Hugging Face. Each provider has its own configuration page in the official documentation, covering client IDs, redirect URLs and scopes.

OAuth is the right choice when the people using an AI application should act under their own identity, so that the server can apply their individual permissions.

Designing Scopes

Scopes describe what a token may do. A small, consistent set is easier to reason about than many fine-grained ones:

  • read:data for listing and reading;
  • write:data for changes;
  • admin:users for administrative actions.

Grant each client the narrowest set it needs. A reporting dashboard, for example, should never hold a token that can write.

Sending Tokens from a Client

The FastMCP client attaches a bearer token through its auth argument:

import os
from fastmcp import Client

client = Client("https://mcp.example.com/mcp", auth=os.environ["MCP_TOKEN"])

Good Security Habits

  • Keep secrets out of code. Read keys and client secrets from environment variables or a secrets manager.
  • Always use HTTPS. Tokens sent over plain HTTP can be intercepted.
  • Keep health checks separate. Custom routes bypass the authentication middleware, so never expose sensitive data through them.
  • Prefer short-lived tokens. They limit the damage if one leaks.
  • Hide internal errors. mask_error_details=True stops unexpected exception details reaching clients.
  • Restrict origins. If browser clients connect, list exact CORS origins and consider host and origin protection.

Testing a Protected Server

Confirm both outcomes before you deploy: a request with a valid token should succeed, and a request without one, or with an expired one, should receive a 401. A StaticTokenVerifier makes the positive case easy to automate in a test suite, while the production verifier is configured through environment variables in each deployed environment.

Frequently Asked Questions

Usually not. A STDIO server runs as a local process launched by the client under the user's own account, so there is no network endpoint for others to reach.

No. Custom routes created with @mcp.custom_route bypass the authentication middleware, which suits load-balancer probes but means they must never expose sensitive data.

If your identity provider issues JWTs and publishes a JWKS endpoint, use JWTVerifier. For opaque tokens, use introspection. For user sign-in with existing accounts, use one of the OAuth providers.

In the Authorization: Bearer <token> header. The FastMCP client does this for you when you pass the token through its auth argument.

Keep reading

Related Guides