模型上下文协议配置平台

一个展示如何通过MCP服务器向AI代理暴露配置管理系统的演示环境,包含配置服务器、MCP服务器和演示消费API。

  • by davidknaack
  • C#
  • 本地部署
  • 更新于
在 GitHub 查看 davidknaack/McpDemo

使用说明

MCP演示配置平台

此存储库包含一个演示环境,用于展示模型上下文协议(MCP)服务器如何向AI代理公开配置管理系统。该解决方案分为三个协作应用程序:

  • ConfigServer –ASP。NET核心Web API,它通过API-key身份验证、SQLite持久性、环境/项目范围和详细的更改历史来模拟配置数据库。
  • ConfigMcpServer –包装配置API的TypeScript MCP服务器,将工具和资源公开给支持MCP的IDE或代理。
  • TodoDemoApi –ASP。NET核心Web API,通过自定义配置提供程序使用配置服务。API演示了对字符串、整数、浮点数和分隔列表设置的类型化访问。

配置服务的实时测试 tests/ConfigServer.Tests 并锻炼核心突变和历史流动。


仓库的规划

.
├── README.md
├── McpDemo.sln
├── src
│   ├── ConfigServer          # Configuration service (ASP.NET Core, EF Core, SQLite)
│   ├── ConfigMcpServer       # MCP server (TypeScript + @modelcontextprotocol/sdk)
│   └── TodoDemoApi           # Demo consumer API (ASP.NET Core)
└── tests
    └── ConfigServer.Tests    # xUnit tests for configuration service behaviours

先决条件

  • .NET SDK 9.0或更高版本
  • Node.js 18 LTS(或更新版本)与npm
  • SQLite(与.NET捆绑在一起)
  • 可选:如果您计划将服务容器化,请使用Docker Desktop

在运行ASP之前。NET应用程序,如果您还没有,请信任本地开发证书:

dotnet dev-certs https --trust

建造一切

# restore and build .NET projects
dotnet build

# install MCP server dependencies and compile TypeScript
cd src/ConfigMcpServer
npm install
npm run build

使用以下工具对配置服务运行自动化测试:

dotnet test

运行配置服务器(ConfigServer)

要在启用HTTPS的情况下运行服务器(建议使用以下URL示例):

dotnet run --project src/ConfigServer --launch-profile https

或者仅使用HTTP运行:

dotnet run --project src/ConfigServer --launch-profile http

主要特征:

  • SQLite数据库文件(config.db / config.dev.db)在EF Core迁移的工作目录中创建。
  • 四种环境(开发、QA、MO、生产)和两个项目中多种设置的种子数据。
  • 通过 X-Api-Key 头球默认密钥在中定义 appsettings.json:
    • mcp-server → mcp-demo-key
    • todo-demo-app → todo-demo-key
    • developer → dev-test-key
  • Swagger用户界面可在 /swagger 一旦服务开始运行。
  • 健康调查 /health (无需身份验证)。

示例请求(使用 curl):

# List environments
curl -H "X-Api-Key: dev-test-key" https://localhost:7226/api/environments

# Get settings for Dev/TodoService
curl -H "X-Api-Key: dev-test-key" https://localhost:7226/api/environments/Dev/projects/TodoService/settings

# Apply a batch update
curl -X PUT https://localhost:7226/api/environments/Dev/projects/TodoService/settings \
     -H "Content-Type: application/json" \
     -H "X-Api-Key: dev-test-key" \
     -d '{ "comment": "Demo change", "settings": [ { "name": "Todo:StaleMinutes", "value": "25" } ] }'

运行演示消费者(TodoDemoApi)

演示API使用自定义配置提供程序从服务器引导配置。默认设置已生效 appsettings.json:

"ConfigServer": {
  "BaseAddress": "https://localhost:7226/",
  "Environment": "Dev",
  "Project": "TodoService",
  "ApiKey": "todo-demo-key",
  "AllowUntrustedCertificates": true
}

在启用HTTPS的情况下启动API(需要 ConfigServer 使用https配置文件运行):

dotnet run --project src/TodoDemoApi --launch-profile https

或者仅使用HTTP运行:

dotnet run --project src/TodoDemoApi --launch-profile http

