Files
next-ai-draw-io/packages/mcp-server/shell/mcp-sync-core.ts
T
Dayuan Jiang 4c7b2cc0c5 feat(mcp-server): the preview is the web app's canvas: same-origin draw.io, version cards, get_selection (#984)
* refactor(canvas): decouple the canvas from the chat engine

The canvas components will be reused by the MCP server's browser shell,
which has no chat and no Next.js. They now read everything they need
from small contexts and the canvas store instead of the chat engine:

- components/canvas/versions-context.tsx: VersionsProvider and
  useVersionsContext give the version cards, the strip and the compare
  dialog a VersionsSource (versions, onCanvasId, undoneId, isBusy,
  canUndo, canRedo, restore, undo, redo). The chat fills it from the
  versions store and the engine in components/chat/chat-versions.tsx.
- stores/canvas-store.ts: isBusy and busyReason; the chat engine sets
  them while a turn runs, SelectionAsk and the version UI read them.
- components/canvas/version-card.tsx: the visual version card and the
  thumbnail, out of tool-activity.tsx; the chat's card composes it and
  adds its "Show XML" link and code panel.
- compare-dialog.tsx and version-strip.tsx move to components/canvas;
  the strip takes the minimum number of versions to show as a prop, the
  chat panel computes it from the cards it has.
- components/canvas/locale-context.tsx: LocaleProvider and useLocale,
  fed by the [lang] layout; CanvasStage no longer uses next/navigation.
  The overlay (SelectionAsk) is a slot and the wait for the saved
  language is a prop, so the shell can leave both out.
- lib/version-text.ts: describeChanges is now describeChangeSummary, so
  it can be imported next to the MCP core's describeChanges.

* test(unit): canvas import boundary and the shared version card

The boundary test scans the canvas modules' imports and bundles the canvas components with esbuild; both fail on anything from components/chat, the tool handlers, next/navigation, next/font, next/script, next/headers or a server-only module. The card test renders VersionCard and VersionStrip on a fake VersionsSource.

* refactor(scripts): pin the draw.io release in one json file and share the zip reader

* feat(mcp-server): bundle a trimmed draw.io into dist/drawio at build time

scripts/fetch-drawio.mjs downloads the pinned draw.war into a cache
(DRAWIO_WAR_CACHE, or DRAWIO_WAR for a local file), checks its SHA-256
and extracts the files named in drawio-files.txt plus a LICENSE with the
Apache-2.0 text. scripts/check-drawio-files.mjs drives the embedded
editor in headless Chromium from a full copy, records every requested
file and checks or rewrites (--update) the list.

* feat(mcp-server): serve the bundled draw.io same-origin behind an api token

GET /drawio/<path> serves dist/drawio with a MIME table, a day of
caching and nosniff; paths are normalized and never reach WEB-INF or
META-INF. The preview embeds /drawio/index.html when the copy exists and
DRAWIO_BASE_URL is unset, else the external draw.io as before (and
start_session says so). Every /api request must carry the per-process
X-Drawio-Token the page gets in its HTML; pages send
frame-ancestors 'self' and nosniff.

* build(mcp-server): require the bundled draw.io in the package and cap the tarball at 40 MB

* fix(mcp-server): review fixes for the preview token and the draw.io static files

- The preview page fetches a fresh copy of itself and retries once when an
  API request is refused with 403: another MCP process, with its own token,
  now answers on this port, and the recovery logic (recoverState) needs its
  polls to go through. The page is sent with Cache-Control: no-store.
- draw.io files are served with an ETag and Cache-Control: no-cache instead
  of a 24 hour max-age: their names do not change between versions, so a
  package upgrade must reach the browser on the next preview. HEAD and
  If-None-Match (304) are answered.
- The file read stream goes through stream.pipeline, so a read error no
  longer ends the MCP process and a client that leaves mid-download no
  longer leaks the file handle.

* fix(mcp-server): review fixes for the draw.io file list and its guard

- The list now ships the templates of Insert > Template, the PlantUML
  parser of Insert > Advanced and the template dialog's icon: the menu
  items were shown but failed with 404s. 15.1 MB packed; the tarball cap
  goes from 40 MB to 20 MB, where it still catches a list that grew by a
  whole js/ directory.
- The guard drives both features, fails when the copy it runs against
  lacks files the editor asked for or when an export does not answer,
  checks that the copy is the pinned draw.io version, and runs in CI
  (npm run check-drawio, one E2E shard) so a list regression cannot reach
  a release.

* docs: review fixes for the draw.io version pointer and the preview's embedding

- The offline deployment guides point at packages/mcp-server/src/
  drawio-version.json for the draw.war version; scripts/fetch-drawio.mjs
  no longer names it.
- The MCP README says what the bundled draw.io copy includes, and that the
  preview page cannot be shown inside an editor's built-in browser since it
  is served with frame-ancestors 'self'.

* refactor(canvas): review fixes for the busy flag, the comparison note and the bundle size check

- busyReason had no reader: the chat engine sets isBusy through the store's
  generic set, like every other flag.
- The isSameDocument comment says the two "same document" rules disagree
  in both directions, so neither is a subset of the other.
- The import boundary test bundles minified with one pako (the MCP core
  resolves its own copy) and caps the canvas core at 160 KB (134 KB now).

* fix(mcp-server): name exports at random and stamp the draw.io version into ETags

- Export requests carried a per-process counter. The preview page retries a
  result refused with 403 against the process that took over the port, so a
  late result of the old process's export could be taken for the new
  process's export with the same number. The id is a random UUID now.
- The ETag of a bundled draw.io file now starts with the version the fetch
  script stamps into dist/drawio/.version. An install that keeps the
  archive's dates (npm does not) would otherwise answer 304 for a changed
  file of the same size after an upgrade.

* docs(mcp-server): mermaid is in the bundled draw.io, which has no image proxy

- Mermaid's converter is in js/extensions.min.js, which the editor loads at
  startup, so the README no longer lists it as left out; the org chart
  layout (js/orgchart.min.js) is the example of a feature that is.
- Note that the bundled copy, like the web app's, has no /drawio/proxy:
  images from other websites, including those in some templates, are left
  out of exports and thumbnails. The guard's comment names this as a known
  limitation it does not cover.

