MCP架构-代理人工智能服务
一个干净的、固态的、面向对象的架构,用于实现模型上下文协议(MCP),并在此基础上构建代理AI服务。网8.0。
架构概述
该解决方案遵循清洁架构原则,明确分离关注点:
- 域层:核心模型、接口和业务逻辑
- 应用层:用例和应用程序服务
- 基础设施层:外部服务实现(HTTP客户端、数据访问)
- API层:ASP。NET核心Web API与REST端点
项目结构
McpArchitecture/
├── McpArchitecture.Domain/ # Core domain models and interfaces
├── McpArchitecture.Application/ # Application services and use cases
├── McpArchitecture.Infrastructure/ # Infrastructure implementations
├── McpArchitecture.Api/ # ASP.NET Core Web API
├── scripts/ # PowerShell setup scripts
└── README.md先决条件
- .NET 8.0 SDK或更高版本
- Visual Studio 2022(或更高版本)
- PowerShell 5.1或更高版本
安装说明
选项1:使用PowerShell脚本(推荐)
- 在项目根目录中打开PowerShell
- 运行安装脚本:
.\scripts\Setup-Project.ps1- 添加NuGet包:
.\scripts\Add-Packages.ps1选项2:手动设置
- 创建解决方案:
dotnet new sln -n McpArchitecture- 创建项目:
dotnet new classlib -n McpArchitecture.Domain
dotnet new classlib -n McpArchitecture.Application
dotnet new classlib -n McpArchitecture.Infrastructure
dotnet new webapi -n McpArchitecture.Api- 将项目添加到解决方案:
dotnet sln add McpArchitecture.Domain/McpArchitecture.Domain.csproj
dotnet sln add McpArchitecture.Application/McpArchitecture.Application.csproj
dotnet sln add McpArchitecture.Infrastructure/McpArchitecture.Infrastructure.csproj
dotnet sln add McpArchitecture.Api/McpArchitecture.Api.csproj- 添加项目引用(引用结构请参见Setup-project.ps1)
运行应用程序
选项1:使用启动脚本(推荐)
Windows(PowerShell):
.\start.ps1Linux/Mac(Bash):
chmod +x start.sh
./start.sh使用自定义选项:
# Run in Production environment
.\start.ps1 -Environment Production
# Run on custom port
.\start.ps1 -Port 5002
# Show help
.\start.ps1 -Help选项2:使用Visual Studio 2022
- 在Visual Studio 2022中打开解决方案
- 集
McpArchitecture.Api作为启动项目 - 按F5运行
选项3:使用。NET-CLI
cd McpArchitecture.Api
dotnet run访问API
启动后,导航到:
- Swagger用户界面:
https://localhost:5001/swagger - API基地:
https://localhost:5001
API终点
标准MCP端点
POST /api/mcp/request-处理MCP请求POST /api/mcp/message-处理MCP消息GET /api/mcp/resources-获取所有可用资源GET /api/mcp/resources/{id}-获取特定资源POST /api/mcp/resources-注册新资源
文件服务器MCP(JSON-RPC 2.0)
POST /api/mcp/json-处理JSON-RPC 2.0 MCP请求
- filesystem/list_files -列出文件和目录 - filesystem/read_file -读取文件内容 - filesystem/write_file -写入文件内容 - filesystem/get_file_info -获取文件/目录信息 - filesystem/create_directory -创建目录 - filesystem/delete_file -删除文件或目录
MCP检验员测试
使用MCP检查器以交互方式测试API:
.\test-mcp\start-inspector.ps1这将启动一个基于网络的检查器工具。看 test-mcp/README.md 了解详情。
设计原则
本项目遵循SOLID原则:
- 单一责任:每个班都有一个改变的原因
- 打开/关闭:打开以进行扩展,关闭以进行修改
- 利斯科夫替补:可以在不破坏功能的情况下交换接口
- 接口隔离:小而专注的界面
- 依赖倒置:取决于抽象,而不是具体
特性
- ✅ 分层分离的干净建筑
- ✅ 全程依赖注入
- ✅ 使用Serilog进行结构化日志记录
- ✅ Swagger/OpenAPI文档
- ✅ RESTful API设计
- ✅ 资源管理系统
- ✅ MCP协议实现
- ✅ 与Visual Studio 2022兼容
配置
编辑 appsettings.json 配置:
- MCP基本URL
- 超时设置
- 日志记录级别
测试
要添加测试,请创建测试项目:
dotnet new xunit -n McpArchitecture.Tests
dotnet add reference ../McpArchitecture.Domain/McpArchitecture.Domain.csproj贡献
这是一个干净的架构模板。根据您的特定要求扩展它,同时保持SOLID原则和干净的架构边界。
许可证
本项目按原样提供,用于教育和商业用途。
