Authentication
mcpfy supports authentication for MCP servers exposed over HTTP. Authentication allows an MCP server to verify incoming access tokens before allowing requests to reach protected MCP endpoints.Authentication Configuration
Authentication is configured through theauth property of MCPServer.
The authentication configuration uses OAuth as the authentication type:
type— authentication mechanism. For the supported OAuth configuration, use"oauth".verifyToken— function used to verify incoming access tokens.authorizationServers— list of authorization server URLs.
authorizationServers is required for the OAuth authentication configuration.
OAuth Authentication
mcpfy represents protected-resource authentication using OAuth metadata. A typical configuration is:JWT / JWKS Verification
mcpfy provides a JWKS-based verifier for validating JWT access tokens. A verifier can be created with:Issuer
Theissuer identifies the authorization server that issued the token.
JWKS URI
ThejwksUri identifies the endpoint containing the public keys used to verify JWT signatures.
Audience
Theaudience identifies the intended recipient of the token.
Complete JWT Verification Example
- Declares the MCP server as OAuth-protected.
- Configures JWT verification through JWKS.
- Identifies the authorization server.
- Exposes the MCP server over HTTP.
Protected Resource Metadata
When OAuth authentication is configured, mcpfy also exposes protected-resource metadata through the standard:Authorization Servers
The authorization server is specified through:Forwarding Authentication Headers
When an authenticated MCP request needs to make an upstream request, mcpfy provides helpers for forwarding supported authentication headers. The relevant APIs are:extractForwardableAuthHeaders
Use this helper to extract headers that are allowed to be forwarded.
extractForwardableAuthHeaders() accepts a Node IncomingMessage and returns the
allowlisted headers. forwardAuthHeaders() accepts a tool-context-shaped object with
requestHeaders (or auth) and prepares headers for an upstream request.
forwardAuthHeaders
The forwarding helper can be used when making an authenticated upstream request.
FORWARDABLE_AUTH_HEADER_NAMES
The SDK also exposes:
Authentication in Tool Context
Authentication can be used together with tool execution. For example:Authentication Flow
A typical protected MCP request follows this flow:Authentication with Custom Authorization Server
The authorization server does not have to use a specific provider. The important requirement is that the server configuration supplies the appropriate issuer, JWKS endpoint, audience, and authorization-server metadata. For example:Client-Side OAuth Helpers
mcpfy also provides helpers for MCP clients that need to complete an OAuth authorization flow.NodeOAuthClientProvider
The Node OAuth client provider is created using its static create() method.
ensureAuthorized
ensureAuthorized() ensures that the OAuth provider is authorized for a specific server URL.
It requires both the provider and the server URL:
Security Recommendations
Use HTTPS in production
Authentication tokens should be transmitted over HTTPS in production environments. Avoid sending bearer tokens over unencrypted HTTP.Validate the issuer
Configure the verifier with the expected authorization-server issuer:Validate the audience
Use an audience value appropriate for the MCP server:Protect signing keys
The MCP server uses the authorization server’s public JWKS keys to verify JWT signatures. Private signing keys should remain under the control of the authorization server.Do not forward arbitrary headers
When making upstream requests, use mcpfy’s authentication-header forwarding helpers instead of copying all inbound headers.Troubleshooting
type: "jwt" is rejected
Use:
"oauth" as its discriminant.
authorizationServers is missing
OAuth configuration requires:
Token verification fails
Check:issuerjwksUriaudience- JWT signature
- token expiration
- authorization-server configuration
OAuth metadata is not available
Verify that HTTP authentication is configured using:Summary
A basic authenticated MCP server uses:- OAuth authentication configuration
- JWT verification
- JWKS public-key discovery
- Authorization-server metadata
- Protected-resource metadata
- Authentication-header forwarding
- OAuth client helpers

