Skip to content

Commit d874ac4

Browse files
committed
fix(mcp-server): respect Vite base in agent module injection
injectAgent always inserted /@id/octane/agent as a root-absolute URL, but Vite serves its /@id/ handler under the configured base prefix. With a non-/ base the script request would 404, so __OCTANE_AGENT__ never appeared and openApp timed out with a misleading error. Detect the base from Vite's @vite/client script tag already present in the transformed HTML, and prefix the agent module URL with it. When no @vite/client tag is found, fall back to /.
1 parent 053b6fb commit d874ac4

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

‎packages/octane-mcp-server/src/agent.js‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
// time from `octane/agent`, so between two operations nothing moves, and the
55
// agent says when time passes (`clock`).
66

7-
const AGENT_MODULE = '/@id/octane/agent';
8-
const AGENT_SCRIPT = `<script type="module" src="${AGENT_MODULE}"></script>`;
7+
const AGENT_MODULE_ID = '@id/octane/agent';
98
const LOG_LIMIT = 500;
109
const BOOT_TIMEOUT_MS = 20_000;
1110

@@ -28,14 +27,14 @@ async function loadPlaywright() {
2827
* time before any app module runs. Module scripts run in document order.
2928
*/
3029
export function injectAgent(html) {
30+
const base = (/src=["']([^"']*?)@vite\/client["']/i.exec(html) ?? [, '/'])[1];
31+
const script = `<script type="module" src="${base}${AGENT_MODULE_ID}"></script>`;
3132
const head = /<head(\s[^>]*)?>/i.exec(html);
3233
if (head)
3334
return (
34-
html.slice(0, head.index + head[0].length) +
35-
AGENT_SCRIPT +
36-
html.slice(head.index + head[0].length)
35+
html.slice(0, head.index + head[0].length) + script + html.slice(head.index + head[0].length)
3736
);
38-
return AGENT_SCRIPT + html;
37+
return script + html;
3938
}
4039

4140
/** A Playwright locator for a target: a test id, an ARIA role and name, text, or a CSS selector. */
@@ -88,7 +87,7 @@ export async function openApp({ url, width = 1280, height = 800, headless = true
8887
});
8988
} catch {
9089
throw new Error(
91-
`${url} did not load ${AGENT_MODULE}. The agent drives a Vite development server ` +
90+
`${url} did not load ${AGENT_MODULE_ID}. The agent drives a Vite development server ` +
9291
'of an app that depends on octane.',
9392
);
9493
}

0 commit comments

Comments
 (0)