-
Notifications
You must be signed in to change notification settings - Fork 680
Expand file tree
/
Copy pathJsonRpcMessageContext.cs
More file actions
86 lines (80 loc) · 4.18 KB
/
JsonRpcMessageContext.cs
File metadata and controls
86 lines (80 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using ModelContextProtocol.Server;
using System.Security.Claims;
using System.Text.Json.Serialization;
namespace ModelContextProtocol.Protocol;
/// <summary>
/// Contains contextual information for JSON-RPC messages that is not part of the JSON-RPC protocol specification.
/// </summary>
/// <remarks>
/// This class holds transport-specific and runtime context information that accompanies JSON-RPC messages
/// but is not serialized as part of the JSON-RPC payload. This includes transport references, execution context,
/// and authenticated user information.
/// </remarks>
public sealed class JsonRpcMessageContext
{
/// <summary>
/// Gets or sets the transport the <see cref="JsonRpcMessage"/> was received on or should be sent over.
/// </summary>
/// <remarks>
/// This property is used to support the Streamable HTTP transport where the specification states that the server
/// SHOULD include JSON-RPC responses in the HTTP response body for the POST request containing
/// the corresponding JSON-RPC request. It can be <see langword="null"/> for other transports.
/// </remarks>
public ITransport? RelatedTransport { get; set; }
/// <summary>
/// Gets or sets the <see cref="ExecutionContext"/> that should be used to run any handlers
/// </summary>
/// <remarks>
/// This property is used to support the Streamable HTTP transport in its default stateful mode. In this mode,
/// the <see cref="McpServer"/> outlives the initial HTTP request context it was created on, and new
/// JSON-RPC messages can originate from future HTTP requests. This behavior allows the transport to flow the
/// context with the JSON-RPC message. This is particularly useful for enabling IHttpContextAccessor
/// in tool calls.
/// </remarks>
public ExecutionContext? ExecutionContext { get; set; }
/// <summary>
/// Gets or sets the authenticated user associated with this JSON-RPC message.
/// </summary>
/// <remarks>
/// <para>
/// This property contains the <see cref="ClaimsPrincipal"/> representing the authenticated user
/// who initiated this JSON-RPC message. This enables request handlers to access user identity
/// and authorization information without requiring dependency on HTTP context accessors
/// or other HTTP-specific abstractions.
/// </para>
/// <para>
/// The user information is automatically populated by the transport layer when processing
/// incoming HTTP requests in ASP.NET Core scenarios. For other transport types or scenarios
/// where user authentication is not applicable, this property can be <see langword="null"/>.
/// </para>
/// <para>
/// This property is particularly useful in the Streamable HTTP transport where JSON-RPC messages
/// might outlive the original HTTP request context, allowing user identity to be preserved
/// throughout the message processing pipeline.
/// </para>
/// </remarks>
public ClaimsPrincipal? User { get; set; }
/// <summary>
/// Gets or sets a key/value collection that can be used to share data within the scope of this message.
/// </summary>
/// <remarks>
/// <para>
/// This property allows data to be flowed throughout the message processing pipeline,
/// including from incoming message filters to request-specific filters and handlers.
/// </para>
/// <para>
/// When creating a <see cref="MessageContext"/> or <see cref="RequestContext{TParams}"/> for server-side
/// processing, the Items dictionary from this context will be used, ensuring data set in message filters
/// is available in request filters and handlers.
/// </para>
/// </remarks>
public IDictionary<string, object?>? Items { get; set; }
/// <summary>
/// Gets or sets the cancellation token associated with the transport request that carried this JSON-RPC message.
/// </summary>
/// <remarks>
/// For HTTP transports, this can be linked to the underlying HTTP request's aborted cancellation token (e.g. HttpContext.RequestAborted)
/// to propagate transport-level cancellations to tool executions.
/// </remarks>
public CancellationToken? CancellationToken { get; set; }
}