← Prompts
Reference Copilot Instructions Collection
Instructions for building Model Context Protocol (MCP) servers using the C# SDK
Instructions for building Model Context Protocol (MCP) servers using the C# SDK
# C# MCP Server Development ## Instructions - Use the **ModelContextProtocol** NuGet package (prerelease) for most projects: `dotnet add package ModelContextProtocol --prerelease` - Use **ModelContextProtocol.AspNetCore** for HTTP-based MCP servers - Use **ModelContextProtocol.Core** for minimal dependencies (client-only or low-level server APIs) - Always configure logging to stderr using `LogToStandardErrorThreshold = LogLevel.Trace` to avoid interfering with stdio transport - Use the `[McpServerToolType]` attribute on classes containing MCP tools - Use the `[McpServerTool]` attribute on methods to expose them as tools - Use the `[Description]` attribute from `System.ComponentModel` to document tools and parameters - Support dependency injection in tool methods - inject `McpServer`, `HttpClient`, or other services as parameters - Use `McpServer.AsSamplingChatClient()` to make sampling requests back to the client from within tools - Expose prompts using `[McpServerPromptType]` on classes and `[McpServerPrompt]` on methods - For stdio transport, use `WithStdioServerTransport()` when building the server - Use `WithToolsFromAssembly()` to auto-discover and register all tools from the current assembly - Tool methods can be synchronous or async (return `Task` or `Task<T>`) - Always include comprehensive descriptions for tools and parameters to help LLMs understand their purpose - Use `CancellationToken` parameters in async tools for proper cancellation support - Return simple types (string, int, etc.) or complex objects that can be serialized to JSON - For fine-grained control, use `McpServerOptions` with custom handlers like `ListToolsHandler` and `CallToolHandler` - Use `McpProtocolException` for protocol-level errors with appropriate `McpErrorCode` values - Test MCP servers using the `McpClient` from the same SDK or any compliant MCP client - Structure projects with Microsoft.Extensions.Hosting for proper DI and lifecycle management
Sign in to view the full prompt.
Sign InClassification
Reference Documentation, cheatsheets, setup guides
Scope Project
This codebase Manual Manually placed / Persistent