@flue/runtime: import @modelcontextprotocol/client on the first MCP connection, not at start-up
Describe the Bug
@flue/runtime evaluates @modelcontextprotocol/client when a Worker isolate starts, also when no agent uses an MCP server. dist/mcp-B5fLKxuW.mjs line 2 in 2.2.2 (source packages/runtime/src/mcp.ts) imports it statically, and dist/index.mjs imports that module:
import { Client, SSEClientTransport, StreamableHTTPClientTransport } from "@modelcontextprotocol/client";The SDK's module-scope work (mostly zod schema construction) runs before the first request. The Cloudflare Agents SDK has the same static import (cloudflare/agents#2418).
We measured both changes together with wrangler check startup on a production build of a Flue Worker (Cloudflare target) that uses no MCP server (20 interleaved runs, busy CPU; the machine was under load, so the absolute values are indicative):
| Build | Median | Min | zod, inclusive | GC |
|---|---|---|---|---|
| Current | 171.3 ms | 126.2 ms | 68.6 ms | 52.5 ms |
| MCP SDK loaded on first use | 97.4 ms | 81.0 ms | 30.7 ms | 14.1 ms |
After both changes, no MCP frame is in the start-up profile. With only the Agents SDK change, @modelcontextprotocol/client stays on the start-up path through this import.
Expected Behavior
The MCP client SDK loads on the first MCP connection.
Proposed change
createMcpConnection is already async, so it can import the SDK on first call. createTransport runs after that import. The diff is against the 2.1.1 dist; 2.2.2 has the same import.
--- a/dist/mcp-BnjbTvhd.mjs
+++ b/dist/mcp-BnjbTvhd.mjs
@@ -1,5 +1,5 @@
import { p as registerPreparedToolAdapter } from "./result-D9Q9s-LD.mjs";
-import { Client, SSEClientTransport, StreamableHTTPClientTransport } from "@modelcontextprotocol/client";
+let mcpClientModule;
//#region package.json
var version = "2.1.1";
//#endregion
@@ -43,6 +43,7 @@
* the returned connection when its tools are no longer needed.
*/
async function createMcpConnection(definition) {
+ const { Client } = mcpClientModule ??= await import("@modelcontextprotocol/client");
const url = definition.url instanceof URL ? definition.url : new URL(definition.url);
const requestInit = mergeRequestInit(definition.requestInit, definition.headers);
const transport = createTransport(url, definition.transport ?? "streamable-http", requestInit, definition.fetch, definition.auth === void 0 ? void 0 : createAuthProvider(definition.auth));
@@ -112,6 +113,7 @@
return [...new Set(names)].map((name) => JSON.stringify(name)).join(", ");
}
function createTransport(url, transport, requestInit, fetchImpl, authProvider) {
+ const { SSEClientTransport, StreamableHTTPClientTransport } = mcpClientModule;
if (transport === "sse") return new SSEClientTransport(url, {
requestInit,
fetch: fetchImpl,Steps to Reproduce
- Build a Flue app for the Cloudflare target with one agent and no MCP server.
- Run
wrangler check startupon the built Worker. - The profile has
@modelcontextprotocol/clientmodule frames.
Source: withastro/flue