Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
| # HF Agent | |
| An MLE agent CLI with MCP (Model Context Protocol) integration and built-in tool support. | |
| ## Quick Start | |
| ### Installation | |
| ```bash | |
| # Clone the repository | |
| git clone git@github.com:huggingface/hf_agent.git | |
| cd hf-agent | |
| ``` | |
| #### Install recommended dependencies | |
| ```bash | |
| uv sync --extra agent # or uv sync --extra all | |
| ``` | |
| ### Interactive CLI | |
| ```bash | |
| uv run python -m agent.main | |
| ``` | |
| This starts an interactive chat session with the agent. Type your messages and the agent will respond, using tools as needed. | |
| The agent will automatically discover and register all tools from configured MCP servers. | |
| ## Architecture | |
| ### Component Overview | |
| ``` | |
| βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| β User/CLI β | |
| ββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββ¬ββββββββββββ | |
| β User request β Events | |
| β β | |
| submission_queue event_queue | |
| β β | |
| β β | |
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β | |
| β submission_loop (agent_loop.py) β β | |
| β ββββββββββββββββββββββββββββββββββββββββββββββββ β β | |
| β β 1. Receive Operation from queue β β β | |
| β β 2. Route to Handler (run_agent/compact/...) β β β | |
| β ββββββββββββββββββββββββββββββββββββββββββββββββ β β | |
| β β β β | |
| β ββββββββββββββββββββββββββββββββββββββββββββββββ β β | |
| β β Handlers.run_agent() β βββββββββββ€ | |
| β β β β Emit β | |
| β β ββββββββββββββββββββββββββββββββββββββββββ β β Events β | |
| β β β Agentic Loop (max 10 iterations) β β β β | |
| β β β β β β β | |
| β β β ββββββββββββββββββββββββββββββββββββ β β β β | |
| β β β β Session β β β β β | |
| β β β β ββββββββββββββββββββββββββββββ β β β β β | |
| β β β β β ContextManager β β β β β β | |
| β β β β β β’ Message history β β β β β β | |
| β β β β β (litellm.Message[]) β β β β β β | |
| β β β β ββββββββββββββββββββββββββββββ β β β β β | |
| β β β β β β β β β | |
| β β β β ββββββββββββββββββββββββββββββ β β β β β | |
| β β β β β ToolRouter β β β β β β | |
| β β β β β ββ bash β β β β β β | |
| β β β β β ββ read_file β β β β β β | |
| β β β β β ββ write_file β β β β β β | |
| β β β β β ββ McpConnectionManager β β β β β β | |
| β β β β β ββ mcp__server1__* β β β β β β | |
| β β β β β ββ mcp__server2__* β β β β β β | |
| β β β β ββββββββββββββββββββββββββββββ β β β β β | |
| β β β ββββββββββββββββββββββββββββββββββββ β β β β | |
| β β β β β β β | |
| β β β Loop: β β β β | |
| β β β 1. LLM call (litellm.acompletion) β β β β | |
| β β β β β β β β | |
| β β β 2. Parse tool_calls[] β β β β | |
| β β β β β β β β | |
| β β β 3. Execute via ToolRouter β β β β | |
| β β β β β β β β | |
| β β β 4. Add results to ContextManager β β β β | |
| β β β β β β β β | |
| β β β 5. Repeat if tool_calls exist β β β β | |
| β β ββββββββββββββββββββββββββββββββββββββββββ β β β | |
| β ββββββββββββββββββββββββββββββββββββββββββββββββ β β | |
| ββββββββββββββββββββββββββββββββββββββββββββββββββββββ΄ββββββββββ | |
| ``` | |
| ### Agentic Loop Flow | |
| ``` | |
| User Message | |
| β | |
| [Add to ContextManager] | |
| β | |
| βββββββββββββββββββββββββββββββββββββββββ | |
| β Iteration Loop (max 10) β | |
| β β | |
| β Get messages + tool specs β | |
| β β β | |
| β litellm.acompletion() β | |
| β β β | |
| β Has tool_calls? ββNoββ> Done β | |
| β β β | |
| β Yes β | |
| β β β | |
| β Add assistant msg (with tool_calls) β | |
| β β β | |
| β For each tool_call: β | |
| β β’ ToolRouter.execute_tool() β | |
| β β’ Add result to ContextManager β | |
| β β β | |
| β Continue loop ββββββββββββββββββ β | |
| β β β β | |
| βββββββββββ§ββββββββββββββββββββββββ§ββββββ | |
| ``` | |
| ## Project Structure | |
| ``` | |
| agent/ | |
| βββ config.py # Configuration models | |
| βββ main.py # Interactive CLI entry point | |
| βββ context_manager/ | |
| β βββ manager.py # Message history management | |
| βββ core/ | |
| βββ agent_loop.py # Main agent loop and handlers | |
| βββ session.py # Session management | |
| βββ mcp_client.py # MCP SDK integration | |
| βββ tools.py # ToolRouter and built-in tools | |
| test_integration.py # Basic integration tests | |
| test_tools.py # Tool execution tests | |
| eval/ # Evaluation suite (see eval/README.md) | |
| ``` | |
| ## Events | |
| The agent emits the following events via `event_queue`: | |
| - `processing` - Starting to process user input | |
| - `assistant_message` - LLM response text | |
| - `tool_call` - Tool being called with arguments | |
| - `tool_output` - Tool execution result | |
| - `turn_complete` - Agent finished processing | |
| - `error` - Error occurred during processing | |
| - `interrupted` - Agent was interrupted | |
| - `compacted` - Context was compacted | |
| - `undo_complete` - Undo operation completed | |
| - `shutdown` - Agent shutting down | |
| ## Development | |
| ### Adding Built-in Tools | |
| Edit `agent/core/tools.py`: | |
| ```python | |
| def create_builtin_tools() -> list[ToolSpec]: | |
| return [ | |
| ToolSpec( | |
| name="your_tool", | |
| description="What your tool does", | |
| parameters={ | |
| "type": "object", | |
| "properties": { | |
| "param": {"type": "string", "description": "Parameter description"} | |
| }, | |
| "required": ["param"] | |
| }, | |
| handler=your_async_handler | |
| ), | |
| # ... existing tools | |
| ] | |
| ``` | |
| ### Adding MCP Servers | |
| Add to your config: | |
| ```python | |
| config = Config( | |
| model_name="anthropic/claude-sonnet-4-5-20250929", | |
| mcp_servers=[ | |
| MCPServerConfig( | |
| name="your_server", | |
| command="command", | |
| args=["arg1", "arg2"], | |
| env={"KEY": "value"} | |
| ) | |
| ] | |
| ) | |
| ``` | |