Table of Contents

Class McpServerHandlers

Namespace
ModelContextProtocol.Server
Assembly
ModelContextProtocol.dll

Provides a container for handlers used in the creation of an MCP server.

public sealed class McpServerHandlers
Inheritance
McpServerHandlers
Inherited Members

Remarks

This class provides a centralized collection of delegates that implement various capabilities of the Model Context Protocol. Each handler in this class corresponds to a specific endpoint in the Model Context Protocol and is responsible for processing a particular type of request. The handlers are used to customize the behavior of the MCP server by providing implementations for the various protocol operations.

Handlers can be configured individually using the extension methods in McpServerBuilderExtensions such as WithListToolsHandler(IMcpServerBuilder, Func<RequestContext<ListToolsRequestParams>, CancellationToken, ValueTask<ListToolsResult>>) and WithCallToolHandler(IMcpServerBuilder, Func<RequestContext<CallToolRequestParams>, CancellationToken, ValueTask<CallToolResponse>>).

When a client sends a request to the server, the appropriate handler is invoked to process the request and produce a response according to the protocol specification. Which handler is selected is done based on an ordinal, case-sensitive string comparison.

Properties

CallToolHandler

Gets or sets the handler for ToolsCall requests.

public Func<RequestContext<CallToolRequestParams>, CancellationToken, ValueTask<CallToolResponse>>? CallToolHandler { get; set; }

Property Value

Func<RequestContext<CallToolRequestParams>, CancellationToken, ValueTask<CallToolResponse>>

Remarks

This handler is invoked when a client makes a call to a tool that isn't found in the McpServerTool collection. The handler should implement logic to execute the requested tool and return appropriate results.

CompleteHandler

Gets or sets the handler for CompletionComplete requests.

public Func<RequestContext<CompleteRequestParams>, CancellationToken, ValueTask<CompleteResult>>? CompleteHandler { get; set; }

Property Value

Func<RequestContext<CompleteRequestParams>, CancellationToken, ValueTask<CompleteResult>>

Remarks

This handler provides auto-completion suggestions for prompt arguments or resource references in the Model Context Protocol. The handler processes auto-completion requests, returning a list of suggestions based on the reference type and current argument value.

GetPromptHandler

Gets or sets the handler for PromptsGet requests.

public Func<RequestContext<GetPromptRequestParams>, CancellationToken, ValueTask<GetPromptResult>>? GetPromptHandler { get; set; }

Property Value

Func<RequestContext<GetPromptRequestParams>, CancellationToken, ValueTask<GetPromptResult>>

Remarks

This handler is invoked when a client requests details for a specific prompt that isn't found in the McpServerPrompt collection. The handler should implement logic to fetch or generate the requested prompt and return appropriate results.

ListPromptsHandler

Gets or sets the handler for PromptsList requests.

public Func<RequestContext<ListPromptsRequestParams>, CancellationToken, ValueTask<ListPromptsResult>>? ListPromptsHandler { get; set; }

Property Value

Func<RequestContext<ListPromptsRequestParams>, CancellationToken, ValueTask<ListPromptsResult>>

Remarks

The handler should return a list of available prompts when requested by a client. It supports pagination through the cursor mechanism, where the client can make repeated calls with the cursor returned by the previous call to retrieve more prompts.

This handler works alongside any prompts defined in the McpServerPrompt collection. Prompts from both sources will be combined when returning results to clients.

ListResourceTemplatesHandler

Gets or sets the handler for ResourcesTemplatesList requests.

public Func<RequestContext<ListResourceTemplatesRequestParams>, CancellationToken, ValueTask<ListResourceTemplatesResult>>? ListResourceTemplatesHandler { get; set; }

Property Value

Func<RequestContext<ListResourceTemplatesRequestParams>, CancellationToken, ValueTask<ListResourceTemplatesResult>>

Remarks

The handler should return a list of available resource templates when requested by a client. It supports pagination through the cursor mechanism, where the client can make repeated calls with the cursor returned by the previous call to retrieve more resource templates.

ListResourcesHandler

Gets or sets the handler for ResourcesList requests.

public Func<RequestContext<ListResourcesRequestParams>, CancellationToken, ValueTask<ListResourcesResult>>? ListResourcesHandler { get; set; }

Property Value

Func<RequestContext<ListResourcesRequestParams>, CancellationToken, ValueTask<ListResourcesResult>>

Remarks

The handler should return a list of available resources when requested by a client. It supports pagination through the cursor mechanism, where the client can make repeated calls with the cursor returned by the previous call to retrieve more resources.

ListToolsHandler

Gets or sets the handler for ToolsList requests.

public Func<RequestContext<ListToolsRequestParams>, CancellationToken, ValueTask<ListToolsResult>>? ListToolsHandler { get; set; }

Property Value

Func<RequestContext<ListToolsRequestParams>, CancellationToken, ValueTask<ListToolsResult>>

Remarks

The handler should return a list of available tools when requested by a client. It supports pagination through the cursor mechanism, where the client can make repeated calls with the cursor returned by the previous call to retrieve more tools.

This handler works alongside any tools defined in the McpServerTool collection. Tools from both sources will be combined when returning results to clients.

ReadResourceHandler

Gets or sets the handler for ResourcesRead requests.

public Func<RequestContext<ReadResourceRequestParams>, CancellationToken, ValueTask<ReadResourceResult>>? ReadResourceHandler { get; set; }

Property Value

Func<RequestContext<ReadResourceRequestParams>, CancellationToken, ValueTask<ReadResourceResult>>

Remarks

This handler is invoked when a client requests the content of a specific resource identified by its URI. The handler should implement logic to locate and retrieve the requested resource.

SetLoggingLevelHandler

Gets or sets the handler for LoggingSetLevel requests.

public Func<RequestContext<SetLevelRequestParams>, CancellationToken, ValueTask<EmptyResult>>? SetLoggingLevelHandler { get; set; }

Property Value

Func<RequestContext<SetLevelRequestParams>, CancellationToken, ValueTask<EmptyResult>>

Remarks

This handler processes LoggingSetLevel requests from clients. When set, it enables clients to control which log messages they receive by specifying a minimum severity threshold.

After handling a level change request, the server typically begins sending log messages at or above the specified level to the client as notifications/message notifications.

SubscribeToResourcesHandler

Gets or sets the handler for ResourcesSubscribe requests.

public Func<RequestContext<SubscribeRequestParams>, CancellationToken, ValueTask<EmptyResult>>? SubscribeToResourcesHandler { get; set; }

Property Value

Func<RequestContext<SubscribeRequestParams>, CancellationToken, ValueTask<EmptyResult>>

Remarks

This handler is invoked when a client wants to receive notifications about changes to specific resources or resource patterns. The handler should implement logic to register the client's interest in the specified resources and set up the necessary infrastructure to send notifications when those resources change.

After a successful subscription, the server should send resource change notifications to the client whenever a relevant resource is created, updated, or deleted.

UnsubscribeFromResourcesHandler

Gets or sets the handler for ResourcesUnsubscribe requests.

public Func<RequestContext<UnsubscribeRequestParams>, CancellationToken, ValueTask<EmptyResult>>? UnsubscribeFromResourcesHandler { get; set; }

Property Value

Func<RequestContext<UnsubscribeRequestParams>, CancellationToken, ValueTask<EmptyResult>>

Remarks

This handler is invoked when a client wants to stop receiving notifications about previously subscribed resources. The handler should implement logic to remove the client's subscriptions to the specified resources and clean up any associated resources.

After a successful unsubscription, the server should no longer send resource change notifications to the client for the specified resources.