using ModelContextProtocol.Protocol;
namespace ModelContextProtocol.Server;
///
/// Provides a context container that provides access to the client request parameters and resources for the request.
///
/// Type of the request parameters specific to each MCP operation.
///
/// The encapsulates all contextual information for handling an MCP request.
/// This type is typically received as a parameter in handler delegates registered with IMcpServerBuilder,
/// and can be injected as parameters into s.
///
public sealed class RequestContext : MessageContext
{
///
/// Initializes a new instance of the class with the specified server and JSON-RPC request.
///
/// The server with which this instance is associated.
/// The JSON-RPC request associated with this context.
/// or is .
public RequestContext(McpServer server, JsonRpcRequest jsonRpcRequest)
: base(server, jsonRpcRequest)
{
}
/// Gets or sets the parameters associated with this request.
public TParams? Params { get; set; }
///
/// Gets or sets the primitive that matched the request.
///
public IMcpServerPrimitive? MatchedPrimitive { get; set; }
///
/// Gets the JSON-RPC request associated with this context.
///
///
/// This property provides access to the complete JSON-RPC request that initiated this handler invocation,
/// including the method name, parameters, request ID, and associated transport and user information.
///
public JsonRpcRequest JsonRpcRequest
{
get => (JsonRpcRequest)JsonRpcMessage;
set => JsonRpcMessage = value;
}
///
/// Ends the current response and enables polling for updates from the server.
///
/// The interval at which the client should poll for updates.
/// The cancellation token.
/// A that completes when polling has been enabled.
/// Thrown when the transport does not support polling.
public async ValueTask EnablePollingAsync(TimeSpan retryInterval, CancellationToken cancellationToken = default)
{
if (JsonRpcRequest.Context?.RelatedTransport is not StreamableHttpPostTransport transport)
{
throw new InvalidOperationException("Polling is only supported for Streamable HTTP transports.");
}
await transport.EnablePollingAsync(retryInterval, cancellationToken).ConfigureAwait(false);
}
}