bridge — Remote Control Integration

Remote Control Integration

Connects local Claude Code instances to claude.ai cloud sessions. Handles JWT-based authentication, permission delegation to cloud, WebSocket transport for real-time messaging, and session compatibility negotiation.

31
Files
13k
Lines
JWT
Auth
TS
TypeScript

🔵 claude.ai Bridge 🟢 JWT Auth 🟡 WebSocket 🔴 Permission Delegation

📚 What is this?

The bridge module is the secure tunnel between a local Claude Code REPL and the claude.ai cloud platform. It negotiates authentication, relays commands and responses over WebSockets, and delegates permission decisions back to the cloud when the local user grants remote control.

💡 Analogy

Think of the bridge as an embassy between two countries. The JWT token is the diplomatic passport, the WebSocket is the secure phone line, permission callbacks are the consular approvals for sensitive actions, and session compatibility is the protocol ensuring both sides speak the same language version.

Architecture

flowchart LR
  subgraph Local["Local Claude Code"]
    REPL["REPL Session"]
    RB["replBridge.tsx"]
    PC["bridgePermissionCallbacks"]
  end
  subgraph Transport["Transport Layer"]
    WS["WebSocket Client"]
    JWT["jwtUtils.ts"]
  end
  subgraph Cloud["claude.ai Cloud"]
    API["bridgeApi.ts"]
    CSA["codeSessionApi.ts"]
    RS["Remote Session"]
  end
  REPL --> RB
  RB --> WS
  WS --> API
  API --> RS
  RS --> CSA
  JWT --> WS
  PC --> RB
  RS -->|permission request| PC

Data Flow

flowchart TD
  USER["Cloud User Action"] --> CSA["codeSessionApi"]
  CSA --> MSG["JSON Message"]
  MSG --> WS["WebSocket Transport"]
  WS --> JWT["JWT Verify"]
  JWT --> RB["replBridge"]
  RB --> PERM{"Permission\nRequired?"}
  PERM -->|yes| PC["bridgePermissionCallbacks"]
  PC --> APPROVE["User Approve/Deny"]
  APPROVE --> RB
  PERM -->|no| EXEC["Execute Command"]
  RB --> EXEC
  EXEC --> RESP["Response Payload"]
  RESP --> WS

🚶 Step-by-Step Walkthrough: Remote command execution

Trace a command from claude.ai to local execution and back:

  1. Cloud user — Sends a command through the claude.ai web interface.
  2. codeSessionApi — Serializes the command into a JSON envelope with session metadata.
  3. WebSocket — Transmits the envelope over an encrypted WebSocket connection.
  4. jwtUtils.parseToken() — Validates the JWT signature and checks expiration/claims.
  5. replBridge — Deserializes the message and routes it to the appropriate handler.
  6. Permission check — If the command requires elevated access, bridgePermissionCallbacks prompts locally.
  7. Local execution — The REPL processes the command (file edit, shell command, etc.).
  8. Response relay — Results are serialized and sent back over the WebSocket to the cloud.
  9. Session sync — Session state is reconciled between local and cloud representations.
  10. Cloud render — The claude.ai interface displays the command output to the remote user.

Key Classes & Functions

SymbolFilePurpose
BridgeApibridgeApi.tsHTTP client for claude.ai REST endpoints
ReplBridgereplBridge.tsxReact component managing bridge connection state
bridgePermissionCallbacksbridgePermissionCallbacks.tsCallback registry for remote permission prompts
parseJwt()jwtUtils.tsDecodes and validates JWT tokens from cloud
refreshToken()jwtUtils.tsHandles JWT refresh before expiration
CodeSessionApicodeSessionApi.tsSession CRUD operations against cloud API
createWebSocket()wsTransport.tsWebSocket factory with auto-reconnect and backoff
serializeMessage()protocol.tsJSON envelope serializer for bridge protocol
deserializeMessage()protocol.tsMessage parser with schema validation
SessionCompatibilitycompatibility.tsVersion negotiation between local and cloud sessions
BridgeEventEmitterevents.tsTyped event bus for bridge lifecycle events
handleDisconnect()reconnect.tsGraceful disconnect with state preservation

Directory Map

Source tree for bridge
bridge/
├── bridgeApi.ts                 ← HTTP client for cloud REST APIs
├── replBridge.tsx               ← Connection state React component
├── bridgePermissionCallbacks.ts ← Remote permission prompt handlers
├── jwtUtils.ts                  ← JWT decode, validate, refresh
├── codeSessionApi.ts            ← Cloud session CRUD
├── wsTransport.ts               ← WebSocket factory with reconnect
├── protocol.ts                  ← Message serialization/deserialization
├── compatibility.ts             ← Version negotiation logic
├── events.ts                    ← Typed event emitter for lifecycle
├── reconnect.ts                 ← Disconnect handling, state persist
├── types.ts                     ← Shared TypeScript interfaces
├── __tests__/
│   ├── bridgeApi.test.ts
│   ├── jwtUtils.test.ts
│   ├── wsTransport.test.ts
│   └── protocol.test.ts
└── index.ts                     ← Barrel export

Configuration

OptionDefaultDescription
BRIDGE_WS_URLwss://claude.ai/bridgeWebSocket endpoint for bridge connections
JWT_REFRESH_MARGIN_MS60000Refresh token this many ms before expiry
WS_RECONNECT_MAX_RETRIES10Max reconnection attempts before giving up
WS_RECONNECT_BACKOFF_MS1000Base delay for exponential backoff reconnect
PERMISSION_TIMEOUT_MS30000Timeout for local permission prompt before auto-deny