LangGraph-based agent loop runtime: build and run multi-step autonomous agents with tool calling, conditional routing, and state persistence.
Two modes:
- CLI / Library —
scripts/agent_loop.py, direct invocation or as a Python library - MCP Server —
scripts/mcp_server.py, exposesrun_agent(single agent) andrun_agents(supervisor multi-agent) tools to any MCP host
- ReAct loop architecture:
START → call_model → [conditional] → execute_tools → (loop back)— the LLM decides its own tool sequence - Security layer (on by default): command templates are statically scanned for dangerous operations (
rm,del,eval,subprocess, ...) at construction; parameter values are validated at call time — shell metacharacters are rejected as injection - Circuit breaker: identical tool calls without progress (default 3) inject a warning forcing a strategy change or conclusion
- Append-only event log: every milestone (task start, tool call, breaker trip, end) written as an immutable JSON file — replayable, no locking needed
- Structured status:
completed/stalled/circuit_open/max_iterations - Supervisor pattern (
run_agents): planner agent splits the task into N subtasks, sub-agents execute in parallel, results merged - Model-agnostic: works with any OpenAI-compatible API (OpenAI, Azure, local LM Studio, Ollama)
pip install langgraph langchain langchain-core langchain-openai
python scripts/agent_loop.py \
--task "Analyze the auth module" \
--tools tools.json \
--max-iter 10 \
--verboseDefine tools as a JSON file — each tool maps to a CLI command with a JSON schema:
[
{
"name": "read_file",
"description": "Read the contents of a file at the given path.",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string", "description": "Absolute path to the file"}
},
"required": ["path"]
},
"command": "python -c \"print(open('{path}', 'r', encoding='utf-8').read()[:4000])\""
}
]"mcp": {
"agent-loop": {
"type": "local",
"command": ["python", "path/to/scripts/mcp_server.py"],
"environment": {
"OPENAI_API_KEY": "sk-xxx",
"OPENAI_BASE_URL": "https://api.deepseek.com/v1",
"AGENT_MODEL": "deepseek-chat"
},
"enabled": true
}
}SKILL.md— full usage guide, parameters, and decision frameworkreferences/patterns.md— common agent design patterns and troubleshooting
Inspired by agent-hub (filesystem-based multi-agent coordination).