forked from modelcontextprotocol/csharp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpServerTransportOptions.cs
More file actions
77 lines (69 loc) · 4.23 KB
/
HttpServerTransportOptions.cs
File metadata and controls
77 lines (69 loc) · 4.23 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
using Microsoft.AspNetCore.Http;
using ModelContextProtocol.Server;
namespace ModelContextProtocol.AspNetCore;
/// <summary>
/// Configuration options for <see cref="M:McpEndpointRouteBuilderExtensions.MapMcp"/>.
/// which implements the Streaming HTTP transport for the Model Context Protocol.
/// See the protocol specification for details on the Streamable HTTP transport. <see href="https://modelcontextprotocol.io/specification/2025-06-18/basic/transports#streamable-http"/>
/// </summary>
public class HttpServerTransportOptions
{
/// <summary>
/// Gets or sets an optional asynchronous callback to configure per-session <see cref="McpServerOptions"/>
/// with access to the <see cref="HttpContext"/> of the request that initiated the session.
/// </summary>
public Func<HttpContext, McpServerOptions, CancellationToken, Task>? ConfigureSessionOptions { get; set; }
/// <summary>
/// Gets or sets an optional asynchronous callback for running new MCP sessions manually.
/// This is useful for running logic before a sessions starts and after it completes.
/// </summary>
public Func<HttpContext, McpServer, CancellationToken, Task>? RunSessionHandler { get; set; }
/// <summary>
/// Gets or sets whether the server should run in a stateless mode that does not require all requests for a given session
/// to arrive to the same ASP.NET Core application process.
/// </summary>
/// <remarks>
/// If <see langword="true"/>, the "/sse" endpoint will be disabled, and client information will be round-tripped as part
/// of the "MCP-Session-Id" header instead of stored in memory. Unsolicited server-to-client messages and all server-to-client
/// requests are also unsupported, because any responses may arrive at another ASP.NET Core application process.
/// Client sampling and roots capabilities are also disabled in stateless mode, because the server cannot make requests.
/// Defaults to <see langword="false"/>.
/// </remarks>
public bool Stateless { get; set; }
/// <summary>
/// Gets or sets whether the server should use a single execution context for the entire session.
/// If <see langword="false"/>, handlers like tools get called with the <see cref="ExecutionContext"/>
/// belonging to the corresponding HTTP request which can change throughout the MCP session.
/// If <see langword="true"/>, handlers will get called with the same <see cref="ExecutionContext"/>
/// used to call <see cref="ConfigureSessionOptions" /> and <see cref="RunSessionHandler"/>.
/// </summary>
/// <remarks>
/// Enabling a per-session <see cref="ExecutionContext"/> can be useful for setting <see cref="AsyncLocal{T}"/> variables
/// that persist for the entire session, but it prevents you from using IHttpContextAccessor in handlers.
/// Defaults to <see langword="false"/>.
/// </remarks>
public bool PerSessionExecutionContext { get; set; }
/// <summary>
/// Gets or sets the duration of time the server will wait between any active requests before timing out an MCP session.
/// </summary>
/// <remarks>
/// This is checked in background every 5 seconds. A client trying to resume a session will receive a 404 status code
/// and should restart their session. A client can keep their session open by keeping a GET request open.
/// Defaults to 2 hours.
/// </remarks>
public TimeSpan IdleTimeout { get; set; } = TimeSpan.FromHours(2);
/// <summary>
/// Gets or sets maximum number of idle sessions to track in memory. This is used to limit the number of sessions that can be idle at once.
/// </summary>
/// <remarks>
/// Past this limit, the server will log a critical error and terminate the oldest idle sessions even if they have not reached
/// their <see cref="IdleTimeout"/> until the idle session count is below this limit. Clients that keep their session open by
/// keeping a GET request open will not count towards this limit.
/// Defaults to 10,000 sessions.
/// </remarks>
public int MaxIdleSessionCount { get; set; } = 10_000;
/// <summary>
/// Used for testing the <see cref="IdleTimeout"/>.
/// </summary>
public TimeProvider TimeProvider { get; set; } = TimeProvider.System;
}