内容
# OpenAPI to MCP
<a href="https://x.com/janwilmake/status/1965060951444508938">Thread</a>
<img src="v3.drawio.png">
OpenAPI-to-MCP 自动发现 OpenAPI 规范,构建正确的 MCP 配置,并通过完整的参数处理和身份验证支持代理请求!
要进行测试,请运行 `npx @modelcontextprotocol/inspector` 并通过传递 URL https://mcp.openapisearch.com/{hostname}/mcp 将任何 API 用作 MCP
请注意,openapi 可以通过提供带有 `info.x-mcp` 的过滤器来为用于 MCP 的工具提供过滤器:
```ts
type MCPConfig = {
/** defaults to 2025-03-26 */
protocolVersion?: string;
/** GET endpoint that returns MCP-compatible 401 if authentication isn't valid.
*
* e.g. '/me'
*
* Will be used before responding with "initialize" and '.../list' methods
*/
authEndpoint?: string;
serverInfo?: {
name: string;
version: string;
};
promptOperationIds?: string[];
toolOperationIds?: string[];
resourceOperationIds?: string[];
};
```
例如:
```json
{
"openapi": "3.0.3",
"info": {
"x-mcp": {
"protocolVersion": "2025-03-26",
// this would only enable the myOperation operation, not the other ones
"toolOperationIds": ["myOperation"]
}
//... other info
}
"paths": {
"/some/operation":{
"get": {
"operationId": "myOperation",
//...config
}
}
///.....
},
//.....
}
```
另外,请注意,如果 OpenAPI 服务器公开 OAUTH,则应遵循 MCP 建议/规范:https://modelcontextprotocol.io/specification/draft/basic/authorization。
示例:https://mcp.openapisearch.com/curlmcp.com/mcp
应该与 `https://curlmcp.com/mcp` 相同,因为它基于 https://curlmcp.com/openapi.json
## 早期尝试:
<img src="openapi-to-mcp.drawio.png">
<a href="https://x.com/janwilmake/status/1913196601679523922">https://x.com/janwilmake/status/1913196601679523922</a>
<img src="toolflare.drawio.png">
<a href="https://x.com/janwilmake/status/1913660585356501195">https://x.com/janwilmake/status/1913660585356501195</a>
SPECS
- Swagger (openapi 2) - <a href="https://docs.swagger.io/spec.html">https://docs.swagger.io/spec.html</a>
- Openapi 3 <a href="https://raw.githubusercontent.com/OAI/OpenAPI-Specification/refs/heads/main/versions/3.1.1.md">https://raw.githubusercontent.com/OAI/OpenAPI-Specification/refs/heads/main/versions/3.1.1.md</a>
- Moonwalk (openapi 4) <a href="https://github.com/OAI/sig-moonwalk">https://github.com/OAI/sig-moonwalk</a>
- Arazzo specification - <a href="https://raw.githubusercontent.com/OAI/Arazzo-Specification/refs/heads/main/schemas/v1.0/schema.json">https://raw.githubusercontent.com/OAI/Arazzo-Specification/refs/heads/main/schemas/v1.0/schema.json</a>
- A2A (Google) <a href="https://github.com/google/A2A/blob/main/specification/json/a2a.json">https://github.com/google/A2A/blob/main/specification/json/a2a.json</a>
- Agents.json - <a href="https://raw.githubusercontent.com/wild-card-ai/agents-json/refs/heads/master/agents_json/agentsJson.schema.json">https://raw.githubusercontent.com/wild-card-ai/agents-json/refs/heads/master/agents_json/agentsJson.schema.json</a>
- MCP (Anthropic) - <a href="https://raw.githubusercontent.com/modelcontextprotocol/modelcontextprotocol/refs/heads/main/schema/2025-03-26/schema.json">https://raw.githubusercontent.com/modelcontextprotocol/modelcontextprotocol/refs/heads/main/schema/2025-03-26/schema.json</a>
- OAuth2 Server Metadata <a href="https://datatracker.ietf.org/doc/html/rfc8414">https://datatracker.ietf.org/doc/html/rfc8414</a>
想法:
- 改进 openapisearch,使其跟踪通过 MCP 找到的已用 openapi 规范(请参阅 <a href="https://github.com/janwilmake/openapisearch">https://github.com/janwilmake/openapisearch</a>)
- 为每个 OpenAPI 创建远程 MCP 服务器(此仓库)
- 为每个 OpenAPI 创建远程 A2A 服务器(尚未启动)
- 以模块化的方式进行设置,以便其他人可以轻松贡献其他规范(尚未启动)
<a href="experiments">Experiments</a>:
- `cloudflare-remote-mcp-demo-analysis`: 从 <a href="https://github.com/cloudflare/ai/tree/main/demos/remote-mcp-server">https://github.com/cloudflare/ai/tree/main/demos/remote-mcp-server</a> 获取代码和一些依赖项来提示其规范
- `dynamic-remote-mcp-server` <a href="https://github.com/cloudflare/ai/tree/main/demos/remote-mcp-server">https://github.com/cloudflare/ai/tree/main/demos/remote-mcp-server</a> 的改编,试图使其动态化