Features
- Clerk Authentication: Secure your MCP tools with Clerk
- 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
- A Clerk account and application configured
- Clerk Frontend API and Secret Key
Setup
Configure Clerk
- Create a Clerk application at clerk.com
- Enable JWT templates in your Clerk dashboard
- Note your Frontend API (e.g.,
your-app.clerk.accounts.dev) - Get your Secret Key from the API Keys section
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 Clerk
Use Clerk’s authentication flow (e.g., Sign In, Sign Up) 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:
Clerk-Specific Features
JWT Templates
Clerk uses JWT templates to customize token claims. Make sure your JWT template includes:sub(subject/user ID)emailemail_verifiedgiven_name(first name)family_name(last name)
Enable Refresh Tokens
To enable refresh tokens in Clerk: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 - Clerk JWT tokens are verified using JWKS (JSON Web Key Set)
- Token verification includes signature validation and issuer 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