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
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
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
ServerOptions
Gets the options used to construct this server.
public abstract McpServerOptions ServerOptions { get; }
Property Value
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
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
transportITransportTransport to use for the server representing an already-established MCP session.
serverOptionsMcpServerOptionsConfiguration options for this server, including capabilities.
loggerFactoryILoggerFactoryLogger factory to use for logging. If null, logging will be disabled.
serviceProviderIServiceProviderOptional service provider to create new instances of tools and other dependencies.
Returns
Exceptions
- ArgumentNullException
transportis null.- ArgumentNullException
serverOptionsis 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
requestElicitRequestParamsThe parameters for the elicitation request.
cancellationTokenCancellationTokenThe 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
messagestringThe message to present to the user.
serializerOptionsJsonSerializerOptionsSerializer options that influence property naming and deserialization.
cancellationTokenCancellationTokenThe CancellationToken to monitor for cancellation requests.
Returns
- ValueTask<ElicitResult<T>>
An ElicitResult<T> with the user's response, if accepted.
Type Parameters
TThe 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
requestListRootsRequestParamsThe parameters for the list roots request.
cancellationTokenCancellationTokenThe 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
cancellationTokenCancellationToken
Returns
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
requestCreateMessageRequestParamsThe parameters for the sampling request.
cancellationTokenCancellationTokenThe 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
messagesIEnumerable<ChatMessage>The messages to send as part of the request.
optionsChatOptionsThe options to use for the request, including model parameters and constraints.
cancellationTokenCancellationTokenThe CancellationToken to monitor for cancellation requests. The default is None.
Returns
- Task<ChatResponse>
A task containing the chat response from the model.
Exceptions
- ArgumentNullException
messagesis null.- InvalidOperationException
The client does not support sampling.