使用说明
Zyla API集线器MCP服务器
一 MCP(模型上下文协议) 允许任何AI代理调用的服务器 任何API 在 Zyla API中心 --只需一个工具。
Zyla API中心 • 浏览API • 获取API密钥
您将此服务器连接到您的AI代理(Claude、Cursor、, 开爪、OpenAI代理等),并且代理自动学习如何向任何Zyla API端点发出HTTP请求。不需要自定义代码。
与 官方的 MCP Python SDK (v1.x)。
目录
它是如何工作的(简单解释)
You (human) AI Agent This MCP Server Zyla API Hub
│ │ │ │
│ "Get crime data │ │ │
│ for zip 90210" │ │ │
│ ──────────────────► │ │ │
│ │ call_api(GET, url, │ │
│ │ headers, params) │ │
│ │ ─────────────────────► │ HTTP GET │
│ │ │ ─────────────────────► │
│ │ │ JSON response │
│ │ │ ◄───────────────────── │
│ │ {status: 200, │ │
│ │ response: {...}} │ │
│ │ ◄───────────────────── │ │
│ "Crime grade is │ │ │
│ B+ for 90210..." │ │ │
│ ◄────────────────── │ │ │
简单地说:
- 你问你的AI代理一个问题(用自然语言)。
- 代理决定需要调用API,并使用
call_api此服务器上的工具。 - 该服务器向Zyla API集线器发出HTTP请求并返回数据。
- 代理读取数据并用自然语言回答您。
代理人想出来了 调用哪个API,使用什么参数,以及如何解释结果 --一切都靠自己。你只要问问题。
快速入门(3个步骤)
1.安装
git clone https://github.com/zyla-labs/zyla-api-hub-mcp.git
cd zyla-api-hub-mcp
pip install -r requirements.txt
2.跑步
python mcp_server.py
3.连接您的AI代理
从下面的列表中选择您的代理,并按照一次性设置进行操作。之后,只需正常聊天——代理将在需要时使用Zyla API。
连接到AI代理
克劳德桌面版
添加到配置文件(claude_desktop_config.json):
| 操作系统 | 路径 |
|---|---|
| macOS | ~/Library/Application Support/Claude/claude_desktop_config.json |
| 窗户 | %APPDATA%\Claude\claude_desktop_config.json |
| Linux | ~/.config/Claude/claude_desktop_config.json |
{
"mcpServers": {
"zyla-api-hub": {
"command": "python",
"args": ["/absolute/path/to/mcp_server.py"],
"env": {}
}
}
}
重新启动克劳德桌面。您将在聊天中看到一个锤子图标,这意味着该工具可用。只要问:
“使用Zyla API获取邮政编码为90210的犯罪率。我的API密钥是Bearer sk-zyla。.."
克劳德代码(CLI)
claude mcp add zyla-api-hub -- python /absolute/path/to/mcp_server.py
然后正常聊天。Claude Code将调用 call_api 当它需要调用API时。
光标IDE
增添 .cursor/mcp.json 在项目中(或全局游标设置):
{
"mcpServers": {
"zyla-api-hub": {
"command": "python",
"args": ["/absolute/path/to/mcp_server.py"]
}
}
}
在Cursor的 代理模式,AI将使用 call_api 当您要求它从API获取数据时。
开爪
开爪 是一个自托管的人工智能代理网关,支持多个聊天渠道(WhatsApp、Telegram、Slack、Discord、iMessage等),并可以通过MCP使用外部工具。
选项A——通过CLI添加(推荐):
openclaw mcp add --transport stdio zyla-api-hub python /absolute/path/to/mcp_server.py
这将注册MCP服务器,以便OpenClaw代理可以发现和使用 call_api 工具。
选项B——通过配置添加(~/.openclaw/openclaw.json):
如果您更喜欢手动配置,请在OpenClaw配置中添加MCP服务器:
{
// ... your existing openclaw.json config ...
"mcpServers": {
"zyla-api-hub": {
"command": "python",
"args": ["/absolute/path/to/mcp_server.py"],
"transport": "stdio"
}
}
}
然后重新启动网关:
openclaw gateway restart
选项C——Docker+SSE(网络部署):
如果您的OpenClaw网关在远程服务器或Docker中运行,请使用SSE传输:
# Start the MCP server with SSE transport
docker run -p 8000:8000 -e MCP_TRANSPORT=sse ghcr.io/zyla-labs/mcp-server:latest
# Then register it in OpenClaw pointing to the network URL
openclaw mcp add --transport sse zyla-api-hub http://localhost:8000/sse
使用它:
连接后,通过任何渠道(WhatsApp、Slack、Telegram等)与您的OpenClaw代理聊天,它将在需要时自动使用Zyla API:
You (via WhatsApp): "What's the weather like in Buenos Aires?"
OpenClaw agent → calls call_api(
method="GET",
url="https://www.zylalabs.com/api/.../weather",
headers={"Authorization": "Bearer sk-zyla..."},
params={"city": "Buenos Aires"}
)
OpenClaw agent: "It's currently 18C and partly cloudy in Buenos Aires."
有关OpenClaw设置的更多信息,请参阅 入门指南.
OpenAI代理SDK
这 OpenAI代理SDK 原生支持MCP服务器作为工具提供者:
from agents import Agent
from agents.mcp import MCPServerStdio
async with MCPServerStdio(
command="python",
args=["/absolute/path/to/mcp_server.py"],
) as mcp_server:
agent = Agent(
name="Zyla Assistant",
instructions="You can call any API on the Zyla API Hub using the call_api tool.",
mcp_servers=[mcp_server],
)
# The agent now has access to call_api
LangChain
使用 LangChain MCP适配器 将MCP工具包装为LangChain工具:
from langchain_mcp_adapters.client import MultiServerMCPClient
async with MultiServerMCPClient({
"zyla-api-hub": {
"command": "python",
"args": ["/absolute/path/to/mcp_server.py"],
"transport": "stdio",
}
}) as client:
tools = client.get_tools()
# Use tools with any LangChain agent
自定义Python代理
使用官方MCP Python SDK构建自己的代理:
import asyncio
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def main():
# 1. Point to the MCP server
server_params = StdioServerParameters(
command="python",
args=["mcp_server.py"],
)
# 2. Connect
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
# 3. See available tools
tools = await session.list_tools()
print("Tools:", [t.name for t in tools.tools])
# Output: Tools: ['call_api']
# 4. Call an API
result = await session.call_tool("call_api", arguments={
"method": "GET",
"url": "https://www.zylalabs.com/api/824/crime+data+by+zipcode+api/583/get+crime+rates+by+zip",
"headers": {"Authorization": "Bearer YOUR_ZYLA_API_KEY"},
"params": {"zip": "90210"},
})
# 5. Read the response
print("Status:", result.structured_content["status_code"])
print("Data:", result.structured_content["response"])
asyncio.run(main())
SSE网络代理
对于通过HTTP连接的代理(web应用程序、微服务、远程部署):
# Start the server with SSE transport first
python mcp_server.py sse
import asyncio
from mcp import ClientSession
from mcp.client.sse import sse_client
async def main():
async with sse_client("http://localhost:8000/sse") as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
result = await session.call_tool("call_api", arguments={
"method": "GET",
"url": "https://www.zylalabs.com/api/XXXX/your+api/YYY/endpoint",
"headers": {"Authorization": "Bearer YOUR_KEY"},
})
print(result.structured_content)
asyncio.run(main())
MCP检验员(测试)
这 MCP检查员 允许您在浏览器中交互式测试服务器:
# Terminal 1: start the server
python mcp_server.py sse
# Terminal 2: start the inspector
npx -y @modelcontextprotocol/inspector
打开检查器UI并连接到 http://localhost:8000/sse。您可以浏览工具模式并调用 call_api 手动。
代理人看到了什么
当AI代理连接时,它会通过MCP协议自动接收此工具模式:
工具名称: call_api
说明(由法学硕士阅读):
从Zyla API中心调用任何API端点。支持GET、POST、PUT、DELETE、PATCH、HEAD、OPTIONS。请始终在Zyla API密钥中包含授权标头。
输入参数:
| 参数 | 类型 | 必填 | 功能 |
|---|---|---|---|
method |
string |
Yes | HTTP方法(GET、POST、PUT、DELETE、PATCH等) |
url |
string |
是 | API端点的完整URL |
headers |
dict[str, str] |
否 | HTTP标头(此处包括授权) |
params |
dict[str, str] |
否 | 查询字符串参数(在所有方法上发送) |
data |
dict |
否 | JSON正文(仅适用于POST、PUT、PATCH) |
输出(结构化):
| 字段 | 类型 | 含义 |
|---|---|---|
status_code |
int |
HTTP状态码(200, 404等等)。 0 =请求未到达服务器 |
response |
str | dict | list |
解析的JSON正文,如果不是JSON,则为原始文本 |
error |
str | null |
如果出现问题,则显示错误消息; null 论成功 |
使用示例
简单GET
{
"method": "GET",
"url": "https://www.zylalabs.com/api/824/crime+data+by+zipcode+api/583/get+crime+rates+by+zip",
"headers": { "Authorization": "Bearer YOUR_ZYLA_API_KEY" },
"params": { "zip": "90210" }
}
带有JSON正文的POST
{
"method": "POST",
"url": "https://www.zylalabs.com/api/XXXX/some+api/YYY/endpoint",
"headers": {
"Authorization": "Bearer YOUR_ZYLA_API_KEY",
"Content-Type": "application/json"
},
"data": { "input_text": "Hello, world!", "language": "en" }
}
带查询参数+正文的POST
{
"method": "POST",
"url": "https://www.zylalabs.com/api/XXXX/some+api/YYY/search",
"headers": { "Authorization": "Bearer YOUR_ZYLA_API_KEY" },
"params": { "page": "1", "limit": "10" },
"data": { "query": "machine learning" }
}
成功响应
{
"status_code": 200,
"response": { "data": "...", "count": 42 },
"error": null
}
错误响应
{
"status_code": 0,
"response": "",
"error": "Request timed out after 30 seconds"
}
Docker部署
构建
docker build -t zyla-mcp-server .
运行(stdio--用于本地代理)
docker run -i zyla-mcp-server
运行(SSE--用于网络代理)
docker run -p 8000:8000 -e MCP_TRANSPORT=sse zyla-mcp-server
预构建图像
docker pull ghcr.io/zyla-labs/mcp-server:latest
docker run -p 8000:8000 -e MCP_TRANSPORT=sse ghcr.io/zyla-labs/mcp-server:latest
CI管道在每次推送时自动发布到GitHub容器注册表 master.
项目结构
zyla-api-hub-mcp/
├── mcp_server.py # MCP server (single file, all logic)
├── pyproject.toml # Python packaging (PEP 621)
├── requirements.txt # Pinned dependencies
├── Dockerfile # Docker image
├── .dockerignore
├── .github/
│ └── workflows/
│ └── publish.yml # CI/CD → GHCR
└── README.md
依赖关系: mcp[cli]>=1.26.0, httpx>=0.27.0 --就是这样。
关键设计选择:
- 官方
mcpSDK(mcp.server.fastmcp.FastMCP),而不是第三方fastmcp包裹 httpx而不是requests(异步就绪,与MCP生态系统保持一致)- 派丹蒂克
ApiResponse结构化输出模型(LLM得到一个类型化的JSON模式) - 没有可变的默认参数(
None而不是{}) - 精细的错误处理(超时、请求错误、意外错误——每个错误都有明确的消息)
- 在所有HTTP方法上发送的查询参数(不仅仅是GET)
- 可通过CLI参数选择传输(
stdio或sse)
故障排除
| 问题 | 解决方案 |
|---|---|
ModuleNotFoundError: No module named 'mcp' |
快跑 pip install -r requirements.txt |
| 服务器已启动,但代理无法看到该工具 | 请确保您使用了正确的传输方式。本地代理(Claude Desktop、Cursor)使用 stdio (默认)。网络代理使用 sse. |
| 请求超时错误 | 默认超时为30秒。如果上游的Zyla API很慢,你会得到一个明确的错误消息。 |
| Docker SSE“连接被拒绝” | 暴露端口: docker run -p 8000:8000 -e MCP_TRANSPORT=sse zyla-mcp-server |
| OpenClaw看不到该工具 | 添加MCP服务器后重新启动网关: openclaw gateway restart |
许可证
麻省理工学院