This example shows how to build an MCP server using the Vercel MCP Handler with Descope's Node SDK for session validation. The server provides a simple echo tool and demonstrates how to integrate Descope authentication with the Model Context Protocol (MCP) using Vercel's serverless functions.
You can connect to the server using the MCP Inspector or any other MCP client. Be sure to include the /api/mcp
path in the connection URL.
Before proceeding, make sure you have the following:
First, add the environment variables in a .env
file at the root:
NEXT_PUBLIC_DESCOPE_PROJECT_ID= # Your Descope project IDNEXT_PUBLIC_DESCOPE_BASE_URL= # Your Descope base URL (optional, defaults to https://api.descope.com)
Then, install dependencies:
npm i
Finally, run the server:
npm run dev
The server will start on port 3000 (or the port specified in your environment variables).
GET/POST /api/[transport]
: Handles incoming MCP protocol messages (supports SSE and HTTP transports)The server uses Descope's Node SDK for session validation. The verifyToken
function:
If you want Descope to manage your API keys or OAuth tokens for your MCP, you can use functions in the Node SDK to fetch outbound app tokens at either a user or tenant level:
// Fetch user token with specific scopesconst userToken =await descopeClient.management.outboundApplication.fetchTokenByScopes("my-app-id","user-id",["read", "write"],{ withRefreshToken: false },"tenant-id");// Fetch latest user tokenconst latestUserToken =await descopeClient.management.outboundApplication.fetchToken("my-app-id","user-id","tenant-id",{ forceRefresh: false });// Fetch tenant token with specific scopesconst tenantToken =await descopeClient.management.outboundApplication.fetchTenantTokenByScopes("my-app-id","tenant-id",["read", "write"],{ withRefreshToken: false });// Fetch latest tenant tokenconst latestTenantToken =await descopeClient.management.outboundApplication.fetchTenantToken("my-app-id","tenant-id",{ forceRefresh: false });