使用说明
MCP服务器和客户端开发。网
一个全面的学习项目,展示 模型上下文协议(MCP) 实施与。网10。此解决方案包括一个带有实际工具的MCP服务器、一个MCP客户端库和一个用于测试的主机应用程序。
📋 目录
🎯 概述
这 模型上下文协议(MCP) 是一个开放协议,规范了人工智能应用程序和工具的通信方式。该项目展示了:
- 构建一个向AI助手公开工具的MCP服务器
- 创建MCP客户端以连接到服务器
- 真实世界的用例,包括天气、计算器、待办事项管理和文本实用程序
🏗️ 建筑
┌─────────────────┐ stdio ┌─────────────────┐
│ MCP Client │◄────────────►│ MCP Server │
│ (Host) │ transport │ (Tools) │
└─────────────────┘ └─────────────────┘
│ │
│ ▼
│ ┌───────────────────┐
│ │ Weather Tool │
│ │ Calculator Tool │
│ │ Todo Tool │
│ │ Text Utility │
│ └───────────────────┘
▼
┌─────────────────┐
│ AI Assistant │
│ (Claude, etc) │
└─────────────────┘
📁 项目结构
MyFirstMCPDemo/
├── src/
│ ├── MCP.Shared/ # Common types and models
│ │ └── Models/
│ │ ├── WeatherInfo.cs
│ │ ├── TodoItem.cs
│ │ └── CalculationResult.cs
│ │
│ ├── MCP.Server/ # MCP Server with tools
│ │ ├── Tools/
│ │ │ ├── WeatherTool.cs
│ │ │ ├── CalculatorTool.cs
│ │ │ ├── TodoTool.cs
│ │ │ └── TextUtilityTool.cs
│ │ └── Program.cs
│ │
│ ├── MCP.Client/ # MCP Client library
│ │ └── Program.cs
│ │
│ └── MCP.Host/ # Demo host application
│ └── Program.cs
│
├── tests/
│ └── MCP.Tests/ # Unit tests
│ └── UnitTest1.cs
│
├── MyFirstMCPDemo.sln
└── README.md
✅ 先决条件
- .NET 10 SDK 或以后
- 代码编辑器(VS code、Visual Studio、Rider等)
🚀 入门指南
1.克隆存储库
git clone https://github.com/rijwanansari/MyFirstMCPDemo.git
cd MyFirstMCPDemo
2.构建解决方案
dotnet build
3.运行测试
dotnet test
🎮 运行演示
运行主机应用程序
主机应用程序演示如何连接到MCP服务器并调用工具:
dotnet run --project src/MCP.Host
这将:
- 启动MCP服务器
- 通过stdio传输连接到它
- 列出所有可用工具
- 为每个工具运行交互式演示
独立运行服务器
要在独立模式下运行MCP服务器(用于与AI助手集成):
dotnet run --project src/MCP.Server
🛠️ 可用工具
🌤️ 天气工具
获取任何位置的模拟天气信息。
| 工具 | 说明 | 参数 |
|---|---|---|
GetWeather |
获取当前天气 | location (字符串), unit (摄氏/华氏) |
GetForecast |
获取多日预报 | location (字符串), days (1-7) |
🔢 计算器工具
进行数学计算。
| 工具 | 说明 | 参数 |
|---|---|---|
Add |
加两个数字 | a, b |
Subtract |
减去数字 | a, b |
Multiply |
将数字相乘 | a, b |
Divide |
分割数字 | a, b |
Power |
提升到权力 | baseNumber, exponent |
SquareRoot |
计算平方根 | number |
Percentage |
计算百分比 | part, whole |
Modulo |
计算余数 | a, b |
📝 Todo工具
管理任务列表。
| 工具 | 说明 | 参数 |
|---|---|---|
CreateTodo |
创建新任务 | title, description?, priority? |
GetTodos |
列出任务 | filter?, priority? |
CompleteTodo |
标记任务已完成 | id |
UpdateTodo |
修改任务 | id, title?, description?, priority? |
DeleteTodo |
删除任务 | id |
GetTodoStats |
获取统计数据 | - |
ClearCompleted |
删除已完成的任务 | - |
📄 文本实用工具
文本操作和分析。
| 工具 | 说明 | 参数 |
|---|---|---|
AnalyzeText |
统计单词、字符、行数 | text |
ConvertCase |
更改文本大小写 | text, caseType |
ReverseText |
反转字符 | text |
RemoveDuplicateLines |
消除重复行 | text, caseSensitive? |
ExtractEmails |
查找电子邮件地址 | text |
ExtractUrls |
查找URL | text |
Slugify |
创建URL友好字符串 | text |
Truncate |
缩短文本 | text, maxLength, addEllipsis? |
🧪 测试
运行测试套件:
# Run all tests
dotnet test
# Run tests with detailed output
dotnet test --logger "console;verbosity=detailed"
# Run specific test class
dotnet test --filter "FullyQualifiedName~CalculatorToolTests"
🤖 与AI助手集成
Claude桌面配置
将此添加到您的Claude Desktop配置文件中(claude_desktop_config.json):
{
"mcpServers": {
"mcp-demo": {
"command": "dotnet",
"args": ["run", "--project", "/path/to/MyFirstMCPDemo/src/MCP.Server"]
}
}
}
VS代码扩展配置
对于带有副副本的VS代码:
{
"github.copilot.chat.mcpServers": {
"mcp-demo": {
"command": "dotnet",
"args": ["run", "--project", "/path/to/MyFirstMCPDemo/src/MCP.Server"]
}
}
}
📚 学习资源
🤝 贡献
欢迎投稿!请随时提交拉取请求。
- 分叉存储库
- 创建功能分支(
git checkout -b feature/AmazingFeature) - 提交您的更改(
git commit -m 'Add some AmazingFeature') - 推到分支(
git push origin feature/AmazingFeature) - 打开拉取请求
📄 许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
👤 作者
Rijwan Ansari
⭐ 如果你觉得这个项目有帮助,请给它一颗星!
