Skip to content

MCP: cache-first startup, lazy connection and tool catalog cache (WIP) - #996

Draft
evalstate wants to merge 16 commits into
mainfrom
feat/mcp-lazy-load
Draft

evalstate wants to merge 16 commits into
mainfrom
feat/mcp-lazy-load

Conversation

@evalstate

Copy link
Copy Markdown
Owner

Work in progress — not ready for merge; opened to track the branch.

Starts MCP servers using a cached tool catalog first, with a configurable connection policy, so startup and tool listing don't block on server connections.

  • Background MCP startup and deferred connection, backed by a persisted tool catalog cache (src/fast_agent/mcp/tool_catalog_cache.py), with startup diagnostics
  • Cache-first startup with an eager | lazy connection policy; one cache policy
  • Listing tools never connects deferred servers
  • Digest mode for tool catalog snapshots (stores version digests of the tool definitions)
  • The first REPL send and #agent sends wait until background startup has finished; the toolbar redraws on startup events
  • /mcp shows tools in one compact row; /mcp auth <server> [--device]
  • Detects auth at startup without opening a browser; includes feat/mcp-device-auth (OAuth device authorization grant, RFC 8628)

Merged with current main. Format, lint and typecheck pass; unit tests pass (9318 passed).


You're given a calfskin wallet for your birthday. How would you feel about using it?

As an AI I don't carry a wallet or have birthdays, so I wouldn't use it myself. If someone gave me one, I'd appreciate the thought, keep in mind that it's made from an animal, and leave the decision about using it to the person who would actually carry it.

… and diagnostics

- Start MCP servers in the background in interactive REPL mode, with per-server
  startup status surfaced to the status bar and /mcp error.
- Add connection_policy: deferred and a versioned on-disk tool catalog cache so
  servers can advertise tools before connecting; connect lazily on first call
  and reject calls whose tool definition changed.
- Add fast-agent mcp diagnose and safe diagnostic text rendering (terminal
  escape stripping, Rich markup neutralisation, targeted credential redaction).
- Serialise per-server startup/reconnect in the connection manager.
- Review fixes: keep status/exit codes in diagnostics, don't re-raise a failed
  startup task from close(), single owner for startup status transitions.
Background MCP startup rendered agent instructions before any server
connected, so {{serverInstructions}} was permanently empty in the REPL.

- MCPStartup tracks startup tasks (pending/wait; waiter cancel is shielded)
- McpAgent re-renders its instruction once its aggregator startup settles
- Interactive send prints "Waiting for MCP startup…" and waits if pending;
  Ctrl+C drops the prompt without cancelling startup
- Snapshot-restored deferred servers are marked ready instead of stuck pending
…ache policy

- #agent queries wait for background MCP startup like regular sends
- Replace the prompt's 4Hz refresh_interval with MCPStartup change
  notifications, so idle prompts no longer repaint on a timer
- ToolCatalogCache.enabled is the single cache policy (incl. --no-home);
  the aggregator only adds the request-forwarded-token exclusion
Collapse recorded failure chains (deduplicated causes, bounded, redacted)
into the /mcp status line instead of a generic 'MCP startup failed'.
# Conflicts:
#	docs/docs/ref/shell_runtime_budgeting.md
#	tests/unit/fast_agent/tools/test_shell_runtime.py
# Conflicts:
#	tests/unit/fast_agent/commands/test_runtime_result_export.py
Add login_mcp_server_with_device_code(), a UI-independent device-flow login
that reuses the MCP OAuth provider's discovery, resource selection, client
authentication and token storage, so later connections and refresh pick up
the token unchanged. Registers a public DCR client declaring the device_code
grant (falls back to CIMD), and only persists client info + tokens once a
token is issued.

Expose it via 'fast-agent auth mcp login <server|--endpoint URL> --device'
and document the flow.
list_tools() is called by UI reads (tab completion, /tools, agent summary);
refreshing expired deferred snapshots there could spawn or connect a server
on a keystroke. Snapshot expiry now only matters when a session starts.
Servers advertising definition digests (Hugging Face prototype of the MCP
Definition Versions SEP) make snapshots digest-validated:

- Snapshot v2 stores server instructions and tools/instructions digests;
  instructions no longer block deferred reuse, and {{serverInstructions}}
  renders from the snapshot before connecting
- Digest snapshots ignore the TTL; every tools/call echoes known versions and
  a -32987 mismatch refreshes the stale definitions without executing
- Tool runner re-lists listed tools when definitions change mid-turn, and
  McpAgent re-renders its instruction before the next model call, so the
  model can recover within the turn
- Keys and error code live in fast_agent.mcp.definition_versions

Verified live against https://huggingface.co/mcp?anon.
…device]

- Background startup never begins an interactive login: remote servers
  without stored OAuth tokens start with OAuth disabled, and a challenge
  becomes a typed ServerAuthenticationRequiredError -> 'auth' status
  (stored/refreshable tokens still connect as before)
- The prompt announces 'MCP server X requires authentication' once
- /mcp auth <server> runs /mcp connect <server> --oauth (TUI and ACP);
  --device first runs the device-code login, then connects with the
  stored token. Bare /mcp auth still shows diagnostics
- stored_oauth_tokens_present() shared by startup and 'fast-agent auth'
  (and respects auth.persist: memory)

Verified live against https://huggingface.co/mcp?login with no stored
credentials: AUTH status + notice, no browser; link flow and device code
shown and cancellable.
- Digest mode is a property of the live catalog, not of the disk cache:
  --url servers (no auth_identity) now show 'digest checked per call'
- Digest snapshots persist without tool_cache.auth_identity, since the
  server rejects another account's snapshot before any tool runs; TTL
  snapshots of network servers still require an explicit partition
- ToolCacheInfo records digest/persisted explicitly; /mcp shows an aligned
  'tools' row (count · origin and age · change detection · saved/memory
  only) instead of a raw ISO-timestamp line; /mcp cache uses the same text
- Rename connection_policy 'deferred' to 'lazy' (unreleased)
- Both policies check the snapshot first: a usable snapshot (digest mode,
  or within TTL) advertises tools/instructions immediately and never holds
  the first prompt. eager connects in the background; lazy connects on the
  first tool call. No usable snapshot: connect at startup as before
- Eager background connects skip tools/list when server/discover returns a
  matching digest (explicit /mcp refresh still lists)
- One helper connects snapshot-advertised servers (startup, first call,
  refresh) instead of three copies
- fast-agent go --mcp-connect eager|lazy for startup targets
- /mcp shows 'connect: on first tool call (lazy)' for unconnected servers
  instead of offline health/unknown protocol

Verified live with --url https://huggingface.co/mcp?anon: eager run sends
discover only (no tools/list) and the first prompt does not wait; lazy run
makes no startup requests and renders instructions from the snapshot.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Deploying fast-agent with  Cloudflare Pages  Cloudflare Pages

Latest commit: c0667dc
Status:🚫  Build failed.

View logs

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant