Skip to content

Commit 55e7e2b

Browse files
committed
feat: add authentication specification for MCP HTTP+SSE transport
This adds a comprehensive authentication specification for the Model Context Protocol that uses OAuth 2.1 for HTTP+SSE transport. The spec defines the complete authentication flow including metadata discovery, dynamic client registration, token handling, and security best practices. It supports both localhost and non-localhost scenarios with appropriate security measures.
1 parent 363e36b commit 55e7e2b

File tree

2 files changed

+383
-1
lines changed

2 files changed

+383
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
This repo contains the specification and protocol schema for the Model Context Protocol.
44

55
The schema is [defined in TypeScript](schema/2024-11-05/schema.ts) first, but
6-
[made available as JSON Schema](schema/2024-11-05/schema.json) as well, for wider compatibility.
6+
[made available as JSON Schema](schema/2024-11-05/schema.json) as well, for wider
7+
compatibility.
78

89
## Contributing
910

Lines changed: 381 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,381 @@
1+
---
2+
title: Authorization
3+
type: docs
4+
weight: 15
5+
---
6+
7+
{{< callout type="info" >}} **Protocol Revision**: draft {{< /callout >}}
8+
9+
## 1. Introduction
10+
11+
### 1.1 Purpose and Scope
12+
13+
The Model Context Protocol provides authorization capabilities at the transport level,
14+
enabling MCP clients to make requests to restricted MCP servers on behalf of resource
15+
owners. This specification defines the authorization flow for HTTP+SSE transport.
16+
17+
### 1.2 Protocol Requirements
18+
19+
Authorization is **OPTIONAL** for MCP implementations. When supported:
20+
21+
- Implementations using an HTTP+SSE transport **SHOULD** conform to this specification.
22+
- Implementations using an STDIO transport **SHOULD NOT** follow this specification, and
23+
instead retrieve credentials from the environment.
24+
- Implementations using alternative transports **MUST** follow established security best
25+
practices for their protocol.
26+
27+
### 1.3 Standards Compliance
28+
29+
This authorization mechanism is based on established specifications listed below, but
30+
implements a selected subset of their features to ensure security and interoperability
31+
while maintaining simplicity:
32+
33+
- [OAuth 2.1 IETF DRAFT](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-12)
34+
- OAuth 2.0 Authorization Server Metadata
35+
([RFC8414](https://datatracker.ietf.org/doc/html/rfc8414))
36+
- OAuth 2.0 Dynamic Client Registration Protocol
37+
([RFC7591](https://datatracker.ietf.org/doc/html/rfc7591))
38+
39+
## 2. Authorization Flow
40+
41+
### 2.1 Overview
42+
43+
1. MCP auth implementations **MUST** implement OAuth 2.1 with appropriate security
44+
measures for both confidential and public clients.
45+
46+
2. MCP auth implementations **SHOULD** support the OAuth 2.0 Dynamic Client Registration
47+
Protocol ([RFC7591](https://datatracker.ietf.org/doc/html/rfc7591)).
48+
49+
3. MCP servers **SHOULD** and MCP clients **MUST** implement OAuth 2.0 Authorization
50+
Server Metadata ([RFC8414](https://datatracker.ietf.org/doc/html/rfc8414)). Servers
51+
that do not support Authorization Server Metadata **MUST** follow the default URI
52+
schema.
53+
54+
### 2.2 Basic OAuth 2.1 Authorization
55+
56+
When authorization is required and not yet prooven by the client, servers **MUST**
57+
respond with _HTTP 401 Unauthorized_.
58+
59+
Clients initiate the
60+
[OAuth 2.1 IETF DRAFT](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-12)
61+
authorization flow after receiving the _HTTP 401 Unauthorized_.
62+
63+
The following demonstrates the basic OAuth 2.1 for public clients using PKCE.
64+
65+
```mermaid
66+
sequenceDiagram
67+
participant B as User-Agent (Browser)
68+
participant C as Client
69+
participant M as MCP Server
70+
71+
C->>M: MCP Request
72+
M->>C: HTTP 401 Unauthorized
73+
Note over C: Generate code_verifier and code_challenge
74+
C->>B: Open browser with authorization URL + code_challenge
75+
B->>M: GET /authorize
76+
Note over M: User logs in and authorizes
77+
M->>B: Redirect to callback URL with auth code
78+
B->>C: Callback with authorization code
79+
C->>M: Token Request with code + code_verifier
80+
M->>C: Access Token (+ Refresh Token)
81+
C->>M: MCP Request with Access Token
82+
Note over C,M: Begin standard MCP message exchange
83+
```
84+
85+
### 2.3 Server Metadata Discovery
86+
87+
For server capability discovery:
88+
89+
- MCP clients _MUST_ follow the OAuth 2.0 Authorization Server Metadata protocol defined
90+
in [RFC8414](https://datatracker.ietf.org/doc/html/rfc8414).
91+
- MCP server _SHOULD_ support follow the OAuth 2.0 Authorization Server Metadata
92+
protocol.
93+
- MCP servers that do not support the OAuth 2.0 Authorization Server Metadata protocol,
94+
_MUST_ support fallback URLs..
95+
96+
The discovery flow is illustrated below:
97+
98+
```mermaid
99+
sequenceDiagram
100+
participant C as Client
101+
participant S as Server
102+
103+
C->>S: GET /.well-known/oauth-authorization-server
104+
alt Discovery Success
105+
S->>C: 200 OK + Metadata Document
106+
Note over C: Use endpoints from metadata
107+
else Discovery Failed
108+
S->>C: 404 Not Found
109+
Note over C: Fall back to default endpoints
110+
end
111+
Note over C: Continue with authorization flow
112+
```
113+
114+
#### 2.3.1 Server Metadata Discover Headers
115+
116+
MCP clients _SHOULD_ include the header `MCP-Protocol-Version: <protocol-version>` during
117+
Server Metadata Discover to allow MCP server to respond based on MCP protocol version.
118+
119+
For example: `MCP-Protocol-Version: 2024-11-05`
120+
121+
#### 2.3.1 Fallbacks for Servers without Metadata Discovery
122+
123+
For servers that do not implement OAuth 2.0 Authorization Server Metadata, clients
124+
**MUST** use the following default endpoint paths relative to the server's base URL:
125+
126+
| Endpoint | Default Path | Description |
127+
| ---------------------- | ------------ | ------------------------------------ |
128+
| Authorization Endpoint | /authorize | Used for authorization requests |
129+
| Token Endpoint | /token | Used for token exchange & refresh |
130+
| Registration Endpoint | /register | Used for dynamic client registration |
131+
132+
Clients **MUST** first attempt to discover endpoints via the metadata document before
133+
falling back to default paths. When using default paths, all other protocol requirements
134+
remain unchanged.
135+
136+
### 2.3 Dynamic Client Registration
137+
138+
MCP authorization makes use of the
139+
[OAuth 2.0 Dynamic Client Registration Protocol](https://datatracker.ietf.org/doc/html/rfc7591)
140+
to allow MCP clients to obtain OAuth client IDs without user interaction.
141+
142+
- MCP clients **SHOULD** support
143+
[OAuth 2.0 Dynamic Client Registration Protocol](https://datatracker.ietf.org/doc/html/rfc7591)
144+
- MCP servers with non-localhost redirect URIs **SHOULD** support Dynamic Client
145+
Registration.
146+
- MCP servers with localhost redirect URIs **OPTIONALLY** support Dynamic Client
147+
Registration.
148+
149+
Note that are not required to support Dynamic Client Registration. MCP clients that do
150+
not support Dynamic Client Registration need to provide alternative ways to obtain a
151+
client id (and if applicable client secret).
152+
153+
#### 2.3.1 Localhost Redirect URIs
154+
155+
When using localhost redirect URIs (http://localhost:{port} or http://127.0.0.1:{port}),
156+
clients:
157+
158+
- Dynamic registration is **OPTIONAL** (a client ID is not required)
159+
- **MAY** proceed directly to authorization
160+
- **MUST NOT** require client secrets
161+
162+
This exception for localhost is explicitly supported by OAuth 2.1 for public clients and
163+
provides a secure flow through the combination of PKCE and localhost-only redirects.
164+
165+
#### 2.3.2 Non-Localhost Redirect URIs
166+
167+
For all other redirect URIs, MCP clients and servers **SHOULD** support dynamic client
168+
registration. This provides a standardized way for clients to automatically register with
169+
new servers, which is crucial for MCP because:
170+
171+
- Clients cannot know all possible servers in advance
172+
- Manual registration would create friction for users
173+
- It enables seamless connection to new servers
174+
- Servers can implement their own registration policies
175+
176+
### 2.4 Authorization Flow Steps
177+
178+
The complete Authorization flow proceeds as follows:
179+
180+
```mermaid
181+
sequenceDiagram
182+
participant B as User-Agent (Browser)
183+
participant C as Client
184+
participant M as MCP Server
185+
186+
C->>M: GET /.well-known/oauth-authorization-server
187+
alt Server Supports Discovery
188+
M->>C: Authorization Server Metadata
189+
else No Discovery
190+
M->>C: 404 (Use default endpoints)
191+
end
192+
193+
alt Non-Localhost Redirect URI
194+
C->>M: POST /register
195+
M->>C: Client Credentials
196+
end
197+
198+
Note over C: Generate PKCE Parameters
199+
C->>B: Open browser with authorization URL + code_challenge
200+
B->>M: Authorization Request
201+
Note over M: User /authorizes
202+
M->>B: Redirect to callback with authorization code
203+
B->>C: Authorization code callback
204+
C->>M: Token Request + code_verifier
205+
M->>C: Access Token + Refresh Token
206+
C->>M: API Requests with Access Token
207+
```
208+
209+
#### 2.4.1 Decision Flow Overview
210+
211+
```mermaid
212+
flowchart TD
213+
A[Start Auth Flow] --> B{Check Metadata Discovery}
214+
B -->|Available| C[Use Metadata Endpoints]
215+
B -->|Not Available| D[Use Default Endpoints]
216+
217+
C --> E{Check Redirect URI}
218+
D --> E
219+
220+
E -->|Localhost| F[Skip Registration]
221+
E -->|Non-localhost| G{Check Registration Endpoint}
222+
223+
G -->|Available| H[Perform Dynamic Registration]
224+
G -->|Not Available| I[Alternative Registration Required]
225+
226+
F --> J[Start OAuth Flow]
227+
H --> J
228+
I --> J
229+
230+
J --> K[Generate PKCE Parameters]
231+
K --> L[Request Authorization]
232+
L --> M[User Authorization]
233+
M --> N[Exchange Code for Tokens]
234+
N --> O[Use Access Token]
235+
```
236+
237+
### 2.5 Access Token Usage
238+
239+
#### 2.5.1 Token Requirements
240+
241+
Access token handling **MUST** conform to
242+
[OAuth 2.1 Section 5](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-12#section-5)
243+
requirements for resource requests. Specifically:
244+
245+
1. MCP client **MUST** use the Authorization request header field
246+
[Section 5.1.1](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-12#section-5.1.1):
247+
248+
```
249+
Authorization: Bearer <access-token>
250+
```
251+
252+
2. Access tokens **MUST NOT** be included in the URI query string
253+
254+
Example request:
255+
256+
```http
257+
GET /v1/contexts HTTP/1.1
258+
Host: mcp.example.com
259+
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
260+
```
261+
262+
#### 2.5.2 Token Handling
263+
264+
Resource servers **MUST** validate access tokens as described in
265+
[Section 5.2](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-12#section-5.2).
266+
If validation fails, servers **MUST** respond according to
267+
[Section 5.3](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-12#section-5.3)
268+
error handling requirements. Invalid or expired tokens **MUST** receive a HTTP 401
269+
response.
270+
271+
### 2.6 Security Considerations
272+
273+
The following security requirements **MUST** be implemented:
274+
275+
1. Clients **MUST** securely store tokens following OAuth 2.0 best practices
276+
2. Servers **SHOULD** enforce token expiration and rotation
277+
3. All authorization endpoints **MUST** be served over HTTPS
278+
4. Servers **MUST** validate redirect URIs to prevent open redirect vulnerabilities
279+
5. Redirect URIs **MUST** be either localhost URLs or HTTPS URLs
280+
281+
### 2.7 Error Handling
282+
283+
Servers **MUST** return appropriate HTTP status codes for authorization errors:
284+
285+
| Status Code | Description | Usage |
286+
| ----------- | ------------ | ------------------------------------------ |
287+
| 401 | Unauthorized | Authorization required or token invalid |
288+
| 403 | Forbidden | Invalid scopes or insufficient permissions |
289+
| 400 | Bad Request | Malformed authorization request |
290+
291+
### 2.8 Implementation Requirements
292+
293+
1. Implementations **MUST** follow OAuth 2.1 security best practices
294+
2. PKCE is **REQUIRED** for all clients
295+
3. Token rotation **SHOULD** be implemented for enhanced security
296+
4. Token lifetimes **SHOULD** be limited based on security requirements
297+
298+
### 2.9 Third-Party Authorization Flow
299+
300+
#### 2.9.1 Overview
301+
302+
MCP servers **MAY** support delegated authorization through third-party authorization
303+
servers. In this flow, the MCP server acts as both an OAuth client (to the third-party
304+
auth server) and an OAuth authorization server (to the MCP client).
305+
306+
#### 2.9.2 Flow Description
307+
308+
The third-party authorization flow comprises these steps:
309+
310+
1. MCP client initiates standard OAuth flow with MCP server
311+
2. MCP server redirects user to third-party authorization server
312+
3. User authorizes with third-party server
313+
4. Third-party server redirects back to MCP server with authorization code
314+
5. MCP server exchanges code for third-party access token
315+
6. MCP server generates its own access token bound to the third-party session
316+
7. MCP server completes original OAuth flow with MCP client
317+
318+
```mermaid
319+
sequenceDiagram
320+
participant B as User-Agent (Browser)
321+
participant C as MCP Client
322+
participant M as MCP Server
323+
participant T as Third-Party Auth Server
324+
325+
C->>M: Initial OAuth Request
326+
M->>B: Redirect to Third-Party /authorize
327+
B->>T: Authorization Request
328+
Note over T: User authorizes
329+
T->>B: Redirect to MCP Server callback
330+
B->>M: Authorization code
331+
M->>T: Exchange code for token
332+
T->>M: Third-party access token
333+
Note over M: Generate bound MCP token
334+
M->>B: Redirect to MCP Client callback
335+
B->>C: MCP authorization code
336+
C->>M: Exchange code for token
337+
M->>C: MCP access token
338+
```
339+
340+
#### 2.9.3 Session Binding Requirements
341+
342+
MCP servers implementing third-party authorization **MUST**:
343+
344+
1. Maintain secure mapping between third-party tokens and issued MCP tokens
345+
2. Validate third-party token status before honoring MCP tokens
346+
3. Implement appropriate token lifecycle management
347+
4. Handle third-party token expiration and renewal
348+
349+
#### 2.9.4 Security Considerations
350+
351+
When implementing third-party authorization, servers **MUST**:
352+
353+
1. Validate all redirect URIs
354+
2. Securely store third-party credentials
355+
3. Implement appropriate session timeout handling
356+
4. Consider security implications of token chaining
357+
5. Implement proper error handling for third-party auth failures
358+
359+
## 3. Best Practices
360+
361+
#### 3.1 Local clients as Public OAuth 2.1 Clients
362+
363+
We strongly recommend that local clients implement OAuth 2.1 as public client:
364+
365+
1. Utilizing code challenges (PKCE) for authorization requests to prevent interception
366+
attacks
367+
2. Implementing secure token storage appropriate for the local system
368+
3. Following token refresh best practices to maintain sessions
369+
4. Properly handling token expiration and renewal
370+
371+
#### 3.2 Authorization Metadata Discovery
372+
373+
We strongly recommend, that all clients implement metadata discovery. This reduces the
374+
need for users to provide endpoints either manually or fallback to the defined defaults.
375+
376+
#### 3.3 Dynamic Client Registration
377+
378+
Since clients do not know the set of MCP servers in advance, we strongly recommend the
379+
implementation of dynamic client registration. This allows applications to automatically
380+
register with the MCP server, and removes the need for users to obtain client ids
381+
manually.

0 commit comments

Comments
 (0)