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
Extension Methods

Constructors

McpServer()

Initializes a new instance of the McpServer class.

[Experimental("MCPEXP002", UrlFormat = "https://github.com/modelcontextprotocol/csharp-sdk/blob/main/docs/list-of-diagnostics.md#mcpexp002")]
protected McpServer()

Properties

ClientCapabilities

Gets the capabilities supported by the client.

public abstract ClientCapabilities? ClientCapabilities { get; }

Property Value

ClientCapabilities

Remarks

On protocol revisions that use the initialize handshake (2025-11-25 and earlier), these capabilities are established once during initialization and are session-scoped: they are available both on the root McpServer and on the server exposed to request handlers.

On the 2026-07-28 revision and later (SEP-2575) there is no initialize handshake; the client declares its capabilities per-request in _meta, and the server MUST NOT infer them from previous requests. In that mode this property is only meaningful on the request-scoped server accessed via the Server property of the RequestContext<TParams> passed to a handler; on the root McpServer (for example one constructed manually over a Stream) it is null. It is also null in stateless transport mode, where server-to-client requests are unsupported.

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.

On protocol revisions that use the initialize handshake (2025-11-25 and earlier) this information is provided once during initialization and is session-scoped. On the 2026-07-28 revision and later it is carried per-request in _meta, so read it from the request-scoped server accessed via the Server property of the RequestContext<TParams> passed to a handler rather than from the root McpServer.

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

IsMrtrSupported

Gets a value indicating whether the connected client supports Multi Round-Trip Requests (MRTR).

public virtual bool IsMrtrSupported { get; }

Property Value

bool

Remarks

When this property returns true, tool handlers can throw InputRequiredException to return an InputRequiredResult with InputRequests and/or RequestState to the client.

When this property returns false, tool handlers should provide a fallback experience (for example, returning a text message explaining that the client does not support the required feature) instead of throwing InputRequiredException.

LoggingLevel

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

[Obsolete("The Logging feature is deprecated as of specification version 2026-07-28 and may be removed in a future version. See SEP-2577 for more information.", DiagnosticId = "MCP9005", UrlFormat = "https://github.com/modelcontextprotocol/csharp-sdk/blob/main/docs/list-of-diagnostics.md#obsolete-apis")]
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.

[Obsolete("The Logging feature is deprecated as of specification version 2026-07-28 and may be removed in a future version. See SEP-2577 for more information.", DiagnosticId = "MCP9005", UrlFormat = "https://github.com/modelcontextprotocol/csharp-sdk/blob/main/docs/list-of-diagnostics.md#obsolete-apis")]
public ILoggerProvider AsClientLoggerProvider()

Returns

ILoggerProvider

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

AsSamplingChatClient(JsonSerializerOptions?)

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

[Obsolete("The Sampling feature is deprecated as of specification version 2026-07-28 and may be removed in a future version. See SEP-2577 for more information.", DiagnosticId = "MCP9005", UrlFormat = "https://github.com/modelcontextprotocol/csharp-sdk/blob/main/docs/list-of-diagnostics.md#obsolete-apis")]
public IChatClient AsSamplingChatClient(JsonSerializerOptions? serializerOptions = null)

Parameters

serializerOptions JsonSerializerOptions

The JsonSerializerOptions to use for serialization. If null, DefaultOptions is used.

Returns

IChatClient

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

Remarks

When the server is using the Streamable HTTP transport, prefer obtaining this chat client from the McpServer instance available via RequestContext from inside a tool, prompt, or resource handler. That routes sampling requests through the originating POST response stream via RelatedTransport, which is always open for the duration of the request, rather than relying on the optional standalone GET SSE stream.

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

The 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 or 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 requestParams, CancellationToken cancellationToken = default)

Parameters

requestParams 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.

Remarks

When the server is using the Streamable HTTP transport, prefer calling this method on the McpServer instance available via RequestContext from inside a tool, prompt, or resource handler. That routes the request through the originating POST response stream via RelatedTransport, which is always open for the duration of the request, rather than relying on the optional standalone GET SSE stream.

Exceptions

ArgumentNullException

requestParams is null.

InvalidOperationException

The client does not support elicitation.

McpException

The request failed or the client returned an error response.

ElicitAsync<T>(string, RequestOptions?, 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, RequestOptions? options = null, CancellationToken cancellationToken = default)

