From ec39a511b8e7b1151fd90542c233c1781f5b4b26 Mon Sep 17 00:00:00 2001 From: Flying Web Solutions Date: Mon, 2 Nov 2026 17:58:57 +0000 Subject: [PATCH 1/6] =?UTF-8?q?=F0=9F=90=9B=20fix(mcp):=20keep=20agents=20?= =?UTF-8?q?connected=20to=20the=20editor=20and=20scope=20their=20scripts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Agents reported the Instatic MCP tools "keep disconnecting". The cause was the browser bridge, not the MCP endpoint: - A browser tool failed instantly when the editor tab's stream was in a routine reconnect gap (idle recycle, network blip, restart). Calls now wait up to 15s for the workspace (waitForEditorWorkspace). - Two open editor tabs stole the workspace from each other on every idle recycle, cancelling in-flight calls. Tabs now send a stable tabId and keep their claim across reconnects; superseded tabs stay connected and take over when the active tab closes. - Interrupted calls surfaced the in-app chat message "AI chat stream ended before tool result arrived". The transport now raises a typed BrowserToolInterruptedError recording whether the editor received the call: undelivered calls retry once transparently, delivered ones tell the agent the edit may or may not have applied and to re-read first. Agents now follow the God Mode build practice: single-page JS goes in the new site_write_page_script tool (the page script the JS panel edits, loaded only on that page), and a new script written with site_write_code_asset must declare its runtime.scope instead of silently loading on every page. MCP server instructions, the site system prompt, and docs are updated. Co-Authored-By: Claude Opus 5.5 --- docs/features/agent.md | 7 +- docs/features/god-mode.md | 5 + docs/features/mcp-connectors.md | 18 +- server/ai/mcp/editorBridge.test.ts | 81 +++++++- server/ai/mcp/editorBridge.ts | 177 +++++++++++++++--- server/ai/mcp/handlers/editorBridge.ts | 18 +- server/ai/mcp/instructions.ts | 15 +- server/ai/mcp/server.test.ts | 80 +++++++- server/ai/mcp/server.ts | 127 ++++++++++--- server/ai/mcp/tools/contextTool.ts | 2 +- server/ai/runtime/index.ts | 1 + server/ai/runtime/transport.ts | 44 ++++- server/ai/tools/site/systemPrompt.ts | 6 +- server/ai/tools/site/writeTools.ts | 14 +- .../data/contentCollectionStepUp.test.tsx | 2 +- src/__tests__/agent/contentBridge.test.ts | 2 + src/__tests__/agent/executor.test.ts | 45 ++++- src/__tests__/ai/editorBridgeLease.test.ts | 8 +- .../architecture/agent-tool-surface.test.ts | 4 +- .../architecture/ai-tool-schema-ssot.test.ts | 2 + src/admin/ai/useMcpWorkspaceBridge.ts | 10 +- src/admin/pages/site/agent/codeAssetTools.ts | 78 +++++++- src/admin/pages/site/agent/executor.ts | 8 +- .../pages/site/agent/toolClassification.ts | 1 + .../site/panels/AgentPanel/toolCallDisplay.ts | 2 + src/core/ai/index.ts | 2 + src/core/ai/toolSchemas.ts | 28 ++- 27 files changed, 670 insertions(+), 117 deletions(-) diff --git a/docs/features/agent.md b/docs/features/agent.md index 127d117f1..94c28f383 100644 --- a/docs/features/agent.md +++ b/docs/features/agent.md @@ -454,11 +454,12 @@ Scripts and user stylesheets live in `site.files[]`; runtime targeting and loadi |------------------------|--------------------------------------------|-----------------------------------------|-------------------------------------------------------| | `site_list_code_assets` | `{ type?: 'script' \| 'style' }` | `{ assets }` | List runtime code assets with file ids, paths, full-content hashes, sizes, timestamps, and runtime config | | `site_read_code_asset` | `{ fileId? \| path?, part?, maxChars? }` | `{ fileId, path, type, content, hash, runtime, pageInfo }` | Read an exact script/stylesheet content slice. The `hash` is for the full file; page through with `pageInfo.nextPart` | -| `site_write_code_asset` | `{ path, type, content, runtime?, dependencies? }` | asset summary + `{ action, dependencies }` | Create or replace a runtime script/stylesheet and normalize its runtime config. Existing paths are updated, new paths are created. For module scripts, `dependencies` is a package-name → version/range map added to `site.packageJson.dependencies` | +| `site_write_code_asset` | `{ path, type, content, runtime?, dependencies? }` | asset summary + `{ action, dependencies }` | Create or replace a runtime script/stylesheet and normalize its runtime config. Existing paths are updated, new paths are created. A **new script** must pass `runtime.scope` (`pages`, `templates`, or `all-pages`); without it the call fails and points at `site_write_page_script`, so a script never silently loads on every page. For module scripts, `dependencies` is a package-name → version/range map added to `site.packageJson.dependencies` | +| `site_write_page_script` | `{ pageId?, content, dependencies? }` | asset summary + `{ pageId, action, dependencies }` | Write the page script the God Mode JS panel edits (`findPageScript`): created on first write via `createPageScript` (file + page-only scope in one undo step), replaced afterwards. `pageId` defaults to the active page; a Visual Component document has no page script. The default home for single-page behavior | | `site_patch_code_asset` | `{ fileId? \| path?, expectedHash, replacements }` | asset summary + `{ replacements }` | Apply exact text replacements only when `expectedHash` matches the latest content. Ambiguous matches require a wider `oldText` or explicit `replaceAll:true` | | `site_inspect_code_runtime` | `{ document?: { type, id } }` | `{ pageId, document, scripts, styles }` | Report which runtime scripts/stylesheets apply to the current page/template or supplied page/template document ref | -`site_insert_html` / `site_replace_node_html` intentionally strip `