A Simple MCP Weather Server written in Python

A Simple MCP Weather Server written in Python

未知

这是一个使用 Python 实现的简单 MCP (Model Context Protocol) 天气服务器。

安装

pip install mcp-python

基本用法

from mcp.server import MCPServer
from mcp.types import Request, Response

# 创建服务器实例
server = MCPServer()

# 定义天气模型处理函数
@server.model("weather")
async def weather_model(request: Request) -> Response:
    location = request.params.get("location", "未指定位置")
    return Response({"forecast": f"{location}今天晴天,温度25°C"})

# 启动服务器
if __name__ == "__main__":
    server.run(host="localhost", port=8000)

测试

使用 curl 发送请求:

curl -X POST http://localhost:8000/v1 \
  -H "Content-Type: application/json" \
  -d '{"model":"weather","params":{"location":"北京"}}'

更多信息

请参阅完整的快速入门教程