server_sse.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from mcp.server.fastmcp import FastMCP
  2. # 在创建FastMCP实例时指定host和port
  3. mcp = FastMCP("sdg", log_level="ERROR", host="127.0.0.1", port=8001)
  4. @mcp.tool(
  5. name="query_high_frequency_question",
  6. description="从知识库中检索常见问题解答(FAQ),返回包含问题和答案的结构化JSON数据。",
  7. )
  8. async def query_high_frequency_question() -> str:
  9. """
  10. 高频问题查询
  11. """
  12. try:
  13. print("调用查询高频问题的tool成功!!")
  14. return "高频问题是: 恐龙是怎么灭绝的?"
  15. except Exception as e:
  16. print(f"Unexpected error in question retrieval: {str(e)}")
  17. raise
  18. @mcp.tool(
  19. name="get_weather",
  20. description="查询天气"
  21. )
  22. async def get_weather() -> str:
  23. """
  24. 查询天气的tools
  25. """
  26. try:
  27. print("调用查询天气的tools")
  28. return "北京的天气是多云"
  29. except Exception as e:
  30. print(f"Unexpected error in question retrieval: {str(e)}")
  31. raise
  32. def main():
  33. print("正在启动MCP SSE服务器...")
  34. print("SSE端点: http://localhost:8001/sse")
  35. print("按 Ctrl+C 停止服务器")
  36. try:
  37. # 运行SSE服务器
  38. mcp.run(transport="sse")
  39. except KeyboardInterrupt:
  40. print("\n服务器已停止")
  41. except Exception as e:
  42. print(f"服务器启动失败: {e}")
  43. if __name__ == "__main__":
  44. main()