API集合转换工具

一个将Bruno API集合转换为MCP工具的服务,使API集合可通过MCP协议访问,便于AI代理和其他MCP客户端使用。

  • by djkz
  • TypeScript
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 djkz/bruno-api-mcp

使用说明

Bruno API MCP服务器

模型上下文协议(MCP)服务器,将Bruno API集合公开为MCP工具。该服务器允许您通过MCP协议与Bruno API集合交互,使AI代理和其他MCP客户端可以访问您的API集合。

为什么这很重要:源代码和数据协同工作

当开发人员需要集成API时,他们通常会面临三个核心挑战:

  1. 跨系统边界调试:在单独的代码和数据环境中诊断问题需要不断的上下文切换,这使得故障排除效率低下。

  2. 创建自定义工具:每个第三方API集成都需要构建和维护自定义工具,从而导致开发开销和技术债务。

  3. 建筑服务用户界面:为每个后端服务开发用户界面会大大增加复杂性和维护成本。

该服务器通过将源代码与数据并置来解决这些精确的问题。它将Bruno API集合转换为模型上下文协议工具,使您能够:

  • 在具有完整上下文的以前独立的环境中进行调试
  • 将任何API变成一个代理就绪的工具,而无需额外的自定义开发
  • 构建可通过AI接口控制的无头服务

对于需要加速API集成同时减少维护开销的开发团队来说,这种方法从根本上改变了可能的情况,使以前复杂的集成变得简单和可访问。

特性

  • 将Bruno API集合自动转换为MCP工具
  • 针对不同API配置的环境管理
  • HTTP与SSE传输
  • 跨来源支持
  • 用于API集合管理的内置工具

用法

  1. 安装依赖项:

    npm install
    
  2. 使用Bruno API集合启动服务器:

    node --loader ts-node/esm src/index.ts --bruno-path /path/to/bruno/collection [--environment env_name] [--include-tools tool1,tool2,tool3] [--exclude-tools tool4,tool5]
    

    选项:

    • --bruno-path 或 -b:Bruno API集合目录的路径(必需)
    • --environment 或 -e:要使用的环境名称(可选)
    • --include-tools:以逗号分隔的要包含的工具名称列表,过滤掉所有其他名称(可选)
    • --exclude-tools:以逗号分隔的要排除的工具名称列表(可选)

    工具筛选选项支持这两种格式:

    --include-tools tool1,tool2,tool3    # Space-separated format
    --include-tools=tool1,tool2,tool3    # Equals-sign format
    
  3. 从客户端连接:

    • 本地连接: http://localhost:8000/sse
    • 从Windows到WSL: http://<WSL_IP>:8000/sse
    • 通过以下方式获取WSL IP: hostname -I | awk '{print $1}'

预定义脚本

该存储库包括几个用于常见用例的预定义npm脚本:

# Start the server with default settings
npm start

# Start with CFI API path
npm run start:cfi

# Start with local environment
npm run start:local

# Start with only specific tools included
npm run start:include-tools

# Start with specific tools excluded
npm run start:exclude-tools

发展

运行测试

运行所有测试:

npm test

运行特定测试文件:

npm test test/bruno-parser-auth.test.ts

调试

服务器使用 debug 详细日志记录库。您可以通过设置来启用不同的调试命名空间 DEBUG 环境变量:

# Debug everything
DEBUG=* npm start

# Debug specific components
DEBUG=bruno-parser npm start    # Debug Bruno parser operations
DEBUG=bruno-request npm start   # Debug request execution
DEBUG=bruno-tools npm start     # Debug tool creation and registration

# Debug multiple specific components
DEBUG=bruno-parser,bruno-request npm start

# On Windows CMD:
set DEBUG=bruno-parser,bruno-request && npm start

# On Windows PowerShell:
$env:DEBUG='bruno-parser,bruno-request'; npm start

可用的调试命名空间:

  • bruno-parser:Bruno API集合解析和环境处理
  • bruno-request:请求执行和响应处理
  • bruno-tools:在MCP服务器上创建和注册工具

工具

列出环境

列出Bruno API集合中的所有可用环境:

  • 无需参数
  • 退货:
    • 可用环境列表
    • 当前活动环境

回声

回显您发送的消息(对测试有用):

  • 参数: message (字符串)

Bruno API收集结构

您的Bruno API系列应遵循标准Bruno结构:

collection/
├── collection.bru       # Collection settings
├── environments/       # Environment configurations
│   ├── local.bru
│   └── remote.bru
└── requests/          # API requests
    ├── request1.bru
    └── request2.bru

您集合中的每个请求都将自动转换为MCP工具,使其可通过MCP协议使用。

使用工具自定义参数

当调用从Bruno API集合生成的工具时,您可以通过提供以下内容来定制请求:

环境覆盖

您可以为特定请求指定不同的环境:

{
  "environment": "us-dev"
}

这将使用指定环境中的变量,而不是默认环境。

可变替换

您可以为单个请求覆盖特定变量:

{
  "variables": {
    "dealId": "abc123",
    "customerId": "xyz789",
    "apiKey": "your-api-key"
  }
}

这些变量将在URL、标头和请求正文中替换。例如,如果您的请求URL为:

{{baseUrl}}/api/deal/{{dealId}}

你提供 { "variables": { "dealId": "abc123" } },实际使用的URL将是:

https://api.example.com/api/deal/abc123

查询参数

您可以直接添加或覆盖查询参数:

{
  "query": {
    "limit": "10",
    "offset": "20",
    "search": "keyword"
  }
}

这将把这些查询参数添加到URL中,而不管它们是否在原始请求中定义。例如,如果您的请求URL为:

{{baseUrl}}/api/deals

你提供 { "query": { "limit": "10", "search": "keyword" } },实际使用的URL将是:

https://api.example.com/api/deals?limit=10&search=keyword

这种方法比使用变量覆盖查询参数更清晰、更明确。

自定义车身参数

您还可以在请求正文中提供自定义参数:

{
  "body": {
    "name": "John Doe",
    "email": "john@example.com"
  }
}

完整示例

这是一个结合了所有四种定制类型的完整示例:

{
  "environment": "staging",
  "variables": {
    "dealId": "abc123",
    "apiKey": "test-key-staging"
  },
  "query": {
    "limit": "5",
    "sort": "created_at"
  },
  "body": {
    "status": "approved",
    "amount": 5000
  }
}

许可证

麻省理工学院