File size: 10,767 Bytes
a56db97
 
 
 
 
 
 
 
 
 
 
bdbcdab
 
a56db97
bdbcdab
 
a56db97
 
914a44a
a1a2253
914a44a
 
 
 
 
 
 
 
 
0127ea2
4d92a9f
7b4e2da
914a44a
7b4e2da
 
 
914a44a
 
 
 
 
 
 
 
 
 
 
d13cac7
 
 
 
 
 
 
 
 
914a44a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d13cac7
914a44a
 
 
 
d13cac7
 
e83f59a
d13cac7
 
 
 
 
 
914a44a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d13cac7
 
914a44a
d13cac7
914a44a
 
 
 
 
 
d13cac7
 
 
 
914a44a
 
 
 
 
 
 
 
 
 
 
 
d13cac7
914a44a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d13cac7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
914a44a
d13cac7
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
---
title: HF Agent
emoji: πŸ€–
colorFrom: blue
colorTo: purple
sdk: docker
app_port: 7860
hf_oauth: true
hf_oauth_scopes:
  - read-repos
  - write-repos
  - contribute-repos
  - manage-repos
  - inference-api
  - jobs
  - write-discussions
---

# 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.


### Env Setup
```bash
ANTHROPIC_API_KEY=<one-key-to-rule-them-all>
HF_TOKEN=<hf-token-to-access-the-hub>
GITHUB_TOKEN=<gh-pat-key-for-not-reinventing-the-wheel>
HF_NAMESPACE=<hf-namespace-to-use>
```

## 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[])      β”‚  β”‚  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  β”‚  β”‚ β€’ Auto-compaction (180k)   β”‚  β”‚  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  β”‚                                  β”‚  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  β”‚  β”‚ ToolRouter                 β”‚  β”‚  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  β”‚  β”‚  β”œβ”€ explore_hf_docs        β”‚  β”‚  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  β”‚  β”‚  β”œβ”€ fetch_hf_docs          β”‚  β”‚  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  β”‚  β”‚  β”œβ”€ find_hf_api            β”‚  β”‚  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  β”‚  β”‚  β”œβ”€ plan_tool              β”‚  β”‚  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  β”‚  β”‚  β”œβ”€ hf_jobs*               β”‚  β”‚  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  β”‚  β”‚  β”œβ”€ hf_private_repos*      β”‚  β”‚  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  β”‚  β”‚  β”œβ”€ github_* (3 tools)     β”‚  β”‚  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  β”‚  β”‚  └─ MCP tools (e.g.,       β”‚  β”‚  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  β”‚  β”‚      model_search, etc.)   β”‚  β”‚  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚                                        β”‚  β”‚  β”‚         β”‚
β”‚  β”‚  β”‚  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
β”œβ”€β”€ prompts/
β”‚   └── system_prompt.yaml   # Agent behavior and personality
β”œβ”€β”€ context_manager/
β”‚   └── manager.py           # Message history & auto-compaction
└── 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

configs/
└── main_agent_config.json   # Model and MCP server configuration

tests/                       # Integration and unit 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
- `approval_request` - Requesting user approval for sensitive operations
- `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

Edit `configs/main_agent_config.json`:

```json
{
  "model_name": "anthropic/claude-sonnet-4-5-20250929",
  "mcpServers": {
    "your-server-name": {
      "transport": "http",
      "url": "https://example.com/mcp",
      "headers": {
        "Authorization": "Bearer ${YOUR_TOKEN}"
      }
    }
  }
}
```

Note: Environment variables like `${YOUR_TOKEN}` are auto-substituted from `.env`.