GraphQL转换工具

将OpenLibrary的REST搜索API转换为GraphQL端点,并通过MCP为AI助手提供安全工具。

  • by chris-page-gov
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 chris-page-gov/openlibrary-mesh-mcp

使用说明

OpenLibrary Mesh+MCP(开源StepZen风格演示)

将公共OpenLibrary REST搜索包装为 图查询语言 使用 GraphQL网格,然后暴露一个安全工具 主控程序 AI助手。

这反映了StepZen的工作流程(curl→ 图查询语言→ MCP)使用 100%开源 组件。


你得到了什么

  • GraphQL端点 在 http://localhost:4000/graphql 带有字段:
    searchBooks(title: String!, page: Int): {
      numFound, start, docs { title, author_name, first_publish_year, key }
    }
    
  • MCP服务器 暴露一个工具: open-library-search(title, page?)

先决条件

  • Node.js 18.17+
  • 互联网访问(OpenLibrary是一个公共的API)

快速启动

# 1) Install deps
npm i

# 2) Start GraphQL Mesh (Terminal A)
npm run mesh:dev
# -> http://localhost:4000/graphql

# 3) Start MCP server (Terminal B)
npm run mcp:server
# (optional) change endpoint via env:
#   MESH_GRAPHQL=http://localhost:4000/graphql npm run mcp:server

# 4) (Optional) Run a smoke test (requires Mesh running)
npm test

打开GraphiQL http://localhost:4000/graphql 并运行:

query($title: String!, $page: Int) {
  searchBooks(title: $title, page: $page) {
    numFound
    start
    docs {
      title
      author_name
      first_publish_year
      key
    }
  }
}

变量:

{ "title": "the lord of the rings", "page": 1 }

使用MCP工具

添加包含 mcp/mcp.json 将代码片段添加到启用MCP的客户端配置中。它声明了一个名为 openlibrary-mesh-mcp 这始于:

{
  "command": "node",
  "args": ["mcp/server.mjs"],
  "env": { "MESH_GRAPHQL": "http://localhost:4000/graphql" }
}

然后,您的客户端应该列出一个名为 open-library-search。用以下方式称呼它:

{ "title": "the lord of the rings", "page": 1 }

项目布局

openlibrary-mesh-mcp/
├─ package.json
├─ mesh.config.yml
├─ openlibrary.yaml
├─ src/
│  └─ search.mjs           # shared function used by MCP + tests
├─ mcp/
│  ├─ server.mjs           # minimal MCP server (stdio)
│  └─ mcp.json             # example client config
├─ test/
│  └─ test.mjs             # smoke test against Mesh endpoint
└─ .gitignore

注意事项和后续步骤

  • 架构形状:展开 Doc 内部 openlibrary.yaml 具有更多字段(例如。, subject, publisher)Mesh会立即将其暴露出来。
  • 安全:箝位参数(例如。, page)并考虑向仅项目所需字段添加转换。
  • 缓存:使用简单的缓存或使用Mesh插件。
  • 认证:OpenLibrary搜索是公开的。对于经过身份验证的API,请添加 operationHeaders 在 mesh.config.yml 并通过env提供机密。

许可证

MIT为这个示例代码;OpenLibrary API术语适用于上游数据。