大家好,随着智能体生态系统的持续扩张,我们注意到一个Agent 生态开发的问题:当你可能拥有一个API或者是专有的Dataset,但为了使其能在不同的Agent 和用户以各种方式接入比如 Agent的命令行CLI,MCP,本地Skills库,路由的REST API等。如果要是考虑语言的SDK(Python / TypeScript),那需要维护5-10个版本packages的Agent接入API的方式,就更加复杂了。尤其是当你想要升级时,你需要对所有的Distribution的包进行升级,非常痛苦。这里介绍一个更简单的方案,对于已经有API的开发者来说维护一个稳定API,通过 OneKey Gateway注册一个 Agent的API Registry, 然后平台负责把各种格式的Agent 流量接入转化Converter到你的API,实现了从 API2MCP,API2CLI,API2Skills,API2RestAPI的各类格式转化。你只需要维护一个API/数据,用户/智能体就能以各种格式使用它。不用再一个一个发布nodejs/python的包了。
核心代码库:https://github.com/aiagenta2z/onekey-gateway
1. 介绍
你只需要:准备好你的API → 准备 agent.json 文件和api的meta (可以codex 等自动生成)→ API Registry Host → 完成。
不用再发布任何的包,然后用户可以通过:- CLI, Skills,- MCP,REST Router 等方式使用你的API -> 路由到你的API.
例子:API2MCP,API2CLI,API2Skills,API2Rest
## api to CLI npx onekey agent <unique_id> <api_id> $data_json ## api to skills npx agtm skills build <unique_id> ### api to MCP npx onekey mcp <unique_id> ### api to router REST curl -X POST "https://agent.deepnlp.org/agent_router" -H "Content-Type: application/json" -H "X-OneKey: $DEEPNLP_ONEKEY_ROUTER_ACCESS" -d '{"unique_id":"<unique_id>","api_id":"<api_id>","data":{}}'
From \ To |
API |
CLI |
Skills |
Routed API |
MCPs (StreamingHTTP) |
MCPs (Stdio Local) |
API |
- |
✅ |
✅ |
✅ |
✅ |
- |
CLI |
- |
- |
✅ |
- |
- |
✅ |
Skills |
- |
- |
- |
- |
✅ |
✅ |
Routed API |
- |
- |
- |
- |
- |
- |
MCPs (Streaming HTTP) |
- |
- |
- |
- |
- |
- |
MCPs (Stdio Local) |
- |
- |
- |
- |
- |
- |
例如:原始API:
USDA美国农业部的食品营养nutrition的API (支持GET/POST并且有access key验证Open API),利用 agtm 包管理工具 (agent manager) 注册后,就可以使用了哈。
curl -XPOST -H "Content-Type:application/json" -d '{"pageSize":25}' https://api.nal.usda.gov/fdc/v1/foods/list?api_key=DEMO_KEY``
API2CLI:API到CLI的转化
格式:npx onekey agent <unique_id> <api_id> $data_json
npx onekey agent fdcnal/usda-fooddata-central-agent search_foods '{"query": "Cheddar Cheese","pageSize": 10}'```
API2Skills:用户本地Agent环境拉取你的API meta,构建Skills,包含OneKey 验证
格式:npx agtm skills build <unique_id>
npx agtm skills build fdcnal/usda-fooddata-central-agent
执行之后就会返回一个 ./skills/yourskill/SKILL.md /scripts等文件
API2MCP:API转化为MCP格式
用户Agent通过添加StreamingHttpServer来验证
格式:npx onekey mcp <unique_id>
npx onekey mcp fdcnal/usda-fooddata-central-agent
API2RestAPI:原始API转化为OneKey Gateway 统一路由的API,支持调用量监控
例如请求 search_foods 搜索食物卡路里的API
export DEEPNLP_ONEKEY_ROUTER_ACCESS=YOUR_ROUTER_KEY curl -X POST "https://agent.deepnlp.org/agent_router" -H "Content-Type: application/json" -H "X-OneKey: $DEEPNLP_ONEKEY_ROUTER_ACCESS" -d '{"unique_id":"fdcnal/usda-fooddata-central-agent","api_id":"search_foods","data":{"query":"Cheddar Cheese","pageSize":10}}'
2. 具体API转化为Agent接入格式的步骤
详细介绍文档 Document
USDA FoodData Central API
让我们看看如何注册和转换一个食品营养API:
准备API规范
- 使用USDA的OpenAPI规范(fdc_api.json)
agtm.yaml
api_list: - api_id: search_foods protocol: http description: Search for food items and calories endpoint: https://api.nal.usda.gov/fdc/v1/foods/search method: POST params: query: str pageSize: int auth: type: API_KEY header: "X-Api-Key" value: $YOUR_FDC_API_KEY
不想手写的同学可以直接利用codex/Claude Code描述和生成,利用这个prompt,在Github代码库的examples下面的例子作为格式的模板
Generate agent.json and agent.yaml file following the format of ./examples/usda_food_api/agent.json and agent.yaml format, fill in the api_list key, with meta of APIs: api_id,protocol,description,endpoint,method,params,auth, For params, you can read the necessary informations from the the OpenAPI specs of fdc_api.json. Output to /agent.json or /agent.yaml formats
注册你的API的Meta信息
获取你的OneKey Gateway API的注册管理密钥
export AI_AGENT_MARKETPLACE_ACCESS_KEY=YOUR_REGISTRY_KEY npx agtm upload --config ./agent.yaml
你的智能体的唯一ID格式是 user_id/repo_name,例如:fdcnal/usda-fooddata-central-agent
使用示例
API to REST路由API请求
export DEEPNLP_ONEKEY_ROUTER_ACCESS=YOUR_ROUTER_KEY curl -X POST "https://agent.deepnlp.org/agent_router" \ -H "Content-Type: application/json" \ -H "X-OneKey: $DEEPNLP_ONEKEY_ROUTER_ACCESS" \ -d '{ "unique_id": "fdcnal/usda-fooddata-central-agent", "api_id": "search_foods", "data": {"query": "Cheddar Cheese","pageSize": 10} }'
API to CLI
搜索 search_foods,Cheddar Cheese的营养配料表
npx onekey agent fdcnal/usda-fooddata-central-agent search_foods '{"query": "Cheddar Cheese","pageSize": 10}'
API to Skills
export AI_AGENT_MARKETPLACE_ACCESS_KEY=YOUR_REGISTRY_KEY npx agtm skills build fdcnal/usda-fooddata-central-agent
用户本地项目目录下创建的 Skills目录,可以参考
skills/fdcnal-usda-fooddata-central-agent/ ├── SKILL.md ├── scripts/ └── reference/
API to MCP
执行命令行,然后把 mcp 配置添加到 mcp.config里面
npx onekey mcp fdcnal/usda-fooddata-central-agent
{ "mcpServers": { "deepnlp-onekey-fdcnal-usda-fooddata-central-agent": { "url": "https://agent.deepnlp.org/mcp?server_name=fdcnal/usda-fooddata-central-agent&onekey=YOUR_KEY" } }}
相关文档
OneKey Gateway 代码库: https://github.com/aiagenta2z/onekey-gateway
Agtm Skills 包管理命令行工具:https://github.com/aiagenta2z/agtm