LangGraph MCP 服务器是一个干净且模块化实现的服务器,用于处理 LangGraph 文档中的 Model Context Protocol (MCP)。它能为相关应用提供高效、稳定的MCP服务,提升系统的整体性能和可维护性。
要运行服务器,只需执行以下命令:
python -m app.server
该项目遵循清洁架构模式,以便随着功能的增加使MCP服务器更易于维护和调试。
app/
├── config.py # 配置设置
├── server.py # 主服务器入口点
├── resources/ # 客户端可以访问的资源
│ ├── __init__.py # 资源注册
│ └── langgraph_resources.py # LangGraph特定资源
├── tools/ # 客户端可以调用的工具
│ ├── __init__.py # 工具注册
│ └── langgraph_tools.py # LangGraph特定工具
└── utils/ # 全应用中使用的公用函数
├── __init__.py
└── logging_utils.py # 记录日志的实用程序
app/tools/目录中创建一个新的文件(例如,weather_tools.py)。register_weather_tools)。app/tools/__init__.py中调用。示例:
# app/tools/weather_tools.py
def register_weather_tools(mcp):
mcp.tool()(get_weather)
def get_weather(city: str):
"""获取某个城市的天气"""
# 实现细节
return f"{city}的天气:晴朗,75°F"
app/resources/目录中创建一个新的文件(例如,weather_resources.py)。register_weather_resources)。app/resources/__init__.py中调用。示例:
# app/resources/weather_resources.py
def register_weather_resources(mcp):
mcp.resource("weather://forecast")(get_weather_forecast)
def get_weather_forecast():
"""获取天气预报"""
# 实现细节
return "5天天气预报数据"