You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The MCP TypeScript SDK provides optional, opt-in support for authentication (AuthN) and authorization (AuthZ). This enables you to protect your MCP server resources, tools, and prompts using industry-standard schemes like OAuth 2.1 Bearer tokens.
4
+
5
+
## Key Concepts
6
+
7
+
-**Authenticator**: Responsible for extracting and validating authentication information from an incoming request.
8
+
-**AuthInfo**: A structure containing information about the authenticated entity (e.g., user name, active scopes).
9
+
-**Authorizer**: Used by the MCP server to verify if the authenticated entity has the required scopes to access a specific resource, tool, or prompt.
10
+
-**Scopes**: Optional strings associated with registered items that define the required permissions.
11
+
12
+
## Implementing Authentication
13
+
14
+
To enable authentication, provide an `authenticator` in the `ServerOptions` when creating your server.
15
+
16
+
### Using Bearer Token Authentication
17
+
18
+
The SDK includes a `BearerTokenAuthenticator` for validating OAuth 2.1 Bearer tokens.
Copy file name to clipboardExpand all lines: docs/server.md
+22Lines changed: 22 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,7 @@ Building a server takes three steps:
11
11
1. Create an {@linkcode@modelcontextprotocol/server!server/mcp.McpServer | McpServer} and register your [tools, resources, and prompts](#tools-resources-and-prompts).
12
12
2. Create a transport — [Streamable HTTP](#streamable-http) for remote servers or [stdio](#stdio) for local, process‑spawned integrations.
13
13
3. Wire the transport into your HTTP framework (or use stdio directly) and call `server.connect(transport)`.
14
+
1. (Optional) Configure [authentication and authorization](#authentication-and-authorization) to protect your server.
14
15
15
16
The sections below cover each of these. For a feature‑rich starting point, see [`simpleStreamableHttp.ts`](https://github.com/modelcontextprotocol/typescript-sdk/blob/main/examples/server/src/simpleStreamableHttp.ts) — remove what you don't need and register your own tools, resources, and prompts. For stateless or JSON‑response‑mode alternatives, see the examples linked in [Transports](#transports) below.
16
17
@@ -444,6 +445,27 @@ Task-based execution enables "call-now, fetch-later" patterns for long-running o
444
445
> [!WARNING]
445
446
> The tasks API is experimental and may change without notice.
446
447
448
+
## Authentication and Authorization
449
+
450
+
The MCP TypeScript SDK provides optional, opt-in support for authentication (AuthN) and authorization (AuthZ). For a comprehensive guide, see the [Authentication and Authorization guide](./auth.md).
451
+
452
+
Quick example:
453
+
454
+
```ts
455
+
const server =newMcpServer({ name: 'my-server', version: '1.0.0' }, {
456
+
authenticator: newBearerTokenAuthenticator({
457
+
validate: async (token) => {
458
+
if (token==='secret') return { name: 'admin', scopes: ['all'] };
0 commit comments