MCP with Next.js and Descope

R
Renaudil y a 2 jours
0

Description

Run an Model Context Protocol (MCP) server on Vercel with Next.js and Descope.

npx boilerapp mcp-with-next-js-and-descope

文档

Next.js MCP Server with Vercel MCP Handler and Descope Node SDK

Descope Banner

Introduction

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.

Key Components

  • Vercel's mcp-handler: Handles the MCP protocol communication and serverless function deployment
  • Descope Node SDK: Validates user sessions and provides authentication context
  • Echo Tool: A simple example tool that returns a "Hello, world!" message

Deployment

Deploy with Vercel

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.

Requirements

Before proceeding, make sure you have the following:

Running the Server

First, add the environment variables in a .env file at the root:

NEXT_PUBLIC_DESCOPE_PROJECT_ID=      # Your Descope project ID
NEXT_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).

API Endpoints

  • GET/POST /api/[transport]: Handles incoming MCP protocol messages (supports SSE and HTTP transports)

Authentication

The server uses Descope's Node SDK for session validation. The verifyToken function:

  1. Extracts the bearer token from the request
  2. Uses the Descope Node SDK to validate the session
  3. Returns authentication context including user scopes and client ID
  4. All MCP endpoints require a valid bearer token

Managing API Keys and OAuth Tokens for Tools

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 scopes
const userToken =
  await descopeClient.management.outboundApplication.fetchTokenByScopes(
    "my-app-id\

Prix

Gratuit

FREE

评论 (0)

常见问题

常见问题解答 (FAQ)

有问题?我们有答案。如果您找不到想要的答案,请随时联系我们。

Boilerapp 是一个专门用于分享 Boilerplates、入门套件(Starter Kits)和项目模版的开发者社区平台。我们的目标很简单:为您节省初始配置(Setup)的时间,让您可以专注于真正重要的代码。无论您是在寻找简单的代码库还是完整的 SaaS 项目,都能在这里找到。

还有其他问题?

我们的团队随时为您提供帮助。联系我们,我们将尽快回复。