# Agent Bridge

Status: Phase A (identity, tokens, permissions, audit, Settings →
Connections) is **implemented** (`25b7c50`). Phase B (protocol,
conversations, messages, tasks, approvals, durable events with SSE and
polling, MCP inbox tools, the Workspace Context Service backend and a
reference client) is **implemented, backend only** (`590b515`). Phases
C (Agent page and side panel) and D (cloud providers and function
calling) are **design**.

The Agent Bridge lets people, external agents and model providers work in
the same vault: talk in persistent conversations, hand over tasks, attach
research objects, and act through Phantom's own services under one
permission system. It runs inside the Phantom backend; there is no
separate service or cloud dependency.

## Components

| Component | Where | Responsibility |
|---|---|---|
| Authentication (A, implemented) | `api/agent_auth.py`, `mcp/principal.py`, `core/agents/tokens.py`, `services/agents/auth_service.py`, `api/v1/bridge/deps.py` | bearer tokens on MCP and the Bridge API; principal per handler ([Agent authentication](authentication.md)) |
| Agent registry (A, implemented) | `models/agent.py`, `services/agents/registry_service.py`, `services/agents/status.py` | stable agent identity, declared vs granted capabilities, status |
| Permissions (A, implemented) | `core/agents/permissions.py`, `presets.py`, `principal.py`, `context.py` (`require()`), `mcp/enforcement.py` | grant sets, presets, the principal context, the per-capability check ([Agent permissions](permissions.md)) |
| Capability registry (A: MCP only) | `mcp/toolkit.py` (`Toolkit.requirements`, `Toolkit.guarded`) | every MCP tool and resource with schema, annotations and `requires`; the Bridge and providers will dispatch through `guarded` |
| Protocol (B, implemented) | `core/bridge/protocol.py`, `api/v1/bridge/*`, `services/bridge/hello.py` | versions, handshake, routes ([Agent Bridge protocol](bridge-protocol.md), [Agent Bridge routes](bridge-routes.md)) |
| Conversation service (B) | `services/bridge/conversations.py`, `messages.py`, `delivery.py`, `streaming.py` | conversations and messages, idempotency, gapless order, delivery state, streamed text |
| Task service (B) | `core/bridge/tasks.py`, `services/bridge/tasks.py`, `task_work.py`, `sweeps.py` | task state machine, leases, retries, deadlines, restart recovery |
| Approval service (B) | `services/bridge/approvals.py` | human decisions on operations that need them |
| Event log and streaming (B) | `models/bridge_events.py` (`agent_events`), `services/bridge/events.py`, `event_reads.py`, `runtime/bridge_hub.py`, `api/v1/bridge/events.py` | durable ordered events written with each change, SSE with polling fallback, WebSocket forwarding |
| Authorization changes (B) | `services/bridge/authorization.py` | revoke, rotate, grant edit, disconnect stop the agent's open work |
| Workspace Context Service (B backend) | `core/bridge/context_schema.py`, `context_view.py`, `runtime/workspace_context.py`, `services/bridge/context.py` | what the user is looking at, filtered per agent (`design/workspace-context.md`) |
| MCP access (B) | `mcp/tools/bridge.py`, `bridge_tasks.py`, `mcp/resources/bridge.py` | inbox, reply, task claim and update, context, `phantom://bridge/inbox` |
| Runtime (B) | `runtime/bridge.py`, `runtime/bridge_state.py` | recovery at start, sweep every 5 s, context persistence |
| Provider adapters | `agents/providers/*` | OpenAI-compatible, Anthropic, Gemini; streaming and tool calls in one internal format ([Cloud and local model providers](model-providers.md)) |
| Audit (A, implemented) | `agent_audit`, `services/agents/audit_service.py` | who did what, where, with what outcome; bounded |

MCP stays the standard interface to Phantom's tools and resources. The
Bridge API adds what plain MCP does not reliably give: server-initiated
messages, durable conversations and tasks, and streaming to the agent.

## Connection modes

**A. External MCP agent** (Claude Code, Codex, custom agents). Connects to
`/mcp` with a token. Agents that speak only MCP tools can use the Bridge
through tools (`bridge_inbox`, `bridge_reply`, `task_claim`,
`task_update`) and, on protocol versions that support it, the
`phantom://bridge/inbox` resource subscription. The interface states the
limitation plainly: "MCP only: checks for messages, no push".

Claude Code gets push as well, through Phantom's channel
(`backend/mcp_server.py --channel`): the user's messages arrive in the
running session as they are written, Claude replies with `phantom_reply`,
and the same registration relays every Phantom tool. Setup, the opt-in
permission relay and troubleshooting: [Claude Code](claude-code.md).
Every agent connection, this one included, stops while the user has turned
the MCP server off in Settings → AI & Agents (`503 mcp_disabled`).

**B. Cloud or local model provider.** Phantom calls the provider itself,
streams the answer into the conversation and runs approved function calls
through the capability registry. Providers: OpenAI, Anthropic, DeepSeek,
Gemini, any OpenAI-compatible endpoint, Ollama and LM Studio.

**C. Remote Agent API.** The versioned Bridge API, for agents running
elsewhere. **Off by default.** When enabled it is reached only through an
explicitly configured secure channel (a tunnel or outbound relay the user
sets up); the proxy listener, the database and the user-facing REST API are
never exposed through it. Remote MCP needs standard OAuth (audience-bound
tokens, authorization server metadata); this mode ships after the local
modes and is documented separately when it does.

## What the agent sees of the page

The Workspace Context Service (`design/workspace-context.md`) keeps a
structured description of what the user is looking at: vault, module,
open resources, selection, visible data, filters and recent navigation.
Each agent receives only the part its grants allow, redacted and as
references, so the user never has to explain what is on screen and seeing
the page never bypasses permissions.

## Phase B: deferred

- The Agent page and panel and the interface composable that publishes
  the workspace context: built in Phase C
  ([Design: Agent workspace](../design/agent-workspace.md)).
- Providers (Phase D): conversations with `provider_id` exist and the
  streaming service API is ready; nothing calls a provider yet. Approvals
  are recorded and decided but do not yet gate MCP tools or function
  calls.
- Rate limits on Bridge sends (the protocol reserves `rate_limited`).
- References are validated in shape and kind, not checked for existence.
- Event order relies on SQLite serialising writers; a multi-writer
  database needs a commit horizon on reads.
- Remote Agent API (mode C) and OAuth for remote MCP.

## Documents

- [Agent authentication](authentication.md): tokens, identity, MCP authentication.
- [Agent permissions](permissions.md): grants, enforcement, hard rules.
- [Agent Bridge protocol](bridge-protocol.md): Bridge API rules: identity, messages, tasks, approvals, events, errors.
- [Agent Bridge routes](bridge-routes.md): every Bridge route with its body and answer.
- `examples/bridge_client.py`: the reference client.
- [Cloud and local model providers](model-providers.md): adapters, secrets, egress guard, usage.
- `design/agent-workspace.md`: the Agent page and the global panel.
- `design/workspace-context.md`: the Workspace Context Service.
