Architecture · Decision record 0014
ADR 0014: Platform connections (MCP server, MCP client connectors, GitHub)
On this page
- Status: Accepted
- Date: 2026-11-25
- Pulls forward: SPEC M6 "MCP server, MCP client connectors"; Demo B step 1
Context#
People want to drive Delegate from the tools they already use (Claude Code, Codex, Cursor, any MCP client), give Delegate access to their own systems (their MCP servers, their GitHub repositories), and let routine work run without a human click when it stays inside policy. Each of these opens a new trust boundary:
- Client → Delegate. A terminal process holds a credential that acts for a workspace, outside the browser and its same-origin protections.
- Delegate → connector. A third-party MCP server returns content we do not control (tool output and tool descriptions), and some of its tools have side effects.
- Delegate → GitHub. A visitor grants a GitHub App access to repositories; the binding between that installation and a Delegate workspace must not be forgeable.
Production runs on Vercel serverless functions (300 s cap, no sticky sessions) and Neon. Everything stays $0 and keyless by default (ADR 0003).
Decision#
1. Delegate as a remote MCP server (/ api/ mcp)#
- Transport: Streamable HTTP from
@modelcontextprotocol/1.32 (sdk WebStandardStreamableHTTPServerTransport), stateless (sessionIdGenerator: undefined) with JSON responses (enableJsonResponse: true). Every POST builds a freshMcpServer+ transport, handles one JSON-RPC message batch and is done. No session map, so any function instance can answer any request. GET (standalone SSE) and DELETE return 405; we send no server-initiated notifications. - Tools:
start_mission,get_run,wait_for_run,approve_plan,reject_plan,list_runs,get_artifact; one resource templatedelegate://. The tool set lives inruns/ {runId}/ deliverable packages/behind amcp-server DelegateBackendinterface, so the same definitions serve the remote endpoint (in-process backend over the server functions) and the local stdio server (backend over the REST API through@delegate/).sdk wait_for_runis a bounded long-poll (default 25 s, max 120 s), well under the 300 s function cap, polling the run record. Clients call it again until the run is terminal or a plan needs a decision. Runs started over MCP are ordinary runs: the event log drives the web UI live (ADR 0005).- Auth: per-workspace API tokens.
dlg_+ base64url(32 random bytes). Stored asprefix(first 12 characters, unique, for lookup) andsha256(token)hex; the plaintext is shown once. Revocation setsrevoked_at;last_used_atis updated at most once a minute. ASECURITY DEFINERfunctionresolve_api_token(prefix, hash)(owned bydelegate_definer) is the only path from a bearer token to an identity: it requires a matching hash, no revocation, and that the token's creator is still an operator+ member of the workspace. It returns(token_id, workspace_id, user_id), and every request then runs throughwithTenant(user_id)exactly like a cookie session: same RLS, same per-user and per-IP rate limits. The REST routes accept the same bearer token (and skip the same-origin check only when a valid bearer token is presented, because bearer auth is not ambient and cannot be forged cross-site). - Setup snippets for Claude Code, Codex, Cursor/VS Code and a generic client are generated on
/from the request's origin.settings/ connections
2. MCP client connectors#
- A workspace admin adds a remote MCP server: Streamable HTTP URL plus an optional secret header (default
Authorization: Bearer …). - URL guard:
validateConnectorUrlreuses the fetch hardening (ADR 0004): no credentials in URLs, no IP literals, no localhost /.internal/ metadata names, ports 80/443 only, andhttps:only whenNODE_ENV=production. Every connection goes throughcreatePinnedFetch, so DNS is resolved and checked at connect time (no rebinding), and redirects are refused. - Secrets at rest: AES-256-GCM (
node:crypto), 12-byte random IV, 16-byte tag, key fromDELEGATE_ENCRYPTION_KEY(64 hex characters,openssl rand -hex 32). The stored value isv1.<iv>.<tag>.<ciphertext>(base64url) with the connector id as additional authenticated data, so a ciphertext cannot be moved to another row. If the key is missing or malformed, adding a connector with a secret is refused with a clear message; secret-less connectors still work. Secrets are never returned to the browser, never logged and never sent to a model. - Tools become capabilities. On connect we list tools and store
{name, description, readOnly, inputSchema}. Each becomes aRegisteredToolnamedmcp__<connector>__<tool>:annotations.readOnlyHint === true→ capabilityconnector.read. Runs under the normal policy engine, egress target = the connector host.- anything else → capability
connector.write, which is inSIDE_EFFECT_CAPABILITIES: the fail-closed baseline returnsrequire_approvalfor every call, whatever the workspace policies say.
- Untrusted output. Tool output never reaches a privileged model raw. It is capped (64 KB text, 20 s per call), stored as a PII-redacted artifact for the trace, and read by the quarantined reader (ADR 0002) with a connector extraction schema. The calling agent receives only the typed facts, each with a verbatim quote tagged
untrusted, and the reader'sinjectionSuspectedflag. - Untrusted descriptions (tool-description poisoning). Remote tool descriptions are attacker-controllable text that would otherwise land in a privileged model's tool list. They are normalised (NFKC, control and format characters stripped), truncated to 200 characters and run through the same injection detector as page text; a tool whose name or description trips it is not registered and is shown as "withheld" in settings. Read-only classification never looks at the description.
- Events: each call emits
connector.call{toolCallId, connectorId, server, tool, ok, durationMs, bytes, truncated, summary}with the summary PII-redacted. This is a newRunEventtype (event-type check updated in the migration).
3. Approvals and automation#
executeToolCallgains an optionalapprovalsgate on the run context. When the policy decision isrequire_approval, the runtime emitsapproval.requested, asks the gate, emitsapproval.resolved, and runs the tool only onapproved. Without a gate the call is refused as before.- Pre-approved connector tools.
/stores per-workspace defaults insettings/ connections workspace_settings:auto_approve_plans(persisting the existing toggle) andpreapproved_tools(a list ofmcp__…tool names). The pre-approval gate approves exactly those names, as actor{type: "system", id: "workspace-preapproval"}, so the approval and the precedingpolicy.decided: require_approvalare both in the event log and the audit view. Anything not on the list goes to the human approver, or is refused when there is none. Text in tool output cannot add to the list: the list is read from the database when the run starts. - Human approval for everything else. In web runs the fallback gate is durable, like Ship-a-PR's: it opens an
approvalsrow (steptool, action{kind: "tool", approvalId, tool, argsSummary}), the run page shows an approval card with the tool, the server and the PII-redacted arguments, andPOST /(same origin, cookie session, operator+ throughapi/ runs/ {id}/ tool-approval resolve_approval()) records the decision inaudit_logastool.decision. Unanswered requests are rejected after 10 minutes bysystem:tool-approval-timeout. A rejection is returned to the agent as a refused call; nothing runs. - Where connector tools are used. The custom-goal mission adds a
use_connectorsstep (connector.read,connector.write) when the workspace has connectors; a worker agent getsctx.connectorToolsand a versioned prompt (custom-goal.connectors.v1.md), and each connector result becomes a source of kindconnectorwhose quarantined facts (quotes re-checked against the stored, redacted text) feed synthesis and the critic. The research gap-filler of the public-signal missions does not get connectors: those missions are about public sources, and mixing private workspace data into a public brief would blur its citations. - Resumed runs reload the workspace's connectors and pre-approval list when the durable sweeper resumes a run (ADR 0007).
4. Connect GitHub (user authorisation of the GitHub App)#
- "Connect GitHub" redirects to
https://withgithub.com/ login/ oauth/ authorize client_idand astatethat is an HMAC (session secret) over{workspaceId, userId, nonce, iat}; the nonce is also set as anHttpOnly; SameSite=Laxcookie. The callback (/) accepts the code only when the state verifies, matches the cookie, belongs to the current session and is under 10 minutes old.api/ github/ callback - The code is exchanged for a user access token, used within the request to call
GET /(anduser/ installations GET /foruser/ installations/ {id}/ repositories selectedinstallations), then dropped. We do not store the user token. - The setup URL (
/, GitHub's "Setup URL" with "Redirect on update") receivesapi/ github/ setup installation_id; it is trusted only after the same user-token check proves that the authorising user can access that installation. Then{workspaceId, installationId, account, repositorySelection, repositories}is upserted intogithub_installations(RLS). findInstallationForRepo(workspaceId, owner, repo)returns the installation whose account matchesownerand which coversrepo(all repositories, or the stored selection). Installation-token minting and PR creation belong to Ship-a-PR; this ADR only provides the binding.
Consequences#
- One credential type (API token) for MCP, REST, CLI and the stdio server; revocation is immediate because every request resolves the token in the database.
- Stateless MCP means no server→client notifications and no resumable streams;
wait_for_runis the progress mechanism. That fits every listed client. - Connector read-only classification trusts the server's own
readOnlyHint. A malicious server that marks a write tool read-only can bypass the approval gate for its own tools; the workspace admin chose to add that server, and the egress allowlist still limits it to its own host. Recorded as residual risk T11 indocs/.SECURITY.md repositoriesforselectedinstallations is a snapshot from the last setup redirect; GitHub calls the setup URL again on updates. Ship-a-PR must still handle a 404 from GitHub when the selection has shrunk.- New tables:
api_tokens,mcp_connectors,github_installations,workspace_settings, all with FORCE RLS and admin-only writes. One new event type,connector.call.