Skip to content

Load the MCP client SDK lazily on first outbound connection - #1747

Merged
RhysSullivan merged 1 commit into
mainfrom
perf/mcp-client-lazy-import
Aug 25, 2026
Merged

Load the MCP client SDK lazily on first outbound connection#1747
RhysSullivan merged 1 commit into
mainfrom
perf/mcp-client-lazy-import

Conversation

@RhysSullivan

Copy link
Copy Markdown
Collaborator

Follow-up to #1742. That patch removed the eager schema preload, but the statically-imported @modelcontextprotocol/client barrel still costs ~11MB of heap plus module-eval CPU per isolate, and @modelcontextprotocol/core (imported for one JSON schema) another ~8.6MB. Post-#1742 traces show the remaining p95 tail is entirely first-request-on-fresh-isolate cost: session verify p50 is 2.5s on isolates whose JWKS cache was empty at start and 0ms otherwise.

  • client-module.ts lazy-loads and memoizes the client barrel + cf-worker validator on first outbound connect, mirroring the existing stdio dynamic import. Isolates that never dial an MCP server never load it.
  • Synchronous error classification (SdkHttpError.isInstance etc.) consults the loader's mcpClientSdkIfLoaded: errors of those classes are only constructed by the loaded module, so "not loaded" soundly means "not an SDK error".
  • The call-tool-result output schema is baked into a generated file (call-tool-result-schema.gen.ts + drift test + regen script), removing the runtime @modelcontextprotocol/core import entirely — it was needed for exactly one schema, including on cached-catalog paths that never connect.

In the built cloud worker, the client chunk is now reachable only through dynamic import (via the execution stack, itself lazy); it was previously in the eager first-request graph.

Verify after deploy: isolate reuse (requests per service.instance.id) up from ~4-5, and cold-isolate local_verify p50 down from ~2.5s in the executor-cloud dataset.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 25, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
executor-cloud e6eaeff Aug 25 2026, 09:50 PM

@github-actions

github-actions Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Cloudflare preview

Torn down — the PR is closed.

@pkg-pr-new

pkg-pr-new Bot commented Aug 25, 2026

Copy link
Copy Markdown

Open in StackBlitz

@executor-js/cli

npm i https://pkg.pr.new/@executor-js/cli@1747

@executor-js/config

npm i https://pkg.pr.new/@executor-js/config@1747

@executor-js/execution

npm i https://pkg.pr.new/@executor-js/execution@1747

@executor-js/sdk

npm i https://pkg.pr.new/@executor-js/sdk@1747

@executor-js/codemode-core

npm i https://pkg.pr.new/@executor-js/codemode-core@1747

@executor-js/runtime-quickjs

npm i https://pkg.pr.new/@executor-js/runtime-quickjs@1747

@executor-js/plugin-file-secrets

npm i https://pkg.pr.new/@executor-js/plugin-file-secrets@1747

@executor-js/plugin-graphql

npm i https://pkg.pr.new/@executor-js/plugin-graphql@1747

@executor-js/plugin-keychain

npm i https://pkg.pr.new/@executor-js/plugin-keychain@1747

@executor-js/plugin-mcp

npm i https://pkg.pr.new/@executor-js/plugin-mcp@1747

@executor-js/plugin-onepassword

npm i https://pkg.pr.new/@executor-js/plugin-onepassword@1747

@executor-js/plugin-openapi

npm i https://pkg.pr.new/@executor-js/plugin-openapi@1747

executor

npm i https://pkg.pr.new/executor@1747

commit: e6eaeff

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
executor-marketing e6eaeff Commit Preview URL

Branch Preview URL
Aug 25 2026, 09:48 PM

@RhysSullivan
RhysSullivan marked this pull request as ready for review August 25, 2026 22:05
@RhysSullivan
RhysSullivan merged commit 91062c2 into main Aug 25, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant