Skip to content

perf: build era wire schemas lazily through memoized factories - #2476

Merged
felixweinberger merged 2 commits into
mainfrom
fweinberger/lazy-era-schemas
Jul 13, 2026
Merged

felixweinberger merged 2 commits into
mainfrom
fweinberger/lazy-era-schemas

Conversation

@felixweinberger

ghost commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Importing the client or server package currently constructs both frozen protocol-revision wire-schema graphs (2025-11-25 and 2026-07-28) at module evaluation, before any message is validated. This PR moves each revision's schema definitions into a buildSchemas() factory memoized at module level, so the construction cost is paid on first validation instead of at import.

Motivation and Context

A consumer that imports @modelcontextprotocol/client or @modelcontextprotocol/server pays for building ~150 zod schemas per protocol revision, twice over (both revisions), per bundled copy — all before it has connected to anything. For short-lived CLI-style consumers this is pure startup overhead on every invocation.

The fix:

  • Each revision's schema definitions move verbatim into a buildSchemas() factory memoized at module level (the CallToolResultWireSchema wire seam moves verbatim from the 2025 registry into the same factory).
  • The revision registries keep method membership and the exported method lists as static null-valued key objects mapped over the same wire unions — the both-direction drift guards remain compile errors, and importing a registry constructs nothing.
  • Codecs and the input-required maps pull schemas through the memo at first use.
  • Each revision's schemas.ts becomes an eager named-export shim over the memo for tests and tooling; the shim is not on any runtime import path and tree-shakes out of the published bundles.

This eliminates the per-revision schema-construction cost at import (~2 revisions' worth per bundled copy of core-internal). Together with #2458 and #2459, this closes ~55% of a measured consumer import regression and ~96% of its end-to-end startup regression.

How Has This Been Tested?

  • Full package suites pass unchanged: core-internal (1355), server (442), client (721), core; typecheck:all and lint:all clean.
  • The claim that the schemas are unchanged is machine-checked: every zod expression in the two factories was verified token-identical (comments/whitespace-insensitive token-stream comparison) to the corresponding module-level const it replaces — 297 declarations across both revisions, zero differences.
  • Reference identity is covered by the registry pin tests: registry lookups, codecs, and the shim all serve objects from one memo, so the by-reference pins hold under any import order (the pin test now warms the memo via the shim import).
  • Exercised the built packages end-to-end in a scratch consumer: a real client against a real Streamable HTTP server on both protocol revisions — initialize, tools/list, tools/call, malformed-request validation errors, and unknown-method rejection all behave identically; the construction cost observably moves from import to the first validation.

Breaking Changes

None. No public API changes; the schema definitions are token-identical to before; registry lookups keep returning reference-identical schema objects; the exported method lists are unchanged.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

  • The factories are synchronous and memoized (memo ??= build()), so there is no async seam in any validation path and the first-use cost is paid exactly once per process.
  • Membership stays static: the registries' key objects are typed as mapped types over the wire unions, so adding or removing a method without updating the registry is a compile error in both directions.
  • The wire files are behavior-frozen; this change moves their contents verbatim (hence the token-stream check above) rather than editing them.
  • Possible follow-up: extend the existing wire-layer import lint/invariant test to also ban runtime imports of the eager schemas.ts shims from within src/wire, making the laziness invariant mechanical there too.

Changeset: patch for client, server, and server-legacy (their bundles inline core-internal).

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants