Rust Song API与MCP集成
该工作区实现了一个带有模型上下文协议(MCP)集成的RESTful Song API服务。该项目演示了如何构建和部署一个web服务,该服务提供随机歌曲信息,同时通过MCP支持AI工具集成。
开发容器包括所有必需的工具:Azure CLI、Azure Developer CLI(azd)、Rust工具链和Docker。
在浏览器中打开
](https://codespaces.new/dfberry/azure-rust-mcp-server-apim)
在本地开发容器中打开
- 当地发展先决条件:
- 克隆并在开发容器中打开:
git clone https://github.com/yourusername/rust-mcp.git
cd rust-mcp
code .出现提示时,单击“在容器中重新打开”
使用Azure Developer CLI进行部署
- 部署到Azure:
azd auth login
azd init
azd up- 测试部署:
curl https:///song您的自定义API现在已部署到Azure容器应用程序。
创建Azure API管理资源
- 使用 Azure门户 以创建Azure API管理资源。
- 使用 openapi.json.
- 为MCP服务器配置资源。Azure为API管理资源设置MCP连接的估计时间最长为2.5小时。
配置Visual Studio代码以使用MCP服务器
- 在Azure门户中,获取APIM终结点和订阅密钥。
- 在Visual Studio代码中,使用命令托盘添加具有远程URI的MCP服务器。
- 编辑MCP服务器以添加所需的标头
Ocp-Apim-Subscription-Key:
{
"files.autoSave": "afterDelay",
"rust-analyzer.initializeStopped": true,
"@azure.argTenant": "",
"mcp": {
"servers": {
"my-mcp-server-": {
"url": "/mcp/sse",
"headers": {
"Ocp-Apim-Subscription-Key": ""
}
}
}
}
}- 使用聊天功能询问歌曲:
What is a good hello song.
项目结构
rust-mcp/
├── server/ # Main API server implementation
│ ├── src/ # Server source code
│ ├── openapi.json # API specification
│ └── Cargo.toml # Server dependencies
├── song-lib/ # Song generation library
│ ├── src/ # Library source code
│ └── Cargo.toml # Library dependencies
├── infra/ # Infrastructure as Code
│ ├── main.bicep # Azure deployment template
│ └── main.bicepparam # Azure deployment parameters
├── http/ # API test files
│ ├── song.http # REST API tests
│ └── song-mcp.http # MCP integration tests
└── Dockerfile # Container definition特性
- 用于随机歌曲生成的RESTful API端点
- 模块化架构,配备独立的歌曲库
- Azure容器应用部署支持
- AI工具的模型上下文协议(MCP)集成
- OpenAPI规范
- Docker容器化
- 使用二头肌的基础设施即代码
入门指南
手动设置先决条件
- Rust工具链(通过安装 安宁)
- Docker(用于容器构建)
- Azure CLI(用于部署)
- Azure开发者命令行界面(azd)
本地开发(无开发容器)
- 构建工作空间:
cargo build --workspace- 运行测试:
cargo test --workspace- 启动服务器:
cd server
cargo run- 请尝试API:
curl http://localhost:3000/songDocker构建
在本地构建并运行容器:
docker build -t song-api .
docker run -p 3000:3000 song-apiAzure部署
使用提供的二头肌模板部署到Azure容器应用程序:
cd infra
az deployment sub create --location eastus2 --template-file main.bicep --parameters main.bicepparam组件
歌曲库(song-lib)
一个Rust库机箱,为随机歌曲生成提供核心功能。看 歌曲库/README.md 了解详情。
API服务器(server)
通过REST API和MCP公开歌曲生成功能的主要web服务。看 服务器/README.md API文档。
基础设施(infra)
包含用于将服务部署到具有适当配置和缩放规则的Azure容器应用程序的二头肌模板。
API文档
API是使用OpenAPI 3.0规范编写的。您可以在中找到完整的API文档 server/openapi.json.
模型上下文协议集成
服务器实现了用于AI集成的MCP工具:
get_random_song-返回一首随机歌曲,包含标题、艺术家和歌词- 支持REST和MCP接口
贡献
- 分叉存储库
- 创建功能分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add some amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
