Skip to content

Commit d5e0a0e

Browse files
committed
Establish sessions on server, not client
1 parent 4001c48 commit d5e0a0e

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

docs/specification/draft/basic/transports.md

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -148,25 +148,26 @@ act as a cursor within that particular stream.
148148

149149
### Session Management
150150

151-
Where a client desires to share a single logical session across multiple requests, it
152-
**MAY** attach an `Mcp-Session-Id` HTTP header to its requests. This permits maintenance
153-
of session state across separate POSTs.
154-
155-
1. It is the client's responsibility to generate or select the session ID.
156-
2. This session ID **SHOULD** be globally unique and cryptographically secure (e.g., a
157-
UUID or a JWT), unless it is specifically desired to share a session ID across users
158-
or clients.
159-
3. The server **MAY** use this header to associate state with the logical session.
160-
- If the server agrees to re-establish an existing session, it **MUST** include a
161-
`Mcp-Session-Status: resumed` HTTP header in its response.
162-
- If the server begins a new session associated with the session ID, it **MUST**
163-
include a `Mcp-Session-Status: created` HTTP header in its response.
164-
- The server **MAY** delete session state at any time; however, it **MUST NOT**
165-
prevent the same session ID from creating a new session again in future.
166-
4. If [authorization]({{< ref "authorization" >}}) is used _and_ the server makes use of
167-
the `Mcp-Session-Id` header:
168-
- The server **SHOULD** bind the session ID to the authorization context, and return
169-
an error if the session ID is reused in a different authorization context.
151+
An MCP "session" consists of logically related interactions between a client and a
152+
server, beginning with the [initialization phase]({{< ref "lifecycle" >}}). To support
153+
servers which want to establish stateful sessions:
154+
155+
1. A server using the Streamable HTTP transport **MAY** assign a session ID at
156+
initialization time, by including it in the `sessionId` field of the
157+
`InitializeResult`.
158+
- The session ID **SHOULD** be globally unique and cryptographically secure (e.g., a
159+
securely generated UUID, a JWT, or a cryptographic hash).
160+
- The session ID **MUST** only contain visible ASCII characters.
161+
2. If a `sessionId` is returned in the `InitializeResult`, clients using the Streamable
162+
HTTP transport **MUST** include it in the `Mcp-Session-Id` header on all of their
163+
subsequent HTTP requests.
164+
- Servers that require a session ID **SHOULD** respond to requests without an
165+
`Mcp-Session-Id` header (other than initialization) with HTTP 400 Bad Request.
166+
3. The server **MAY** terminate the session at any time, after which it **MUST** respond
167+
to requests containing that session ID with HTTP 404 Not Found.
168+
4. When a client receives HTTP 404 in response to a request containing an
169+
`Mcp-Session-Id`, it **MUST** start a new session by sending a new `InitializeRequest`
170+
without a session ID attached.
170171

171172
### Sequence Diagram
172173

@@ -177,25 +178,15 @@ sequenceDiagram
177178
178179
note over Client, Server: initialization
179180
180-
Client->>+Server: POST InitializeRequest<br>Mcp-Session-Id: foo
181-
Server->>Client: InitializeResponse
182-
deactivate Server
183-
184-
Client->>+Server: POST InitializedNotification<br>Mcp-Session-Id: foo
181+
Client->>+Server: POST InitializeRequest
182+
Server->>-Client: InitializeResponse<br>Mcp-Session-Id: 1868a90c...
185183
186-
alt single HTTP response
187-
Server->>Client: 202 Accepted
188-
else server opens SSE stream
189-
loop while connection remains open
190-
Server-)Client: ... SSE messages from server ...
191-
end
192-
Client-->Server: (client or server MAY disconnect)
193-
end
194-
deactivate Server
184+
Client->>+Server: POST InitializedNotification<br>Mcp-Session-Id: 1868a90c...
185+
Server->>-Client: 202 Accepted
195186
196187
note over Client, Server: normal operation (sequentially or concurrently)
197188
198-
Client->>+Server: POST ... other messages ...<br>Mcp-Session-Id: foo
189+
Client->>+Server: POST ... other messages ...<br>Mcp-Session-Id: 1868a90c...
199190
200191
alt single HTTP response
201192
Server->>Client: ... response ...

schema/draft/schema.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ export interface InitializeResult extends Result {
167167
protocolVersion: string;
168168
capabilities: ServerCapabilities;
169169
serverInfo: Implementation;
170+
171+
/**
172+
* A session ID, if the server assigns one. This can be used to implement
173+
* resumable sessions; for an example, see the Streamable HTTP transport.
174+
*
175+
* This string must only contain visible ASCII characters (ranging from 0x21 to 0x7E).
176+
*
177+
* @TJS-pattern ^[\x21-\x7E]+$
178+
*/
179+
sessionId?: string;
180+
170181
/**
171182
* Instructions describing how to use the server and its features.
172183
*

0 commit comments

Comments
 (0)