MCP Agent

May 19, 2025 · View on GitHub

ai

Diagram

diagram

Overview

This project implements a LangGraph agent based on the ReWOO (Reasoning WithOut Observation) pattern. Similar to the ReAct (Reasoning and Acting) pattern, it reasons and acts, but aims to reduce the amount and complexity of prompts, especially when using Small LLMs (SLMs), by pre-defining the necessary evidence during the planning phase instead of explicitly separating an Observation step.

It also includes several optimization logics, such as tool filtering, plan validation and correction, and evidence management, for efficient interaction with the MCP (Model Context Protocol) server.

Architecture: LangGraph Nodes

This agent executes the ReWOO workflow using the following LangGraph nodes:

  1. tool_filter: At the start of agent execution, filters the most relevant tools for the current task based on the user query and descriptions of all available tools.
  2. planner: Based on the filtered tool information, generates a step-by-step execution plan (in YAML format) to resolve the user query. Also handles plan correction logic upon errors.
  3. plan_parser: Parses the raw YAML plan generated by the planner and validates its structural integrity. May include logic for automatic correction using LLM upon parsing/validation errors.
  4. plan_validator: Finally validates whether the tool names included in the parsed plan exist in the valid tool list selected during the tool_filter step.
  5. tool_selector: Selects the tool for the current step to be executed from the validated plan.
  6. tool_input_preparer: Prepares the input for the selected tool. In this process, it replaces placeholders (#E1, etc.) with actual values by referencing the evidence collected in previous steps.
  7. tool_executor: Executes the actual tool (via the MCP server) with the prepared input and retrieves the result.
  8. evidence_processor: Processes the tool execution result and stores it in the evidence dictionary. (Currently a placeholder, further logic can be implemented).
  9. generate_final_answer: When all plan steps are completed or the plan finishes without tool calls, synthesizes the final answer by compiling all collected evidence.

Project Structure

.
├── agent/             # ReWOO agent core logic
│   ├── graph.py       # LangGraph graph definition
│   ├── state.py       # Agent state definition (ReWOOState)
│   ├── nodes/         # Graph node implementations (planner, parser, executor, etc.)
│   ├── prompts/       # LLM prompt definitions
│   └── utils.py       # Utility functions
├── core/              # Core utilities
│   └── llm_loader.py  # LLM instance loading and management
├── tests/             # Test code (unit/integration)
├── images/            # Images for README
├── mcp.json           # MCP server configuration file
├── main.py            # Agent execution script
├── .env.example       # Environment variable setup example
└── README.md          # Project documentation

Usage

python main.py