Files
SmartVoyage/mcp_server/weather_server.py
liangfangxing 0b087df55e feat: mcp
2026-03-20 11:26:44 +08:00

38 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from services.sql_service import SqlService
from mcp.server.fastmcp import FastMCP
from create_logger import logger
# 创建天气MCP服务器
def create_weather_mcp_server():
# 创建FastMCP实例
weather_mcp = FastMCP(name="WeatherTools",
instructions="天气查询工具,基于 weather_data 表。",
log_level="ERROR",
host="127.0.0.1", port=8002)
# 实例化sql查询服务
service = SqlService("天气查询")
@weather_mcp.tool(
name="query_weather",
description="查询天气数据,输入 SQL'SELECT * FROM weather_data WHERE city = \"北京\" AND fx_date = \"2025-07-30\"'"
)
def query_weather(sql: str) -> str:
logger.info(f"执行天气查询: {sql}")
return service.execute_query(sql)
# 打印服务器信息
logger.info("=== 天气MCP服务器信息 ===")
logger.info(f"名称: {weather_mcp.name}")
logger.info(f"描述: {weather_mcp.instructions}")
# 运行服务器
try:
print("服务器已启动,请访问 http://127.0.0.1:8002/mcp")
weather_mcp.run(transport="streamable-http") # 使用 streamable-http 传输方式
except Exception as e:
print(f"服务器启动失败: {e}")# 创建天气MCP服务器
if __name__ == "__main__":
create_weather_mcp_server()