* refactor(canvas): getDrawioSrc takes the editor's source, and CanvasStage can be given one

* feat(mcp-server): canvas shell bundled from the web app's canvas with esbuild

The shell (packages/mcp-server/shell) renders DrawioFrame inside the
shared providers without the chat: its config comes from
window.__MCP_CONFIG__, the four dictionaries ship in the bundle, the
theme is kept under an mcp: localStorage key, and system fonts stand in
for the web fonts. scripts/build-shell.mjs bundles it into dist/shell
with the root's esbuild and Tailwind; check-package caps shell.js at
1.5 MB and shell.css at 300 KB.

* feat(mcp-server): serve the canvas shell at /shell/ behind DRAWIO_PREVIEW_UI

GET /shell/ fills the page template's {{CONFIG_JSON}} with the session,
the API token, where draw.io comes from and the host's editor settings,
with the classic page's security headers; /shell/<file> serves the built
files like the draw.io copy. start_session opens the shell when
DRAWIO_PREVIEW_UI=shell; the classic page stays the default.

* test(mcp-server): open the built shell with playwright

npm run test:e2e starts dist/index.js over the MCP SDK with
DRAWIO_PREVIEW_UI=shell, opens the URL start_session returns in headless
Chromium, and checks that the bundled draw.io loads without console
errors and that the theme toggle switches draw.io in place.

* ci: install the root dependencies before building the mcp package

The shell build needs the root's esbuild, Tailwind and React.

* feat(canvas): DiagramProvider exports with options and shows a transient document

requestExport(request, timeoutMs) runs one draw.io export with the
request's own parameters (a PNG's pageId and width) and resolves with its
answer (data, or xml for format "xml"); null when draw.io does not answer.
showTransient(xml) puts a document on the canvas for an export only: it is
not recorded, its autosaves are ignored, and the next loadDiagram brings
the user back to the page they were on. The MCP's canvas shell answers the
server's export requests with both.

* feat(mcp-server): the shell syncs with the server like the classic page

