主控程序
使用Connect API的客户端和服务器的简单示例。
用法
运行MCP服务器:
CONNECT_API_KEY="" SWAGGER_FILE="swagger.yaml" make server然后运行MCP客户端:
make client在主功能中修改聊天 app.py 问不同的问题。
Swagger使用方法
下面的代码是如何在Chatlas注册Swagger工具的代码片段。
import chatlas
import requests
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,
)
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,
),
)要查看此操作,请运行本地OpenAPI兼容服务器并运行Shiny应用程序:
# Run the OpenAPI-compatible server
make ex-starwars# Run Shiny
make shiny