Back to Frameworks & SDKs
C# MCP SDK logo
framework345

C# MCP SDK

這是由微軟協作維護的 Model Context Protocol (MCP) 官方 C# SDK。它讓 .NET 開發者能輕鬆建構 MCP 伺服器與客戶端,將工具、資源和提示詞公開給像 Claude、Cursor 和 Gemini 這類的 AI 代理程式。

概述

C# MCP SDK(儲存庫:modelcontextprotocol/csharp-sdk)是 Model Context Protocol 的官方 .NET SDK,由與 Microsoft 協作開發。它讓 .NET 開發者能夠使用慣用的 C# API 來實作 MCP 伺服器與客戶端,輕鬆地將 AI 代理程式連接至 .NET 應用程式、企業資料來源、內部 API 以及自訂工作流程。

該 SDK 已於 2026 年 3 月釋出穩定版 v1.0,完整支援 2025-11 -25 MCP 規格,在官方 MCP 生態系中屬於第一級(Tier 1),是 C#/.NET 團隊參與日益擴大的 MCP 領域的推薦方式。

主要功能

  • 完整 MCP 規格支援:實作最新的 2025-11-25 通訊協定,包含工具(Tools)、資源(Resources)、提示(Prompts)、引導(Elicitation)、取樣(Sampling)及實驗性的任務(Tasks)。
  • 三個 NuGet 套件
    • ModelContextProtocol.Core — 客戶端與低階伺服器所需的最小依賴項。
    • ModelContextProtocol — 適用於多數使用場景的完整功能套件。
    • ModelContextProtocol.AspNetCore — 與 ASP.NET Core 無縫整合,用於 HTTP/SSE 傳輸。
  • 輕鬆定義工具:使用屬性裝飾方法或流暢式 API,將 C# 函式公開為 MCP 工具,並自動產生 JSON Schema。
  • 內建授權機制:原生支援 OAuth 2.1、OpenID Connect 探索、PKCE 以及漸進式範圍同意(incremental scope consent)。
  • 多種傳輸方式:stdio、HTTP、SSE 以及可串流的 HTTP(Streamable HTTP)。
  • Microsoft.Extensions.AI 整合:與 Semantic Kernel 及其他 .NET AI 函式庫順暢互通。
  • 生產環境就緒:可觀測性、日誌記錄、相依性注入、HttpContext 存取,以及 AOT 相容選項。
  • 完善的說明文件:完整的 API 文件位於 csharp.sdk.modelcontextprotocol.io。

運作方式

建置 MCP 伺服器(最簡範例)

using ModelContextProtocol;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddMcpServer()
    .WithHttpTransport()
    .WithTools<MyCustomTools>();

var app = builder.Build();
app.MapMcp();
app.Run();

簡單定義工具:

public class MyCustomTools
{
    [McpTool]
    public string GetWeather(string city) 
        => $"Sunny in {city}";
}

AI 代理程式(Claude Desktop、Cursor 等)便能自動探索並呼叫這些工具。

使用場景

  • 企業資料整合:安全地將內部資料庫、CRM 系統或商業 API 公開給 AI 代理程式。
  • .NET 工具供代理程式使用:建置 MCP 伺服器,讓 AI 能夠控制 dotnet CLI、Roslyn 分析器、NuGet 套件或 Azure 資源。
  • 自訂 AI 工作流程:將傳統 .NET 服務連接到現代化的代理程式應用。
  • 混合式 AI 應用:結合 MCP 與 Semantic Kernel 或 Microsoft.Extensions.AI,以提供豐富的代理程式體驗。
  • 內部開發者工具:建立組織專用的工具,用於程式碼生成、部署或監控。

快速開始

  1. 安裝主套件:

    dotnet add package ModelContextProtocol
    
  2. 依照官方快速入門指南操作,位於 GitHub 儲存庫SDK 文件

  3. microsoft/mcp-dotnet-samples 中探索豐富的範例。

此 SDK 適用於主控台應用程式、ASP.NET Core、Azure Functions 等。完整支援伺服器與用戶端實作。

優勢

C# MCP SDK 將 Model Context Protocol 的強大功能帶入 .NET 生態系,具備熟悉的模式、強型別、相依性注入及企業級安全性。它降低了 .NET 團隊構建生產級 MCP 伺服器的門檻,同時保持與更廣泛的 MCP 生態系(如 Claude、Cursor、Gemini CLI 等)的完全相容性。

作為由 Microsoft 維護的第一級官方 SDK,它能及時獲得與 MCP 規格演進同步的更新,並廣泛用於內部工具和公共 AI 整合。

官方連結

  • GitHub:https://github.com/modelcontextprotocol/csharp-sdk
  • 文件:https://csharp.sdk.modelcontextprotocol.io/
  • .NET 部落格公告:https://devblogs.microsoft.com/dotnet/release-v10-of-the-official-mcp-csharp-sdk/

Tags

mcpcsharpdotnet框架sdkai代理微軟模型上下文協議工具

Related Entries

Keep exploring similar tools and resources in this category.

Browse Frameworks & SDKs
C# MCP SDK logo

C# MCP SDK

Frameworks & SDKs

The official C# SDK for the Model Context Protocol (MCP), maintained in collaboration with Microsoft. It enables .NET developers to easily build MCP servers and clients, exposing tools, resources, and prompts to AI agents like Claude, Cursor, and Gemini.

OpenViking logo

OpenViking

Frameworks & SDKs

OpenViking 是一個為 AI 智能體打造的開源上下文資料庫,採用檔案系統範式(viking:// URI)來組織記憶、資源和技能,實現階層式上下文傳遞、分層載入與自我進化的智能體。

Cloudflare Workers MCP logo

Cloudflare Workers MCP

Frameworks & SDKs

Build and deploy custom remote MCP servers on Cloudflare Workers to expose tools, APIs, and Cloudflare services to AI agents like Claude and Cursor via standardized protocol.

OpenViking logo

OpenViking

Frameworks & SDKs

OpenViking is an open-source context database for AI agents that organizes memory, resources, and skills using a filesystem paradigm (viking:// URIs), enabling hierarchical context delivery, layered loading, and self-evolving agents.

FastMCP logo

FastMCP

Frameworks & SDKs

The fast, Pythonic framework for building MCP (Model Context Protocol) servers and clients. Turn any Python function into production-ready LLM tools with zero boilerplate.

Hindsight logo

Hindsight

Frameworks & SDKs

Hindsight 是一個開源的智能體記憶系統,能讓 AI 智能體隨著時間真正學習,透過保留事實、採用混合策略進行回憶,以及反思以形成心智模型。