Introducing a new way to review your PRs.

Read the announcement

jamaljsr / polar

One-click Bitcoin Lightning networks for local app development & testing

View on GitHub

19 documents · Updated Oct 19, 2026

docs / application-state-and-mcp-tools

Application state and MCP tools

The renderer uses one Easy Peasy store to coordinate the app shell, saved network definitions, live node data, network diagrams, modal forms, and MCP (Model Context Protocol) tools. The store keeps these concerns linked so a user action or an MCP request can update the network, refresh node data, and keep the designer and user interface aligned.

Renderer store

The renderer store is assembled by home/…/index.ts from a root model in src/…/index.ts. Each model owns one state domain and exposes actions for direct changes or thunks for work that needs services, other models, or asynchronous steps. Shared service access is injected into the store, which keeps the same model actions usable by the UI, MCP tools, and tests.

The root model connects these domains:

DomainMain responsibility
appStartup, settings, Docker image data, navigation, notices, and app-level services
networkSaved networks, nodes, network status, node lifecycle, simulations, and auto-mining
bitcoin, lightning, tap, litLive data and operations for the node services
designerThe visible network chart and its active network
modalsForm state and visibility for user actions
mcpMCP tool definitions, dispatch, and execution through the renderer

References: src/…/index.ts• L1-14 src/…/index.ts• L15-41

State domains

Application state

The app model provides state that affects the whole renderer. It loads and saves user settings, tracks Docker versions and images, merges repository image data with user commands, controls navigation, and exposes notices and small app services such as opening a window or clearing proxy data.

Startup is a single sequence. The model loads settings and repository data, loads networks, reads Docker information, optionally checks for image updates, and then marks the app as initialized. A settings change updates the in-memory value and persists it before applying language or theme changes.

References: src/…/app.ts• L31-69 src/…/app.ts• L71-138 src/…/app.ts• L164-170 src/…/app.ts• L238-301

Network state

The network model is the source for the saved topology and lifecycle of each Polar network. A network contains its Bitcoin, Lightning, and Tap nodes, their links to backends or peers, status, and optional simulation settings. The model also owns the actions that create, import, export, rename, add, remove, start, and stop these resources.

Network changes have effects outside the network slice. Adding or importing a network creates or restores its chart. Removing or renaming a node updates the matching live node data and chart. Starting or stopping a network updates status and coordinates Docker with the node-service models. LND wallet initialization, unlock, and restore actions also pass through this model so the node returns to the normal startup path after its wallet becomes usable. Restore can use a seed and an optional channel backup, then checks that the node becomes started before it reports success; recovered channel closures are confirmed in the background when the Bitcoin backend is ready.

References: src/…/network.ts• L64-240 src/…/network.ts• L242-378 src/…/network.ts• L495-651 src/…/network.ts• L755-1030 src/…/network.ts• L1041-1137 src/…/network.ts• L1197-1270 src/…/network.ts• L1288-1363

Node service state

The node-service state is the renderer's live view of running services. The bitcoin model keeps chain and wallet data. The lightning model keeps node information, wallet balances, channels, and channel-balance work. It also exposes the LND wallet state needed by the network model to detect whether a locked wallet has become usable. The tap model keeps Taproot Asset data for each Tap node, including assets, balances, and asset roots. The lit model keeps Lightning Terminal sessions and asset-payment data.

These models do not define the saved network topology. They ask the injected service factories for current data and store the result by node name or network backend identity. Their operations can cross domains: funding uses Bitcoin, asset actions can use Lightning, and successful channel or asset changes refresh the designer.

References: src/…/bitcoin.ts• L12-43 src/…/bitcoin.ts• L46-90 src/…/lightning.ts• L12-110 src/…/lightning.ts• L113-201 src/…/tap.ts• L25-129 src/…/tap.ts• L131-239 src/…/lit.ts• L9-79 src/…/lit.ts• L81-180

Designer state

The designer model is the visual view of a network. It stores a chart for each network, identifies the active chart, and tracks chart selection, position, scale, and the last live-data sync. It turns network nodes into chart nodes and keeps backend, peer, and channel links visible.

The chart is not an independent source of network truth. When a running network is synced, the model reads live Lightning and Tap data, applies that data to the active network chart, and redraws it. Network status changes and node or backend edits also update the chart through root-store actions.

References: src/…/designer.ts• L32-80 src/…/designer.ts• L82-161 src/…/designer.ts• L163-244 src/…/designer.ts• L245-289

The modals model holds short-lived UI context for actions that need user input or show a result. Each modal has a visibility flag and the small set of names, IDs, amounts, links, or returned values needed by its form. The model also has a dedicated unlock context for an LND node that needs wallet setup or access.

Showing a modal may prepare related state before the form appears. For example, asset actions find the linked Lightning node and load its wallet balance, a new-address action asks the designer to sync the network, and an unlock action records the target node. Hiding a modal clears its temporary values and can remove a provisional chart link when the user cancels an operation.

