using System.Security.Claims;
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 may be injected as parameters into s.
///
public sealed class RequestContext
{
/// The server with which this instance is associated.
private McpServer _server;
///
/// 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.
public RequestContext(McpServer server, JsonRpcRequest jsonRpcRequest)
{
Throw.IfNull(server);
Throw.IfNull(jsonRpcRequest);
_server = server;
JsonRpcRequest = jsonRpcRequest;
Services = server.Services;
User = jsonRpcRequest.Context?.User;
}
/// Gets or sets the server with which this instance is associated.
public McpServer Server
{
get => _server;
set
{
Throw.IfNull(value);
_server = value;
}
}
///
/// Gets or sets a key/value collection that can be used to share data within the scope of this request.
///
public IDictionary Items
{
get => field ??= new Dictionary();
set => field = value;
}
/// Gets or sets the services associated with this request.
///
/// This may not be the same instance stored in
/// if was true, in which case this
/// might be a scoped derived from the server's
/// .
///
public IServiceProvider? Services { get; set; }
/// Gets or sets the user associated with this request.
public ClaimsPrincipal? User { get; set; }
/// 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; }
}