Building MCP Servers with C#: Connect AI Agents to Your .Net Applications with the Model Context Protocol
暫譯: 使用 C# 建立 MCP 伺服器:透過模型上下文協議將 AI 代理連接到您的 .Net 應用程式

Versluis, Gerald

  • 出版商: Apress
  • 出版日期: 2026-09-21
  • 售價: $1,990
  • 貴賓價: 9.5$1,890
  • 語言: 英文
  • 頁數: 307
  • 裝訂: Quality Paper - also called trade paper
  • ISBN: 9798868830099
  • ISBN-13: 9798868830099
  • 相關分類: AI Coding
  • 海外代購書籍(需單獨結帳)

商品描述

The Model Context Protocol (MCP) is quickly becoming the standard way for AI models to talk to external tools, data, and services. Anthropic announced it late 2024, and Microsoft, OpenAI, Google, and others all jumped on board pretty quickly. It gives a universal protocol so AI agents can call into code: reading databases, calling APIs, accessing files, whatever is needed. People like to call it "USB-C for AI."

This book teaches .NET developers how to build MCP servers using the official C# SDK (ModelContextProtocol on NuGet). Start with the basics, learn how the protocol works, prepare the dev environment, then build increasingly complex servers. The book walks through the three MCP primitives: tools (functions the AI can call), resources (data the AI can read), and prompts (reusable templates), with examples you can actually run as you go.

GitHub Copilot, Claude, ChatGPT, and other AI assistants are adding MCP support right now. If you can build MCP servers, your code and APIs become accessible to every major AI platform at once. Readers build a complete capstone project that ties together tools, resources, prompts, DI, database integration, and deployment. The last chapters cover enterprise stuff like auth, resilience, and observability, plus getting it running with Docker, Azure, and CI/CD. So you can actually ship what you build.

What You Will Learn

  • Understand how MCP works architecturally. Learn how hosts, clients, and servers communicate with JSON-RPC 2.0, how capability negotiation works, and what the transport layers do.
  • Build MCP tools with the C# SDK. Create tools with typed parameters, async execution, progress reporting, and proper error handling using the official ModelContextProtocol NuGet package.
  • Expose data through MCP resources. Serve static and templated resources with proper URI schemes and MIME types so AI models can read your application data.
  • Create reusable prompt templates. Build parameterized prompts with multiple messages that guide AI behavior and can be shared across teams.
  • Pick the right transport layer. Understand when to use stdio vs Streamable HTTP and implement both with ASP.NET Core.
  • Use proper .NET patterns in MCP servers. Use dependency injection, IOptions configuration, structured logging, HttpClientFactory, the same patterns you'd use in any production ASP.NET Core app.
  • Deploy MCP servers for real. Containerize with Docker, publish as .NET tools on NuGet, deploy to Azure, and set up CI/CD with GitHub Actions.

Who This Book is For

This book is for any .NET developers seeking to use Model Context Protocol to develop intelligent applications.

商品描述(中文翻譯)

模型上下文協議(Model Context Protocol, MCP)正迅速成為人工智慧模型與外部工具、數據和服務進行交互的標準方式。Anthropic 在 2024 年底宣布了這一協議,微軟、OpenAI、谷歌等公司也迅速加入了這一行列。它提供了一個通用協議,使得 AI 代理可以調用代碼:讀取數據庫、調用 API、訪問文件,滿足各種需求。人們喜歡稱之為「AI 的 USB-C」。

本書教導 .NET 開發者如何使用官方的 C# SDK(ModelContextProtocol 在 NuGet 上)來構建 MCP 伺服器。從基礎開始,了解協議的運作方式,準備開發環境,然後構建越來越複雜的伺服器。本書將介紹三個 MCP 原語:工具(AI 可以調用的函數)、資源(AI 可以讀取的數據)和提示(可重用的模板),並提供可以實際運行的示例。

GitHub Copilot、Claude、ChatGPT 和其他 AI 助手目前正在添加 MCP 支持。如果您能構建 MCP 伺服器,您的代碼和 API 將同時對每個主要的 AI 平台可用。讀者將構建一個完整的畢業專案,將工具、資源、提示、依賴注入、數據庫集成和部署結合在一起。最後幾章涵蓋企業級的內容,如身份驗證、韌性和可觀察性,以及如何使用 Docker、Azure 和 CI/CD 使其運行。因此,您可以實際交付您所構建的內容。

您將學到什麼


  • 理解 MCP 的架構運作。學習主機、客戶端和伺服器如何通過 JSON-RPC 2.0 進行通信,能力協商如何運作,以及傳輸層的功能。

  • 使用 C# SDK 構建 MCP 工具。創建具有類型參數、異步執行、進度報告和適當錯誤處理的工具,使用官方的 ModelContextProtocol NuGet 套件。

  • 通過 MCP 資源暴露數據。提供靜態和模板資源,使用適當的 URI 協議和 MIME 類型,以便 AI 模型可以讀取您的應用數據。

  • 創建可重用的提示模板。構建帶有多條消息的參數化提示,指導 AI 行為,並可以在團隊之間共享。

  • 選擇正確的傳輸層。理解何時使用 stdio 與 Streamable HTTP,並使用 ASP.NET Core 實現兩者。

  • 在 MCP 伺服器中使用適當的 .NET 模式。使用依賴注入、IOptions 配置、結構化日誌記錄、HttpClientFactory,這些都是您在任何生產環境的 ASP.NET Core 應用中會使用的模式。

  • 實際部署 MCP 伺服器。使用 Docker 容器化,作為 .NET 工具發佈到 NuGet,部署到 Azure,並使用 GitHub Actions 設置 CI/CD。

本書適合誰

本書適合任何希望使用模型上下文協議開發智能應用的 .NET 開發者。

作者簡介

Gerald Versluis is a software developer from the Netherlands. He works at Microsoft on the .NET Maui team, helping to build amazing cross-platform frameworks for developing mobile and desktop apps. He is the host of a hugely successful YouTube channel with over 100k subscribers and often speaks about app development at conferences like Microsoft Build, NET Conf, and more.

作者簡介(中文翻譯)

Gerald Versluis 是來自荷蘭的軟體開發人員。他在微軟的 .NET Maui 團隊工作,幫助建立出色的跨平台框架,以開發行動和桌面應用程式。他擁有一個非常成功的 YouTube 頻道,訂閱人數超過 10 萬,並且經常在 Microsoft Build、.NET Conf 等會議上講述應用程式開發的相關內容。