forked from modelcontextprotocol/csharp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMcpServerRequestHandler.cs
More file actions
45 lines (41 loc) · 2.01 KB
/
Copy pathMcpServerRequestHandler.cs
File metadata and controls
45 lines (41 loc) · 2.01 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
using ModelContextProtocol.Protocol;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Nodes;
namespace ModelContextProtocol.Server;
/// <summary>
/// Represents a custom request handler that can be registered with the MCP server to handle
/// arbitrary JSON-RPC methods.
/// </summary>
/// <remarks>
/// <para>
/// Custom request handlers are registered via <see cref="McpServerOptions.RequestHandlers"/> and
/// are invoked when a JSON-RPC request with the matching <see cref="Method"/> is received.
/// The handler receives the raw <see cref="JsonRpcRequest"/> and returns a serialized
/// <see cref="JsonNode"/> response, giving extensions full control over request/response serialization.
/// </para>
/// </remarks>
[Experimental(Experimentals.Subclassing_DiagnosticId, UrlFormat = Experimentals.Subclassing_Url)]
public sealed class McpServerRequestHandler
{
/// <summary>
/// Gets the JSON-RPC method name this handler responds to.
/// </summary>
public required string Method { get; init; }
/// <summary>
/// Gets an optional predicate that determines whether this handler applies to an incoming request.
/// </summary>
/// <remarks>
/// When multiple custom handlers register the same method, each handler must specify this
/// predicate. The first applicable handler is invoked, allowing extensions to share a method
/// name while dispatching by negotiated protocol version or another request characteristic.
/// </remarks>
public Func<JsonRpcRequest, bool>? IsApplicable { get; init; }
/// <summary>
/// Gets the handler function that processes incoming requests for the specified method.
/// </summary>
/// <remarks>
/// The handler receives the full <see cref="JsonRpcRequest"/> and a <see cref="CancellationToken"/>,
/// and returns a serialized <see cref="JsonNode"/> response (or <see langword="null"/> for void methods).
/// </remarks>
public required Func<JsonRpcRequest, CancellationToken, ValueTask<JsonNode?>> Handler { get; init; }
}