shell/mcp-sync-core.ts ports preview.js's protocol without the page or
React: polling GET /api/state every 2 s with the state id and version,
pushes of the user's edits with their base version and state, 409 and 413
handling, recovery of a recreated session (the tab's copy goes to History),
the token refresh after a 403, sync and export requests (PNG by page id,
SVG of another page through a transient one-page projection with autosave
ignored, the 600 ms wait for icons), thumbnails for History, and History
reads and restores that name the state the list belongs to.

A server write goes on the canvas as one undo step (loadDiagram commit,
with the change marked) when decideLoad finds it changes only the page on
screen, keeps the file variables and needs no load-only setting; the other
pages are compared with the MCP core's contentFingerprint. draw.io's own
re-serialized copy of such a write is recognized with isSameDocument and
not pushed back as an edit.

shell/use-mcp-sync.ts mounts the sync once inside DiagramProvider, reads
draw.io's autosave and load messages, and reports the connection state,
which the shell's status bar shows; notices come as toasts from the
dictionaries.

* test(mcp-server): port the preview protocol tests to the sync core

The recovery, thumbnail, size limit and History cases of
tests/unit/mcp-preview-recovery.test.ts, driven against mcp-sync-core.ts
with a stubbed server and canvas, plus the stale 409 recovery, an export
request answered once, a projection that ignores edits and restores the
document, the thumbnail of a write loaded before draw.io was ready, writes
taken in place as commits (draw.io's own copy is not pushed), and the
commit-or-load decision table of decideLoad.

* test(mcp-server): e2e of the shell's sync with the server

The shell connects, shows what create_new_diagram draws, marks an
edit_diagram change and takes it back with one Ctrl+Z (which get_diagram
then reflects), pushes a shape inserted in the editor, serves
screenshot_diagram, and shows another page only for its SVG export before
the user's page comes back.

* fix(mcp-server): review fixes for the shell's sync core

A recovery loads the server's diagram in full and waits for draw.io's
load report, as the classic page does: whether the canvas takes a write
in place is decided inside DiagramProvider, and an autosave of the
canvas being replaced went to the server as an edit when the decision
was wrong. A paper size change alone is pushed again: autosaves are
compared as documents only for draw.io's own copy of a committed write.
A projection stays on screen until draw.io reports the restore load
(5 s at most), the whole export ends in 15 s even when its result POST
hangs, and an export in flight is dropped when draw.io starts over. A
refused poll shows the tab offline. With several pages and the page on
screen unknown, a write loads in full. hasLoadOnlySettings and the
highlight of an AI change are shared with the editor bridge.

* fix(mcp-server): review fixes for the preview URL and the theme menu

The stalled-tab note names the page start_session opened (the shell
with DRAWIO_PREVIEW_UI=shell) instead of the classic page. With the
shell not built, start_session opens the classic page and the server
says so at startup, as it does for a missing dist/drawio. BROWSER=none
skips the system browser, as Vite and CRA dev servers do: the e2e tests
set it. Without a fixed DRAWIO_UI, the shell's draw.io URL carries
themes=1 so Extras > Theme is offered, as on the classic page.

* fix(mcp-server): review fixes for the shell's build

tsc -p shell checked nothing: the inherited exclude dropped every shell
file; the shell's tsconfig now excludes node_modules only. Tailwind scans
app-toaster.tsx too, so the sync notices look like the web app's toasts.
The bundle keeps its dependencies' license comments (esbuild appends them
at the end). postcss, which build-shell.mjs loads from the root, is a
declared devDependency.

* ci: run the shell's browser tests on the packed tarball, publish on shared code changes

The e2e job's first shard builds the MCP package, packs it, installs the
tarball in an empty directory and runs packages/mcp-server/tests/e2e
against the installed dist/index.js (MCP_SERVER_ENTRY), so a shell that
builds but fails in the browser, or a tarball missing a runtime
dependency, fails the PR. Its traces go with the Playwright report. The
publish workflow also triggers on the web app code the shell is bundled
from (components, contexts, hooks, lib, stores, globals.css) and the
root lockfile.

* fix(canvas): ignore autosaves of the document a pending full load replaces

An autosave that arrives after loadDiagram sent a full load, and before draw.io reports it, is of the canvas being replaced (draw.io reports each load, in order). Until now it overwrote chartXMLRef: in the MCP shell, a late autosave of a one-page projection replaced the cached document, and a remount of an external draw.io then put the projection on the canvas as the whole document.

* fix(mcp-server): review fixes for the shell's commits and export timeout

A write taken in place that leaves the canvas as it is gets no autosave from draw.io, so the marker for its copy stayed armed and ate the user's next paper-size edit: arm it only when the write changed the document. The 15 s export timeout now retires the export's sequence number, so a result POST that answers late no longer restores the document a second time.

* ci: create the tarball directory before npm pack

npm pack does not create its --pack-destination; on a fresh runner the step failed with ENOENT before the packed shell tests ran.

* feat(mcp-server): history entries name their maker, and /api/history the entry on the canvas

Each History entry records who made it (a user edit, a recovering tab's
copy, a restored copy; none for the AI's writes), when, and its page
count. GET /api/history now also returns each entry's XML, the first entry
with the same content (a restore adds a copy of an older one), the state
the ids belong to, and the entry whose content the canvas shows, by the
rule restore_version uses. The shell's version cards read these; the
classic page keeps using index, id and svg.

* feat(mcp-server): version cards in the shell from the server's History

The shell's VersionsSource (shell/node-versions-source.ts) reads GET
/api/history through the sync and shows one version per distinct content
(a restore's copy folds into the entry it copies; the blank page is none),
numbered as they first appeared, with what changed since the one before.
The entry the server says is on the canvas marks the version; undo and
redo of the newest version restore the one before it, or it again, through
POST /api/restore, so hand edits stay as versions of their own. The cards
sit in a panel beside the canvas (toggle in the header); the strip and
Compare come from the shared canvas components. The sync tells listeners
once per server version, and when History changed without one, so the
list is read again only then.

* test(mcp-server): e2e of the shell's version cards

In a session of its own: three writes give three cards; undoing the newest
restores the one before on the server too, redo brings it back, a hand edit
turns the undo into a restore, and restoring the first version keeps that
edit as a version of its own, which restore_version also finds.

* feat(mcp-server): get_selection reads the cells the user selected in the shell

The server asks the preview tab for the selection the way it asks for an export (a random request id in GET /api/state, the answer in a POST with that id, 10 s to answer). The shell answers through the editor bridge with each cell's id, label, an edge's ends, a shape's geometry and the page on screen; without a same-origin editor it says so, and the tool names the external draw.io. The classic page cannot answer, so the tool says that at once. screenshot_diagram's description now says a page selector renders that page without changing the page on screen (PNG exports by pageId already did).

* test(mcp-server): selection requests, get_selection texts, and page screenshots that leave the view alone

Unit tests for the request plumbing (random id, one answer, timeout), the tool's texts (cells, nothing selected, external draw.io) and the shell's side (one answer per request, after draw.io is up, never during a projection); the wiring test starts a session with BROWSER=none to see the no-tab message. Shell e2e: cells selected in the editor reach get_selection; a PNG of another page differs from the page on screen, which stays.

* feat(mcp-server): the canvas shell is the default preview, the classic page behind DRAWIO_PREVIEW_UI=classic

* docs: the MCP preview is the web app's canvas; get_selection, DRAWIO_BASE_URL and the classic page

* chore(mcp-server): version 0.4.0; check-package requires the draw.io version stamp

* ci: an mcp-shell job builds the package, runs its tests, the draw.io file guard and the shell e2e

* fix(mcp-server): review fixes for the version cards and History

The shell's version cards (shell/node-versions-source.ts):
- a version's change and undo target are the state it replaced, the
  History entry right before its first copy, not the card before it: after
  a restore those differ, and undo went to the wrong version (and not
  where restore_version steps_back=1 goes)
- a card restores the newest copy of its content, as restore_version
  does, so page settings the user changed (a "user" copy) are kept
- a blank page after a drawing is a clear of the canvas, a version of its
  own; only the blank page before any drawing is hidden
- numbers and changes are keyed by content, not by the first copy's id,
  so a version keeps them when its first copy drops out of the server's
  20-entry buffer; the caches start over for another server state (the
  process restarted: entry ids name other content)

The server's History (src/history.ts):
- firstCopyIds compares each entry with the first of every group only: a
  bare model matches any page name, so "same content" is not transitive,
  and a card could show one document and restore another
- the time and pages fields had no reader; pages parsed every XML once
  more on every write

Reading History (src/http-server.ts, shell/mcp-sync-core.ts):
- GET /api/state and a push's answer carry a History key (entry count,
  newest id, the entry on the canvas); the shell reads History again only
  when it changes, so a hand edit no longer downloads every entry's XML and
  thumbnail
- a failed History read is told again at the next poll
- a History list from a state the poll has not seen yet is dropped

* fix(mcp-server): review fixes for get_selection

Server (src/http-server.ts, src/index.ts, src/selection.ts,
src/new-diagram.ts):
- overlapping get_selection calls take turns (readSelection, one slot per
  session as the export slot) instead of replacing each other's request,
  which left one of them with a false "tab not in front" timeout
- an answer is taken only while a request is pending (both ids undefined
  compared equal)
- the tool sees the page start_session actually opens: with dist/shell
  missing the classic page is in use, which never answers, so the tool
  says so instead of timing out
- the result lists at most 100 cells and counts the rest: a whole large
  diagram selected would fill the model's context
- every <diagram> the model sends without an id gets one, so the page id
  the shell reports exists in the server's document

Shell (shell/mcp-sync-core.ts, shell/use-mcp-sync.ts,
contexts/diagram-context.tsx, lib/drawio/editor-bridge.ts):
- an answer whose POST failed is sent again at the next poll
- no answer while a full load has yet to reach the editor: it still shows
  the previous document, whose cells and pages the answer would name
- a hidden tab (the same session open twice) answers a poll later, so the
  tab in front answers first; alone, it still answers within the timeout
- a cell's container is reported by the model's isLayer, not by comparing
  with the default parent, which is the group the user entered

* fix(mcp-server): review fixes for the shell page

- a download button in the header opens the web app's export dialog
  (.drawio, .png, .svg, .drawio.svg), which the classic page had and the
  shell lacked when it became the default
- the shell asks draw.io for the custom library menu (libraries=1), as
  the classic page did; the web app keeps libraries=0
- the newest card no longer shows "Rendering preview" for good: the sync
  takes the thumbnail of a diagram the server recovered from its file
  (saved without pictures) while the canvas kept it, and of a write whose
  picture was skipped because an edit came first, once the canvas shows
  the write again
- e2e: the get_selection test covers a shape in a container the user
  entered; a download test saves a .drawio file

* ci: the version bump reminder watches every path publish-mcp.yml does, and compares the version field

The reminder step left out the root lockfile and scripts/, which change
what is bundled into the shell and trigger the publish workflow; and it
took any change of packages/mcp-server/package.json (a dependency bump)
for a version bump. It now compares the version field between the two
commits.

* fix(mcp-server): get_selection lists every selected id, with detail for the first 100

* fix(mcp-server): a hidden tab's delayed selection answer checks the canvas again first

* fix(mcp-server): the version cards keep only the current History in memory, picture the newest copy, and keep a clear the buffer scrolled to

* fix(mcp-server): save who made each History entry with it, so the cards look the same after a restart

* fix(mcp-server): the download waits for a page export's projection to end, and the dialog's styles are bundled

* fix(mcp-server): bundle open.html and js/open.js, the picker of Open Library from > Browser

* fix(mcp-server): the shell shows the versions once, in the side panel

* test(mcp-server): get_selection wiring test expects the classic page's answer when the shell is not built
2026-10-11 21:27:11 +09:00

965 lines
38 KiB
TypeScript

/**
* The shell's side of the MCP server's state protocol (src/http-server.ts),
* ported from the classic preview page (src/preview/preview.js) without the
* page: the server is reached through fetch, the canvas through the
* SyncCanvas functions the hook (use-mcp-sync.ts) passes in. No React here,
* so the state machine can be driven step by step in tests.
*
* The protocol in short: the tab polls GET /api/state every 2 s and loads a
* newer version; a user edit is pushed with the version and state it is
* based on, and the server refuses (409) a push based on a version the AI
* has since written over, or on a state it has lost (it expired, the
* process restarted); the next poll then decides whose diagram wins
* (recoverState). get_diagram asks for a sync (the canvas as draw.io has
* it) and screenshots and file exports for an image, both through the poll.
*/
import { isSameDocument, sameFileVars } from "@/lib/diagram-diff"
import { hasLoadOnlySettings } from "@/lib/drawio/editor-bridge"
import { contentFingerprint } from "@/packages/mcp-server/src/edit-gate.ts"
import {
normalizeToMxfile,
parseMxfile,
serializeMxfile,
} from "@/packages/mcp-server/src/pages.ts"
import type { SelectionAnswer } from "@/packages/mcp-server/src/selection.ts"
export type ExportFormat = "png" | "svg" | "xmlsvg"
/** GET /api/state as the server answers it */
export interface ServerState {
xml: string | null
version: number
stateId: string | null
blank: boolean
syncRequested: boolean
exportFormat: ExportFormat | null
exportXml: string | null
exportOptions: { width?: number; pageId?: string } | null
exportId: string | null
/** A get_selection request waiting for this tab's answer */
selectionId?: string | null
/** Changes when History or the entry on the canvas does (the version
* cards read History again then); older servers send none */
historyKey?: string
}
/** An entry of GET /api/history */
export interface HistoryEntryInfo {
id: number
index: number
svg: string
xml: string
/** Who made it: "user", "recover", "restore"; null for the AI's writes */
source: string | null
/** The oldest entry with the same content (itself when it is the first) */
firstId: number
}
/** GET /api/history as the server answers it, for the state the list is of */
export interface HistoryList {
entries: HistoryEntryInfo[]
stateId: string | null
/** The entry whose content the canvas shows (the server's rule), or null */
currentId: number | null
}
/**
* How a server write goes on the canvas: as one undo step on the page the
* user is viewing (the editor highlights it and Ctrl+Z takes it back), or
* as a full load of the document (see decideLoad)
*/
export type LoadDecision =
| { mode: "commit"; pageId: string | null }
| { mode: "load" }
export interface ExportRequest {
format: string
[key: string]: unknown
}
export interface ExportResult {
data?: string
xml?: string
}
/** The canvas as the sync drives it (the hook maps it onto DiagramProvider) */
export interface SyncCanvas {
load(xml: string, decision: LoadDecision): void
/** Show a one-page projection for an export only: not recorded, its
* autosaves ignored until the next load */
showTransient(xml: string): void
/** draw.io's export; null when it does not answer in time */
export(
request: ExportRequest,
timeoutMs: number,
): Promise<ExportResult | null>
/** The document on the canvas now ("" before the first load) */
currentXml(): string
/** The page on screen; null when unknown (external draw.io) */
currentPageId(): string | null
/** A full load was sent and draw.io has not reported it yet: the
* editor still shows the previous document */
loadPending(): boolean
/** The cells selected in the editor, for get_selection; `unavailable`
* when the page cannot reach the editor (external draw.io) */
readSelection(): SelectionAnswer
}
export type SyncStatus = "waiting" | "connected" | "offline"
/** Texts shown to the user (the shell takes them from its dictionary) */
export type SyncNotice =
| "tooLarge"
| "unreachable"
| "restoredFromFile"
| "aiChanged"
| "historyChanged"
| "restoreFailed"
export interface SyncOptions {
sessionId: string
/** Prefix of the server's API paths ("/api") */
apiBase: string
/** Every request carries it in the X-Drawio-Token header */
token: string
canvas: SyncCanvas
onNotice: (notice: SyncNotice) => void
onStatus?: (status: SyncStatus) => void
/** The tab is hidden; another tab of this session may be in front */
isHidden?: () => boolean
/** The server made a new state for the session: History entries got
* new ids, so a list on screen is stale */
onStateRecreated?: () => void
/** The token of the process now answering on this port, read from a
* fresh copy of this page; default: tokenFromPage of location.href */
refreshToken?: () => Promise<string | null>
fetch?: typeof fetch
}
export interface McpSync {
/** Poll now and every 2 s */
start(): void
stop(): void
poll(): Promise<void>
/** draw.io is ready for exports (DiagramProvider's isDrawioReady) */
setReady(ready: boolean): void
/** draw.io sent an autosave: the canvas changed to this document */
onAutoSave(xml: string): void
/** draw.io reported a load done */
onDrawioLoad(): void
/** The server's History, with the state its ids belong to; null when
* it could not be read, or the state changed meanwhile (the version
* cards, shell/node-versions-source.ts) */
fetchHistory(): Promise<HistoryList | null>
/** Put a History entry back, naming the state its list belongs to */
restoreEntry(
id: number,
listStateId: string | null,
): Promise<"ok" | "stale" | "failed">
/** Called when the server's diagram or History changed (a write
* loaded, an edit saved, a thumbnail sent, the state recreated): the
* version cards read History again. Returns the unsubscribe */
onServerChange(listener: () => void): () => void
/** For tests and the status bar */
read(): {
stateId: string | null
currentVersion: number
lastXml: string | null
latestXml: string | null
projectionActive: boolean
status: SyncStatus
}
}
export const POLL_INTERVAL_MS = 2000
/** The API token the server wrote into a shell page (window.__MCP_CONFIG__) */
export function tokenFromPage(html: string): string | null {
const match = html.match(/window\.__MCP_CONFIG__ = (\{.*?\});/)
if (!match) return null
try {
const token = JSON.parse(match[1]).token
return typeof token === "string" && token ? token : null
} catch {
return null
}
}
/**
* The same diagram, as text or as documents (draw.io re-serializes what it
* loads: another attribute order, filled-in defaults). Only documents with
* a page or a model are compared as documents: two without any would
* otherwise count as the same.
*/
function sameDiagram(a: string | null, b: string | null): boolean {
if (a === b) return true
if (a === null || b === null) return false
const hasModel = (xml: string) => /<(mxGraphModel|diagram)[\s>]/.test(xml)
return hasModel(a) && hasModel(b) && isSameDocument(a, b)
}
/** SVG as a data URL, as the server stores thumbnails */
function svgDataUrl(svg: string): string {
if (svg.startsWith("data:")) return svg
return `data:image/svg+xml;base64,${btoa(unescape(encodeURIComponent(svg)))}`
}
/**
* Whether a server write can go on the canvas as one undo step: it has the
* same pages as the canvas, changes only the one the user is viewing (the
* one with currentPageId; a one-page document needs no id, several pages
* with the page unknown load in full), keeps the file variables and needs
* no load-only page setting. Pages are compared with the MCP core's
* contentFingerprint (names and cells). The editor bridge checks the same
* against the live editor and falls back to a full load on its own.
*/
export function decideLoad(
currentXml: string,
nextXml: string,
currentPageId: string | null,
): LoadDecision {
const parse = (xml: string) => {
const normalized = normalizeToMxfile(xml)
return normalized ? parseMxfile(normalized) : null
}
const full: LoadDecision = { mode: "load" }
const current = parse(currentXml)
const next = parse(nextXml)
if (!current || !next) return full
if (
!sameFileVars(
current.documentElement.getAttribute("vars"),
next.documentElement.getAttribute("vars"),
)
) {
return full
}
const pagesNow = Array.from(current.querySelectorAll("diagram"))
const pagesNext = Array.from(next.querySelectorAll("diagram"))
if (pagesNow.length === 0 || pagesNow.length !== pagesNext.length) {
return full
}
let index = pagesNow.findIndex(
(page) => page.getAttribute("id") === currentPageId,
)
if (index < 0) {
// The page on screen is unknown (an external draw.io cannot tell):
// with several pages the target cannot be told, load in full
if (pagesNow.length > 1) return full
index = 0
}
const target = pagesNext[index]
// The page is found by its id when the document has several
if (
pagesNow.length > 1 &&
target.getAttribute("id") !== pagesNow[index].getAttribute("id")
) {
return full
}
const targetModel = target.querySelector("mxGraphModel")
if (targetModel && hasLoadOnlySettings(targetModel)) return full
// The other pages, as the MCP core compares them (edit-gate.ts)
const others = (doc: Document, pages: Element[]) => {
pages[index].remove()
return contentFingerprint(serializeMxfile(doc))
}
if (others(current, pagesNow) !== others(next, pagesNext)) return full
return { mode: "commit", pageId: target.getAttribute("id") }
}
export function createMcpSync(options: SyncOptions): McpSync {
const { sessionId, apiBase, canvas, onNotice, onStatus } = options
const fetchFn = options.fetch ?? ((...args) => globalThis.fetch(...args))
let token = options.token
let currentVersion = 0
let isReady = false
// What the server has (the text it sent or we pushed) and the newest
// diagram on the canvas, saved to the server or not
let lastXml: string | null = null
let latestXml: string | null = null
// The server state this tab is in step with; null until the first poll
let stateId: string | null = null
let pushFailing = false
const pushesInFlight: string[] = []
// After a recovery replaced the canvas, until draw.io reports the load:
// an autosave still on its way belongs to the canvas being replaced
let awaitingLoad = false
let awaitingLoadTimer: ReturnType<typeof setTimeout> | null = null
// A write taken in place (a commit) makes draw.io autosave its own copy
// of it, serialized its way: that one autosave is not an edit
let awaitingCommitCopy = false
let pollSeq = 0
let lastHandledPoll = 0 // polls overlap; older answers are dropped
// The edit whose SVG export is pending, and what it was based on
let pendingSvgExport: string | null = null
// The latest thumbnail export of a loaded server write: the state and
// version it showed, and the XML loaded
let thumbExport: {
stateId: string | null
version: number
xml: string
} | null = null
// Its picture was not taken because the user had changed the canvas by
// then: taken when the canvas shows the write again (an undo)
let thumbMissed = false
let pendingMcpExport: ExportFormat | null = null
let mcpExportSeq = 0
let mcpExportId: string | null = null
// A one-page projection is on screen for a page-targeted export
let projectionExportActive = false
// The restore load of the real document was sent; until draw.io reports
// it (or 5 s), the projection still counts as on screen
let projectionRestoreTimer: ReturnType<typeof setTimeout> | null = null
const endProjection = () => {
projectionExportActive = false
if (projectionRestoreTimer) clearTimeout(projectionRestoreTimer)
projectionRestoreTimer = null
}
// Load the server state on the next poll even at the same version
let forceReload = false
let pendingSyncExport = false
let syncExportSeq = 0
// The selection request answered last: the server shows a request until
// the answer arrives, and polls overlap
let answeredSelectionId: string | null = null
let status: SyncStatus = "waiting"
let interval: ReturnType<typeof setInterval> | null = null
const setStatus = (next: SyncStatus) => {
if (status === next) return
status = next
onStatus?.(next)
}
// Listeners of onServerChange, told once per History key seen (the
// server sends it with every state and push answer; a write, a restore
// and the first hand edit change it, further hand edits do not) and
// whenever History changed without one (a rejected edit kept, a
// thumbnail stored)
const changeListeners = new Set<() => void>()
let notedKey: string | null = null
let notedStateId: string | null = null
const emitChange = () => {
for (const listener of changeListeners) listener()
}
const noteHistory = (key: string, sid: string | null) => {
if (key === notedKey && sid === notedStateId) return
notedKey = key
notedStateId = sid
emitChange()
}
// A 403 means another MCP process (with its own token) now answers on
// this port: read its token and retry once, so the next poll can recover
// the session instead of being refused forever
let tokenRefresh: Promise<void> | null = null
const refreshToken = () => {
if (!tokenRefresh) {
const read =
options.refreshToken ??
(() =>
fetchFn(location.href, { cache: "no-store" })
.then((r) => r.text())
.then(tokenFromPage))
tokenRefresh = read()
.then((next) => {
if (next) token = next
})
.catch(() => {})
.finally(() => {
tokenRefresh = null
})
}
return tokenRefresh
}
const api = async (path: string, init: RequestInit = {}) => {
const send = () =>
fetchFn(`${apiBase}${path}`, {
...init,
headers: { ...(init.headers ?? {}), "X-Drawio-Token": token },
})
let r = await send()
if (r.status === 403) {
await refreshToken()
r = await send()
}
return r
}
const postJson = (path: string, body: unknown) =>
api(path, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
})
/** Put a server write on the canvas and take its thumbnail */
const loadFromServer = (xml: string, how: "decide" | "full") => {
lastXml = xml
latestXml = xml
const before = canvas.currentXml()
const decision: LoadDecision =
how === "decide"
? decideLoad(before, xml, canvas.currentPageId())
: { mode: "load" }
canvas.load(xml, decision)
// draw.io autosaves its copy only when the write changed its
// document: after one that left the canvas as it was, the next
// autosave is the user's
awaitingCommitCopy =
decision.mode === "commit" && !sameDiagram(before, xml)
// currentVersion is the write's version
thumbExport = { stateId, version: currentVersion, xml }
thumbMissed = false
setTimeout(captureThumbnail, 500)
}
// The image of the server write on the canvas, for its History entry:
// only for the latest load, once draw.io is ready, and only if the
// canvas still shows it when the export answers
const captureThumbnail = () => {
const t = thumbExport
if (!t || !isReady) return
canvas.export({ format: "svg" }, 5000).then((result) => {
if (!result?.data || thumbExport !== t) return
if (!sameDiagram(latestXml, t.xml)) {
thumbMissed = true
return
}
thumbExport = null
thumbMissed = false
postJson("/history-svg", {
sessionId,
svg: svgDataUrl(result.data),
stateId: t.stateId,
version: t.version,
})
// The write's History entry has its picture now
.then(emitChange)
.catch(() => {})
})
}
// Until draw.io reports the load (its messages come in order), an
// autosave is from the canvas being replaced; in case no report comes,
// not for long
const expectLoad = () => {
awaitingLoad = true
if (awaitingLoadTimer) clearTimeout(awaitingLoadTimer)
awaitingLoadTimer = setTimeout(() => {
awaitingLoad = false
}, 5000)
}
// Restore the user's real document after a page-targeted projection
// export by reloading the server state (it also has any autosave that
// was still in flight when the projection started). projectionExportActive
// stays set until the poll loads the document: an edit on the
// projection before that must not be pushed.
const restoreFromProjection = () => {
if (!projectionExportActive) return
forceReload = true
poll()
}
// source is "sync" for replies to a server sync request, "recover" for
// the tab's copy after the server recovered the session, else "edit".
// sid is the server state the push is based on.
async function pushState(
xml: string,
svg = "",
baseVersion = currentVersion,
source: "edit" | "sync" | "recover" = "edit",
sid = stateId,
): Promise<void> {
if (!sessionId) return
pushesInFlight.push(xml)
try {
const r = await postJson("/state", {
sessionId,
xml,
svg,
baseVersion,
source,
stateId: sid,
})
pushFailing = false
if (r.ok) {
const d = await r.json()
// An answer about a state this tab has left since, or one
// that comes after a newer version was loaded or saved
if (sid !== stateId || d.version < currentVersion) return
currentVersion = d.version
lastXml = xml
noteHistory(d.historyKey ?? String(d.version), stateId)
// The canvas changed while this edit was on its way, to
// something no pending autosave will send (an undo back to
// the previous version): send it now. A sync reply is
// draw.io's export of the canvas, in another format than its
// autosave.
if (
latestXml &&
latestXml !== xml &&
pendingSvgExport !== latestXml &&
source === "edit"
) {
pushState(latestXml)
}
} else if (r.status === 413) {
// Over the server's size limit: the image is most of it, so
// try once without it
if (svg) pushState(xml, "", baseVersion, source, sid)
else onNotice("tooLarge")
} else if (r.status === 409) {
// The AI wrote a newer version, or the server lost the state
// this push was based on; the next poll sorts it out
const d = await r.json().catch(() => ({}))
if (d.savedToHistory) {
onNotice(
source === "recover" ? "restoredFromFile" : "aiChanged",
)
// History grew without a new version
emitChange()
}
poll()
}
} catch (error) {
console.error("Push failed:", error)
setStatus("offline")
if (!pushFailing) {
pushFailing = true
onNotice("unreachable")
}
} finally {
pushesInFlight.splice(pushesInFlight.indexOf(xml), 1)
}
}
// The server made a new state for this session: it expired, or the MCP
// process restarted. Decide whose diagram wins.
const recoverState = (s: ServerState) => {
stateId = s.stateId
options.onStateRecreated?.()
// The old state's pending work is gone with it
const projectionShown = projectionExportActive
endProjection()
forceReload = false
pendingMcpExport = null
pendingSyncExport = false
const mine = latestXml
currentVersion = s.version
if (s.blank || s.xml === lastXml) {
// The server knows nothing, or exactly what this tab last saved:
// the canvas can only be newer, so it wins (edits made while the
// server was down are saved now)
if (projectionShown && mine) {
canvas.load(mine, { mode: "load" })
expectLoad()
}
if (mine && !sameDiagram(mine, s.xml)) {
pushState(mine, "", s.version)
} else if (s.xml && !s.blank && mine) {
// The canvas still shows the diagram the server recovered:
// its History entry came back without a picture (the file
// keeps none), take one now
thumbExport = { stateId, version: currentVersion, xml: s.xml }
thumbMissed = false
captureThumbnail()
}
} else if (s.xml) {
// The server has a diagram this tab never showed (an AI write it
// missed, a saved file): show that, and keep this tab's copy in
// History unless it is the same. A full load, as the classic
// page does: whether the canvas can take the write in place is
// decided inside DiagramProvider, and the sync cannot tell; until
// draw.io reports the load, an autosave is from the canvas being
// replaced
loadFromServer(s.xml, "full")
expectLoad()
if (mine && !sameDiagram(mine, s.xml)) {
pushState(mine, "", s.version, "recover")
}
}
}
const startSyncExport = () => {
pendingSyncExport = true
// The version and state the export is taken at: a newer AI write may
// load meanwhile, and this older XML must not overwrite it
const base = currentVersion
const sid = stateId
const seq = ++syncExportSeq
canvas.export({ format: "xml" }, 5000).then((result) => {
// A late reply to an earlier request was taken at another version
if (seq !== syncExportSeq) return
pendingSyncExport = false
if (result?.xml) pushState(result.xml, "", base, "sync", sid)
})
}
// Handle an export request from the MCP server (png/svg).
//
// Plain export: capture whatever page is on screen (PNG: pageId picks a
// page, width caps the size).
//
// Page-targeted export: the server sends a single-page <mxfile>
// projection in exportXml. It goes on the canvas as a transient
// document, draw.io renders it, exports, then the real document is
// loaded back from the server. The session state is never changed.
const startMcpExport = (s: ServerState, justLoaded: boolean) => {
const format = s.exportFormat as ExportFormat
pendingMcpExport = format
const seq = ++mcpExportSeq
mcpExportId = s.exportId
const extra = s.exportOptions ?? {}
const fire = () => {
const request: ExportRequest =
format === "png"
? {
format: "png",
scale: 2,
currentPage: !extra.pageId,
...extra,
}
: { format }
canvas.export(request, 10000).then((result) => {
// A later export is running by now, or this one ended
if (seq !== mcpExportSeq || pendingMcpExport === null) return
const d = result?.data ?? ""
const isPng = format === "png" && d.startsWith("data:image/png")
const isSvg =
format !== "png" &&
(d.startsWith("data:image/svg") || d.startsWith("<svg"))
const done = () => {
if (seq !== mcpExportSeq) return
pendingMcpExport = null
// Page-targeted export: restore the user's real document
restoreFromProjection()
}
if (!isPng && !isSvg) {
done()
return
}
// Keep pendingMcpExport set until the server has the result:
// a poll answered before that still sees the request and
// would start the same export again
postJson("/state", {
sessionId,
exportData: d,
exportId: mcpExportId,
})
.catch(() => {})
.finally(done)
})
}
if (s.exportXml) {
projectionExportActive = true
canvas.showTransient(s.exportXml)
// Let draw.io render the loaded page before exporting
setTimeout(fire, 600)
} else if (justLoaded) {
// A write with a screenshot: give the new diagram's external
// icon images a moment to load before the PNG is taken
setTimeout(fire, 600)
} else {
fire()
}
// The whole export, result delivery included, ends in time: a result
// POST that never answers must not keep the projection on screen
// past the server's deadline (15 s). Only for this export: a later
// one may be running by then. The export is over: its result POST
// answering after this must not restore the document a second time.
setTimeout(() => {
if (pendingMcpExport && seq === mcpExportSeq) {
mcpExportSeq++
pendingMcpExport = null
restoreFromProjection()
}
}, 15000)
}
async function poll(): Promise<void> {
if (!sessionId) return
const seq = ++pollSeq
try {
const r = await api(
`/state?sessionId=${encodeURIComponent(sessionId)}`,
)
if (!r.ok) {
// Refused (another process answers on this port and its
// token could not be read) or failing: edits are not saved
setStatus("offline")
return
}
const s: ServerState = await r.json()
// An older answer than one already handled (the interval, the
// 409 handler and the projection restore each poll): it could
// name a state that is gone
if (seq < lastHandledPoll) return
lastHandledPoll = seq
setStatus("connected")
if (stateId === null) stateId = s.stateId
else if (s.stateId && s.stateId !== stateId) recoverState(s)
// Load a new version (before an export, so it pictures the
// latest). While a projection is on screen, only the restore
// (forceReload) replaces it, so a new version doesn't fight the
// projection; currentVersion stays unadvanced until then. The
// tab's own push still on its way is not loaded back: the canvas
// may have moved on since (an undo), and its answer follows.
const ownPush = s.xml !== null && pushesInFlight.includes(s.xml)
let justLoaded = false
if (
(forceReload ||
(s.version > currentVersion &&
!projectionExportActive &&
!ownPush)) &&
s.xml
) {
// The restore after a projection is a full load of the
// document the user had
const how = forceReload ? "full" : "decide"
forceReload = false
currentVersion = s.version
loadFromServer(s.xml, how)
// The projection stays on screen until draw.io reports this
// load (its messages come in order): an autosave before that
// is of the projection, not an edit
if (projectionExportActive && !projectionRestoreTimer) {
projectionRestoreTimer = setTimeout(endProjection, 5000)
}
justLoaded = true
}
// Also the tab's own push, and a version a projection holds back
noteHistory(s.historyKey ?? String(s.version), stateId)
// A sync request (get_diagram): after the load above, so draw.io
// exports what it just loaded; never while a one-page projection
// is on screen, which would be sent as the whole document
if (
s.syncRequested &&
!pendingSyncExport &&
isReady &&
!projectionExportActive
) {
startSyncExport()
}
// Nor an export while the projection is still on screen: it
// would picture the projection
if (
s.exportFormat &&
!pendingMcpExport &&
isReady &&
!projectionExportActive
) {
startMcpExport(s, justLoaded)
}
// A selection request (get_selection): the editor's selection
// on the page the user is viewing, once draw.io is up (the
// bridge attaches to it at its first load), not while a
// projection replaces that page, and not while a full load (the
// one above, or one still on its way) has yet to reach the
// editor: it still shows the previous document
if (
s.selectionId &&
s.selectionId !== answeredSelectionId &&
isReady &&
!projectionExportActive &&
!canvas.loadPending()
) {
const id = s.selectionId
answeredSelectionId = id
const answer = () => {
// A hidden tab answers later: the canvas may have moved
// on since the poll (a projection, a full load, draw.io
// starting over). The same conditions as above; when
// they no longer hold, the next poll decides again
if (
!isReady ||
projectionExportActive ||
canvas.loadPending()
) {
if (answeredSelectionId === id) {
answeredSelectionId = null
}
return
}
postJson("/state", {
sessionId,
selectionId: id,
selection: canvas.readSelection(),
})
.then((r) => {
if (!r.ok) throw new Error(String(r.status))
})
.catch(() => {
// Not delivered: the next poll answers it again
if (answeredSelectionId === id) {
answeredSelectionId = null
}
})
}
// A hidden tab lets a tab in front (the same session open
// twice) answer first; the server takes the first answer
// and ignores the later one
if (options.isHidden?.()) {
setTimeout(answer, POLL_INTERVAL_MS + 500)
} else {
answer()
}
}
} catch {
setStatus("offline")
}
}
return {
start() {
if (!sessionId || interval) return
poll()
interval = setInterval(poll, POLL_INTERVAL_MS)
},
stop() {
if (interval) clearInterval(interval)
interval = null
},
poll,
setReady(ready) {
isReady = ready
// draw.io is starting over (an external editor reloads on a
// theme switch): an export in flight gets no answer, and a
// projection on the canvas is gone with the old frame. The next
// poll puts the document back and starts the server's export
// again
if (!ready && pendingMcpExport !== null) {
mcpExportSeq++
pendingMcpExport = null
if (projectionExportActive) forceReload = true
}
// A write loaded before draw.io was ready gets its thumbnail now
if (ready && thumbExport) captureThumbnail()
},
onAutoSave(xml) {
// Ignore autosave while a single-page projection is on screen
// for a page-targeted export; otherwise the projection would be
// pushed as the session's document
if (projectionExportActive) return
// An edit of the canvas that recovery is replacing: kept in
// History, never over the recovered diagram
if (awaitingLoad) {
pushState(xml, "", currentVersion, "recover")
return
}
// Also an edit undone back to what the server has, or draw.io's
// own copy of a write it took in place (a commit). Compared as
// documents only for that copy: a change of the paper size alone
// is the same document to isSameDocument when the server's copy
// names none
latestXml = xml
const commitCopy = awaitingCommitCopy
awaitingCommitCopy = false
// The canvas is back at a write whose picture was not taken (an
// edit came first): take it now
if (
thumbMissed &&
thumbExport &&
sameDiagram(xml, thumbExport.xml)
) {
thumbMissed = false
captureThumbnail()
}
if (xml === lastXml || (commitCopy && sameDiagram(xml, lastXml))) {
return
}
// Request an SVG export, then push the edit with it; remember
// the version and state it is based on. Without an answer in
// time, push it without the image.
pendingSvgExport = xml
const base = currentVersion
const sid = stateId
canvas.export({ format: "svg" }, 2000).then((result) => {
// A later edit took over the slot, or this one was sent
if (pendingSvgExport !== xml) return
pendingSvgExport = null
const svg = result?.data ? svgDataUrl(result.data) : ""
pushState(xml, svg, base, "edit", sid)
})
},
onDrawioLoad() {
awaitingLoad = false
// A commit that fell back to a full load sends no copy
awaitingCommitCopy = false
// The restore load after a projection export is done
if (projectionRestoreTimer) endProjection()
},
async fetchHistory() {
// A list for a state the server recreated meanwhile has old ids
const sid = stateId
// A failed read is told again at the next poll: no other event
// comes for the same History
const failed = () => {
notedKey = null
return null
}
try {
const r = await api(
`/history?sessionId=${encodeURIComponent(sessionId)}`,
)
if (!r.ok) return failed()
const d = await r.json()
if (sid !== stateId) return null
// A list from a state the poll has not seen yet (another
// process answers): its poll tells the cards to read again
if (typeof d.stateId === "string" && d.stateId !== sid) {
return null
}
return {
entries: d.entries ?? [],
stateId: sid,
currentId:
typeof d.currentId === "number" ? d.currentId : null,
}
} catch {
return failed()
}
},
async restoreEntry(id, listStateId) {
try {
const r = await postJson("/restore", {
sessionId,
id,
stateId: listStateId,
})
if (r.ok) {
await poll()
return "ok"
}
if (r.status === 409) {
onNotice("historyChanged")
return "stale"
}
} catch {
// reported below
}
onNotice("restoreFailed")
return "failed"
},
onServerChange(listener) {
changeListeners.add(listener)
return () => {
changeListeners.delete(listener)
}
},
read() {
return {
stateId,
currentVersion,
lastXml,
latestXml,
projectionActive: projectionExportActive,
status,
}
},
}
}