亮点:

  • 自定义配置提供程序在启动和按需刷新时获取设置(POST /api/configuration/refresh).
  • TodoSettingsProvider 将原始字符串转换为类型值(string、int、double、分隔字符串数组)。
  • TodoController 演示了配置感知行为(过时检测、标签默认值、打开项限制)。
  • 根URL处提供Web UI(/)提供了一个基于浏览器的界面来管理待办事项
  • Swagger用户界面可在 /swagger;健康检查 /health.

示例用法:

# Access the web UI in your browser
# HTTPS: https://localhost:7224
# HTTP: http://localhost:5179

# List todos (stale items flagged using config value)
curl -k https://localhost:7224/api/todos

# Inspect current configuration snapshot
curl -k https://localhost:7224/api/configuration

# Force reload from the configuration server
curl -k -X POST https://localhost:7224/api/configuration/refresh

运行MCP服务器(ConfigMcpServer)

MCP服务器使用配置API,并通过stdio传输公开工具/资源。

  1. 安装依赖项并构建(如果尚未完成):

    cd src/ConfigMcpServer
    npm install
    npm run build
    
  2. 导出(或覆盖)环境变量以控制连接:

    export CONFIG_SERVER_BASE_URL=https://localhost:7226/
    export CONFIG_SERVER_API_KEY=mcp-demo-key
    export CONFIG_SERVER_DEFAULT_ENV=Dev
    export CONFIG_SERVER_DEFAULT_PROJECT=TodoService
    export CONFIG_SERVER_API_KEY_HEADER=X-Api-Key
    # When using self-signed certificates (like the dotnet dev cert):
    export NODE_TLS_REJECT_UNAUTHORIZED=0
    # optional: CONFIG_SERVER_TIMEOUT_MS (default 15000)
    
  3. 启动MCP服务器(stdio传输):

    node dist/index.js
    

注册的工具包括:

  • config.listEnvironments –使用只读标志枚举环境。
  • config.setEnvironmentMode –切换只读/读写模式。
  • config.listProjects –列出可用项目。
  • config.getSettings –检索环境/项目的设置。
  • config.updateSettings –批量更新并添加注释要求。
  • config.getHistory –查看更改历史记录(可选范围为某个设置)。

服务器还公开了一个动态资源模板(config://settings/{environment}/{project})该流式传输任何环境/项目对的设置。

您可以通过引用stdio命令和上述环境变量,将此MCP服务器连接到MCP感知工具,如Claude桌面应用程序、Cursor或VS Code MCP支持。


数据库和种子详细信息

  • SQLite文件位于可执行文件旁边(config.db 为了生产, config.dev.db 开发环境)。
  • ConfigurationDbContext 种子环境(开发、QA、MO、生产)、两个项目(TodoService、ReportingService)以及涵盖字符串、整数、双精度和分隔字符串列表的丰富设置。
  • 变更跟踪使用 ChangeSet 加上每个设置 SettingChange API调用程序提供的带有注释和时间戳的条目。
  • 测试覆盖率(ConfigServer.Tests)验证更新流、只读强制和历史检索。

下一步/想法

  • 添加Dockerfiles(或docker compose)来容器化这三个应用程序。
  • 实现API密钥之外的身份验证后台(例如,OAuth客户端凭据)。
  • 使用提示模板或更丰富的资源扩展MCP服务器。
  • 在中引入背景刷新 TodoDemoApi (例如。, IHostedService)或整合 IOptionsMonitor.
  • 构建UI工具,用于手动批准环境之间的配置升级。

故障排除

  • 证书颁发 –设置 AllowUntrustedCertificates 到 true 在本地开发时,或者信任ASP。NET开发人员证书 dotnet dev-certs https --trust.
  • API关键故障 –确保请求提供正确的信息 X-Api-Key 价值。密钥名称可以在配置中调整,但必须在服务和MCP服务器上匹配。
  • SQLite锁定 –在服务之间切换或运行测试时,请停止任何正在运行的API实例以释放文件句柄。
  • MCP连接 –确认MCP服务器共享中配置的相同基本URL和API密钥 ConfigServer.使用 CONFIG_SERVER_TIMEOUT_MS 放松缓慢的连接。

快乐的实验!请随意调整服务或设置以匹配您的演示场景。