References: src/…/modals.ts• L7-120 src/…/modals.ts• L122-209 src/…/modals.ts• L211-265 src/…/modals.ts• L383-445 src/…/modals.ts• L473-502

MCP tool state

The mcp model is an action boundary rather than a second copy of application state. It groups tool definitions and executors for network, Bitcoin, Lightning, Taproot Asset, and LitD operations, then exposes IPC handlers that let the Electron main process ask the renderer to discover or run a tool.

Each tool resolves its target network and node from the root store and calls the same domain actions used by the UI. Results pass through status serialization so MCP consumers receive status names instead of internal numeric enum values. The registry is the single mapping between a public tool definition and its renderer action.

References: src/…/index.ts• L51-105 src/…/index.ts• L107-167 src/…/toolRegistry.ts• L54-87 src/…/toolRegistry.ts• L87-311 src/…/helpers.ts• L5-55

Cross-domain coordination

Network selection

Selecting a network changes both navigation and the data that the designer and node-service models consider active. The app action sets the active chart, clears cached Bitcoin, Lightning, and Tap data from the prior network, and then moves the router to the selected network view.

This reset prevents live data from one network from appearing while the next network is loading. The selected network remains the durable object in network; the active chart and service data are supporting views of that selection.

References: src/…/app.ts• L276-291 src/…/designer.ts• L82-108 src/…/bitcoin.ts• L46-60 src/…/lightning.ts• L113-125 src/…/tap.ts• L131-185

Network startup

Network startup moves from a saved definition to live node data and then to a usable chart. The network model starts the containers and waits for each service to become ready. It then connects Lightning peers, adds event listeners, and lets the live-data models fill the UI.

An LND node can be reachable while its wallet is still locked. In that case the model marks the node as Locked instead of treating startup as a permanent failure. It checks the wallet state in the background and re-enters the normal startup path when the wallet becomes active. Explicit initialization, unlock, and restore actions use the same readiness path. Restore also validates that the node reaches Started before it reports success. Guards prevent an external-unlock check from racing with an action already resuming the node.

The sequence below shows the main relationship between the state domains. Bitcoin readiness also supports initial mining, while Lightning readiness enables peer and channel updates.

flowchart TD
    A[Saved network] --> B[network model starts services]
    B --> C[Node services become ready]
    D[Restore seed and optional channel backup] --> C
    C --> E{LND wallet state}
    E -->|Active| F[bitcoin, lightning, tap, and lit models fetch live data]
    E -->|Locked| G[Network model waits for wallet access]
    G -->|Unlocked| C
    F --> H[designer model updates the network chart]
    C --> I[Lightning peers and event listeners become active]
    I --> H
    H --> J[Renderer shows current network state]

References: src/…/network.ts• L755-801 src/…/network.ts• L897-1046 src/…/network.ts• L1247-1363 src/…/lightning.ts• L169-201 src/…/designer.ts• L128-161 src/…/network.spec.ts• L1103-1292

Live data refresh

Live data changes through two related paths. A direct operation such as opening a channel or sending an asset waits for the node services and then refreshes the affected data and chart. A mined block also triggers listeners that refresh Lightning and Tap information for started nodes.

The designer limits chart sync frequency because node reads are costly and channel events can arrive close together. This keeps the chart current without making every event start a full refresh. Peer connection work also continues for the other nodes when one node cannot connect.

References: src/…/lightning.ts• L202-314 src/…/lightning.ts• L315-393 src/…/tap.ts• L240-365 src/…/tap.ts• L383-448 src/…/designer.ts• L128-161 src/…/lightning.spec.ts• L151-189

Renderer MCP flow

MCP requests enter the Electron main process through the local HTTP bridge and then cross into the renderer over Electron IPC. The renderer finds the public tool in its registry, runs the matching MCP action, serializes the result, and sends the result back on the response channel.

The flow keeps tool execution inside the same state graph as UI work. A tool that starts a network or changes an asset therefore updates the same network, node-service, designer, and cache state that a user action would update.

sequenceDiagram
    participant Client as MCP client
    participant Bridge as Electron MCP bridge
    participant Renderer as Renderer store
    participant Domain as Domain model and node service

    Client->>Bridge: Discover or execute tool
    Bridge->>Renderer: IPC request with tool name and arguments
    Renderer->>Renderer: Resolve registry entry
    Renderer->>Domain: Run model action
    Domain-->>Renderer: Return result and update state
    Renderer-->>Bridge: IPC response
    Bridge-->>Client: JSON result or error

References: electron/mcpBridge.ts• L57-113 electron/mcpBridge.ts• L140-183 src/…/ipc.ts• L11-47 src/…/ipc.ts• L50-80 src/…/index.ts• L70-75

MCP tool groups

Network operations

Network tools let an MCP client manage the same saved networks and node lifecycle exposed by the renderer. They cover network creation and transfer, node changes, backend links, lifecycle changes, and node command or version queries.

See MCP network operations for the tool contracts and network-specific flow.

References: src/…/index.ts• L24-42 src/…/toolRegistry.ts• L87-165

