Features
- Auth0 Authentication: Secure your MCP tools with Auth0
- Token Management: Refresh tokens and manage user sessions
- Protected Endpoints: Demonstrate authenticated tool access
- Zero-Config Service Discovery: Services are automatically discovered from the
./mcpdirectory - Concurrency-Safe: The
authUservariable is implemented using AsyncLocalStorage for safe concurrent request handling
Prerequisites
- Node.js 18+ installed
- An Auth0 account and application configured
- Auth0 API configured with appropriate permissions
Setup
Configure Auth0
- Create an Auth0 application (Regular Web Application or Single Page Application)
- Create an Auth0 API
- Note your Domain, Client ID, Client Secret, and API Audience
Create protected demo service
Create
mcp/demo/index.ts with authenticated endpoints:mcp/demo/index.ts
Running the Server
- Development
- Production
http://localhost:3000Authentication Flow
Obtain tokens from Auth0
Use Auth0’s authentication flow (e.g., Authorization Code Flow) to obtain:
access_tokenid_tokenrefresh_token
Use protected tools
Pass the
id_token via _meta.authorization.token in your MCP request.
The @Authenticated decorator automatically extracts and verifies the token.Available Tools
AuthService
refreshToken
Refresh an expired access token using a refresh token.
Input:
getAuthInfo
Get information about the authentication configuration.
Output:
DemoService (Protected)
getUserProfile
Get the authenticated user’s profile information.
Output:
echo
Echo back a message with user information.
Input:
How It Works
Service Discovery
The MCP server automatically discovers and registers all services exported from files in the./mcp directory.
Authentication
- The
@Authenticateddecorator protects tools that require authentication - Auth0 JWT tokens are verified using JWKS (JSON Web Key Set)
- Token verification includes signature validation, expiration checks, and issuer/audience verification
- The
authUservariable is automatically available in protected methods - Concurrency Safe:
authUseris implemented as a getter that reads from AsyncLocalStorage
Package.json Scripts
package.json