MCPcopy Create free account
hub / github.com/CopilotKit/open-mcp-client / chat_node

Function chat_node

agent/sample_agent/agent.py:53–87  ·  view source on GitHub ↗

This is a simplified agent that uses the ReAct agent as a subgraph. It handles both chat responses and tool execution in one node.

(state: AgentState, config: RunnableConfig)

Source from the content-addressed store, hash-verified

51}
52
53async def chat_node(state: AgentState, config: RunnableConfig) -> Command[Literal["__end__"]]:
54 """
55 This is a simplified agent that uses the ReAct agent as a subgraph.
56 It handles both chat responses and tool execution in one node.
57 """
58 # Get MCP configuration from state, or use the default config if not provided
59 mcp_config = state.get("mcp_config", DEFAULT_MCP_CONFIG)
60
61 print(f"mcp_config: {mcp_config}, default: {DEFAULT_MCP_CONFIG}")
62
63 # Set up the MCP client and tools using the configuration from state
64 async with MultiServerMCPClient(mcp_config) as mcp_client:
65 # Get the tools
66 mcp_tools = mcp_client.get_tools()
67
68 # Create the react agent
69 model = ChatOpenAI(model="gpt-4o")
70 react_agent = create_react_agent(model, mcp_tools)
71
72 # Prepare messages for the react agent
73 agent_input = {
74 "messages": state["messages"]
75 }
76
77 # Run the react agent subgraph with our input
78 agent_response = await react_agent.ainvoke(agent_input)
79
80 # Update the state with the new messages
81 updated_messages = state["messages"] + agent_response.get("messages", [])
82 await copilotkit_exit(config)
83 # End the graph with the updated messages
84 return Command(
85 goto=END,
86 update={"messages": updated_messages},
87 )
88
89# Define the workflow graph with only a chat node
90workflow = StateGraph(AgentState)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected