MCP服务器集成模板

提供用于将Model Context Protocol (MCP)服务器与.NET Aspire和MCP Inspector集成的模板和可重用组件。

  • by JakeRadMSFT
  • C#
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 JakeRadMSFT/Aspire-MCP-Templates

使用说明

# Aspire MCP模板

此存储库提供模板和可重用组件,用于将模型上下文协议(MCP)服务器与集成。NET Aspire和MCP检查器。

概述

存储库分为两个主要部分:

可重用组件

这些库可以在任何地方使用。NET项目添加MCP和Aspire集成:

  • Mcp.NamedPipeTransport -MCP服务器的命名管道服务器传输实现
  • Mcp.NamedPipeProxy -用于将MCP检查器连接到基于Stdio的MCP服务器的命名管桥
  • Mcp.Aspire.McpInspector -Aspire AppHost扩展用于添加MCP检查器集成
  • Mcp.AspireIntegration -用于透明Aspire支持的MCP服务器扩展

模板

AspireWithMcp/ -与集成的MCP服务器的完整工作示例。NET Aspire:

  • AspireWithMcp。McpServer -支持Aspire Inspector的MCP服务器
  • AspireWithMcp。AppHost -使用MCP服务器和检查器进行Aspire编排
  • AspireWithMcp。服务默认值 -标准Aspire服务默认值

入门指南

先决条件

  • .NET 9.0 SDK
  • MCP检查员 全局安装: npm install -g @modelcontextprotocol/inspector

运行模板

  1. 克隆此存储库:

    git clone https://github.com/JakeRadMSFT/Aspire-MCP-Templates.git
    cd Aspire-MCP-Templates
    
  2. 构建解决方案:

    dotnet build AspireWithMcp/AspireWithMcp.sln
    
  3. 运行Aspire AppHost:

    dotnet run --project AspireWithMcp/AspireWithMcp.AppHost
    
  4. 打开Aspire仪表板(控制台输出中显示的URL)

  5. MCP检查器将自动启动并连接到MCP服务器

运作原理

MCP服务器集成

MCP服务器使用一个简单的扩展来启用Aspire集成:

builder.Services
    .AddMcpServer()
    .WithStdioServerTransport()
    .WithTools<RandomNumberTools>()
    .WithAspireMcpInspectorSupport();  // Enables Aspire integration

这 .WithAspireMcpInspectorSupport() 扩展名:

  • 在Aspire中运行时检测(通过 --pipe-name 论点)
  • 透明地将stdio传输替换为命名管道传输
  • 允许MCP服务器代码保持不变

AppHost配置

Aspire AppHost连接MCP服务器和检查器:

// Add MCP server with named pipe argument
var mcpServer = builder.AddProject<Projects.AspireWithMcp_McpServer>("mcp-server")
    .WithArgs(["--pipe-name=aspire-mcp-server"]);

// Add MCP Inspector that connects via proxy
var proxyPath = Path.GetFullPath("../../Mcp.NamedPipeProxy/bin/Debug/net9.0/Mcp.NamedPipeProxy.exe");
builder.AddMcpInspector("mcp-inspector", mcpServer, proxyPath, "aspire-mcp-server");

建筑


   MCP Inspector     
   (Web UI)          

            stdio

  NamedPipeProxy     
  (stdio  pipes)    

            named pipes

   MCP Server        
  (NamedPipe         
   Transport)        

  1. MCP服务器 由Aspire启动时,使用命名管道传输运行
  2. NamedPipeProxy 将stdio(由检查员使用)连接到指定的管道
  3. MCP检查员 通过stdio连接到代理并提供web UI

使用可重用组件

在您的MCP服务器中

  1. 添加包引用:

    dotnet add package ModelContextProtocol
    dotnet add reference path/to/Mcp.AspireIntegration/Mcp.AspireIntegration.csproj
    
  2. 将扩展添加到MCP服务器:

    builder.Services
        .AddMcpServer()
        .WithStdioServerTransport()
        .WithTools<YourTools>()
        .WithAspireMcpInspectorSupport();
    

在您的Aspire AppHost中

  1. 添加项目引用:

    dotnet add reference path/to/Mcp.Aspire.McpInspector/Mcp.Aspire.McpInspector.csproj
    
  2. 配置AppHost:

    using Mcp.Aspire.McpInspector;
    
    var mcpServer = builder.AddProject<Projects.YourMcpServer>("mcp-server")
        .WithArgs(["--pipe-name=your-pipe-name"]);
    
    var proxyPath = "path/to/Mcp.NamedPipeProxy.exe";
    builder.AddMcpInspector("mcp-inspector", mcpServer, proxyPath, "your-pipe-name");
    

组件详细信息

麦克。名称管道运输

提供 WithNamedPipeServerTransport() MCP服务器使用命名管道而不是stdio的扩展。使用双管架构(单独的输入/输出管道)进行双向通信。

麦克。NamedPipeProxy

将MCP服务器的stdio连接到命名管道的控制台应用程序。接受 --pipe-name=<name> argument并创建两个管道: <name> (输入)和 <name>-out (输出)。

麦克。向往。McpInspector

Aspire扩展,将MCP Inspector添加到您的分布式应用程序中。这 AddMcpInspector() 方法:

  • 启动MCP检查器(需要全局npm安装)
  • 通过NamedPipeProxy将其连接到MCP服务器
  • 管理依赖关系以确保正确的启动顺序

麦克。Aspire集成

提供 WithAspireMcpInspectorSupport() MCP服务器的扩展。当 --pipe-name 检测到:

  • 从DI中删除stdio传输
  • 改为添加命名管道传输
  • 记录传输覆盖

许可证

MIT许可证-有关详细信息,请参阅许可证文件

贡献

欢迎投稿!请随时提交问题和拉取请求。