-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMcpHttpHandler.cs
More file actions
398 lines (335 loc) · 15.2 KB
/
Copy pathMcpHttpHandler.cs
File metadata and controls
398 lines (335 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using ModelContextProtocol;
using ModelContextProtocol.Protocol;
namespace Shiny.Net.HttpServer.Mcp;
/// <summary>
/// The Streamable HTTP transport, as three verbs on one path.
/// <para>
/// <c>POST</c> carries a JSON-RPC message in and gets either an SSE stream of responses back or a
/// bare 202. <c>GET</c> opens the stream the server uses to speak first — sampling, elicitation,
/// notifications. <c>DELETE</c> ends the session. Everything protocol-shaped below this line is the
/// MCP SDK's <c>StreamableHttpServerTransport</c>; what is here is the HTTP around it, which is
/// the part the SDK's own ASP.NET Core package would otherwise supply.
/// </para>
/// </summary>
sealed class McpHttpHandler(McpHttpSessionManager sessions, ILoggerFactory? loggerFactory = null)
{
// ILoggerFactory rather than ILogger<T>, and optional: the core server runs happily with no
// logging registered at all, and an endpoint that refused to resolve for want of a logger
// would be a gratuitous difference.
readonly ILogger logger = (loggerFactory ?? NullLoggerFactory.Instance).CreateLogger<McpHttpHandler>();
public const string SessionIdHeader = "Mcp-Session-Id";
public const string ProtocolVersionHeader = "MCP-Protocol-Version";
const string EventStream = "text/event-stream";
const string Json = "application/json";
// Resolved once: it is the same type info for every request, and looking it up per message
// would be pure overhead on the hot path.
static readonly JsonTypeInfo<JsonRpcMessage> MessageTypeInfo =
(JsonTypeInfo<JsonRpcMessage>)McpJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonRpcMessage));
McpHttpOptions Options => sessions.Options;
// ---- POST: the client speaks ----
public async ValueTask PostAsync(HttpContext context)
{
if (!this.ApplyOriginPolicy(context))
{
await ForbiddenOriginAsync(context).ConfigureAwait(false);
return;
}
// The spec requires both, because the server chooses per response which one it is sending
// and a client that only advertised one has no way to be told about the switch.
if (this.Options.ValidateAcceptHeader && !(Accepts(context.Request, Json) && Accepts(context.Request, EventStream)))
{
await WriteErrorAsync(
context,
StatusCodes.Status406NotAcceptable,
"Accept must include both application/json and text/event-stream."
).ConfigureAwait(false);
return;
}
JsonRpcMessage? message;
try
{
message = await JsonSerializer
.DeserializeAsync(context.Request.Body, MessageTypeInfo, context.RequestAborted)
.ConfigureAwait(false);
}
catch (JsonException ex)
{
this.logger.LogDebug(ex, "Rejected an MCP request with an unparseable body");
await WriteErrorAsync(
context,
StatusCodes.Status400BadRequest,
"The request body is not a JSON-RPC message.",
McpErrorCode.ParseError
).ConfigureAwait(false);
return;
}
if (message is null)
{
await WriteErrorAsync(
context,
StatusCodes.Status400BadRequest,
"The request body is empty.",
McpErrorCode.ParseError
).ConfigureAwait(false);
return;
}
var (session, owned) = await this.ResolveSessionAsync(context, message).ConfigureAwait(false);
if (session is null)
return;
session.Enter();
try
{
// Set before the first write and never after: the response starts on that write, and a
// header set afterwards throws rather than silently going nowhere.
context.Response.ContentType = EventStream;
context.Response.Headers.Set(HeaderNames.CacheControl, "no-cache");
context.Response.Headers.Set("X-Accel-Buffering", "no");
var wroteResponse = await session.Transport
.HandlePostRequestAsync(message, context.Response.Body, context.RequestAborted)
.ConfigureAwait(false);
if (!wroteResponse && !context.Response.HasStarted)
{
// Nothing came back, which means the client sent only notifications or responses.
// 202 with no body is what the spec asks for, so the content type has to go.
context.Response.StatusCode = StatusCodes.Status202Accepted;
context.Response.ContentType = null;
context.Response.ContentLength = 0;
await context.Response.StartAsync(context.RequestAborted).ConfigureAwait(false);
}
}
finally
{
session.Exit();
if (owned)
await session.DisposeAsync().ConfigureAwait(false);
}
}
/// <summary>
/// Finds the session this request belongs to, creating one when the request is an initialize.
/// Returns a null session when it has already written the failure response.
/// </summary>
async ValueTask<(McpHttpSession? Session, bool Owned)> ResolveSessionAsync(HttpContext context, JsonRpcMessage message)
{
if (this.Options.Stateless)
return (sessions.CreateTransient(), true);
var sessionId = context.Request.Headers.GetFirst(SessionIdHeader);
if (sessionId is not null)
{
if (sessions.TryGet(sessionId, out var existing))
return (existing, false);
// 404 rather than 400: the spec has clients treat it as "start over with a fresh
// initialize", which is exactly the right recovery after a server restart.
await WriteErrorAsync(
context,
StatusCodes.Status404NotFound,
"Unknown or expired session. Send an initialize request to start a new one."
).ConfigureAwait(false);
return (null, false);
}
// An initialize with no session id is a client asking for one. It gets a real session, and
// with it everything a session buys: state that survives between calls, and a GET stream
// the server can speak down.
if (message is JsonRpcRequest request && request.Method == RequestMethods.Initialize)
{
var created = sessions.Create();
if (created is null)
{
await WriteErrorAsync(
context,
StatusCodes.Status429TooManyRequests,
"Too many open MCP sessions."
).ConfigureAwait(false);
return (null, false);
}
// Set now, before the transport writes the InitializeResult: this is the response that
// tells the client its session id, and after the first body byte it is too late.
context.Response.Headers.Set(SessionIdHeader, created.Id);
return (created, false);
}
// Anything else without a session id is the discovery flow: a client that skipped the
// handshake because server/discover already told it everything it needed. Each such request
// gets a server of its own and is answered on its own terms. Rejecting these would be
// stricter than the protocol and would lock out every client that connects this way.
return (sessions.CreateTransient(), true);
}
// ---- GET: the server speaks first ----
public async ValueTask GetAsync(HttpContext context)
{
if (!this.ApplyOriginPolicy(context))
{
await ForbiddenOriginAsync(context).ConfigureAwait(false);
return;
}
if (!this.Options.AllowServerToClientStream || this.Options.Stateless)
{
// 405 is the spec's "this server never initiates", and clients that see it stop asking.
context.Response.StatusCode = StatusCodes.Status405MethodNotAllowed;
context.Response.Headers.Set("Allow", "POST, DELETE");
context.Response.ContentLength = 0;
await context.Response.StartAsync(context.RequestAborted).ConfigureAwait(false);
return;
}
if (this.Options.ValidateAcceptHeader && !Accepts(context.Request, EventStream))
{
await WriteErrorAsync(
context,
StatusCodes.Status406NotAcceptable,
"Accept must include text/event-stream."
).ConfigureAwait(false);
return;
}
var sessionId = context.Request.Headers.GetFirst(SessionIdHeader);
if (sessionId is null || !sessions.TryGet(sessionId, out var session))
{
await WriteErrorAsync(
context,
StatusCodes.Status404NotFound,
"Unknown or expired session."
).ConfigureAwait(false);
return;
}
session.Enter();
try
{
context.Response.ContentType = EventStream;
context.Response.Headers.Set(HeaderNames.CacheControl, "no-cache");
context.Response.Headers.Set("X-Accel-Buffering", "no");
// Headers go out before anything is streamed, so the client's stream opens now rather
// than whenever the server first has something to say.
await context.Response.StartAsync(context.RequestAborted).ConfigureAwait(false);
await session.Transport
.HandleGetRequestAsync(context.Response.Body, context.RequestAborted)
.ConfigureAwait(false);
}
catch (OperationCanceledException)
{
// The client closed the stream. That is how these end.
}
finally
{
session.Exit();
}
}
// ---- DELETE: the client is done ----
public async ValueTask DeleteAsync(HttpContext context)
{
if (!this.ApplyOriginPolicy(context))
{
await ForbiddenOriginAsync(context).ConfigureAwait(false);
return;
}
var sessionId = context.Request.Headers.GetFirst(SessionIdHeader);
if (sessionId is null || !await sessions.RemoveAsync(sessionId).ConfigureAwait(false))
{
await WriteErrorAsync(
context,
StatusCodes.Status404NotFound,
"Unknown or expired session."
).ConfigureAwait(false);
return;
}
context.Response.StatusCode = StatusCodes.Status204NoContent;
context.Response.ContentLength = 0;
await context.Response.StartAsync(context.RequestAborted).ConfigureAwait(false);
}
// ---- OPTIONS: the browser asks first ----
public async ValueTask PreflightAsync(HttpContext context)
{
if (!this.ApplyOriginPolicy(context))
{
await ForbiddenOriginAsync(context).ConfigureAwait(false);
return;
}
context.Response.StatusCode = StatusCodes.Status204NoContent;
context.Response.Headers.Set("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS");
context.Response.Headers.Set(
"Access-Control-Allow-Headers",
$"Content-Type, Authorization, Last-Event-ID, {SessionIdHeader}, {ProtocolVersionHeader}"
);
context.Response.Headers.Set("Access-Control-Max-Age", "86400");
context.Response.ContentLength = 0;
await context.Response.StartAsync(context.RequestAborted).ConfigureAwait(false);
}
// ---- Shared ----
/// <summary>
/// Decides whether a browser may talk to this endpoint, and stamps the CORS headers when it may.
/// <para>
/// A request with no <c>Origin</c> is not from a page and passes untouched — that is every
/// native MCP client. A request with one is only allowed if it was named, because a server
/// bound to localhost is otherwise reachable by any site the user visits, which is the whole
/// DNS-rebinding attack the spec warns about.
/// </para>
/// </summary>
bool ApplyOriginPolicy(HttpContext context)
{
var origin = context.Request.Headers.GetFirst("Origin");
if (origin is null)
return true;
if (!this.Options.IsOriginAllowed(origin))
{
this.logger.LogWarning("Rejected an MCP request from origin {Origin}", origin);
return false;
}
context.Response.Headers.Set("Access-Control-Allow-Origin", origin);
context.Response.Headers.Append("Vary", "Origin");
context.Response.Headers.Set("Access-Control-Expose-Headers", SessionIdHeader);
return true;
}
static ValueTask ForbiddenOriginAsync(HttpContext context)
=> WriteErrorAsync(context, StatusCodes.Status403Forbidden, "Origin is not allowed.");
/// <summary>
/// Writes a JSON-RPC error. HTTP status codes are what the transport layer understands; MCP
/// clients read the body, so a bare status with no body leaves them guessing.
/// </summary>
static async ValueTask WriteErrorAsync(
HttpContext context,
int statusCode,
string message,
McpErrorCode errorCode = McpErrorCode.InvalidRequest
)
{
context.Response.StatusCode = statusCode;
// Serialized through the SDK's own message type rather than hand-written: these bodies are
// read by MCP clients, and an error response that is itself malformed is the worst possible
// thing to hand something already having a bad time.
var error = new JsonRpcError
{
Error = new JsonRpcErrorDetail { Code = (int)errorCode, Message = message }
};
var body = JsonSerializer.SerializeToUtf8Bytes(error, MessageTypeInfo);
await context.Response.WriteBytesAsync(body, Json, context.RequestAborted).ConfigureAwait(false);
}
/// <summary>
/// Whether the request's <c>Accept</c> covers a media type, honouring <c>*/*</c> and
/// <c>type/*</c>. Quality values are ignored: this is a yes/no question about whether the
/// client can handle the response at all, not a negotiation.
/// </summary>
static bool Accepts(HttpRequest request, string mediaType)
{
if (!request.Headers.TryGetValue("Accept", out var values))
return false;
var slash = mediaType.IndexOf('/');
foreach (var value in values)
{
if (value is null)
continue;
foreach (var range in value.Split(','))
{
var candidate = range.AsSpan().Trim();
var parameters = candidate.IndexOf(';');
if (parameters >= 0)
candidate = candidate[..parameters].TrimEnd();
if (candidate.SequenceEqual("*/*") || candidate.Equals(mediaType, StringComparison.OrdinalIgnoreCase))
return true;
if (candidate.EndsWith("/*") &&
candidate[..^2].Equals(mediaType.AsSpan(0, slash), StringComparison.OrdinalIgnoreCase))
return true;
}
}
return false;
}
}