Model Context Protocol (MCP) Enterprise Security: Implementing OAuth 2.0 Dynamic Client Registration (RFC 7591) and Scope-Based Access Delegation
Why Static Bearer Tokens Fail in Remote MCP Deployments
Early Model Context Protocol (MCP) integrations ran locally over stdio, where inheriting the developer's local .env credentials was acceptable for single-user workflows.
As engineering organizations move to remote HTTP/SSE MCP gateways shared across hundreds of developers and CI agents, static API keys introduce severe security gaps:
- Zero User Attribution: Audit logs cannot distinguish whether a destructive database query came from an intern's IDE or a production SRE bot.
- Over-Privileged Blast Radius: Every client connecting to the MCP server inherits the same monolithic service account permissions.
- Manual Client Provisioning: Registering every new IDE client or agent instance manually in Okta/Auth0 does not scale.
The MCP Auth Architecture: RFC 7591 + PKCE + Tool Scopes
The updated MCP Authorization specification adopts OAuth 2.1 with Dynamic Client Registration (RFC 7591) and Protected Resource Metadata (RFC 9728):
text┌──────────────────────┐ 1. GET /.well-known/oauth-protected-resource │ MCP Client / IDE │ ──────────────────────────────────────────────────► ┌──────────────────────┐ └──────────┬───────────┘ │ Remote MCP Server │ │ 2. POST /register (RFC 7591 Dynamic Client Reg) └──────────┬───────────┘ ▼ │ ┌──────────────────────┐ 3. Browser PKCE Login + Consent │ │ Identity Provider │ ◄──────────────────────────────────────────────────────────────┤ │ (Okta / Auth0 / KC) │ 4. Short-Lived JWT (scopes: mcp:tools:read) │ └──────────┬───────────┘ │ │ 5. Authorization: Bearer <jwt> │ └────────────────────────────────────────────────────────────────────────────► Verify JWT & Tool Scope
Implementing Scope-Gated MCP Tools in Python
Here is how to enforce granular OAuth scopes per MCP tool using FastAPI middleware and JWT verification:
pythonfrom functools import wraps from fastapi import FastAPI, HTTPException, Request import jwt app = FastAPI() JWKS_PUBLIC_KEY = "..." # Loaded from IdP /.well-known/jwks.json def require_mcp_scope(required_scope: str): def decorator(func): @wraps(func) async def wrapper(request: Request, *args, **kwargs): auth_header = request.headers.get("Authorization", "") if not auth_header.startswith("Bearer "): raise HTTPException( status_code=401, headers={"WWW-Authenticate": 'Bearer resource_metadata="/.well-known/oauth-protected-resource"'}, detail="Missing bearer token", ) token = auth_header.removeprefix("Bearer ").strip() claims = jwt.decode(token, JWKS_PUBLIC_KEY, algorithms=["RS256"], audience="https://mcp.internal.dev") granted_scopes = set(claims.get("scope", "").split()) if required_scope not in granted_scopes: raise HTTPException( status_code=403, detail=f"Insufficient scope: requires '{required_scope}'" ) return await func(request, user_sub=claims["sub"], *args, **kwargs) return wrapper return decorator @app.get("/.well-known/oauth-protected-resource") async def protected_resource_metadata(): return { "resource": "https://mcp.internal.dev", "authorization_servers": ["https://auth.internal.dev"], "scopes_supported": ["mcp:postgres:read", "mcp:postgres:write", "mcp:github:pr"], } @require_mcp_scope("mcp:postgres:read") async def tool_query_metrics(request: Request, user_sub: str, sql: str): # Executed with full user attribution in audit logs return {"executed_by": user_sub, "rows": []}
Production Checklist
- Always enforce
resourceaudience binding (RFC 8707): Prevents a token issued for a Slack MCP server from being replayed against your Production Postgres MCP server. - Keep access token TTLs under 15 minutes: Pair with rotating refresh tokens stored in OS Keychains (
keytar/ macOS Keychain). - Separate Read vs Mutate scopes: Never bundle
readandwriteunder a single genericmcp:accessscope.