csharp-sdk/src/ModelContextProtocol.Core
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Copilot 5084a79068
Add ILoggerFactory to StreamableHttpServerTransport (#1213)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: Stephen Toub <stoub@microsoft.com>
2026-01-30 17:45:39 -05:00
..
Authentication Include response body in HttpRequestException for transport client errors (#1193) 2026-01-29 17:23:21 -05:00
Client Include response body in HttpRequestException for transport client errors (#1193) 2026-01-29 17:23:21 -05:00
Protocol Add Trace-level logging for JSON-RPC payloads in transports (#1191) 2026-01-29 11:26:33 -08:00
Server Add ILoggerFactory to StreamableHttpServerTransport (#1213) 2026-01-30 17:45:39 -05:00
.editorconfig Streamable HTTP resumability + redelivery + SSE polling via server-side disconnect (#1077) 2026-01-14 22:54:25 -08:00
AIContentExtensions.cs Fix ToJsonObject serialization failure with anonymous types and add support for custom JsonSerializerOptions (#1113) 2026-01-20 11:21:11 +02:00
AssemblyNameHelper.cs Restructure IMcpEndpoint, IMcpClient, and IMcpServer (#723) 2025-09-16 10:22:10 -07:00
Diagnostics.cs Align telemetry with OpenTelemetry MCP semantic conventions (#1139) 2026-01-13 18:07:49 -08:00
McpErrorCode.cs Add ResourceNotFound error code (-32002) (#1062) 2025-12-05 10:39:30 -08:00
McpException.cs MCP Core docs edit pass (#1033) 2025-11-26 08:36:19 -05:00
McpJsonUtilities.cs Implement MCP task support (SEP-1686) (#1170) 2026-01-27 19:21:02 -08:00
McpProtocolException.cs Add support for URL mode elicitation (#1021) 2025-12-05 00:03:16 -08:00
McpSession.cs Add missing exception documentation to public API surface (#1103) 2025-12-15 12:51:09 -05:00
McpSession.Methods.cs Add missing exception documentation to public API surface (#1103) 2025-12-15 12:51:09 -05:00
McpSessionHandler.cs Streamable HTTP resumability + redelivery + SSE polling via server-side disconnect (#1077) 2026-01-14 22:54:25 -08:00
McpTaskCancellationTokenProvider.cs Implement MCP task support (SEP-1686) (#1170) 2026-01-27 19:21:02 -08:00
ModelContextProtocol.Core.csproj Fix keyset pagination with monotonic UUIDv7-like task IDs (#1215) 2026-01-30 17:09:24 -05:00
NopProgress.cs Add ModelContextProtocol.Core package (#428) 2025-06-02 15:16:45 -07:00
NotificationHandlers.cs Add McpClient/Session methods using .Protocol types (#1063) 2025-12-05 13:45:12 -05:00
ProcessHelper.cs Use Process.Kill(entireProcessTree: true) on .NET for faster process termination (#1187) 2026-01-29 11:29:08 -08:00
ProgressNotificationValue.cs MCP Core docs edit pass (#1033) 2025-11-26 08:36:19 -05:00
README.md Add request options bag to high level requests and include Meta (#970) 2025-12-04 17:32:12 -05:00
RequestHandlers.cs Add middleware and authz support for server-side handlers (#733) 2025-09-10 10:07:45 -07:00
RequestOptions.cs Add McpClientTool.WithMeta (#1027) 2025-12-05 15:41:44 -05:00
SemaphoreSlimExtensions.cs Add ModelContextProtocol.Core package (#428) 2025-06-02 15:16:45 -07:00
TokenProgress.cs Add request options bag to high level requests and include Meta (#970) 2025-12-04 17:32:12 -05:00
UriTemplate.cs Fix reserved expansion (e.g. "{+var}") when matching resources (#1142) 2026-01-27 17:23:31 -05:00
UrlElicitationRequiredException.cs Add McpClient/Session methods using .Protocol types (#1063) 2025-12-05 13:45:12 -05:00

MCP C# SDK Core

NuGet preview version

Core .NET SDK for the Model Context Protocol, enabling .NET applications, services, and libraries to implement and interact with MCP clients and servers. Please visit our API documentation for more details on available functionality.

Note

This project is in preview; breaking changes can be introduced without prior notice.

About MCP

The Model Context Protocol (MCP) is an open protocol that standardizes how applications provide context to Large Language Models (LLMs). It enables secure integration between LLMs and various data sources and tools.

For more information about MCP:

Installation

To get started, install the core package from NuGet

dotnet add package ModelContextProtocol.Core --prerelease

Getting Started (Client)

To get started writing a client, the McpClient.CreateAsync method is used to instantiate and connect an McpClient to a server. Once you have an McpClient, you can interact with it, such as to enumerate all available tools and invoke tools.

var clientTransport = new StdioClientTransport(new StdioClientTransportOptions
{
    Name = "Everything",
    Command = "npx",
    Arguments = ["-y", "@modelcontextprotocol/server-everything"],
});

var client = await McpClient.CreateAsync(clientTransport);

// Print the list of tools available from the server.
foreach (var tool in await client.ListToolsAsync())
{
    Console.WriteLine($"{tool.Name} ({tool.Description})");
}

// Execute a tool (this would normally be driven by LLM tool invocations).
var result = await client.CallToolAsync(
    "echo",
    new Dictionary<string, object?>() { ["message"] = "Hello MCP!" },
    cancellationToken: CancellationToken.None);

// echo always returns one and only one text content object
Console.WriteLine(result.Content.First(c => c.Type == "text").Text);

Clients can connect to any MCP server, not just ones created using this library. The protocol is designed to be server-agnostic, so you can use this library to connect to any compliant server.

Tools can be easily exposed for immediate use by IChatClients, because McpClientTool inherits from AIFunction.

// Get available functions.
IList<McpClientTool> tools = await client.ListToolsAsync();

// Call the chat client using the tools.
IChatClient chatClient = ...;
var response = await chatClient.GetResponseAsync(
    "your prompt here",
    new() { Tools = [.. tools] },

Getting Started (Server)

The core package provides the basic server functionality. Here's an example of creating a simple MCP server without dependency injection:

using ModelContextProtocol.Server;
using System.ComponentModel;

// Create server options
var serverOptions = new McpServerOptions();

// Add tools directly
serverOptions.Capabilities.Tools = new()
{
    ListChanged = true,
    ToolCollection = [
        McpServerTool.Create((string message) => $"hello {message}", new()
        {
            Name = "echo",
            Description = "Echoes the message back to the client."
        })
    ]
};

// Create and run server with stdio transport
var server = new McpServer(serverOptions);
using var stdioTransport = new StdioServerTransport();
await server.RunAsync(stdioTransport, CancellationToken.None);

For more advanced scenarios with dependency injection, hosting, and automatic tool discovery, see the ModelContextProtocol package.