using Microsoft.AspNetCore.Http;
using ModelContextProtocol.Server;
namespace ModelContextProtocol.AspNetCore;
///
/// Represents configuration options for ,
/// which implements the Streamable HTTP transport for the Model Context Protocol.
/// See the protocol specification for details on the Streamable HTTP transport.
///
///
/// For details on the Streamable HTTP transport, see the protocol specification.
///
public class HttpServerTransportOptions
{
///
/// Gets or sets an optional asynchronous callback to configure per-session
/// with access to the of the request that initiated the session.
///
public Func? ConfigureSessionOptions { get; set; }
///
/// Gets or sets an optional asynchronous callback for running new MCP sessions manually.
///
///
/// This callback is useful for running logic before a session starts and after it completes.
///
/// The parameter comes from the request that initiated the session (e.g., the
/// initialize request) and may not be usable after starts, since that
/// request will have already completed.
///
///
/// Consider using instead, which provides access to the
/// of the initializing request with fewer known issues.
///
///
/// This API is experimental and may be removed or change signatures in a future release.
///
///
[System.Diagnostics.CodeAnalysis.Experimental(Experimentals.RunSessionHandler_DiagnosticId, UrlFormat = Experimentals.RunSessionHandler_Url)]
public Func? RunSessionHandler { get; set; }
///
/// Gets or sets a value that indicates whether the server runs in a stateless mode that doesn't track state between requests,
/// allowing for load balancing without session affinity.
///
///
/// if the server runs in a stateless mode; if the server tracks state between requests. The default is .
///
///
/// If , will be null, and the "MCP-Session-Id" header will not be used,
/// the will be called once for each request, and the "/sse" endpoint will be disabled.
/// Unsolicited server-to-client messages and all server-to-client requests are also unsupported, because any responses
/// might arrive at another ASP.NET Core application process.
/// Client sampling, elicitation, and roots capabilities are also disabled in stateless mode, because the server cannot make requests.
///
public bool Stateless { get; set; }
///
/// Gets or sets the event store for resumability support.
/// When set, events are stored and can be replayed when clients reconnect with a Last-Event-ID header.
///
///
/// When configured, the server will:
///
/// - Generate unique event IDs for each SSE message
/// - Store events for later replay
/// - Replay missed events when a client reconnects with a Last-Event-ID header
/// - Send priming events to establish resumability before any actual messages
///
///
/// This can be set directly, or an can be registered in DI.
/// If this property is not set, the server will attempt to resolve an from DI.
///
///
public ISseEventStreamStore? EventStreamStore { get; set; }
///
/// Gets or sets the session migration handler for cross-instance session migration.
///
///
///
/// When configured, the server will support session migration between instances.
/// If a request arrives with a session ID that is not found locally, the handler
/// is consulted to determine if the session can be migrated from another instance.
///
///
/// This can be set directly, or an can be registered in DI.
/// If this property is not set, the server will attempt to resolve an from DI.
///
///
public ISessionMigrationHandler? SessionMigrationHandler { get; set; }
///
/// Gets or sets a value that indicates whether the server uses a single execution context for the entire session.
///
///
/// if the server uses a single execution context for the entire session; otherwise, . The default is .
///
///
/// If , handlers like tools get called with the
/// belonging to the corresponding HTTP request, which can change throughout the MCP session.
/// If , handlers will get called with the same
/// used to call and .
/// Enabling a per-session can be useful for setting variables
/// that persist for the entire session, but it prevents you from using IHttpContextAccessor in handlers.
///
public bool PerSessionExecutionContext { get; set; }
///
/// Gets or sets the duration of time the server will wait between any active requests before timing out an MCP session.
///
///
/// The amount of time the server waits between any active requests before timing out an MCP session. The default is 2 hours.
///
///
/// This value is checked in the 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.
///
public TimeSpan IdleTimeout { get; set; } = TimeSpan.FromHours(2);
///
/// Gets or sets the maximum number of idle sessions to track in memory. This value is used to limit the number of sessions that can be idle at once.
///
///
/// The maximum number of idle sessions to track in memory. The default is 10,000 sessions.
///
///
/// Past this limit, the server logs a critical error and terminates the oldest idle sessions, even if they have not reached
/// their , until the idle session count is below this limit. Clients that keep their session open by
/// keeping a GET request open don't count towards this limit.
///
public int MaxIdleSessionCount { get; set; } = 10_000;
///
/// Gets or sets the time provider that's used for testing the .
///
public TimeProvider TimeProvider { get; set; } = TimeProvider.System;
}