Skip to content

Commit a54c6c2

Browse files
Copilotstephentoub
andcommitted
Add key types and architectural layers documentation
Co-authored-by: stephentoub <[email protected]>
1 parent c4db428 commit a54c6c2

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

.github/copilot-instructions.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ The SDK consists of three main packages:
9898
- **Clean**: `dotnet clean` or `make clean`
9999

100100
### SDK Requirements
101-
- The project currently uses .NET SDK 10.0 RC (this is temporary while waiting for .NET 10 to go GA)
101+
- The repo currently requires the .NET SDK 10.0 to build and run tests.
102102
- Target frameworks: .NET 10.0, .NET 9.0, .NET 8.0, .NET Standard 2.0
103103
- Support Native AOT compilation
104104

@@ -109,6 +109,86 @@ The SDK consists of three main packages:
109109
- Documentation: `docs/`
110110
- Build artifacts: `artifacts/` (not committed)
111111

112+
## Key Types and Architectural Layers
113+
114+
The SDK is organized into distinct architectural layers, each with specific responsibilities:
115+
116+
### Protocol Layer (DTO Types)
117+
- Located in `ModelContextProtocol.Core/Protocol/`
118+
- Contains Data Transfer Objects (DTOs) for the MCP specification
119+
- All protocol types follow JSON-RPC 2.0 conventions
120+
- Key types:
121+
- **JsonRpcMessage** (abstract base): Represents any JSON-RPC message (request, response, notification, error)
122+
- **JsonRpcRequest**, **JsonRpcResponse**, **JsonRpcNotification**: Concrete message types
123+
- **Tool**, **Prompt**, **Resource**: MCP primitive definitions
124+
- **CallToolRequestParams**, **GetPromptRequestParams**, **ReadResourceRequestParams**: Request parameter types
125+
- **ClientCapabilities**, **ServerCapabilities**: Capability negotiation types
126+
- **Implementation**: Server/client identification metadata
127+
128+
### JSON-RPC Implementation
129+
- Built-in JSON-RPC 2.0 implementation for MCP communication
130+
- **JsonRpcMessage.Converter**: Polymorphic converter that deserializes messages into correct types based on structure
131+
- **JsonRpcMessageContext**: Transport-specific metadata (transport reference, execution context, authenticated user)
132+
- Message routing handled automatically by session implementations
133+
- Error responses generated via **McpException** with **McpErrorCode** enumeration
134+
135+
### Transport Abstraction
136+
- **ITransport**: Core abstraction for bidirectional communication
137+
- Provides `MessageReader` (ChannelReader) for incoming messages
138+
- `SendMessageAsync()` for outgoing messages
139+
- `SessionId` property for multi-session scenarios
140+
- **IClientTransport**: Client-side abstraction that establishes connections and returns ITransport
141+
- **TransportBase**: Base class for transport implementations with common functionality
142+
143+
### Transport Implementations
144+
Two primary transport implementations with different invariants:
145+
146+
1. **Stdio-based transports** (`StdioServerTransport`, `StdioClientTransport`):
147+
- Single-session, process-bound communication
148+
- Uses standard input/output streams
149+
- No session IDs (returns null)
150+
- Automatic lifecycle tied to process
151+
152+
2. **HTTP-based transports**:
153+
- **SseResponseStreamTransport**: Server-Sent Events for server-to-client streaming
154+
- Unidirectional (server → client) event stream
155+
- Client posts messages to separate endpoint (e.g., `/message`)
156+
- Supports multiple concurrent sessions via SessionId
157+
- **StreamableHttpServerTransport**: Bidirectional HTTP with streaming
158+
- Request/response model with streamed progress updates
159+
- Session management for concurrent connections
160+
161+
### Session Layer
162+
- **McpSession** (abstract base): Core bidirectional communication for clients and servers
163+
- Manages JSON-RPC request/response correlation
164+
- Handles notification routing
165+
- Provides `SendRequestAsync<TResult>()`, `SendNotificationAsync()`, `RegisterNotificationHandler()`
166+
- Properties: `SessionId`, `NegotiatedProtocolVersion`
167+
168+
- **McpClient** (extends McpSession): Client-side MCP implementation
169+
- Connects to servers via `CreateAsync(IClientTransport)`
170+
- Exposes `ServerCapabilities`, `ServerInfo`, `ServerInstructions`
171+
- Methods: `ListToolsAsync()`, `CallToolAsync()`, `ListPromptsAsync()`, `GetPromptAsync()`, etc.
172+
173+
- **McpServer** (extends McpSession): Server-side MCP implementation
174+
- Configured via `McpServerOptions` and `IMcpServerBuilder`
175+
- Primitives registered as services: `McpServerTool`, `McpServerPrompt`, `McpServerResource`
176+
- Handles incoming requests through `McpServer.Methods.cs`
177+
- Supports filters via `McpRequestFilter` for cross-cutting concerns
178+
179+
### Serialization Architecture
180+
- **McpJsonUtilities.DefaultOptions**: Singleton JsonSerializerOptions for all MCP types
181+
- Hardcoded to use source-generated serialization for JSON-RPC messages (Native AOT compatible)
182+
- Source generation defined in `McpJsonUtilities` via `[JsonSerializable]` attributes
183+
- Includes Microsoft.Extensions.AI types via chained TypeInfoResolver
184+
185+
- **User-defined types** (tool parameters, return values):
186+
- Accept custom `JsonSerializerOptions` via `McpServerToolCreateOptions.SerializerOptions`
187+
- Default to `McpJsonUtilities.DefaultOptions` if not specified
188+
- Can use reflection-based serialization or custom source generators
189+
190+
- **Enum handling**: `CustomizableJsonStringEnumConverter` for flexible enum serialization
191+
112192
## Architecture and Design Patterns
113193

114194
### Server Implementation Architecture

0 commit comments

Comments
 (0)