-
Notifications
You must be signed in to change notification settings - Fork 209
Description
Here is a small example that I wrote.
supervisor_with_mcp.py
import asyncio
from mcp import ClientSession
from mcp.client.stdio import stdio_client
from langchain_mcp_adapters.tools import load_mcp_tools
from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.prebuilt import create_react_agent
from langgraph_supervisor import create_supervisor
from chatchat.utils import gat_openai_model
async def create_math_agent_via_stdio():
server_params = {
"command": "python",
"args": ["math_server.py"],
"transport": "stdio",
}
client = MultiServerMCPClient({"math": server_params})
tools = await client.get_tools()
model = gat_openai_model()
math_agent = create_react_agent(model=model, tools=tools, name="math_agent",prompt="You are a mathematics expert and only perform mathematical calculations.")
return math_agent
async def create_supervised_system():
# 创建 math_agent
math_agent = await create_math_agent_via_stdio()
async def simple_research(state):
"""A simple research tool that always returns a placeholder response."""
return "I'm currently unable to connect to the internet, but this is the default response from research_agent."
research_agent = create_react_agent(
model=gat_openai_model(),
tools=[simple_research],
name="research_agent",
prompt="You are a research expert. Don't do any mathematical calculations."
)
# 创建 supervisor,负责路由
supervisor = create_supervisor(
[research_agent, math_agent],
model=gat_openai_model(),
prompt=(
"You are the supervisor, managing the research_agent and the math_agent."
"For mathematical calculations, they will be handled by the math_agent; for the rest, they will be dealt with by the research_agent."
),
supervisor_name="main_supervisor"
)
app = supervisor.compile()
# 测试调用
resp1 = app.invoke({"messages": [{"role": "user", "content": "Please calculate 7 + 8 * 3"}]})
print("Mathematical request:",resp1)
resp2 = app.invoke({"messages": [{"role": "user", "content": "Tell me about the weather in Tokyo"}]})
print("Non-mathematical request:", resp2)
async def main():
await create_supervised_system()
if name == "main":
asyncio.run(main())
math_server.py
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("MathTools")
@mcp.tool()
def add(a: int, b: int) -> int:
"""两个数进行相加"""
return a + b
@mcp.tool()
def multiply(a: int, b: int) -> int:
"""两个数进行相乘"""
return a * b
if name == "main":
mcp.run(transport="stdio")
Here is the log output:
Mathematical request: {'messages': [HumanMessage(content='Please calculate 7 + 8 * 3', additional_kwargs={}, response_metadata={}, id='0bcf837e-a437-43af-b78d-807e9b445ba3'), AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'chatcmpl-tool-342d9c8fca3e4faa90dc2f6ed1195fae', 'function': {'arguments': None, 'name': 'transfer_to_math_agent'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'model_name': 'gpt-4o'}, name='main_supervisor', id='run--9a0cda6c-a57f-4140-9d10-9e46f02c52dd-0', invalid_tool_calls=[{'name': 'transfer_to_math_agent', 'args': None, 'id': 'chatcmpl-tool-342d9c8fca3e4faa90dc2f6ed1195fae', 'error': None, 'type': 'invalid_tool_call'}])]}
Non-mathematical request: {'messages': [HumanMessage(content='Tell me about the weather in Tokyo', additional_kwargs={}, response_metadata={}, id='feb16419-0b2e-45d9-ad37-6886e3287696'), AIMessage(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'chatcmpl-tool-254017d005894db98d28e411074251e6', 'function': {'arguments': None, 'name': 'transfer_to_research_agent'}, 'type': 'function'}]}, response_metadata={'finish_reason': 'tool_calls', 'model_name': 'gpt-4o'}, name='main_supervisor', id='run--0905a9e5-a5ef-4078-adba-f43e2cda356a-0', invalid_tool_calls=[{'name': 'transfer_to_research_agent', 'args': None, 'id': 'chatcmpl-tool-254017d005894db98d28e411074251e6', 'error': None, 'type': 'invalid_tool_call'}])]}