Table of Contents

Class McpServerExtensions

Namespace
ModelContextProtocol.Server
Assembly
ModelContextProtocol.dll

Provides extension methods for interacting with an IMcpServer instance.

public static class McpServerExtensions
Inheritance
McpServerExtensions
Inherited Members

Methods

AsClientLoggerProvider(IMcpServer)

Gets an ILogger on which logged messages will be sent as notifications to the client.

public static ILoggerProvider AsClientLoggerProvider(this IMcpServer server)

Parameters

server IMcpServer

The server to wrap as an ILogger.

Returns

ILoggerProvider

An ILogger that can be used to log to the client..

AsSamplingChatClient(IMcpServer)

Creates an Microsoft.Extensions.AI.IChatClient wrapper that can be used to send sampling requests to the client.

public static IChatClient AsSamplingChatClient(this IMcpServer server)

Parameters

server IMcpServer

The server to be wrapped as an Microsoft.Extensions.AI.IChatClient.

Returns

IChatClient

The Microsoft.Extensions.AI.IChatClient that can be used to issue sampling requests to the client.

Exceptions

ArgumentNullException

server is null.

ArgumentException

The client does not support sampling.

RequestRootsAsync(IMcpServer, ListRootsRequestParams, CancellationToken)

Requests the client to list the roots it exposes.

public static Task<ListRootsResult> RequestRootsAsync(this IMcpServer server, ListRootsRequestParams request, CancellationToken cancellationToken)

Parameters

server IMcpServer

The server initiating the request.

request ListRootsRequestParams

The parameters for the list roots request.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests.

Returns

Task<ListRootsResult>

A task containing the list of roots exposed by the client.

Remarks

This method requires the client to support the roots capability. Root resources allow clients to expose a hierarchical structure of resources that can be navigated and accessed by the server. These resources might include file systems, databases, or other structured data sources that the client makes available through the protocol.

Exceptions

ArgumentNullException

server is null.

ArgumentException

The client does not support roots.

RequestSamplingAsync(IMcpServer, CreateMessageRequestParams, CancellationToken)

Requests to sample an LLM via the client using the specified request parameters.

public static Task<CreateMessageResult> RequestSamplingAsync(this IMcpServer server, CreateMessageRequestParams request, CancellationToken cancellationToken)

Parameters

server IMcpServer

The server instance initiating the request.

request CreateMessageRequestParams

The parameters for the sampling request.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests.

Returns

Task<CreateMessageResult>

A task containing the sampling result from the client.

Remarks

This method requires the client to support sampling capabilities. It allows detailed control over sampling parameters including messages, system prompt, temperature, and token limits.

Exceptions

ArgumentNullException

server is null.

ArgumentException

The client does not support sampling.

RequestSamplingAsync(IMcpServer, IEnumerable<ChatMessage>, ChatOptions?, CancellationToken)

Requests to sample an LLM via the client using the provided chat messages and options.

public static Task<ChatResponse> RequestSamplingAsync(this IMcpServer server, IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)

Parameters

server IMcpServer

The server initiating the request.

messages IEnumerable<ChatMessage>

The messages to send as part of the request.

options ChatOptions

The options to use for the request, including model parameters and constraints.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests. The default is None.

Returns

Task<ChatResponse>

A task containing the chat response from the model.

Remarks

This method converts the provided chat messages into a format suitable for the sampling API, handling different content types such as text, images, and audio.

Exceptions

ArgumentNullException

server is null.

ArgumentNullException

messages is null.

ArgumentException

The client does not support sampling.