Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
DEPRECATED: Replaced by mcp-app-builder. Previously used to build MCP servers with tools, resources, and prompts via mcp-use.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/authentication/clerk.md
1# Clerk Authentication23Setting up OAuth with Clerk. DCR mode only — MCP clients register themselves directly with Clerk via Dynamic Client Registration; the MCP server only verifies the resulting JWTs against Clerk's JWKS.45**Learn more:** [Clerk OAuth Docs](https://clerk.com/docs/guides/configure/auth-strategies/oauth/how-clerk-implements-oauth) · [Runnable example](https://github.com/mcp-use/mcp-use/tree/main/libraries/typescript/packages/mcp-use/examples/server/oauth/clerk)67---89## Quick Start1011```typescript12import { MCPServer, oauthClerkProvider, object } from "mcp-use/server";1314const server = new MCPServer({15name: "my-server",16version: "1.0.0",17oauth: oauthClerkProvider(),18});1920server.tool(21{ name: "whoami", description: "Get authenticated user info" },22async (_args, ctx) =>23object({24userId: ctx.auth.user.userId,25email: ctx.auth.user.email,26name: ctx.auth.user.name,27})28);2930server.listen();31```3233With a `.env` file:3435```bash36# Development: https://[verb-noun-##].clerk.accounts.dev37# Production: https://clerk.[YOUR_APP_DOMAIN].com38MCP_USE_OAUTH_CLERK_FRONTEND_API_URL=https://verb-noun-42.clerk.accounts.dev39```4041That's it. JWT verification, OAuth discovery, and `.well-known` passthrough are handled automatically.4243---4445## Setup46471. Sign up at [clerk.com](https://clerk.com) and create an application.482. **Clerk Dashboard → Configure → OAuth Applications** — enable **Dynamic Client Registration**. This is required for MCP clients to self-register.493. **Clerk Dashboard → Configure → API Keys** — copy the **Frontend API URL** (shown in the `.env.local` quickstart).50- Development example: `https://verb-noun-42.clerk.accounts.dev`51- Production example: `https://clerk.yourdomain.com`5253---5455## Environment Variables5657| Variable | Required | Description |58|----------|----------|-------------|59| `MCP_USE_OAUTH_CLERK_FRONTEND_API_URL` | Yes | Clerk Frontend API URL (e.g. `https://verb-noun-42.clerk.accounts.dev`) |6061---6263## Configuration Options6465Zero-config (reads from env vars):6667```typescript68oauth: oauthClerkProvider()69```7071Explicit config (overrides env vars):7273```typescript74oauth: oauthClerkProvider({75frontendApiUrl: "https://verb-noun-42.clerk.accounts.dev",76verifyJwt: process.env.NODE_ENV === "production", // default: true77})78```7980| Option | Type | Default | Description |81|--------|------|---------|-------------|82| `frontendApiUrl` | `string` | env var | Clerk Frontend API URL |83| `verifyJwt` | `boolean?` | `true` | Set `false` to skip JWT verification (**development only**) |8485---8687## User Context8889Clerk populates these fields on `ctx.auth.user`:9091| Field | Type | Source |92|-------|------|--------|93| `userId` | `string` | `sub` claim |94| `email` | `string?` | `email` claim |95| `name` | `string?` | `name` claim |96| `roles` | `string[]?` | `roles` claim |97| `permissions` | `string[]?` | `permissions` claim |9899### Organization Context100101Clerk includes the active organization on the JWT payload. The fields are non-standard, so cast off `ctx.auth.user`:102103```typescript104server.tool(105{ name: "get-organization-info", description: "Get the active organization" },106async (_args, ctx) => {107const { org_id, org_role, org_slug } = ctx.auth.user as {108org_id?: string;109org_role?: string;110org_slug?: string;111};112113if (!org_id) {114return error("No active organization. Select one in Clerk first.");115}116117return object({ org_id, org_role, org_slug });118}119);120```121122---123124## Common Mistakes125126- **DCR not enabled** — If MCP Inspector stalls at the registration step, confirm **Dynamic Client Registration** is enabled in Clerk Dashboard → Configure → OAuth Applications. Without DCR, there is no `client_id` for MCP clients to obtain.127- **Wrong Frontend API URL** — Must be the full URL (with `https://`), not just the subdomain.128- **Skipping JWT verification in production** — `verifyJwt: false` is development only.129130---131132## Next Steps133134- **Auth overview** → [overview.md](overview.md)135- **Auth0 setup** → [auth0.md](auth0.md)136- **WorkOS setup** → [workos.md](workos.md)137- **Build tools** → [../server/tools.md](../server/tools.md)138