mcp-server-auth-template

mcp-server-auth-template

A reusable template for building MCP servers that act as OAuth 2.1 resource servers, verifying bearer tokens against Microsoft Entra ID or any standards-compliant OIDC authorization server.

Category
Visit Server

README

mcp-server-auth-template

quality python license

Leia em português

A reusable template for an MCP server that acts as an OAuth 2.1 resource server - never an authorization server - against either Microsoft Entra ID or any standards-compliant OIDC authorization server (Auth0, Keycloak, WorkOS AuthKit, ...). Targets the MCP 2026-07-28 specification.

The MCP 2026-07-28 authorization spec models every remote MCP server this way: it verifies bearer tokens minted elsewhere, it never mints them itself. Entra ID also can't act as a full MCP authorization server for arbitrary clients (no Dynamic Client Registration, no Client ID Metadata Documents), so a real integration needs an adapter either way. This template is that adapter, built once, correctly, so a new MCP server doesn't have to re-derive JWKS caching, issuer/audience checks, and Entra's split scp/roles claim shape from scratch. See docs/adr/0002-oauth21-resource-server.md for the full reasoning, and the companion repository, mcp-client-auth-template, for the client-side half of this pattern.

Auth quick start

  1. Copy .env.example to .env and fill in one of the two provider blocks (Entra ID or a generic OIDC authorization server).

  2. Run the server:

    uv run uvicorn mcp_server_auth_template.entrypoints.mcp_server:create_app --factory --reload
    
  3. Protected Resource Metadata is served automatically at /.well-known/oauth-protected-resource - point an MCP client at http://localhost:8000/mcp and it will discover the configured authorization server from there:

    {
      "resource": "https://mcp.example.invalid/",
      "authorization_servers": ["https://as.example.invalid"],
      "bearer_methods_supported": ["header"]
    }
    

    A request with no (or an invalid) bearer token gets a 401 with a WWW-Authenticate header pointing back at that same metadata document, exactly as the spec requires - the server never issues its own login page:

    HTTP/1.1 401 Unauthorized
    www-authenticate: Bearer error="invalid_token", error_description="Authentication required",
      resource_metadata="https://mcp.example.invalid/.well-known/oauth-protected-resource"
    
  4. Two example tools are registered: whoami returns the identity carried by the caller's token (client ID, subject, scopes), and health is a liveness check for an already-authenticated caller.

Swap MCP_SERVER_AUTH_PROVIDER between entra and generic to switch adapters - no other code changes. See src/mcp_server_auth_template/adapters/ for the two TokenVerifier implementations and tests/unit/test_*_token_verifier.py for how each is tested offline with a locally-signed JWT (no network, no real IdP needed).

Authentication flow

docs/ARCHITECTURE.md has a sequence diagram of the full bearer-token round trip - the 401 challenge, Protected Resource Metadata discovery, token acquisition on the authorization server (out of scope for this repo), and signature/issuer/audience verification on every subsequent call. See Diagrams.

Development

uv lock --check
uv sync --frozen --all-groups --extra observability
uv run pytest
uv run python scripts/quality_gate.py

List or select gate checks with --list and --check NAME. See AGENTS.md for build, lint, format, typecheck, test, security, architecture, MCP, and completion requirements, and docs/DEVELOPMENT.md for the container build and local setup.

Codex loads the checked-in .codex/config.toml, .codex/hooks.json, and .agents/skills/ only within the appropriate project/trust context. Review lifecycle hooks with /hooks before use.

License

MIT

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured