How modelcontextprotocol/csharp-sdk works
Main Components
• Core SDK Library: Represented by the main source under “src/ModelContextProtocol” which includes:
– Client components (e.g., IMcpClient, McpClient, McpClientFactory) allowing applications to consume MCP servers.
– Server components (e.g., IMcpServer, McpServer, McpServerFactory) that configure, start, and manage an MCP server.
– Protocol definitions: “Protocol/Messages”, “Protocol/Types”, and “Protocol/Transport”. These define the JSON RPC messages, types for tool calls, notifications, error codes, and transport abstraction.
– Shared utilities and logging mechanisms (e.g., “Logging/Log.cs”, “Utils/Json”, “Utils/ProcessHelper.cs”).
• Communication Transports: The SDK supports various transport mechanisms. Under “Protocol/Transport” you see providers for:
– Stdio-based transports (e.g., StdioClientTransport, StdioServerTransport)
– Server-Sent Events (SSE) based transport (e.g., HttpListenerSseServerTransport, SseClientTransport)
– HTTP listeners and other potential modes.
• Polyfills and Common Utilities: Found under “src/Common/Polyfills” demonstrate that compatibility and additional functionality for standard .NET libraries is provided as part of the SDK.
• Samples: Located in the “samples” directory, they offer real applications demonstrating how to:
– Build an ASP.NET Core SSE server.
– Create and use chat clients integrated with available tools.
– Run a “TestServer with Hosting” example.
• Testing: The “tests” folder includes focused tests on the client, server, protocol handling, transport integration, and utilities to validate core protocol behavior.
Out Relationships and Interactions
• Client and Server Communication:
– Illustrate the use of different transport channels (Stdio, SSE, HttpListener) which are abstracted by the Protocol/Transport layer.
• Protocol Exchange:
– The “Protocol/Messages” and “Protocol/Types” directories define the schema (e.g., JsonRpcMessage, JsonRpcNotification, RequestId, etc.), which are exchanged between the client and server.
• Tools and Integration Points:
– Highlight that server-side “tools” (registered by tools registration extensions like “WithTools” in the samples) are discovered through reflection (with attributes like [McpTool] and [McpToolType]).
– Emphasize how these tools interface with client calls, thereby forming an extension point for LLM interactions.
Architectural Patterns and Design Principles
• Configurability: The presence of builder extension methods (e.g., “WithStdioServerTransport”, “WithTools”) and configuration objects (e.g., McpClientOptions, McpServerConfig) shows that the system is configurable. Represent these configuration objects as connectors or interfaces between client/server and their underlying transport/protocol engine.
Technologies and Frameworks to Highlight
• .NET's Host and Dependency Injection: Samples show usage of “Host.CreateEmptyApplicationBuilder” and built-in dependency injection to set up the server.
• JSON and RPC Handling: The project defines converters and utilities for JSON RPC (e.g., JsonRpcMessageConverter, McpJsonUtilities), which are integral in serializing and deserializing protocol messages.
• HTTP and SSE: In the samples (e.g., AspNetCoreSseServer) and in the transport implementations (HTTP/SSE clients), there is integration with ASP.NET Core frameworks and HTTP listeners.