MCP
这个GitHub仓库托管了MCP,这是一个使用Connect API并集成了Swagger的客户端-服务器通信工具,用于可定制的聊天交互和API测试。
MCP 是一个简单的客户端-服务器通信工具,使用 Connect API 并集成了 Swagger 功能。
基本用法
-
启动服务器:
CONNECT_API_KEY="<你的密钥>" SWAGGER_FILE="swagger.yaml" make server
-
运行客户端:
make client
-
在
app.py
的主函数中修改聊天内容以询问不同问题。
Swagger 集成
将 Swagger 工具注册到 Chatlas:
import chatlas
import requests
import os
from openapi_mcp.chatlas import SwaggerTool
from openapi_mcp.connect_api import map_operations_to_tools
from openapi_mcp.swagger import expand_all_references, transform_swagger_to_operation_dict
# 获取 OpenAPI 文档
api_url = "127.0.0.1:8000/"
openapi_dict = requests.get(f"{api_url}/swagger.json").json()
# 转换为操作字典
openapi_doc = expand_all_references(openapi_dict)
operations = transform_swagger_to_operation_dict(openapi_doc)
tools = map_operations_to_tools(operations)
# 初始化聊天模型
aws_model = os.getenv("AWS_MODEL", "us.anthropic.claude-3-5-sonnet-20241022-v2:0")
aws_region = os.getenv("AWS_REGION", "us-east-1")
chat = chatlas.ChatBedrockAnthropic(model=aws_model, aws_region=aws_region)
# 注册工具
for operation in api_operations.values():
SwaggerTool.register_tool(
chat,
SwaggerTool(
base_url=api_url,
operation=operation,
),
)
示例应用
要启动星球大战 API 示例和 Shiny 应用:
# 启动示例服务器
make ex-starwars
# 启动 Shiny 应用
make shiny