模型上下文协议网关

mcp-bento是一个可组合的模型上下文协议(MCP)网关,允许通过单个HTTP端点聚合多个上游MCP服务器的工具和提示。

  • by hanai
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 hanai/mcp-bento

使用说明

mcp便当

mcp-bento是一个可组合的模型上下文协议(mcp)网关,它允许您使用来自许多上游mcp服务器的工具和提示来水合单个HTTP端点。它以CLI的形式发布,在任何Node.js 20+可用的地方运行,并基于官方的MCP TypeScript SDK构建,以提供一流的流式HTTP和stdio支持。

亮点

  • 将HTTP和stdio MCP服务器的任意组合聚合到一个网关后。
  • 馆长命名 个人资料 该过滤器、前缀和重新导出工具/提示。
  • 反思网关暴露了什么 listTools / listPrompts 命令。
  • 使用强Zod模式(支持env变量)验证JSON或YAML配置。
  • 为生产做好准备 /mcp 由Hono+流式HTTP驱动的端点。
  • 通过Pino享受优雅的关机、确定性清理和结构化日志记录。

用法

直接使用以下命令运行CLI npx (无需全局安装):

npx mcp-bento serve ./config.json

快速开始

  1. 创建配置文件(例如 config.local.json)使用下面的基线模板,并对其进行调整以匹配您的MCP服务器。
  2. 通过环境或环境文件提供任何所需的机密(见下文)。
  3. 运行网关:
    npx mcp-bento serve config.local.json
    # equivalent: npx mcp-bento -c config.local.json
    
  4. 将您的MCP兼容客户端指向 http://localhost:3003/mcp?profile=default.

基线配置示例

{
  "listen": "localhost:3003",
  "mcpServers": {
    "context7": {
      "type": "http",
      "url": "https://mcp.context7.com/mcp"
    },
    "github": {
      "type": "http",
      "url": "https://api.githubcopilot.com/mcp/",
      "headers": {
        "Authorization": "Bearer ${GITHUB_MCP_PAT}"
      }
    },
    "time": {
      "type": "stdio",
      "command": "docker",
      "args": ["run", "-i", "--rm", "mcp/time"]
    }
  },
  "profiles": {
    "default": {
      "context7": { "tools": ["resolve-library-id"] },
      "time": { "prefix": false, "tools": ["get_current_time"] },
      "github-profile": { "prefix": "gh__", "tools": ["github__list_commits"] }
    },
    "github-profile": {
      "github": { "prefix": "github__", "tools": ["list_commits"] }
    }
  }
}

配置指南

案例1:仅支持HTTP,选择性导出

通过一个网关路由两个远程MCP服务器,同时重命名文档工具以避免冲突。

{
  "listen": "0.0.0.0:8080",
  "mcpServers": {
    "search": { "type": "http", "url": "https://mcp.search.example/mcp" },
    "docs": { "type": "http", "url": "https://mcp.docs.example/mcp" }
  },
  "profiles": {
    "default": {
      "search": { "tools": ["web_search", "news_search"] },
      "docs": { "prefix": "docs__", "tools": ["read_article"] }
    }
  }
}

案例2:带有自定义环境的Stdio sidecar

通过stdio公开本地Docker化MCP服务器,保持上游工具名称不变。

{
  "listen": "localhost:3004",
  "mcpServers": {
    "shell-tools": {
      "type": "stdio",
      "command": "docker",
      "args": ["run", "-i", "--rm", "mcp/shell-tools"],
      "env": { "SHELL_TOOLS_MODE": "safe" }
    }
  },
  "profiles": {
    "default": {
      "shell-tools": {
        "prefix": false
      }
    }
  }
}

案例3:作为可重用构建块的配置文件

编写一个只读的GitHub配置文件,并在具有不同前缀的分析配置文件中重用它。

{
  "listen": "localhost:3005",
  "mcpServers": {
    "github": { "type": "http", "url": "https://api.githubcopilot.com/mcp/" },
    "analytics": { "type": "http", "url": "https://analytics.example.com/mcp" }
  },
  "profiles": {
    "github-readonly": {
      "github": {
        "tools": ["list_commits", "list_pull_requests"]
      }
    },
    "analytics-suite": {
      "github-readonly": { "prefix": "gh__" },
      "analytics": { "prefix": "ops__", "tools": ["list_dashboards"] }
    }
  }
}

