🚀 .NET MCP与MCPSharp
一个完整的。NET解决方案演示 模型上下文协议(MCP) 使用MCPSharp库实现。本项目展示了如何在中创建MCP服务器和客户端。NET,实现AI模型和外部工具之间的无缝通信。
📋 目录
🎯 概述
此解决方案演示了完整的MCP实现,包括:
- 🔧 MCP服务器:将数学运算作为MCP工具公开的计算器服务
- 📱 MCP客户端:发现并使用MCP工具的客户端应用程序
- 🔄 沟通:客户端和服务器之间基于JSON-RPC的通信
- ⚡ 实时:具有适当错误处理的异步操作
主要特点
- ✅ 工具发现:自动发现可用的MCP工具
- ✅ 类型安全:强类型参数和返回值
- ✅ 异步操作:完全异步/等待支持
- ✅ 错误处理:全面的错误处理和验证
- ✅ 可扩展:易于添加新工具和功能
🏗️ 建筑
graph TB
A[MCP Client] -->|JSON-RPC| B[MCP Server]
B --> C[Calculator Tool]
C --> D[Addition Function]
subgraph "Client Side"
A1[Tool Discovery]
A2[Tool Invocation]
A3[Result Processing]
end
subgraph "Server Side"
B1[Tool Registration]
B2[Request Handling]
B3[Response Generation]
end
A --> A1
A --> A2
A --> A3
B --> B1
B --> B2
B --> B3📁 项目结构
📦 Dotnet MCP with McpSharp/
├── 📁 McpClient/ # MCP Client Application
│ ├── 📄 Program.cs # Client entry point
│ ├── 📄 McpClient.csproj # Client project file
│ └── 📁 bin/ # Build output
├── 📁 McpServer/ # MCP Server Application
│ ├── 📄 Program.cs # Server entry point with tool registration
│ ├── 📄 McpServer.csproj # Server project file
│ └── 📁 bin/ # Build output
└── 📄 Dotnet_MCP_with_McpSharp.sln # Solution file🛠️ 先决条件
在运行此解决方案之前,请确保您已经:
- .NET 9.0 SDK 或以后
- Visual Studio 2022 或 Visual Studio Code C#扩展
- Windows 10/11 (在Windows 10.0.22631上测试)
安装
- 安装。净9.0 SDK:
# Download from: https://dotnet.microsoft.com/download/dotnet/9.0- 验证安装:
dotnet --version
# Should output: 9.0.x or later⚡ 快速开始
1.克隆和构建
# Clone the repository
git clone
cd "Dotnet MCP with McpSharp"
# Restore packages
dotnet restore
# Build the solution
dotnet build2.运行MCP服务器
# Navigate to server directory
cd McpServer
# Run the server
dotnet run服务器将启动并注册计算器工具。您应该看到指示服务器正在运行的输出。
3.运行MCP客户端
# In a new terminal, navigate to client directory
cd McpClient
# Run the client
dotnet run客户将:
- 🔍 从服务器中发现可用工具
- 📊 显示工具信息
- 🧮 使用示例参数执行添加工具
- 📋 显示结果
🔧 配置
服务器配置
MCP服务器配置在 McpServer/Program.cs:
MCPServer.Register();
await MCPServer.StartAsync(
serverName: "SimpleMcp.Server",
version: "v1.0.0"
);客户端配置
MCP客户端已在中配置 McpClient/Program.cs:
var client = new MCPClient(
name: "McpClient",
version: "v1.0.0",
server: "path/to/McpServer.exe"
);⚠️ 重要:更新客户端配置中的服务器路径以匹配您的系统。
📚 使用示例
创建新的MCP工具
要向服务器添加新工具,请使用以下命令创建一个新类 McpTool 属性:
public class MathTool
{
[McpTool(name: "multiply", Description = "Multiply two numbers")]
public static int Multiply(
[McpParameter(required: true, Description = "First number")] int a,
[McpParameter(required: true, Description = "Second number")] int b)
{
return a * b;
}
}然后在中注册 Program.cs:
MCPServer.Register();从客户端调用工具
// Discover available tools
List tools = await client.GetToolsAsync();
// Call a specific tool
var result = await client.CallToolAsync(
name: "addition",
parameters: new Dictionary
{
{ "firstNumber", 15 },
{ "secondNumber", 25 }
}
);
Console.WriteLine($"Result: {result.Content[0].Text}");🔍 api参考
MCP客户端类
| 方法 | 说明 | 参数 |
|---|---|---|
GetToolsAsync() | 检索所有可用工具 | 无 |
CallToolAsync() | 执行特定工具 | name, parameters |
MCP服务器类
| 方法 | 说明 | 参数 |
|---|---|---|
Register() | 注册工具类 | 工具类类型 |
StartAsync() | 启动MCP服务器 | serverName, version |
属性
| 属性 | 描述 | 属性 |
|---|---|---|
McpTool | 将方法标记为MCP工具 | name, Description |
McpParameter | 定义工具参数 | required, Description |
🧪 测试
手动测试
- 启动服务器 并验证它是否正在运行
- 运行客户端 并检查输出
- 验证工具发现 显示正确的工具数量
- 测试工具执行 具有不同的参数
预期输出
服务器输出:
MCP Server started successfully
Tool registered: addition客户端输出:
Available Tools: 1
Name: addition
Description: This tool will add two numbers.
Result: 15📦 依赖项
核心依赖关系
| 包 | 版本 | 描述 |
|---|---|---|
MCPSharp | 1.0.11 | MCP实现。NET |
System.Formats.Asn1 | 9.0.9 | ASN.1格式支持 |
目标框架
- .NET 9.0 -具有现代C#功能的最新LTS版本
🚀 高级用法
自定义工具实现
public class WeatherTool
{
[McpTool(name: "get_weather", Description = "Get current weather for a city")]
public static async Task GetWeather(
[McpParameter(required: true, Description = "City name")] string city)
{
// Implementation here
return $"Weather in {city}: Sunny, 25°C";
}
}错误处理
try
{
var result = await client.CallToolAsync("addition", parameters);
Console.WriteLine($"Success: {result.Content[0].Text}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}🤝 贡献
我们欢迎捐款!请按照以下步骤操作:
- 分叉 存储库
- 创建 特征分支(
git checkout -b feature/amazing-feature) - 提交 您的更改(
git commit -m 'Add amazing feature') - 推 到分行(
git push origin feature/amazing-feature) - 打开 拉取请求
开发指南
- ✅ 遵循C#编码规范
- ✅ 为公共API添加XML文档
- ✅ 包括新功能的单元测试
- ✅ 更新README.md以获取重大更改
📄 许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
🆘 支持
如果您遇到任何问题或有疑问:
- 检查 这 问题 页
- 创建 一期有详细信息的新杂志
- 包含 系统信息和错误消息
______________________________________________________________________
由...制作❤️ 使用。NET和MCPSharp
  
