Bridge protocol
How figma-agent-mcp talks to figma-agent-plugin. All bridge traffic is on localhost.
For the big picture and Mermaid diagrams, see Architecture.
Overview
Default port comes from repo-root bridge.config.json (defaultPort, synced into MCP + plugin UI + manifest.json on build).
Optional MCP-only override: FIGMA_AGENT_MCP_PORT (must match the port baked into the plugin).
Serialization (MessagePack)
Bridge WebSocket frames and leader↔follower POST /rpc bodies use MessagePack (application/msgpack) via msgpackr with useRecords: false.
Why MsgPack
- PNG screenshots travel as MsgPack
bin(Uint8Array/Buffer) — no base64 (~33% size tax) - Large node trees pack denser than JSON
- Same logical message shapes; only the wire format is binary
Screenshot payload
Plugin → MCP:
save_screenshotswritesdatabuffers directly to disk (optional compression)get_screenshotconvertsdatato base64 for agent-facing text output
Roles
If the leader dies, a follower attempts takeover.
Plugin WebSocket
Connect
fileKeyis required (figma.fileKey, or a local fallback for unsaved files).- One active socket per
fileKey(new connection replaces the old one). - Clients must set
binaryType = "arraybuffer".
Heartbeat
Leader sends a MsgPack control object roughly every 30s:
Plugin replies:
These frames have no requestId and must not be treated as tool RPC.
Missing two heartbeat rounds → leader closes with 4002 heartbeat timeout.
Close codes of interest: 4000 missing fileKey, 4001 replaced by newer connection, 4002 heartbeat timeout.
Request (MCP → plugin)
The UI forwards this to the main thread as server-request.
Response (plugin → MCP)
Failure:
Requests time out after 180 seconds on the MCP side.
HTTP (followers / health)
GET /ping
GET /files
POST /rpc
Body (MsgPack of):
Response: MsgPack of { ok: true, data } or { ok: false, error }.
File routing
When several Figma files have the plugin connected, pass fileKey in tool arguments so the leader picks the correct WebSocket. list_files returns the current map.
Stability notes
- Do not log MCP protocol traffic to stdout (reserved for stdio MCP).
- Prefer Figma Desktop; browser tabs may sleep and drop the socket.
- Save the file when possible so
fileKeystays stable. - Upgrade plugin and MCP together — MsgPack binary is required on the bridge path.
