](https://mseep.ai/app/rezawr-mcp-basic-architecture)
LangGraph MCP服务器
用于LangGraph文档的模型上下文协议(MCP)服务器的干净、模块化实现。
建筑
该项目遵循干净的架构模式,随着更多功能的添加,使MCP服务器更易于维护和调试。
目录结构
app/
├── config.py # Configuration settings
├── server.py # Main server entry point
├── resources/ # Resources that can be accessed by clients
│ ├── __init__.py # Resource registration
│ └── langgraph_resources.py # LangGraph-specific resources
├── tools/ # Tools that can be called by clients
│ ├── __init__.py # Tool registration
│ └── langgraph_tools.py # LangGraph-specific tools
└── utils/ # Utility functions
├── __init__.py
└── logging_utils.py # Logging utilities核心组件
- 服务器:初始化MCP服务器并注册所有工具和资源的主要入口点。
- 配置:所有配置设置的中心位置。
- 工具:客户端可以调用以执行特定任务的函数。
- 资源:客户端可以访问的数据源。
- 工具集:整个应用程序中使用的实用功能。
添加新功能
添加新工具
- 在中创建新文件
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):
"""Get weather for a city"""
# Implementation
return f"Weather for {city}: Sunny, 75°F"
# app/tools/__init__.py
from app.tools.langgraph_tools import register_langgraph_tools
from app.tools.weather_tools import register_weather_tools
def register_tools(mcp):
register_langgraph_tools(mcp)
register_weather_tools(mcp)添加新资源
- 在中创建新文件
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():
"""Get weather forecast"""
# Implementation
return "5-day weather forecast data"
# app/resources/__init__.py
from app.resources.langgraph_resources import register_langgraph_resources
from app.resources.weather_resources import register_weather_resources
def register_resources(mcp):
register_langgraph_resources(mcp)
register_weather_resources(mcp)运行服务器
要运行服务器,请执行以下操作:
python -m app.server这种架构的好处
- 模块化:每个组成部分都有一个单一的职责。
- 可扩展性:无需修改现有代码,即可轻松添加新工具和资源。
- 可维护性:有组织的结构使调试更容易。
- 可扩展性:随着更多功能的添加,可以处理增长。
- 可测试性:组件可以单独测试。
