apigene/clerk-mcp-server
If you are the rightful owner of clerk-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
This is a sample Next.js application that demonstrates how to build a Model Context Protocol (MCP) server with Clerk authentication.
Clerk MCP Server
A sample Next.js application that demonstrates how to build an MCP (Model Context Protocol) server with Clerk authentication.
Features
- š Secure MCP server with Clerk OAuth authentication
- š ļø Example tool:
get-clerk-user-data
that retrieves authenticated user information - š” Support for both HTTP and SSE transport protocols
- šļø Built with Next.js 14 and TypeScript
- š OAuth metadata endpoints for MCP compliance
Prerequisites
- Node.js 18+
- A Clerk account (sign up at clerk.com)
Setup
-
Clone and install dependencies:
npm install
-
Set up Clerk:
- Go to your Clerk Dashboard
- Create a new application or use an existing one
- Copy your publishable key and secret key
-
Configure environment variables:
cp env.example .env.local
Then edit
.env.local
and add your Clerk keys:NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_your_publishable_key_here CLERK_SECRET_KEY=sk_test_your_secret_key_here
-
Run the development server:
npm run dev
-
Open your browser: Navigate to http://localhost:3000
MCP Server Endpoints
The MCP server is available at the following endpoints:
- HTTP Transport:
http://localhost:3000/mcp
- SSE Transport:
http://localhost:3000/sse
OAuth Metadata Endpoints
The server exposes the required OAuth metadata endpoints:
- Authorization Server Metadata:
http://localhost:3000/.well-known/oauth-authorization-server
- Protected Resource Metadata:
http://localhost:3000/.well-known/oauth-protected-resource/mcp
Available Tools
get-clerk-user-data
Retrieves information about the authenticated Clerk user.
Parameters: None
Returns: JSON string containing user data
Example Response:
{
"id": "user_123",
"emailAddresses": [...],
"firstName": "John",
"lastName": "Doe",
...
}
Project Structure
clerk-mcp-server/
āāā app/
ā āāā [transport]/
ā ā āāā route.ts # MCP server implementation
ā āāā .well-known/
ā ā āāā oauth-authorization-server/
ā ā ā āāā route.ts # OAuth server metadata
ā ā āāā oauth-protected-resource/
ā ā āāā mcp/
ā ā āāā route.ts # Protected resource metadata
ā āāā layout.tsx # Root layout
ā āāā page.tsx # Home page
ā āāā globals.css # Global styles
āāā middleware.ts # Clerk middleware
āāā package.json # Dependencies
āāā next.config.js # Next.js config
āāā tsconfig.json # TypeScript config
āāā env.example # Environment variables template
āāā README.md # This file
Adding Custom Tools
To add your own MCP tools, modify the app/[transport]/route.ts
file:
server.tool(
'your-tool-name',
'Description of what your tool does',
{
// Define your tool parameters here
paramName: {
type: 'string',
description: 'Parameter description'
}
},
async (params, { authInfo }) => {
// Your tool implementation
const userId = authInfo!.extra!.userId! as string
// Your logic here...
return {
content: [{ type: 'text', text: 'Your response' }],
}
},
)
Testing with MCP Clients
You can test the MCP server with any MCP-compatible client. The server supports:
- OAuth 2.0 authentication with Clerk
- HTTP and SSE transport protocols
- Standard MCP tool invocation
Deployment
This project can be deployed to Vercel, Netlify, or any other Next.js-compatible hosting platform. Make sure to:
- Set the environment variables in your hosting platform
- Configure your Clerk application's allowed origins
- Update OAuth redirect URLs if needed
Learn More
License
MIT