Class RequestContext<TParams> | MCP C# SDK

Namespace
ModelContextProtocol.Server
Assembly
ModelContextProtocol.Core.dll

Provides a context container that provides access to the client request parameters and resources for the request.

public sealed class RequestContext<TParams> : MessageContext

Type Parameters

TParams

Type of the request parameters specific to each MCP operation.

Inheritance

RequestContext<TParams>

Inherited Members

Constructors

RequestContext(McpServer, JsonRpcRequest)

Initializes a new instance of the RequestContext<TParams> class with the specified server and JSON-RPC request.

[Obsolete("Use the constructor overload that accepts a parameters argument.", DiagnosticId = "MCP9003", UrlFormat = "https://github.com/modelcontextprotocol/csharp-sdk/blob/main/docs/list-of-diagnostics.md#mcp9003")]
public RequestContext(McpServer server, JsonRpcRequest jsonRpcRequest)

Parameters

server McpServer

The server with which this instance is associated.

jsonRpcRequest JsonRpcRequest

The JSON-RPC request associated with this context.

Exceptions

ArgumentNullException

server or jsonRpcRequest is null.

RequestContext(McpServer, JsonRpcRequest, TParams)

Initializes a new instance of the RequestContext<TParams> class with the specified server, JSON-RPC request, and request parameters.

public RequestContext(McpServer server, JsonRpcRequest jsonRpcRequest, TParams parameters)

Parameters

server McpServer

The server with which this instance is associated.

jsonRpcRequest JsonRpcRequest

The JSON-RPC request associated with this context.

parameters TParams

The parameters associated with this request.

Exceptions

ArgumentNullException

server or jsonRpcRequest is null.

Properties

JsonRpcRequest

Gets the JSON-RPC request associated with this context.

public JsonRpcRequest JsonRpcRequest { get; set; }

Property Value

JsonRpcRequest

MatchedPrimitive

Gets or sets the primitive that matched the request.

public IMcpServerPrimitive? MatchedPrimitive { get; set; }

Property Value

IMcpServerPrimitive

Params

Gets or sets the parameters associated with this request.

public TParams Params { get; set; }

Property Value

TParams

Methods

EnablePollingAsync(TimeSpan, CancellationToken)

Ends the current response and enables polling for updates from the server.

public ValueTask EnablePollingAsync(TimeSpan retryInterval, CancellationToken cancellationToken = default)

Parameters

retryInterval TimeSpan

The interval at which the client should poll for updates.

cancellationToken CancellationToken

The cancellation token.

Returns

ValueTask

A ValueTask that completes when polling has been enabled.

Exceptions

InvalidOperationException

Thrown when the transport does not support polling.