react_agent
Process financial queries using AI to generate responses for research and analysis within the Finance MCP server.
Instructions
A React agent that answers user queries.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | query |
Input Schema (JSON Schema)
{
"properties": {
"query": {
"description": "query",
"type": "string"
}
},
"required": [
"query"
],
"type": "object"
}
Implementation Reference
- The primary handler method `async_execute` that runs the ReAct agent loop: initializes tools, builds messages, iterates through reasoning (LLM call) and acting (tool execution) steps until completion or max_steps.async def async_execute(self): """ Main execution loop implementing the ReAct (Reasoning + Acting) pattern. The agent alternates between: 1. Reasoning: Invoking the LLM to analyze the situation and decide on actions 2. Acting: Executing the chosen tools and incorporating their results This loop continues until: - The agent decides no more tools are needed (final answer reached) - The maximum number of steps is reached """ from .think_tool_op import ThinkToolOp # Initialize think tool operator if needed think_op = ThinkToolOp(language=self.language) # Build dictionary of available tool operators from context tool_op_dict = await self.build_tool_op_dict() # Optionally add think_tool to available tools if self.add_think_tool: tool_op_dict["think_tool"] = think_op # Initialize conversation message history messages = await self.build_messages() # Main ReAct loop: alternate between reasoning and acting for step in range(self.max_steps): # Reasoning step: LLM analyzes context and decides on actions assistant_message, should_continue = await self._reasoning_step( messages, tool_op_dict, step, ) # If no tool calls, the agent has reached a final answer if not should_continue: break # Acting step: execute tools and collect results tool_result_messages = await self._acting_step( assistant_message, tool_op_dict, think_op, step, ) # Append tool results to message history for next reasoning step messages.extend(tool_result_messages) # Set final output and store full conversation history in metadata self.set_output(messages[-1].content) self.context.response.metadata["messages"] = messages
- The `build_tool_call` method defines the tool's input schema requiring a 'query' string and provides a description.def build_tool_call(self) -> ToolCall: """Expose metadata describing how to invoke the agent.""" return ToolCall( **{ "description": "A React agent that answers user queries.", "input_schema": { "query": { "type": "string", "description": "query", "required": True, }, }, }, )
- finance_mcp/core/agent/react_agent_op.py:14-16 (registration)The `@C.register_op()` decorator registers the `ReactAgentOp` class, which implements the 'react_agent' tool.@C.register_op() class ReactAgentOp(BaseAsyncToolOp): """React-style agent capable of iterative tool invocation."""