|
| 1 | +--- |
| 2 | +title: Authenticate MCP Servers |
| 3 | +hide_title: true |
| 4 | +sidebar_position: 1 |
| 5 | +pagination_next: null |
| 6 | +pagination_prev: null |
| 7 | +skip_llms_txt: true |
| 8 | +description: >- |
| 9 | + Authenticate MCP server actions using the SuperTokens MCP plugin |
| 10 | + to use with AI tools and agents. |
| 11 | +--- |
| 12 | + |
| 13 | +# Authenticate MCP Servers |
| 14 | + |
| 15 | +This guide explains how to authenticate MCP Servers using the provided code snippet. Follow the steps below to integrate and initialize your MCP server with SuperTokens. |
| 16 | + |
| 17 | +## Overview |
| 18 | + |
| 19 | +This guide covers the following topics: |
| 20 | + |
| 21 | +- Installing the MCP plugin. |
| 22 | +- Initializing the plugin. |
| 23 | +- Registering tools. |
| 24 | +- Initializing the SDK. |
| 25 | + |
| 26 | +We use a sample code snippet to illustrate the server configuration and how to add administrative tools. |
| 27 | + |
| 28 | +## Before you start |
| 29 | + |
| 30 | +Before setting up your MCP servers, ensure you have: |
| 31 | + |
| 32 | +- Installed the required packages (e.g., **mcp-plugin** and its dependencies). |
| 33 | +- Configured your authentication system with SuperTokens. |
| 34 | +- Setup your development environment with Node.js. |
| 35 | + |
| 36 | +Review your permission requirements, as the servers validate user roles to authorize access. |
| 37 | + |
| 38 | +## Steps |
| 39 | + |
| 40 | +### 1. Install the plugin |
| 41 | + |
| 42 | +Install the MCP plugin via npm: |
| 43 | + |
| 44 | +```bash |
| 45 | +npm install mcp-plugin |
| 46 | +``` |
| 47 | + |
| 48 | +
Ensure that all dependencies for SuperTokens are also installed according to your project requirements. |
| 49 | + |
| 50 | +### 2. Initialize the plugin |
| 51 | + |
| 52 | +In your project file, import the necessary components and initialize your MCP server. The following example shows how to create an instance of SuperTokensMCPServer and configure its basic parameters: |
| 53 | + |
| 54 | +```ts |
| 55 | +import { MCPPlugin, SuperTokensMCPServer, AdminToolMCPServer } from "mcp-plugin"; |
| 56 | + |
| 57 | +const mcpServer = new SuperTokensMCPServer({ |
| 58 | + name: "mcp-server", |
| 59 | + version: "1.0.0", |
| 60 | + path: "/mcp-server-for-admins", |
| 61 | + claimValidators: [{ |
| 62 | + UserRoles.validate.includesAll("admin") |
| 63 | + }], |
| 64 | +}); |
| 65 | +``` |
| 66 | +
|
| 67 | +### 3. Register your tools |
| 68 | +
|
| 69 | +Register the tool with your MCP server. This step involves defining the tool name, description, and an execution function that processes the input and returns an output. The execution function used in the example fetches a user by ID and returns a simple greeting: |
| 70 | +
|
| 71 | +```ts |
| 72 | + |
| 73 | +mcpServer.registerTool({ |
| 74 | + name: "mcp-server", |
| 75 | + description: "A tool for the mcp-server", |
| 76 | + execute: (toolInput, extra) => { |
| 77 | + const { user } = getUserById(extra._meta.accessTokenPayload.sub); |
| 78 | + return { |
| 79 | + output: "Hello, world!", |
| 80 | + }; |
| 81 | + }, |
| 82 | +}); |
| 83 | +``` |
| 84 | +
|
| 85 | +Create an administrative version of the MCP server using the AdminToolMCPServer class. This server uses a different claim validator to enforce additional role-based constraints: |
| 86 | +
|
| 87 | +
|
| 88 | +### 4. Initialize the SDK |
| 89 | +
|
| 90 | +Finally, initialize the SuperTokens SDK and add the MCP plugin to your SuperTokens configuration. The MCP plugin is provided with both the standard and administrative server configurations: |
| 91 | +
|
| 92 | +```ts |
| 93 | +SuperTokens.init({ |
| 94 | + plugins: [ |
| 95 | + new MCPPlugin({ |
| 96 | + mcpServers: [mcpServer, adminToolMCPServer], |
| 97 | + }), |
| 98 | + ], |
| 99 | +}) |
| 100 | +``` |
| 101 | +
|
0 commit comments