Skip to content

perf(core): load MCP client lazily - #42468

Merged
kitlangton merged 1 commit into
v2from
lazy-mcp
Aug 14, 2026
Merged

kitlangton merged 1 commit into
v2from
lazy-mcp

Conversation

@kitlangton

Copy link
Copy Markdown
Contributor

What

Keep the MCP SDK out of startup evaluation when a location has no enabled MCP servers. Configured enabled servers still connect eagerly during location service construction; disabled servers and installations without MCP defer the SDK until connect or OAuth authorization is actually used.

How

  • packages/core/src/mcp/index.ts keeps MCPClient as a type-only import.
  • The MCP client module loads immediately before the existing connect call.
  • OAuth support loads only for authenticated remote connects or interactive authorization.
  • Native ESM module caching deduplicates concurrent first imports; existing per-server locks, scopes, status transitions, reconnects, and finalizers are unchanged.
  • packages/core/test/mcp-import-boundary.test.ts bundles the production entry and asserts that both runtime edges remain dynamic.

Impact

Bundle probe Before entry After entry Entry delta Total output delta
MCP entry, split/minified 460,446 B 152,692 B -307,754 B (-66.8%) +972 B
workerd entry, split/minified 4,739,168 B 4,517,506 B -221,662 B (-4.7%) -70 B
workerd entry, flat/minified 11,607,722 B 11,610,589 B +2,867 B +2,867 B

The flat workerd artifact remains one download, so its benefit is deferred evaluation rather than transfer size. In 20-process local measurements after importing mcp/index.ts, first client import averaged 77.31 ms; OAuth followed by client averaged 58.92 ms because the modules share SDK dependencies. That cost moves to the first enabled connect and is absent for no-MCP/disabled startup.

Scope

This does not make configured enabled servers connect on demand and does not change the MCP service API, config, protocol, server routes, connection policy, failure mapping, reconnect behavior, or shutdown behavior.

Testing

  • bun run test in packages/core: 1,771 passed, 16 skipped
  • bun typecheck in packages/core
  • bun run test test/workerd.test.ts in packages/server
  • bun typecheck in packages/server
  • bun run probe:workerd in packages/server
  • push hook: repository-wide bun turbo typecheck --concurrency=3, 35 tasks passed
  • bun run lint packages/core/src/mcp/index.ts packages/core/test/mcp-import-boundary.test.ts: no errors; six pre-existing warnings in mcp/index.ts

@kitlangton
kitlangton merged commit fec4f20 into v2 Aug 14, 2026
10 checks passed
@kitlangton
kitlangton deleted the lazy-mcp branch August 14, 2026 02:48
jinhuang712 pushed a commit to jinhuang712/opencode that referenced this pull request Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant