Class MessageContext
- Namespace
- ModelContextProtocol.Server
- Assembly
- ModelContextProtocol.Core.dll
Provides a context container that provides access to the server and resources for processing a JSON-RPC message.
public class MessageContext
- Inheritance
-
MessageContext
- Derived
-
RequestContext<TParams>
- Inherited Members
Remarks
The MessageContext encapsulates contextual information for handling any JSON-RPC message, including requests, responses, notifications, and errors. This is the base class for RequestContext<TParams>, which adds request-specific properties.
This type is typically received as a parameter in message filter delegates registered via IncomingMessageFilters or OutgoingMessageFilters.
Constructors
MessageContext(McpServer, JsonRpcMessage)
Initializes a new instance of the MessageContext class with the specified server and JSON-RPC message.
public MessageContext(McpServer server, JsonRpcMessage jsonRpcMessage)
Parameters
serverMcpServerThe server with which this instance is associated.
jsonRpcMessageJsonRpcMessageThe JSON-RPC message associated with this context.
Properties
Items
Gets or sets a key/value collection that can be used to share data within the scope of this message.
public IDictionary<string, object?> Items { get; set; }
Property Value
Remarks
This dictionary is shared with the Items property on the underlying JsonRpcMessage, ensuring that data set in message filters flows through to request-specific filters and handlers.
JsonRpcMessage
Gets the JSON-RPC message associated with this context.
public JsonRpcMessage JsonRpcMessage { get; set; }
Property Value
Remarks
This property provides access to the complete JSON-RPC message, including the method name (for requests/notifications), request ID (for requests/responses), and associated transport and user information.
Server
Gets or sets the server with which this instance is associated.
public McpServer Server { get; set; }
Property Value
Services
Gets or sets the services associated with this message.
public IServiceProvider? Services { get; set; }
Property Value
Remarks
This provider might not be the same instance stored in Services if ScopeRequests was true, in which case this might be a scoped IServiceProvider derived from the server's Services.
User
Gets or sets the user associated with this message.
public ClaimsPrincipal? User { get; set; }
Property Value
Remarks
This property is backed by the User property on the underlying JsonRpcMessage, ensuring that user information set in message filters flows through to request-specific filters and handlers.