歌曲API服务

一个集成了模型上下文协议(MCP)的RESTful歌曲API服务,提供随机歌曲信息并支持通过MCP进行AI工具集成。

  • by dfberry
  • Bicep
  • 本地部署
  • 更新于
在 GitHub 查看 dfberry/azure-rust-mcp-server-apim

使用说明

Rust Song API与MCP集成

该工作区实现了一个带有模型上下文协议(MCP)集成的RESTful Song API服务。该项目演示了如何构建和部署一个web服务,该服务提供随机歌曲信息,同时通过MCP支持AI工具集成。

开发容器包括所有必需的工具:Azure CLI、Azure Developer CLI(azd)、Rust工具链和Docker。

在浏览器中打开

在本地开发容器中打开

  1. 当地发展先决条件:

  2. 克隆并在开发容器中打开:

    git clone https://github.com/yourusername/rust-mcp.git
    cd rust-mcp
    code .
    

    出现提示时,单击“在容器中重新打开”

使用Azure Developer CLI进行部署

  1. 部署到Azure:

    azd auth login
    azd init
    azd up
    
  2. 测试部署:

    curl https://<your-app-url>/song
    

您的自定义API现在已部署到Azure容器应用程序。

创建Azure API管理资源

  1. 使用 Azure门户 以创建Azure API管理资源。

  2. 使用 openapi.json.

  3. 为MCP服务器配置资源。Azure为API管理资源设置MCP连接的估计时间最长为2.5小时。

配置Visual Studio代码以使用MCP服务器

  1. 在Azure门户中,获取APIM终结点和订阅密钥。

  2. 在Visual Studio代码中,使用命令托盘添加具有远程URI的MCP服务器。

  3. 编辑MCP服务器以添加所需的标头 Ocp-Apim-Subscription-Key:

    {
        "files.autoSave": "afterDelay",
        "rust-analyzer.initializeStopped": true,
        "@azure.argTenant": "",
        "mcp": {
            "servers": {
                "my-mcp-server-<UNIQUE_ID>": {
                    "url": "<AZURE_API_MANAGEMENT_ENDPOINT_FOR_MCP>/mcp/sse",
                    "headers": {
                        "Ocp-Apim-Subscription-Key": "<AZURE_API_MANAGEMENT_SUBSCRIPTION_KEY>"
                    }
                }
            }
        }
    }
    
  4. 使用聊天功能询问歌曲: 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)

本地开发(无开发容器)

  1. 构建工作空间:
cargo build --workspace
  1. 运行测试:
cargo test --workspace
  1. 启动服务器:
cd server
cargo run
  1. 请尝试API:
curl http://localhost:3000/song

Docker构建

在本地构建并运行容器:

docker build -t song-api .
docker run -p 3000:3000 song-api

Azure部署

使用提供的二头肌模板部署到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接口

贡献

  1. 分叉存储库
  2. 创建功能分支(git checkout -b feature/amazing-feature)
  3. 提交您的更改(git commit -m 'Add some amazing feature')
  4. 推到分支(git push origin feature/amazing-feature)
  5. 打开拉取请求

许可证

此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。