@grantex/mcp-auth — a single function call that handles the entire OAuth stack.
Why MCP Servers Need Authentication
Without authentication, any MCP client that can reach your server can call any tool with full access. In a local development setup, this is fine. In production — where your MCP server reads databases, calls APIs, or modifies infrastructure — it is a serious exposure. Consider a concrete scenario: you build an MCP server that gives Claude Desktop access to your company’s Jira instance. Without auth, anyone who discovers the server URL can list tickets, create issues, and close sprints. With auth, each user gets a scoped token that limits what they can do and expires automatically. MCP authentication solves three problems:- Identity. You know who is calling your tools — which user, which agent, which client application.
- Scoped access. You control what they can do. A read-only user cannot call write tools, even if the MCP client sends the request.
- Revocation. You can cut off access immediately when a user leaves, a session expires, or an agent misbehaves.
Setting Up MCP OAuth Authentication with @grantex/mcp-auth
Step 1: Install Dependencies
@grantex/mcp-auth works in both managed (Grantex Cloud) and self-hosted modes. The API is identical — just point the SDK at a different base URL.
Step 2: Create the Auth Server
ThecreateMcpAuthServer() function spins up a fully compliant OAuth 2.1 authorization server as a Fastify instance. It registers six endpoints that MCP clients auto-discover:
| Endpoint | RFC | Purpose |
|---|---|---|
/.well-known/oauth-authorization-server | RFC 8414 | Metadata discovery for MCP clients |
/register | RFC 7591 | Dynamic Client Registration |
/authorize | OAuth 2.1 | Authorization endpoint (PKCE required) |
/token | OAuth 2.1 | Token exchange and refresh |
/introspect | RFC 7662 | Token introspection |
/revoke | RFC 7009 | Token revocation |
Step 3: Protect Your MCP Routes with Middleware
Use the Express middleware to validate tokens on every incoming request:- Fetches the JWKS from
{issuer}/.well-known/jwks.json(cached after first request) - Validates the JWT signature, expiry, and issuer
- Checks that all required scopes are present in the token
- Attaches the decoded grant to
req.mcpGrantfor your handler to use - Returns 401 for missing/invalid tokens and 403 for insufficient scopes
Step 4: Verify Client Auto-Discovery
MCP clients find your auth server automatically via the well-known metadata endpoint:How the PKCE Authorization Flow Works
When an MCP client connects to your authenticated server, the full OAuth 2.1 + PKCE flow runs automatically:- Discovery. The client fetches
/.well-known/oauth-authorization-serverto find endpoints. - Registration. The client calls
/registerto get aclient_id(Dynamic Client Registration per RFC 7591). - Authorization. The client redirects the user to
/authorizewith a PKCEcode_challenge(S256). The user sees a consent screen listing the requested scopes. - Consent. The user approves or denies. On approval, the auth server redirects back with an authorization code.
- Token exchange. The client sends the code and
code_verifierto/token. The server verifies PKCE, issues a JWT access token and a refresh token. - API calls. The client includes the token as a Bearer token on all subsequent MCP requests.
- Refresh. When the token expires, the client uses the refresh token to get a new one without re-prompting the user.
Customizing the Consent UI
The default consent page works out of the box, but you can brand it:Lifecycle Hooks for Audit and Monitoring
React to authorization events for audit logging, analytics, or downstream notifications:Token Introspection and Revocation
Resource servers can validate tokens by calling the introspection endpoint directly:Managed vs Self-Hosted
@grantex/mcp-auth works in two modes:
| Feature | Managed (Grantex Cloud) | Self-Hosted |
|---|---|---|
| Setup | Point SDK at Grantex Cloud | Point SDK at your infrastructure |
| Client Store | In-memory (stateless, horizontal scale) | Bring your own (Postgres, Redis) |
| Token Signing | Grantex signs tokens | Grantex signs (delegated) |
| Rate Limiting | Built-in per-endpoint | Built-in, configurable |
| Audit Trail | Full audit via Grantex events | Full audit via Grantex events |
| Uptime SLA | 99.9% | Your responsibility |
Next Steps
- MCP Auth Server docs — full configuration reference
- MCP integration docs — Grantex MCP server with 13 tools for Claude Desktop
- Self-hosting guide — run the entire Grantex stack on your infrastructure
- Quickstart — get a Grantex API key in under 5 minutes
- GitHub — open source, Apache 2.0