-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: auto-select JSON Schema validator based on runtime #1456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
bc4373f to
7c5b80d
Compare
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.
- 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).
e3c7cd4 to
87d0181
Compare
Summary
Add runtime shims that automatically select the appropriate implementations based on execution environment, enabling seamless cross-platform support for:
nodejs_compatrequired)Key Features
Auto-selected JSON Schema Validator
AjvJsonSchemaValidator(unchanged behavior)CfWorkerJsonSchemaValidator(previously required explicit configuration)This eliminates the need for Cloudflare Workers users to explicitly configure the validator - it now "just works".
Runtime-aware
processshim for StdioServerTransportprocess.stdin/process.stdoutStreamableHTTPServerTransportWeb Crypto API for cross-platform compatibility
InMemoryTaskStorenow usescrypto.randomUUID()(Web Crypto API) instead of Node.jscrypto.randomBytes()Implementation
Uses package.json export conditions to resolve the correct implementations at runtime:
Server and Client import from their own
_shimsexport 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):DefaultJsonSchemaValidator→AjvJsonSchemaValidatorrandomBytes→node:cryptoprocess→node:processWorkerd/Browser shims (
shimsWorkerd.ts):DefaultJsonSchemaValidator→CfWorkerJsonSchemaValidatorrandomBytes→ Web Crypto API implementationprocess→ Stub that throws "not supported" errorChanges
_shimsexport to@modelcontextprotocol/serverServerandClientto useDefaultJsonSchemaValidatorfrom shims as defaultStdioServerTransportto importprocessfrom shimsrandomBytesshim using Web Crypto API for Cloudflare Workersprocessstub for non-Node environments with helpful error messagesInMemoryTaskStoreto usecrypto.randomUUID()(Web Crypto API)Test plan
pnpm check:all)AjvJsonSchemaValidatorCfWorkerJsonSchemaValidatornodejs_compat)Migration
No breaking changes. This is backwards compatible:
jsonSchemaValidatorconfiguration (optional)🤖 Generated with Claude Code