案例4:通过env文件获取秘密

直接在配置中引用环境变量,并通过以下方式加载它们 --env-file.

{
  "listen": "localhost:3006",
  "mcpServers": {
    "github": {
      "type": "http",
      "url": "https://api.githubcopilot.com/mcp/",
      "headers": { "Authorization": "Bearer ${GITHUB_MCP_PAT}" }
    }
  },
  "profiles": {
    "default": {
      "github": { "tools": ["list_commits"] }
    }
  }
}
# .env.mcp
GITHUB_MCP_PAT=ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
npx mcp-bento --env-file .env.mcp serve config.secret.json

CLI参考

  • npx mcp-bento serve [configPath] 启动HTTP网关。通过 -c/--config 指向JSON/YAML。
  • npx mcp-bento listTools [profileOrServer] -c <config> –列出特定配置文件/服务器(或省略时的每个配置文件)导出的工具。
  • npx mcp-bento listPrompts [profileOrServer] -c <config> –提示也是如此。
  • --env-file <path> (可重复)-在配置解析、镜像之前加载键/值对 node --env-file 语义。

每个命令在执行工作之前都会验证配置文件,因此拼写错误或模式违规会很快失败。

HTTP网关合同

  • 端点: POST|GET|DELETE /mcp?profile=<name>.
  • 正文:原始MCP JSON-RPC消息(可流式HTTP传输)。
  • 响应:从活动配置文件传递JSON-RPC响应。

因为网关会根据每个请求启动一个专用的MCP服务器,所以多个客户端可以并行连接,而无需共享状态。

调用示例

curl \
  -X POST "http://localhost:3003/mcp?profile=default" \
  -H "content-type: application/json" \
  -d '{"jsonrpc":"2.0","id":"1","method":"tools/list"}'

配置深度挖掘

mcp-bento读取JSON、YAML或YML文件。顶级按键:

密钥 描述
listen host:port 嵌入式Hono服务器消耗的配对(例如。 0.0.0.0:8080).
mcpServers 网关可以与之通信的上游连接器词典
profiles 用于重新混合服务器和其他配置文件的导出配置文件字典。

MCP服务器

每个条目下 mcpServers 必须是 http 或 stdio:

"my-http-server": {
  "type": "http",
  "url": "https://example.com/mcp",
  "headers": { "Authorization": "Bearer ${TOKEN}" }
}

"my-stdio-server": {
  "type": "stdio",
  "command": "python",
  "args": ["./server.py"],
  "env": { "PYTHONUNBUFFERED": "1" }
}
  • HTTP连接器重用 StreamableHTTPClientTransport 并接受可选的静态标头。
  • stdio连接器通过以下方式生成声明的命令(加上args/env) StdioClientTransport 并保持持久子流程的活动状态。

档案

配置文件描述了客户可以看到的内容。

字段 含义
tools / prompts 可选的同种异体动物。省略从引用的服务器/配置文件导出所有内容。
prefix 导出名称前附加的字符串(默认值: <serverId>__).吃起来 false 保留原名。
嵌套 配置文件可能会导入其他配置文件,从而启用可重用的构建块(拒绝循环)。

碰撞会自动跳过,因此第一个匹配的工具/提示获胜。

环境变量和秘密

  • 任何 ${ENV_VAR} 配置中的令牌在运行时被替换。缺少变量会记录一个警告并回退到空字符串。
  • 通过shell或可重复的方式提供机密 --env-file 标志(支持 export FOO= 语法、注释和引用值)。

可观察性和生命周期

  • 日志默认为 info level,流到stdout,并镜像到 ./bento.log (套 LOG_LEVEL=debug 更多细节)。
  • 连接器被缓存并优雅地拆除 SIGINT/SIGTERMHTTP服务器在关闭期间还强制执行故障安全超时,以避免挂起进程。

许可证

ISC©mcp bento贡献者