Taproot Asset operations

Taproot Asset tools use the Tap state and the linked Lightning and Bitcoin models to inspect assets, create addresses, mint or send assets, sync universes, and work with asset channels. Their results can require confirmation, balance refresh, or a chart sync before the renderer shows the new state.

See MCP Tap asset operations for the asset tool contracts and operation flow.

References: src/…/index.ts• L43-49 src/…/index.ts• L88-100 src/…/toolRegistry.ts• L234-289 src/…/tap.ts• L240-422

Bitcoin and Lightning operations

Bitcoin and Lightning tools expose the node-service actions that support chain progress, wallets, channels, invoices, and payments. These actions use network state to locate nodes and then update live service data or the designer when the operation changes the topology.

References: src/…/index.ts• L1-6 src/…/index.ts• L13-20 src/…/toolRegistry.ts• L166-233 src/…/bitcoin.ts• L61-90 src/…/lightning.ts• L169-314

Change checklist

When these files or sections are changed, remember to consider these:

  • When a root model is added or renamed, update the root model type, model assembly, and any MCP action type that resolves actions through RootModel.
  • When a network or node identity changes, check the matching live-data key and chart node ID. The domains use node names for most live data and a network-backend identity for Bitcoin data.
  • When a lifecycle action changes readiness or status, keep the network status, node-service refresh, event listeners, and chart status update in step.
  • When locked-wallet handling changes, keep explicit initialization, unlock, or restore actions, external wallet-state checks, and the Locked to Started transition aligned.
  • When wallet restore changes, keep seed and channel-backup handling, recovery timing, backend readiness, recovered-channel confirmation, and follow-up mining aligned.
  • When an MCP tool is added or renamed, update its definition, executor registry entry, model export, and IPC discovery path so the public name and renderer action remain aligned.
  • When a tool result adds status fields or nested response data, check MCP serialization so clients do not receive internal status values.
  • When a modal gains or loses context fields, check the action that opens it and the action that clears provisional chart or form state.

Important failure modes

TriggerConsequenceGuard
A selected network has no chart entryThe active chart cannot be selected or updatedCreate or restore the chart before setting the active network ID
A network or node name is missing or staleDomain actions cannot resolve the target node and the operation failsResolve targets through the network model and validate the name before calling a node service
A node service is not ready when live data is requestedWallet, channel, or asset reads fail and the UI may keep old dataWait for node readiness and refresh only started nodes
An LND wallet remains locked after service startupThe node stays unavailable for normal reads and actionsKeep the node in Locked state and poll wallet state until it becomes active
A locked-wallet poller races with an explicit init, unlock, or restore actionA stale result can start readiness work twice or restore an old statusAssociate checks with the current node and poller, and suppress them while the node is resuming
A wallet restore does not bring the node to StartedThe restore action reports failure even if wallet setup completedCheck the node status after the shared startup monitor finishes
Recovered channel outputs are not confirmed before the recovery wait endsSome force-close transactions may remain unconfirmed and only confirmed outputs are minedUse the bounded confirmation wait, then mine any outputs already confirmed
A running network is changed without the related chart or live-data updateThe saved topology and visible diagram disagreeRoute cross-domain changes through network actions that update the designer and service slices
A peer connection fails for one Lightning nodeLater nodes may not receive peer connectionsHandle peer connection errors per node and continue the sweep
An MCP tool name is absent from the registryTool discovery can omit the tool or execution returns an unknown-tool errorKeep the public definition and executor in the central registry
An MCP IPC listener is registered more than onceOne request can run more than once or receive competing responsesRemove the existing MCP listeners before registering them during store setup
An MCP operation exceeds the bridge timeoutThe caller receives a timeout even if the renderer continues workKeep long-running work within the request contract and return clear errors from the action

Key files

FileWhy to read it
src/…/index.tsShows store creation, injected services, typed hooks, and MCP listener setup
src/…/index.tsShows how the renderer domains form one root model
src/…/app.tsExplains startup, settings, navigation, notices, and app-wide effects
src/…/network.tsExplains saved networks, node lifecycle, wallet readiness and restore, persistence, and cross-domain changes
src/…/bitcoin.tsShows the live Bitcoin view and chain or wallet operations
src/…/lightning.tsShows live Lightning data, wallet readiness checks, channel operations, event refresh, and chart coordination
src/…/tap.tsShows Taproot Asset state and its links to Lightning and Bitcoin actions
src/…/lit.tsShows Lightning Terminal sessions and asset-payment coordination
src/…/designer.tsExplains chart state, live synchronization, and topology link updates
src/…/modals.tsExplains temporary UI context, LND unlock context, and modal side effects
src/…/index.tsLists the MCP model surface and tool groups available in the renderer
src/…/toolRegistry.tsShows the public tool-to-action mapping and tool discovery set
src/…/ipc.tsExplains renderer-side MCP discovery and execution handling
electron/mcpBridge.tsExplains the main-process HTTP-to-IPC boundary for MCP clients