Skip to content

Conversation

@mattzcarey
Copy link
Contributor

@mattzcarey mattzcarey commented Feb 3, 2026

Summary

Add runtime shims that automatically select the appropriate implementations based on execution environment, enabling seamless cross-platform support for:

  • Node.js: Full functionality with native modules
  • Cloudflare Workers (workerd): Web API-based implementations (no nodejs_compat required)
  • Deno: Works via npm compatibility with Node.js shims
  • Bun: Works with Node.js shims

Key Features

Auto-selected JSON Schema Validator

  • Node.js/Deno/Bun: Uses AjvJsonSchemaValidator (unchanged behavior)
  • Cloudflare Workers: Uses CfWorkerJsonSchemaValidator (previously required explicit configuration)

This eliminates the need for Cloudflare Workers users to explicitly configure the validator - it now "just works".

Runtime-aware process shim for StdioServerTransport

  • Node.js/Deno/Bun: Uses native process.stdin/process.stdout
  • Cloudflare Workers: Throws helpful error directing users to use StreamableHTTPServerTransport

Web Crypto API for cross-platform compatibility

  • InMemoryTaskStore now uses crypto.randomUUID() (Web Crypto API) instead of Node.js crypto.randomBytes()
  • Works in all environments without runtime-specific code

Implementation

Uses package.json export conditions to resolve the correct implementations at runtime:

"./_shims": {
  "workerd": { "import": "./dist/shimsWorkerd.mjs" },
  "browser": { "import": "./dist/shimsWorkerd.mjs" },
  "node": { "import": "./dist/shimsNode.mjs" },
  "default": { "import": "./dist/shimsNode.mjs" }
}

Server and Client import from their own _shims export using self-referencing imports, which are kept external during bundling so the runtime resolver picks the correct variant.

Shim exports

Node.js shims (shimsNode.ts):

  • DefaultJsonSchemaValidatorAjvJsonSchemaValidator
  • randomBytesnode:crypto
  • processnode:process

Workerd/Browser shims (shimsWorkerd.ts):

  • DefaultJsonSchemaValidatorCfWorkerJsonSchemaValidator
  • randomBytes → Web Crypto API implementation
  • process → Stub that throws "not supported" error

Changes

  • Add _shims export to @modelcontextprotocol/server
  • Update Server and Client to use DefaultJsonSchemaValidator from shims as default
  • Update StdioServerTransport to import process from shims
  • Add randomBytes shim using Web Crypto API for Cloudflare Workers
  • Add process stub for non-Node environments with helpful error messages
  • Update tsdown configs to build shim entry points and keep self-references external
  • Add integration tests for Cloudflare Workers, Deno, and Bun runtimes
  • Update InMemoryTaskStore to use crypto.randomUUID() (Web Crypto API)

Test plan

  • All existing tests pass (pnpm check:all)
  • Verified Node.js correctly resolves to AjvJsonSchemaValidator
  • Verified Cloudflare Workers (wrangler) correctly resolves to CfWorkerJsonSchemaValidator
  • Added integration test for Cloudflare Workers (no nodejs_compat)
  • Added integration test for Deno runtime
  • Added integration test for Bun runtime
  • Build succeeds with correct output files

Migration

No breaking changes. This is backwards compatible:

  • Node.js users: No change needed
  • Cloudflare Workers users: Can remove explicit jsonSchemaValidator configuration (optional)
  • Deno/Bun users: Should work out of the box

🤖 Generated with Claude Code

@mattzcarey mattzcarey requested a review from a team as a code owner February 3, 2026 10:22
@changeset-bot
Copy link

changeset-bot bot commented Feb 3, 2026

⚠️ No Changeset found

Latest commit: 530f8a5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 3, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1456

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1456

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1456

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1456

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1456

commit: 530f8a5

@mattzcarey mattzcarey force-pushed the feat/runtime-shims branch 2 times, most recently from bc4373f to 7c5b80d Compare February 3, 2026 11:18
Add runtime shims that automatically select the appropriate JSON Schema
validator based on the execution environment:
- Node.js: Uses AjvJsonSchemaValidator (unchanged from before)
- Cloudflare Workers (workerd): Uses CfWorkerJsonSchemaValidator

