Skip to content

Commit 835143f

Browse files
committed
Add complete instructions
1 parent ac4d8d8 commit 835143f

File tree

8 files changed

+436
-117
lines changed

8 files changed

+436
-117
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
---
2+
title: Authenticate MCP Servers
3+
hide_title: true
4+
sidebar_position: 8
5+
pagination_next: null
6+
pagination_prev: null
7+
description: >-
8+
Authenticate MCP server actions using the SuperTokens MCP plugin.
9+
category: authentication
10+
recipe: oauth2
11+
page_type: tutorial
12+
---
13+
14+
# Authenticate MCP Servers
15+
16+
## Overview
17+
18+
This guide explains how to authenticate Model Context Protocol (MCP) Servers using **SuperTokens**.
19+
The instructions make use of the `plugins` functionality.
20+
It is a new way to abstract common functionalities into a reusable package.
21+
22+
## Before you start
23+
24+
<PaidFeatureCallout />
25+
26+
The [MCP authentication flow](https://modelcontextprotocol.io/specification/2025-03-26/basic/authorization) complies with the OAuth2 specifications.
27+
This means that you will have to use the `OAuth2` recipe in your configuration which is a paid feature.
28+
29+
The functionality is only available alongside the `node` SDK at the moment.
30+
31+
## Steps
32+
33+
### 1. Install the plugin
34+
35+
Add the `supertokens-mcp-plugin` package to your project.
36+
37+
<Tabs>
38+
<Tab value="npm" label="npm">
39+
```bash
40+
npm i -s supertokens-mcp-plugin
41+
```
42+
</Tab>
43+
<Tab value="yarn" label="yarn">
44+
```bash
45+
yarn add supertokens-mcp-plugin
46+
```
47+
</Tab>
48+
<Tab value="pnpm" label="pnpm">
49+
```bash
50+
pnpm add supertokens-auth-react supertokens-web-js
51+
```
52+
</Tab>
53+
</Tabs>
54+
55+
56+
### 2. Add the MCP server
57+
58+
Use the `SuperTokensMcpServer` class when in your implementation.
59+
The class extends the base MCP server exposed by the `@modelcontextprotocol/sdk`, and adds custom authentication logic on top of it.
60+
61+
You can authorize the client requests in two different ways.
62+
By using the standard [claim validators](/docs/additional-verification/session-verification/claim-validation).
63+
Or you can write your own custom validation logic in the `validateTokenPayload` function.
64+
65+
The authentication state can be accessed inside a tool call through the second function argument, `extra.authInfo`.
66+
67+
```ts
68+
import UserRoles, { UserRoleClaim } from "supertokens-node/recipe/userroles";
69+
import OAuth2Provider from "supertokens-node/recipe/oauth2provider";
70+
import type { TypeInput } from "supertokens-node/types";
71+
import { z } from "zod";
72+
import SuperTokensMcpPlugin, {
73+
SuperTokensMcpServer,
74+
} from "supertokens-mcp-plugin";
75+
import SuperTokens from "supertokens-node";
76+
77+
const server = new SuperTokensMcpServer({
78+
name: "example-mcp",
79+
version: "1.0.0",
80+
path: "/mcp",
81+
validateTokenPayload: async (_accessTokenPayload, _userContext) => {
82+
// You can check the acccess token payload for any specific values
83+
return {
84+
status: "OK",
85+
};
86+
},
87+
// By default, you can use calim validators to determine who can access the MCP server
88+
claimValidators: [UserRoleClaim.validators.includes("admin")],
89+
});
90+
91+
92+
server.registerTool(
93+
"session-info",
94+
{
95+
inputSchema: {},
96+
description: "Get session information",
97+
},
98+
async (_args, extra) => {
99+
return {
100+
content: [
101+
{
102+
type: "text",
103+
text: JSON.stringify(extra.authInfo),
104+
},
105+
],
106+
};
107+
}
108+
);
109+
110+
```
111+
112+
### 3. Update the SDK initialization code
113+
114+
Now that you have created your server include it in the SuperTokens SDK configuration.
115+
This way, the SDK middleware will expose your new endpoint and authenticate each request.
116+
117+
```ts
118+
import supertokens from "supertokens-node";
119+
import SuperTokensMcpPlugin, {
120+
SuperTokensMcpServer,
121+
} from "supertokens-mcp-plugin";
122+
123+
// The server that you have previously created
124+
const server = new SuperTokensMcpServer({});
125+
126+
supertokens.init({
127+
supertokens: {
128+
connectionURI: "<SUPERTOKENS_CONNECTION_URI>",
129+
apiKey: "<SUPERTOKENS_API_KEY>",
130+
},
131+
appInfo: {
132+
appName: "<APP_NAME>",
133+
apiDomain: "<API_DOMAIN>",
134+
websiteDomain: "<WEBSITE_DOMAIN>",
135+
apiBasePath: "<API_BASE_PATH>",
136+
websiteBasePath: "<WEBSITE_BASE_PATH>",
137+
},
138+
recipeList: [
139+
// Include your existing recipes here
140+
// The OAuth2Provider recipe is required for the MCP authorization process
141+
OAuth2Provider.init(),
142+
],
143+
experimental: {
144+
plugins: [
145+
SuperTokensMcpPlugin.init({
146+
mcpServers: [server],
147+
}),
148+
],
149+
},
150+
});
151+
```
152+
153+

docs/authentication/ai/_category_.json

Whitespace-only changes.

docs/authentication/ai/authenticate-mcp-servers.mdx

Lines changed: 0 additions & 101 deletions
This file was deleted.

docs/authentication/ai/introduction.mdx

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)