| 1 | // Copyright 2025 The Go MCP SDK Authors. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // TODO: move server-side streamable HTTP logic from streamable.go to this file. |
| 6 | |
| 7 | package mcp |
| 8 | |
| 9 | /* |
| 10 | Streamable HTTP Server Design |
| 11 | |
| 12 | This document describes the server-side implementation of the MCP streamable |
| 13 | HTTP transport, as defined by the MCP spec: |
| 14 | https://modelcontextprotocol.io/specification/2025-11-25/basic/transports#streamable-http |
| 15 | |
| 16 | # Overview |
| 17 | |
| 18 | The streamable HTTP transport enables MCP communication over HTTP, with |
| 19 | server-sent events (SSE) for server-to-client messages. The implementation |
| 20 | consists of several layered components: |
| 21 | |
| 22 | ┌─────────────────────────────────────────────────────────────────┐ |
| 23 | │ [StreamableHTTPHandler] │ |
| 24 | │ http.Handler that manages sessions and routes HTTP requests │ |
| 25 | └─────────────────────────────────────────────────────────────────┘ |
| 26 | │ |
| 27 | ▼ |
| 28 | ┌─────────────────────────────────────────────────────────────────┐ |
| 29 | │ [StreamableServerTransport] │ |
| 30 | │ transport implementation, one per session; exposes ServeHTTP │ |
| 31 | └─────────────────────────────────────────────────────────────────┘ |
| 32 | │ |
| 33 | ▼ |
| 34 | ┌─────────────────────────────────────────────────────────────────┐ |
| 35 | │ [streamableServerConn] │ |
| 36 | │ Connection implementation, handles message routing │ |
| 37 | └─────────────────────────────────────────────────────────────────┘ |
| 38 | │ |
| 39 | ▼ |
| 40 | ┌─────────────────────────────────────────────────────────────────┐ |
| 41 | │ [stream] │ |
| 42 | │ Logical message channel within a session, may be resumed │ |
| 43 | └─────────────────────────────────────────────────────────────────┘ |
| 44 | |
| 45 | # Sessions |
| 46 | |
| 47 | As with other transports, a session represents a logical MCP connection between |
| 48 | a client and server. In the streamable transport, sessions are identified by a |
| 49 | unique session ID (Mcp-Session-Id header) and persist across multiple HTTP |
| 50 | requests. |
| 51 | |
| 52 | [StreamableHTTPHandler] maintains a map of active sessions ([sessionInfo]), |
| 53 | each containing: |
| 54 | - The [ServerSession] (MCP-level session state) |
| 55 | - The [StreamableServerTransport] (for message I/O) |
| 56 | - Optional timeout management for idle session cleanup |
| 57 | |
| 58 | Sessions are created on the first POST request (typically containing the |
| 59 | initialize request) and destroyed either by: |
| 60 | - Client sending a DELETE request |
| 61 | - Session timeout due to inactivity |
| 62 | - Server explicitly closing the session |
| 63 | |
| 64 | # Streams |
| 65 | |
| 66 | Within a session, there can be multiple concurrent "streams" - logical channels |
| 67 | for message delivery. This is distinct from HTTP streams; a single [stream] may |
| 68 | span multiple HTTP request/response cycles (via resumption). |
| 69 | |
| 70 | There are two types of streams: |
| 71 | |
| 72 | 1. Optional standalone SSE stream (id = ""): |
| 73 | - Created when client sends a GET request to the endpoint |
| 74 | - Used for server-initiated messages (requests/notifications to client) |
| 75 | - Persists for the lifetime of the session |
| 76 | - Only one standalone stream per session |
| 77 | |
| 78 | 2. Request streams (id = random string): |
| 79 | - Created for each POST request containing JSON-RPC calls |
| 80 | - Used to route responses back to the originating HTTP request |
| 81 | - Completed when all responses have been sent |
| 82 | - Can be resumed via GET with Last-Event-ID if interrupted |
| 83 | |
| 84 | # Message Routing |
| 85 | |
| 86 | When the server writes a message, it must be routed to the correct [stream]: |
| 87 | |
| 88 | - Responses: Routed to the stream that originated the request |
| 89 | - Requests/Notifications made during request handling: Routed to the same |
| 90 | stream as the triggering request (via context) |
| 91 | - Requests/Notifications made outside request handling: Routed to the |
| 92 | standalone SSE stream |
| 93 | |
| 94 | This routing is implemented using: |
| 95 | - [streamableServerConn.requestStreams] maps request IDs to stream IDs |
| 96 | - [idContextKey] is used to store the originating request ID in Context |
| 97 | - [streamableServerConn.streams] maps stream IDs to [stream] objects |
| 98 | |
| 99 | # Stream Resumption |
| 100 | |
| 101 | If an HTTP connection is interrupted (network issues, etc.), clients can |
| 102 | resume a stream by sending a GET request with the Last-Event-ID header. |
| 103 | This requires an [EventStore] to be configured on the server. |
| 104 | |
| 105 | - [EventStore.Open] is called when a new stream is created |
| 106 | - [EventStore.Append] is called for each message written to the stream |
| 107 | - [EventStore.After] is called to replay messages after a given index |
| 108 | - [EventStore.SessionClosed] is called when the session ends |
| 109 | |
| 110 | Event IDs are formatted as "<streamID>_<index>" to identify both the |
| 111 | stream and position within that stream (see [formatEventID] and [parseEventID]). |
| 112 | |
| 113 | # Stateless Mode |
| 114 | |
| 115 | For simpler deployments, the handler supports "stateless" mode |
| 116 | ([StreamableHTTPOptions.Stateless]) where: |
| 117 | - No session ID validation is performed |
| 118 | - Each request creates a temporary session that's closed after the request |
| 119 | - Server-to-client requests are not supported (no way to receive response) |
| 120 | |
| 121 | This mode is useful for simple tool servers that don't need bidirectional |
| 122 | communication. |
| 123 | |
| 124 | # Response Formats |
| 125 | |
| 126 | The server can respond to POST requests in two formats: |
| 127 | |
| 128 | 1. text/event-stream (default): Messages sent as SSE events, supports |
| 129 | streaming multiple messages and server-initiated communication during |
| 130 | request handling. |
| 131 | |
| 132 | 2. application/json ([StreamableHTTPOptions.JSONResponse]): Single JSON |
| 133 | response, simpler but doesn't support streaming. Server-initiated messages |
| 134 | during request handling go to the standalone SSE stream instead. |
| 135 | |
| 136 | # HTTP Methods |
| 137 | |
| 138 | - POST: Send JSON-RPC messages (requests, responses, notifications) |
| 139 | - GET: Open standalone SSE stream or resume an interrupted stream |
| 140 | - DELETE: Terminate the session |
| 141 | |
| 142 | # Key Implementation Details |
| 143 | |
| 144 | The [stream] struct manages delivery of messages to HTTP responses. |
| 145 | |
| 146 | Fields: |
| 147 | - [stream.w] is the ResponseWriter for the current HTTP response (non-nil indicates claimed) |
| 148 | - [stream.done] is closed to release the hanging HTTP request |
| 149 | - [stream.requests] tracks pending request IDs (stream completes when empty) |
| 150 | |
| 151 | Methods: |
| 152 | - [stream.deliverLocked] delivers a message to the stream |
| 153 | - [stream.close] sends a close event and releases the stream |
| 154 | - [stream.release] releases the stream from the HTTP request, allowing resumption |
| 155 | |
| 156 | [streamableServerConn] handles the [Connection] interface: |
| 157 | - [streamableServerConn.Read] receives messages from the incoming channel (fed by POST handlers) |
| 158 | - [streamableServerConn.Write] routes messages to appropriate streams |
| 159 | - [streamableServerConn.Close] terminates the session and notifies the [EventStore] |
| 160 | */ |