|
| 1 | +# Copilot Instructions for MCP C# SDK |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +The SDK consists of three main packages: |
| 8 | +- **ModelContextProtocol** - The main package with hosting and dependency injection extensions |
| 9 | +- **ModelContextProtocol.AspNetCore** - HTTP-based MCP server implementations |
| 10 | +- **ModelContextProtocol.Core** - Client and low-level server APIs with minimal dependencies |
| 11 | + |
| 12 | +## C# Coding Standards |
| 13 | + |
| 14 | +### Language Features |
| 15 | +- Use **file-scoped namespaces** for all C# files |
| 16 | +- Enable **implicit usings** and **nullable reference types** |
| 17 | +- Use **preview language features** (LangVersion: preview) |
| 18 | +- Treat warnings as errors |
| 19 | + |
| 20 | +### Code Style |
| 21 | +- Follow the conventions in `.editorconfig` |
| 22 | +- Use clear, descriptive XML documentation comments for public APIs |
| 23 | +- Follow async/await patterns consistently |
| 24 | +- Use file-scoped namespaces: `namespace ModelContextProtocol.Client;` |
| 25 | + |
| 26 | +### Naming Conventions |
| 27 | +- Use `McpClient`, `McpServer`, `McpSession` for MCP-related classes (capitalize MCP) |
| 28 | +- Prefix MCP-specific types with `Mcp` (e.g., `McpException`, `McpEndpoint`) |
| 29 | +- Use descriptive names for parameters with `[Description("...")]` attributes when exposing to MCP |
| 30 | + |
| 31 | +## Architecture Patterns |
| 32 | + |
| 33 | +### Dependency Injection |
| 34 | +- Use Microsoft.Extensions.DependencyInjection patterns |
| 35 | +- Register services with `.AddMcpServer()` and `.AddMcpClient()` extension methods |
| 36 | +- Support both builder patterns and options configuration |
| 37 | + |
| 38 | +### JSON Serialization |
| 39 | +- Use `System.Text.Json` for all JSON operations |
| 40 | +- Use `McpJsonUtilities.DefaultOptions` for consistent serialization |
| 41 | +- Support source generation for Native AOT compatibility |
| 42 | +- Set `JsonIgnoreCondition.WhenWritingNull` for optional properties |
| 43 | + |
| 44 | +### Async Patterns |
| 45 | +- All I/O operations should be async |
| 46 | +- Use `ValueTask<T>` for hot paths that may complete synchronously |
| 47 | +- Always accept `CancellationToken` parameters for async operations |
| 48 | +- Name parameters consistently: `cancellationToken` |
| 49 | + |
| 50 | +### MCP Protocol |
| 51 | +- Follow the MCP specification at https://spec.modelcontextprotocol.io/ |
| 52 | +- Use JSON-RPC 2.0 for message transport |
| 53 | +- Support standard MCP capabilities: tools, prompts, resources, sampling |
| 54 | +- Implement proper error handling with `McpException` and `McpErrorCode` |
| 55 | + |
| 56 | +## Testing |
| 57 | + |
| 58 | +### Test Organization |
| 59 | +- Unit tests in `tests/ModelContextProtocol.Tests` |
| 60 | +- Integration tests in `tests/ModelContextProtocol.AspNetCore.Tests` |
| 61 | +- Test helpers in `tests/Common` |
| 62 | +- Filter manual tests with `[Trait("Execution", "Manual")]` |
| 63 | + |
| 64 | +### Test Infrastructure |
| 65 | +- Use xUnit for all tests |
| 66 | +- Run tests with: `dotnet test --filter '(Execution!=Manual)'` |
| 67 | +- Tests should be isolated and not depend on external services (except manual tests) |
| 68 | + |
| 69 | +## Build and Development |
| 70 | + |
| 71 | +### Build Commands |
| 72 | +- **Restore**: `dotnet restore` or `make restore` |
| 73 | +- **Build**: `dotnet build` or `make build` |
| 74 | +- **Test**: `dotnet test` or `make test` |
| 75 | +- **Clean**: `dotnet clean` or `make clean` |
| 76 | + |
| 77 | +### SDK Requirements |
| 78 | +- The project uses .NET SDK preview versions |
| 79 | +- Target frameworks: .NET 8.0, .NET 9.0, .NET Standard 2.0 |
| 80 | +- Support Native AOT compilation |
| 81 | + |
| 82 | +### Project Structure |
| 83 | +- Source code: `src/` |
| 84 | +- Tests: `tests/` |
| 85 | +- Samples: `samples/` |
| 86 | +- Documentation: `docs/` |
| 87 | +- Build artifacts: `artifacts/` (not committed) |
| 88 | + |
| 89 | +## Common Patterns |
| 90 | + |
| 91 | +### MCP Server Tools |
| 92 | +Tools are exposed using attributes: |
| 93 | +```csharp |
| 94 | +[McpServerToolType] |
| 95 | +public class MyTools |
| 96 | +{ |
| 97 | + [McpServerTool, Description("Tool description")] |
| 98 | + public static async Task<string> MyTool( |
| 99 | + [Description("Parameter description")] string param, |
| 100 | + CancellationToken cancellationToken) |
| 101 | + { |
| 102 | + // Implementation |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +### MCP Server Prompts |
| 108 | +Prompts are exposed similarly: |
| 109 | +```csharp |
| 110 | +[McpServerPromptType] |
| 111 | +public static class MyPrompts |
| 112 | +{ |
| 113 | + [McpServerPrompt, Description("Prompt description")] |
| 114 | + public static ChatMessage MyPrompt([Description("Parameter description")] string content) => |
| 115 | + new(ChatRole.User, $"Prompt template: {content}"); |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### Client Usage |
| 120 | +```csharp |
| 121 | +var client = await McpClient.CreateAsync( |
| 122 | + new StdioClientTransport(new() { Command = "...", Arguments = [...] }), |
| 123 | + clientOptions: new() { /* ... */ }, |
| 124 | + loggerFactory: loggerFactory); |
| 125 | + |
| 126 | +var tools = await client.ListToolsAsync(); |
| 127 | +var result = await client.CallToolAsync("tool-name", arguments, cancellationToken); |
| 128 | +``` |
| 129 | + |
| 130 | +## OpenTelemetry Integration |
| 131 | + |
| 132 | +- The SDK includes built-in observability support |
| 133 | +- Use ActivitySource name: `"Experimental.ModelContextProtocol"` |
| 134 | +- Use Meter name: `"Experimental.ModelContextProtocol"` |
| 135 | +- Export traces and metrics using OTLP when appropriate |
| 136 | + |
| 137 | +## Documentation |
| 138 | + |
| 139 | +- API documentation is generated using DocFX |
| 140 | +- Conceptual documentation is in `docs/concepts/` |
| 141 | +- Keep README files up to date in package directories |
| 142 | +- Use `///` XML comments for all public APIs |
| 143 | +- Include `<remarks>` sections for detailed explanations |
| 144 | + |
| 145 | +## Security |
| 146 | + |
| 147 | +- Never commit secrets or API keys |
| 148 | +- Use environment variables for sensitive configuration |
| 149 | +- Support authentication mechanisms (OAuth, API keys) |
| 150 | +- Validate all user inputs |
| 151 | +- Follow secure coding practices per SECURITY.md |
| 152 | + |
| 153 | +## Additional Notes |
| 154 | + |
| 155 | +- This is a preview SDK; breaking changes may occur |
| 156 | +- Follow the Model Context Protocol specification |
| 157 | +- Integrate with Microsoft.Extensions.AI patterns where applicable |
| 158 | +- Support both stdio and HTTP transports |
| 159 | +- Maintain compatibility with the broader MCP ecosystem |
0 commit comments