Class McpServer
Hosts a Model Context Protocol server: it exposes registered tools and resources to an MCP client and answers JSON-RPC messages.
[Obfuscation(Exclude = true)]
public sealed class McpServer
- Inheritance
-
McpServer
- Inherited Members
Examples
var server = new McpServer(new McpServerOptions { Name = "my-server", Version = "1.0.0" });
server.RegisterTool(new MyTool());
string reply = await server.HandleMessageAsync(incomingJson, new McpRequestContext());
Remarks
The server is transport-agnostic. It consumes a JSON-RPC message and produces the reply, so a host can drive it over HTTP, over standard input and output, or over any other channel. McpStdioServer provides the standard input and output loop.
Tools are ordinary ITool implementations. A tool that also implements IToolMetadata has its MCP annotations derived from its declared side effect and idempotence, and becomes governable through PermissionPolicy. A tool that implements IMcpTool may additionally return images, resource links, and a structured projection.
Constructors
- McpServer(McpServerOptions)
Initializes a new server.
Fields
- DefaultProtocolVersion
The protocol revision this implementation targets.
Properties
- Capabilities
Gets the capabilities this server advertises, derived from what is actually registered.
- LogLevel
Gets the minimum severity the client has asked to receive. Defaults to Info; a server should not send anything below it.
- Options
Gets the options this server was created with.
- PermissionPolicy
Gets or sets the policy deciding which registered tools are exposed and callable. When
null, every registered tool is exposed.
- Tools
Gets the tools currently exposed, after the permission policy has been applied.
Methods
- BuildLogNotification(McpLogLevel, string, string)
Builds a
notifications/messagelog message. Hosts push this over the server-to-client channel and drop anything below LogLevel.
- BuildProgressNotification(string, double, double?, string)
Builds a
notifications/progressmessage. Hosts push this to the client over whatever channel carries server-initiated messages.
- BuildToolListChangedNotification()
Builds a
notifications/tools/list_changedmessage, sent when the exposed tool set changes so a live session does not have to reconnect to see it.
- HandleMessageAsync(string, McpRequestContext, CancellationToken)
Handles one incoming JSON-RPC message and produces the reply.
- RegisterCompletionProvider(IMcpCompletionProvider)
Registers an argument-completion provider. Providers are consulted in registration order until one answers.
- RegisterPromptProvider(IMcpPromptProvider)
Registers a prompt provider. Providers are consulted in registration order until one claims the prompt being expanded.
- RegisterResourceProvider(IMcpResourceProvider)
Registers a resource provider. Providers are consulted in registration order until one claims the URI being read.
- RegisterTool(ITool)
Registers a tool. Registering a name that is already present replaces the previous tool.
- UnregisterTool(string)
Removes a tool by name.
Events
- LogLevelChanged
Raised when a client sets its minimum log level through
logging/setLevel. Hosts use it to start or stop forwarding log messages.
- ToolListChanged
Raised when the exposed tool set changes. Hosts forward this as a
notifications/tools/list_changednotification so live sessions pick up the change without reconnecting.