This repository contains the official C# SDK for the Model Context Protocol (MCP), enabling .NET applications to implement and interact with MCP clients and servers.
The SDK consists of three main packages:
- ModelContextProtocol.Core - Client and low-level server APIs with minimal dependencies
- ModelContextProtocol - The main package with hosting and dependency injection extensions and which references ModelContextProtocol.Core
- ModelContextProtocol.AspNetCore - HTTP-based MCP server implementations for ASP.NET Core, referencing ModelContextProtocol
- Use file-scoped namespaces for all C# files
- Enable implicit usings and nullable reference types
- Use preview language features (LangVersion: preview)
- Treat warnings as errors
- Follow the conventions in
.editorconfig - Use clear, descriptive XML documentation comments for public APIs
- Follow async/await patterns consistently
- Use file-scoped namespaces:
namespace ModelContextProtocol.Client;
- Use
McpClient,McpServer,McpSessionfor MCP-related classes (capitalize MCP) - Prefix MCP-specific types with
Mcp(e.g.,McpException,McpEndpoint) - Use descriptive names for parameters with
[Description("...")]attributes when exposing to MCP
- Use Microsoft.Extensions.DependencyInjection patterns
- Register services with
.AddMcpServer()and.AddMcpClient()extension methods - Support both builder patterns and options configuration
- Use
System.Text.Jsonfor all JSON operations - Use
McpJsonUtilities.DefaultOptionsfor consistent serialization - Support source generation for Native AOT compatibility
- Set
JsonIgnoreCondition.WhenWritingNullfor optional properties
- All I/O operations should be async
- Use
ValueTask<T>for hot paths that may complete synchronously - Always accept
CancellationTokenparameters for async operations - Name parameters consistently:
cancellationToken
- Follow the MCP specification at https://spec.modelcontextprotocol.io/ (https://github.com/modelcontextprotocol/modelcontextprotocol/tree/main/docs/specification)
- Use JSON-RPC 2.0 for message transport
- Support all standard MCP capabilities (e.g. tools, prompts, resources, sampling)
- Implement proper error handling with
McpExceptionandMcpErrorCode
- Unit tests in
tests/ModelContextProtocol.Tests - Integration tests in
tests/ModelContextProtocol.AspNetCore.Tests - Test helpers in
tests/Common - Filter manual tests with
[Trait("Execution", "Manual")]
- Use xUnit for all tests
- Run tests with:
dotnet test --filter '(Execution!=Manual)' - Tests should be isolated and not depend on external services (except manual tests)
- Restore:
dotnet restoreormake restore - Build:
dotnet buildormake build - Test:
dotnet testormake test - Clean:
dotnet cleanormake clean
- The project uses .NET SDK preview versions
- Target frameworks: .NET 8.0, .NET 9.0, .NET Standard 2.0
- Support Native AOT compilation
- Source code:
src/ - Tests:
tests/ - Samples:
samples/ - Documentation:
docs/ - Build artifacts:
artifacts/(not committed)
Tools are exposed using attributes:
[McpServerToolType]
public class MyTools
{
[McpServerTool, Description("Tool description")]
public static async Task<string> MyTool(
[Description("Parameter description")] string param,
CancellationToken cancellationToken)
{
// Implementation
}
}Prompts are exposed similarly:
[McpServerPromptType]
public static class MyPrompts
{
[McpServerPrompt, Description("Prompt description")]
public static ChatMessage MyPrompt([Description("Parameter description")] string content) =>
new(ChatRole.User, $"Prompt template: {content}");
}var client = await McpClient.CreateAsync(
new StdioClientTransport(new() { Command = "...", Arguments = [...] }),
clientOptions: new() { /* ... */ },
loggerFactory: loggerFactory);
var tools = await client.ListToolsAsync();
var result = await client.CallToolAsync("tool-name", arguments, cancellationToken);- The SDK includes built-in observability support
- Use ActivitySource name:
"Experimental.ModelContextProtocol" - Use Meter name:
"Experimental.ModelContextProtocol" - Export traces and metrics using OTLP when appropriate
- API documentation is generated using DocFX
- Conceptual documentation is in
docs/concepts/ - Keep README files up to date in package directories
- Use
///XML comments for all public APIs - Include
<remarks>sections for detailed explanations
- Never commit secrets or API keys
- Use environment variables for sensitive configuration
- Support authentication mechanisms (OAuth, API keys)
- Validate all user inputs
- Follow secure coding practices per SECURITY.md
- This is a preview SDK; breaking changes may occur
- Follow the Model Context Protocol specification
- Integrate with Microsoft.Extensions.AI patterns where applicable
- Support both stdio and HTTP transports
- Maintain compatibility with the broader MCP ecosystem