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术语适用于上游数据。
