可配置MCP服务器

一个可配置的模型上下文协议(MCP)服务器,通过JSON配置将任意HTTP API暴露为MCP工具。

  • by abusedmedia
  • TypeScript
  • 本地部署
  • 3 星标
  • 更新于
在 GitHub 查看 abusedmedia/mcp-from-api

使用说明

API的MCP

A. 可配置模型上下文协议(MCP)服务器 它将任意HTTP API作为MCP工具公开,完全由JSON配置驱动。

您可以在中定义它们,而不是硬编码工具 src/config/tools.config.json。每个条目描述:

  • 工具元数据: name, description, inputSchema
  • HTTP调用:方法、路径、可选查询/正文映射、标头
  • 响应塑造:如何将HTTP响应转换为MCP result.content

worker运行时仍然是使用Wrangler的Cloudflare Workers。

环境配置

此回购设置为 将本地配置排除在git之外:

  • wrangler.jsonc 是 被忽视 (可能包含个人/内部URL和ID)
  • src/config/tools.config.json 是 被忽视 (可能包含内部工具定义/标题)

对于开源发布,在开发时提交示例文件并在本地复制。

服务器使用 baseUrlEnvKey 从工具配置中查找Worker环境中的基本URL(请参见 wrangler.jsonc).

在默认配置中:

  • baseUrlEnvKey 是 API_BASE_URL
  • wrangler.jsonc 通常为每个环境提供不同的值(示例如 wrangler.jsonc.example).

可选共享令牌(authTokenEnvKey)

您可以保护MCP端点,并将机密传递给您的上游API:

  1. 在 tools.config.json,set authTokenEnvKey 转换为Worker绑定的名称(模式与 baseUrlEnvKey).该示例使用 MCP_AUTH_TOKEN.
  2. 在 wrangler.jsonc,定义该变量(或使用 秘密 用于生产)。如果该值缺失或为空,则禁用令牌身份验证:客户端不需要令牌,上游调用也不会获得额外的令牌 token 查询参数。
  3. 当env值非空时,MCP客户端必须发送 相同的字符串 连接时,可以:
    • 作为路径段: /mcp/<token> 或 /sse/<token>,或
    • 作为 Authorization: Bearer <token>.
  4. 如果客户端令牌与配置的密钥匹配,则每个上游工具请求都会附加 token=<that secret> 作为查询参数(除了任何 query 从工具配置映射)。API可以读取该参数以授权调用。

每个工具的静态标题(例如 Authorization: Bearer ... 对于上游API)仅在每个工具的 http.headers 阻断;它们不是从MCP客户端令牌派生的。

快速启动(本地)

复制示例配置:

cp wrangler.jsonc.example wrangler.jsonc
cp src/config/tools.config.json.example src/config/tools.config.json

然后编辑 wrangler.jsonc / src/config/tools.config.json 有自己的价值观。

用JSON定义工具

工具定义见 src/config/tools.config.json 在...之下 tools 阵列。

有关提交安全参考,请参阅 src/config/tools.config.json.example.

例子:

{
  "server": {
    "name": "mcp-from-api",
    "version": "1.0.0",
    "description": "Configurable MCP server that exposes HTTP APIs as tools."
  },
  "baseUrlEnvKey": "API_BASE_URL",
  "authTokenEnvKey": "MCP_AUTH_TOKEN",
  "tools": [
    {
      "name": "example_get_users",
      "description": "Fetches a paginated list of users from the configured API.",
      "inputSchema": {
        "type": "object",
        "properties": {
          "page": {
            "type": "integer",
            "minimum": 1,
            "default": 1
          }
        },
        "required": [],
        "additionalProperties": false
      },
      "http": {
        "method": "GET",
        "path": "/users",
        "query": {
          "page": "page"
        },
        "headers": {}
      },
      "response": {
        "mode": "json",
        "contentPath": null,
        "wrap": {
          "type": "text",
          "template": "Users response:\\n\\n{{body}}"
        }
      }
    }
  ]
}

HTTP映射

在每个工具的 http 块:

  • method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE"
  • path:与环境中的基本URL连接
  • query:查询参数名称映射→ 论点关键字
    (例如。 "page": "page" 手段 args.page 成为 ?page=...)
  • headers:每次调用该工具时发送的静态标头(仅限配置)。
  • body (可选,适用于以下方法 POST/PUT/PATCH):
    • mode: "json"
    • mapping:
      • "full" –发送全部 arguments 对象为JSON
      • "properties" –仅发送中定义的选定属性 properties
    • properties (当 mapping 是 "properties"):车身特性图→ 论点关键字

响应映射

在每个工具的 response 块:

  • mode:
    • "json" –将HTTP响应正文解析为JSON
    • "text" –使用 response.text()
  • contentPath (可选):
    • 当 mode 是 "json",将其视为解析JSON的虚线路径(例如。 "data.items").
    • 如果省略或未找到,则使用整个JSON。
  • wrap (可选):
    • 目前支持 type: "text" 带着一个 template 弦。
    • 占位符 {{body}} 替换为字符串化的选定内容。

最终字符串总是返回给MCP客户端,如下所示:

{
  "content": [
    {
      "type": "text",
      "text": "..."
    }
  ]
}

运行时行为

  • tools/list 使用JSON配置公开所有工具及其 name, description,以及 inputSchema.
  • tools/call 通过以下方式查找工具 name,根据配置和提供的 arguments,调用远程API,形成响应,并将其作为MCP内容返回。
  • /health 反映当前配置,返回:
    • name, version 从 server
    • tools 作为工具名称列表
    • endpoints 为了 /mcp, /sse,以及 /health

开发与部署

现有脚本仍然适用:

# Start development server
npm run dev

# Deploy to development environment
npm run deploy:dev

# Deploy to production environment
npm run deploy

# Test production configuration locally
npm run dev:prod

要添加或更改工具,请编辑 src/config/tools.config.json 并重新部署。