Table of Contents

Class McpServer

Namespace
ModelContextProtocol.Server
Assembly
ModelContextProtocol.Core.dll

Represents an instance of a Model Context Protocol (MCP) server that connects to and communicates with an MCP client.

public abstract class McpServer : McpSession, IAsyncDisposable
Inheritance
McpServer
Implements
Inherited Members

Properties

ClientCapabilities

Gets the capabilities supported by the client.

public abstract ClientCapabilities? ClientCapabilities { get; }

Property Value

ClientCapabilities

Remarks

These capabilities are established during the initialization handshake and indicate which features the client supports, such as sampling, roots, and other protocol-specific functionality.

Server implementations can check these capabilities to determine which features are available when interacting with the client.

ClientInfo

Gets the version and implementation information of the connected client.

public abstract Implementation? ClientInfo { get; }

Property Value

Implementation

Remarks

This property contains identification information about the client that has connected to this server, including its name and version. This information is provided by the client during initialization.

Server implementations can use this information for logging, tracking client versions, or implementing client-specific behaviors.

LoggingLevel

Gets the last logging level set by the client, or null if it's never been set.

public abstract LoggingLevel? LoggingLevel { get; }

Property Value

LoggingLevel?

ServerOptions

Gets the options used to construct this server.

public abstract McpServerOptions ServerOptions { get; }

Property Value

McpServerOptions

Remarks

These options define the server's capabilities, protocol version, and other configuration settings that were used to initialize the server.

Services

Gets the service provider for the server.

public abstract IServiceProvider? Services { get; }

Property Value

IServiceProvider

Methods

AsClientLoggerProvider()

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

public ILoggerProvider AsClientLoggerProvider()

Returns

ILoggerProvider

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

AsSamplingChatClient()

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

public IChatClient AsSamplingChatClient()

Returns

IChatClient

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

Exceptions

InvalidOperationException

The client does not support sampling.

Create(ITransport, McpServerOptions, ILoggerFactory?, IServiceProvider?)

Creates a new instance of an McpServer.

public static McpServer Create(ITransport transport, McpServerOptions serverOptions, ILoggerFactory? loggerFactory = null, IServiceProvider? serviceProvider = null)

Parameters

transport ITransport

Transport to use for the server representing an already-established MCP session.

serverOptions McpServerOptions

Configuration options for this server, including capabilities.

loggerFactory ILoggerFactory

Logger factory to use for logging. If null, logging will be disabled.

serviceProvider IServiceProvider

Optional service provider to create new instances of tools and other dependencies.

Returns

McpServer

An McpServer instance that should be disposed when no longer needed.

Exceptions

ArgumentNullException

transport is null.

ArgumentNullException

serverOptions is null.

ElicitAsync(ElicitRequestParams, CancellationToken)

Requests additional information from the user via the client, allowing the server to elicit structured data.

public ValueTask<ElicitResult> ElicitAsync(ElicitRequestParams request, CancellationToken cancellationToken = default)

Parameters

request ElicitRequestParams

The parameters for the elicitation request.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests.

Returns

ValueTask<ElicitResult>

A task containing the elicitation result.

Exceptions

InvalidOperationException

The client does not support elicitation.

ElicitAsync<T>(string, JsonSerializerOptions?, CancellationToken)

Requests additional information from the user via the client, constructing a request schema from the public serializable properties of T and deserializing the response into T.

public ValueTask<ElicitResult<T>> ElicitAsync<T>(string message, JsonSerializerOptions? serializerOptions = null, CancellationToken cancellationToken = default)

Parameters

message string

The message to present to the user.

serializerOptions JsonSerializerOptions

Serializer options that influence property naming and deserialization.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests.

Returns

ValueTask<ElicitResult<T>>

An ElicitResult<T> with the user's response, if accepted.

Type Parameters

T

The type describing the expected input shape. Only primitive members are supported (string, number, boolean, enum).

Remarks

Elicitation uses a constrained subset of JSON Schema and only supports strings, numbers/integers, booleans and string enums. Unsupported member types are ignored when constructing the schema.

RequestRootsAsync(ListRootsRequestParams, CancellationToken)

Requests the client to list the roots it exposes.

public ValueTask<ListRootsResult> RequestRootsAsync(ListRootsRequestParams request, CancellationToken cancellationToken = default)

Parameters

request ListRootsRequestParams

The parameters for the list roots request.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests.

Returns

ValueTask<ListRootsResult>

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

Exceptions

InvalidOperationException

The client does not support roots.

RunAsync(CancellationToken)

Runs the server, listening for and handling client requests.

public abstract Task RunAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

Task

SampleAsync(CreateMessageRequestParams, CancellationToken)

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

public ValueTask<CreateMessageResult> SampleAsync(CreateMessageRequestParams request, CancellationToken cancellationToken = default)

Parameters

request CreateMessageRequestParams

The parameters for the sampling request.

cancellationToken CancellationToken

The CancellationToken to monitor for cancellation requests.

Returns

ValueTask<CreateMessageResult>

A task containing the sampling result from the client.

Exceptions

InvalidOperationException

The client does not support sampling.

SampleAsync(IEnumerable<ChatMessage>, ChatOptions?, CancellationToken)

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

public Task<ChatResponse> SampleAsync(IEnumerable<ChatMessage> messages, ChatOptions? options = null, CancellationToken cancellationToken = default)

Parameters

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.

Exceptions

ArgumentNullException

messages is null.

InvalidOperationException

The client does not support sampling.