skills/csharp-mcp-server/SKILL.md
產生或撰寫 C# MCP (Model Context Protocol) 伺服器時的最佳實踐與專案結構規劃。
npx skillsauth add CloudyWing/ai-dotfiles csharp-mcp-serverInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
當使用者需要建立擴充 AI 能力的 MCP (Model Context Protocol) 伺服器,且語言選定為 C# (通常為 .NET 10.0 以上) 時,請遵循此指南。
dotnet new console),使用 Microsoft.Extensions.Hosting 管理生命週期。dotnet new web),以 WebApplicationBuilder 啟動,通常搭配 Docker 使用。端點預設為 /mcp。ModelContextProtocol 預覽套件);Streamable HTTP 模式需要 ModelContextProtocol.AspNetCore。stdio 用來傳輸 JSON-RPC,必須將所有 Logging (如 ILogger) 導向 stderr,或是寫入實體檔案之中。Streamable HTTP 模式使用獨立 HTTP channel,無此限制。這是在 C# 中向 AI 暴露方法的環節:
[McpServerToolType] 屬性緊接其後: /// <summary>
/// Provides MCP tools for ...
/// </summary>
[McpServerToolType]
public sealed class FooTools { ... }
[McpServerTool],並務必加上 [Description("...")] 在方法與參數上。
請注意:參數的
Description將直接影響 LLM 決定何時呼叫此工具,描述越精準、型別宣告越清楚越好。
STDIO 模式:
Host.CreateApplicationBuilder(args)
.AddMcpServer().WithStdioServerTransport().WithTools<FooTools>()
...
.RunAsync();
Streamable HTTP 模式(Docker / Container 部署):
string port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls($"http://0.0.0.0:{port}");
builder.Services.AddSingleton<SomeDependency>();
builder.Services
.AddMcpServer(o => o.ServerInfo = new() { Name = "mcp-name", Version = "1.0.0" })
.WithHttpTransport()
.WithTools<FooTools>();
WebApplication app = builder.Build();
app.MapMcp("/mcp"); // 端點固定為 /mcp
await app.RunAsync();
try-catch 防止單次工具呼叫失敗造成整個 MCP server 崩潰。tools
PowerShell 腳本撰寫規範:嚴格模式、錯誤處理、參數宣告、Verb-Noun 命名與 5.1 相容語法邊界。當撰寫或修改 `*.ps1` / `*.psm1` 腳本時自動套用。
tools
產生或補齊 .gitattributes,統一行尾處理、二進位識別與 lock files 標記,保留既有自訂偏好。
development
產生或補齊前端 Lint 設定(Prettier + ESLint Flat Config),統一格式化與程式碼品質規則,保留既有自訂偏好。
testing
依據事實校閱報告修改技術文件:以事實層為不可違反的約束,由改檔者負責表達層的措辭與行文連貫。Use when the user asks to apply fact-check results to a document, or to edit a document based on a previously produced fact-check-report.md.