Python游乐场-MCP服务器
This repo contains a simple implementation of MCP Server to demostrate possible integration with different ecosystems.
主要关注以下集成:
索引
- 1.最简单的方法:从OpenAPI规范生成 - 2.更多控制:使用工具
测试
要测试MCP服务器,您可以使用 mcp-inspector。运行以下命令:
npx @modelcontextprotocol/inspector无需安装任何东西,它将启动检查器的本地实例,您可以使用它来测试MCP服务器。
服务器将启动,用户界面将可在以下位置访问 http://localhost:6274.
认证
MCP服务器支持承载令牌身份验证。
您可以在身份验证左侧面板的UI中设置令牌。
Authentication in MCP Inspector
运行MCP服务器的不同方法
1.最简单的方法:从OpenAPI规范生成
client = httpx.AsyncClient(base_url="https://", verify=False)
openapi_spec = httpx.get("https:///jsondocs", verify=False).json()
mcp = FastMCP.from_openapi(
name="Integration Example",
openapi_spec=openapi_spec,
client=client,
)
if __name__ == "__main__":
mcp.run(transport="http", host="127.0.0.1", port=8080, path="/mcp")2.更多控制:使用工具
您还可以为要公开的每个端点定义一个工具。
这给了你更多的控制权,但需要更多的代码。
例如,要从Tome API获取主题列表:
@mcp.tool()
async def get_tome_topics(context: Context) -> list[Topic]:
"""Get a list of topics from Tome API."""
token = get_bearer_token(context)
# Call the Tome API to get topics
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
"Accept": "application/json",
}
await context.info("Fetching topics from Tome API...")
async with httpx.AsyncClient(verify=False) as client:
response = await client.get("https:///topics", headers=headers)
response.raise_for_status()
topics_data = response.json()
await context.info(topics_data)
return [Topic(topic) for topic in topics_data.get('topics', [])]