This eliminates the need for Cloudflare Workers users to explicitly
configure the validator. The SDK now uses package.json export conditions
to resolve the correct validator at runtime.

Changes:
- Add `_shims` export to core, server, and client packages
- Update Server and Client to import DefaultJsonSchemaValidator from shims
- Use self-referencing package imports for runtime resolution
- Document the enhancement in migration guide
Move runtime shims from server and client packages to just core:
- Server and client now import from @modelcontextprotocol/core/_shims
- Removed redundant shim files from server and client packages
- Updated migration docs to reference core/_shims import path

This simplifies the shim structure - there's now only one place
to define the runtime-specific validators.
Since @modelcontextprotocol/core is a private package and not published,
we cannot keep core/_shims as an external import in server/client bundles.
Instead, bundle the shims directly so the packages work standalone.
Since core is a private package, shims must be in server/client
for runtime selection to work. Core still exports the validators
as barrel exports for direct use.

- Add shimsNode.ts and shimsWorkerd.ts to server and client
- Remove shims directory from core
- Update package.json exports with runtime conditions
- Server/client import DefaultJsonSchemaValidator from their own _shims
- Add randomBytes shim to shimsNode.ts (re-exports from node:crypto) and
  shimsWorkerd.ts (Web Crypto API implementation)
- Update inMemory.ts to import randomBytes from server shims
- Separate Node.js-specific transports into barrel exports:
  - @modelcontextprotocol/server/stdio for StdioServerTransport
  - @modelcontextprotocol/server/streamableHttp for StreamableHTTPServerTransport
- Remove node:util TextEncoder import (globally available)
- Add Cloudflare Workers integration test (no nodejs_compat required)
- Update examples and tests to use new import paths
- StdioServerTransport is now at @modelcontextprotocol/server/stdio
- StreamableHTTPServerTransport (web standard) at @modelcontextprotocol/server/streamableHttp
- Add browser export condition to client package.json
- Add integration tests for Cloudflare Workers, Bun, and Deno runtimes
The @default tag sufficiently documents the behavior.
KKonstantinov
KKonstantinov previously approved these changes Feb 3, 2026
- Remove @modelcontextprotocol/server/stdio and /streamableHttp entry points
- Export StdioServerTransport and WebStandardStreamableHTTPServerTransport from main entry
- Add process shim that throws in non-Node environments (stdio not supported)
- Fix TextEncoder type issue (use InstanceType<typeof TextEncoder>)
- Update migration docs to reflect simplified imports
- Move tests to test/integration/test/server/
- Test MCP server creation and tool registration
- Remove complex MCP protocol session handling
- Tests verify runtime compatibility without stateful sessions
- Move InMemoryTaskStore to use Web Crypto API (crypto.randomUUID())
  instead of Node.js crypto.randomBytes()
- Keep InMemoryTaskStore in core package as a test fixture/demo
- Server re-exports from core via main index.ts
- Rename runtime test files to camelCase per lint rules
- Fix lint issues in runtime tests
- Use /mcp endpoint with WebStandardStreamableHTTPServerTransport
- Remove /health and /test endpoints
- Test actual MCP client-server communication
- Minimal server code: just register tool and handle requests
- Create runtimeTest.ts helper with common setup/teardown logic
- Standardize server source generation with MCP_SERVER_SETUP template
- Add testMcpConnection helper for actual MCP protocol testing
- Simplify bunRuntime, denoRuntime, and cloudflareWorkers tests
- Add wrangler to pnpm-workspace.yaml devTools catalog
Tests that spawn child processes (processCleanup, runtime tests) were
failing intermittently due to parallel execution causing resource
conflicts. Running test files sequentially ensures stable results.
- Remove unnecessary exports from core package.json (private package)
- Fix prettierignore path in middleware packages (../../ -> ../../../)
Required for runtime compatibility tests (bunRuntime, denoRuntime).
@mattzcarey mattzcarey merged commit bc883af into main Feb 3, 2026
14 checks passed
@mattzcarey mattzcarey deleted the feat/runtime-shims branch February 3, 2026 19:22
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.

3 participants