{"kind":"AgentDefinition","metadata":{"namespace":"community","name":"csharp-mcp-server","version":"0.1.0"},"spec":{"agents_md":"---\ndescription: 'Instructions for building Model Context Protocol (MCP) servers using the C# SDK'\napplyTo: '**/*.cs, **/*.csproj'\n---\n\n# C# MCP Server Development\n\n## Instructions\n\n- Use the **ModelContextProtocol** NuGet package (prerelease) for most projects: `dotnet add package ModelContextProtocol --prerelease`\n- Use **ModelContextProtocol.AspNetCore** for HTTP-based MCP servers\n- Use **ModelContextProtocol.Core** for minimal dependencies (client-only or low-level server APIs)\n- Always configure logging to stderr using `LogToStandardErrorThreshold = LogLevel.Trace` to avoid interfering with stdio transport\n- Use the `[McpServerToolType]` attribute on classes containing MCP tools\n- Use the `[McpServerTool]` attribute on methods to expose them as tools\n- Use the `[Description]` attribute from `System.ComponentModel` to document tools and parameters\n- Support dependency injection in tool methods - inject `McpServer`, `HttpClient`, or other services as parameters\n- Use `McpServer.AsSamplingChatClient()` to make sampling requests back to the client from within tools\n- Expose prompts using `[McpServerPromptType]` on classes and `[McpServerPrompt]` on methods\n- For stdio transport, use `WithStdioServerTransport()` when building the server\n- Use `WithToolsFromAssembly()` to auto-discover and register all tools from the current assembly\n- Tool methods can be synchronous or async (return `Task` or `Task\u003cT\u003e`)\n- Always include comprehensive descriptions for tools and parameters to help LLMs understand their purpose\n- Use `CancellationToken` parameters in async tools for proper cancellation support\n- Return simple types (string, int, etc.) or complex objects that can be serialized to JSON\n- For fine-grained control, use `McpServerOptions` with custom handlers like `ListToolsHandler` and `CallToolHandler`\n- Use `McpProtocolException` for protocol-level errors with appropriate `McpErrorCode` values\n- Test MCP servers using the `McpClient` from the same SDK or any compliant MCP client\n- Structure projects with Microsoft.Extensions.Hosting for proper DI and lifecycle management\n\n## Best Practices\n\n- Keep tool methods focused and single-purpose\n- Use meaningful tool names that clearly indicate their function\n- Provide detailed descriptions that explain what the tool does, what parameters it expects, and what it returns\n- Validate input parameters and throw `McpProtocolException` with `McpErrorCode.InvalidParams` for invalid inputs\n- Use structured logging to help with debugging without polluting stdout\n- Organize related tools into logical classes with `[McpServerToolType]`\n- Consider security implications when exposing tools that access external resources\n- Use the built-in DI container to manage service lifetimes and dependencies\n- Implement proper error handling and return meaningful error messages\n- Test tools individually before integrating with LLMs\n\n## Common Patterns\n\n### Basic Server Setup\n```csharp\nvar builder = Host.CreateApplicationBuilder(args);\nbuilder.Logging.AddConsole(options =\u003e \n    options.LogToStandardErrorThreshold = LogLevel.Trace);\nbuilder.Services\n    .AddMcpServer()\n    .WithStdioServerTransport()\n    .WithToolsFromAssembly();\nawait builder.Build().RunAsync();\n```\n\n### Simple Tool\n```csharp\n[McpServerToolType]\npublic static class MyTools\n{\n    [McpServerTool, Description(\"Description of what the tool does\")]\n    public static string ToolName(\n        [Description(\"Parameter description\")] string param) =\u003e \n        $\"Result: {param}\";\n}\n```\n\n### Tool with Dependency Injection\n```csharp\n[McpServerTool, Description(\"Fetches data from a URL\")]\npublic static async Task\u003cstring\u003e FetchData(\n    HttpClient httpClient,\n    [Description(\"The URL to fetch\")] string url,\n    CancellationToken cancellationToken) =\u003e\n    await httpClient.GetStringAsync(url, cancellationToken);\n```\n\n### Tool with Sampling\n```csharp\n[McpServerTool, Description(\"Analyzes content using the client's LLM\")]\npublic static async Task\u003cstring\u003e Analyze(\n    McpServer server,\n    [Description(\"Content to analyze\")] string content,\n    CancellationToken cancellationToken)\n{\n    var messages = new ChatMessage[]\n    {\n        new(ChatRole.User, $\"Analyze this: {content}\")\n    };\n    return await server.AsSamplingChatClient()\n        .GetResponseAsync(messages, cancellationToken: cancellationToken);\n}\n```\n","description":"Instructions for building Model Context Protocol (MCP) servers using the C# SDK","import":{"commit_sha":"541b7819d8c3545c6df122491af4fa1eae415779","imported_at":"2026-05-18T20:05:35Z","license_text":"MIT License\n\nCopyright GitHub, Inc.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.","owner":"github","repo":"github/awesome-copilot","source_url":"https://github.com/github/awesome-copilot/blob/541b7819d8c3545c6df122491af4fa1eae415779/instructions/csharp-mcp-server.instructions.md"},"manifest":{}},"content_hash":[22,145,169,251,135,109,109,97,214,183,39,219,197,45,26,117,139,12,7,128,105,136,198,21,140,95,213,109,66,177,144,100],"trust_level":"unsigned","yanked":false}