Parameters

message string

The message to present to the user.

options RequestOptions

Optional request options including metadata, serialization settings, and progress tracking.

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.

Exceptions

ArgumentNullException

message is null.

ArgumentException

message is empty or composed entirely of whitespace.

InvalidOperationException

The client does not support elicitation.

McpException

The request failed or the client returned an error response.

RequestRootsAsync(ListRootsRequestParams, CancellationToken)

Requests the client to list the roots it exposes.

[Obsolete("The Roots feature is deprecated as of specification version 2026-07-28 and may be removed in a future version. See SEP-2577 for more information.", DiagnosticId = "MCP9005", UrlFormat = "https://github.com/modelcontextprotocol/csharp-sdk/blob/main/docs/list-of-diagnostics.md#obsolete-apis")]
public ValueTask<ListRootsResult> RequestRootsAsync(ListRootsRequestParams requestParams, CancellationToken cancellationToken = default)

Parameters

requestParams 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.

Remarks

When the server is using the Streamable HTTP transport, prefer calling this method on the McpServer instance available via RequestContext from inside a tool, prompt, or resource handler. That routes the request through the originating POST response stream via RelatedTransport, which is always open for the duration of the request, rather than relying on the optional standalone GET SSE stream.

Exceptions

ArgumentNullException

requestParams is null.

InvalidOperationException

The client does not support roots.

McpException

The request failed or the client returned an error response.

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.

[Obsolete("The Sampling feature is deprecated as of specification version 2026-07-28 and may be removed in a future version. See SEP-2577 for more information.", DiagnosticId = "MCP9005", UrlFormat = "https://github.com/modelcontextprotocol/csharp-sdk/blob/main/docs/list-of-diagnostics.md#obsolete-apis")]
public ValueTask<CreateMessageResult> SampleAsync(CreateMessageRequestParams requestParams, CancellationToken cancellationToken = default)

Parameters

requestParams 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.

Remarks

When the server is using the Streamable HTTP transport, prefer calling this method on the McpServer instance available via RequestContext from inside a tool, prompt, or resource handler. That routes the request through the originating POST response stream via RelatedTransport, which is always open for the duration of the request, rather than relying on the optional standalone GET SSE stream.

Exceptions

ArgumentNullException

requestParams is null.

InvalidOperationException

The client does not support sampling.

McpException

The request failed or the client returned an error response.

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

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

[Obsolete("The Sampling feature is deprecated as of specification version 2026-07-28 and may be removed in a future version. See SEP-2577 for more information.", DiagnosticId = "MCP9005", UrlFormat = "https://github.com/modelcontextprotocol/csharp-sdk/blob/main/docs/list-of-diagnostics.md#obsolete-apis")]
public Task<ChatResponse> SampleAsync(IEnumerable<ChatMessage> messages, ChatOptions? chatOptions = null, JsonSerializerOptions? serializerOptions = null, CancellationToken cancellationToken = default)

Parameters

messages IEnumerable<ChatMessage>

The messages to send as part of the request.

chatOptions ChatOptions

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

serializerOptions JsonSerializerOptions

The JsonSerializerOptions to use for serializing user-provided objects. If null, DefaultOptions is used.

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.

McpException

The request failed or the client returned an error response.

WithOutgoingRequestInterceptor(Func<string, JsonNode?, CancellationToken, ValueTask<JsonNode?>>)

Creates a non-mutating server facade that redirects server-initiated requests through an interceptor.

[Experimental("MCPEXP002", UrlFormat = "https://github.com/modelcontextprotocol/csharp-sdk/blob/main/docs/list-of-diagnostics.md#mcpexp002")]
public McpServer WithOutgoingRequestInterceptor(Func<string, JsonNode?, CancellationToken, ValueTask<JsonNode?>> interceptor)

Parameters

interceptor Func<string, JsonNode, CancellationToken, ValueTask<JsonNode>>

The interceptor invoked for each outgoing request. It receives the request method, the pre-serialized request parameters (or null), and a cancellation token, and returns the serialized result (or null to indicate no result).

Returns

McpServer

A server facade that uses interceptor for outgoing requests.

Remarks

On the returned facade, redirected methods skip their client-capability checks, because the alternate channel is responsible for delivering the request to the client.

Exceptions

ArgumentNullException

interceptor is null.