Commit 47110ca
Review MCP Server connection in agents ACP (#563)
* feat(mcp-agent): add rebuild_instruction_templates for post-connect prompt refresh
Add mechanism to rebuild system prompts after MCP servers connect lazily.
This solves the issue where /connect doesn't update {{serverInstructions}}
because the template was already resolved during initialization.
Changes:
- McpAgent: Store original template in _instruction_template
- McpAgent: Add rebuild_instruction_templates() method
- ACPContext: Add invalidate_instruction_cache() to update session caches
- ACPContext: Add set_resolved_instructions() to share cache reference
- AgentACPServer: Wire up resolved_instructions cache to ACPContext
- HuggingFaceAgent: Call rebuild_instruction_templates() after /connect
* refactor(acp): add public API for updating session instructions
Add SlashCommandHandler.update_session_instruction() public method
to avoid ACPContext accessing private _session_instructions directly.
This improves encapsulation and reduces coupling between classes.
* feat(core): add InstructionBuilder for template-based system prompts
Introduce InstructionBuilder class that cleanly separates template storage
from source resolution. The builder produces instruction strings on demand,
supporting both static values and dynamic async resolvers.
Features:
- Static placeholders: {{name}} with .set("name", "value")
- Dynamic resolvers: .set_resolver("name", async_fn) called on each build()
- URL patterns: {{url:https://...}} fetched at build time
- File patterns: {{file:path}} and {{file_silent:path}} relative to workspaceRoot
- Fluent API: builder.set(...).set_resolver(...).build()
- Utility methods: get_placeholders(), get_unresolved_placeholders()
Integration:
- McpAgent now uses InstructionBuilder internally
- Resolvers registered for serverInstructions and agentSkills
- rebuild_instruction_templates() simply calls builder.build()
- Pattern: construct-on-change-and-replace (stateless builder, agent owns result)
Includes 20 unit tests covering all functionality.
* fix(acp): preserve session context when rebuilding instructions
When rebuild_instruction_templates() is called (e.g., after /connect),
the InstructionBuilder was starting from the original template which
still contained {{env}}, {{workspaceRoot}}, etc. These session-level
variables weren't being resolved because the builder only had resolvers
for serverInstructions and agentSkills.
Fix:
- Add McpAgent.set_instruction_context() to set session variables on builder
- Call set_instruction_context() from ACP server during session setup
- Session context (env, workspaceRoot, agentSkills, etc.) is now available
when rebuilding instructions after /connect
* make instructions visible
* fix(mcp): avoid implicit connect for server instructions
* make server instructions visible (toad xml rendering)
---------
Co-authored-by: Claude <[email protected]>1 parent f4017dd commit 47110ca
File tree
10 files changed
+843
-72
lines changed- publish/hf-inference-acp/src/hf_inference_acp
- src/fast_agent
- acp
- server
- agents
- core
- mcp
- tests
- integration/server_instructions
- unit
- core
- fast_agent/mcp
10 files changed
+843
-72
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
497 | 497 | | |
498 | 498 | | |
499 | 499 | | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
500 | 506 | | |
501 | 507 | | |
502 | 508 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
150 | 154 | | |
151 | 155 | | |
152 | 156 | | |
| |||
357 | 361 | | |
358 | 362 | | |
359 | 363 | | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
360 | 373 | | |
361 | 374 | | |
362 | 375 | | |
| |||
416 | 429 | | |
417 | 430 | | |
418 | 431 | | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
419 | 467 | | |
420 | 468 | | |
421 | 469 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
748 | 748 | | |
749 | 749 | | |
750 | 750 | | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
751 | 762 | | |
752 | 763 | | |
753 | 764 | | |
| |||
784 | 795 | | |
785 | 796 | | |
786 | 797 | | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
787 | 801 | | |
788 | 802 | | |
789 | 803 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
174 | 174 | | |
175 | 175 | | |
176 | 176 | | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
177 | 195 | | |
178 | 196 | | |
179 | 197 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
| |||
102 | 103 | | |
103 | 104 | | |
104 | 105 | | |
105 | | - | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
106 | 109 | | |
107 | 110 | | |
108 | 111 | | |
| |||
169 | 172 | | |
170 | 173 | | |
171 | 174 | | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
172 | 182 | | |
173 | 183 | | |
174 | 184 | | |
| |||
268 | 278 | | |
269 | 279 | | |
270 | 280 | | |
271 | | - | |
| 281 | + | |
272 | 282 | | |
273 | 283 | | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
287 | | - | |
288 | | - | |
| 284 | + | |
| 285 | + | |
289 | 286 | | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
295 | 293 | | |
296 | 294 | | |
297 | 295 | | |
| |||
309 | 307 | | |
310 | 308 | | |
311 | 309 | | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
312 | 342 | | |
313 | 343 | | |
314 | 344 | | |
| |||
335 | 365 | | |
336 | 366 | | |
337 | 367 | | |
338 | | - | |
| 368 | + | |
339 | 369 | | |
340 | 370 | | |
341 | | - | |
| 371 | + | |
342 | 372 | | |
343 | 373 | | |
344 | 374 | | |
345 | 375 | | |
346 | 376 | | |
347 | 377 | | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
348 | 410 | | |
349 | 411 | | |
350 | 412 | | |
| |||
430 | 492 | | |
431 | 493 | | |
432 | 494 | | |
433 | | - | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
434 | 498 | | |
435 | 499 | | |
436 | 500 | | |
| |||
517 | 581 | | |
518 | 582 | | |
519 | 583 | | |
520 | | - | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
521 | 587 | | |
522 | 588 | | |
523 | 589 | | |
| |||
905 | 971 | | |
906 | 972 | | |
907 | 973 | | |
908 | | - | |
| 974 | + | |
909 | 975 | | |
910 | 976 | | |
911 | 977 | | |
| |||
937 | 1003 | | |
938 | 1004 | | |
939 | 1005 | | |
940 | | - | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
941 | 1009 | | |
942 | 1010 | | |
943 | 1011 | | |
| |||
0 commit comments