v2: cf workers fix (#1843)
Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com>
This commit is contained in:
parent
df4b6cc88d
commit
7ba58dac51
13 changed files with 48 additions and 31 deletions
|
|
@ -480,7 +480,10 @@ new McpServer(
|
|||
new McpServer({ name: 'server', version: '1.0.0' }, {});
|
||||
```
|
||||
|
||||
Access validators via `_shims` export: `import { DefaultJsonSchemaValidator } from '@modelcontextprotocol/server/_shims';`
|
||||
Access validators explicitly:
|
||||
- Runtime-aware default: `import { DefaultJsonSchemaValidator } from '@modelcontextprotocol/server/_shims';`
|
||||
- AJV (Node.js): `import { AjvJsonSchemaValidator } from '@modelcontextprotocol/server';`
|
||||
- CF Worker: `import { CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/server/validators/cf-worker';`
|
||||
|
||||
## 15. Migration Steps (apply in this order)
|
||||
|
||||
|
|
|
|||
|
|
@ -835,7 +835,8 @@ This means Cloudflare Workers users no longer need to explicitly pass the valida
|
|||
**Before (v1) - Cloudflare Workers required explicit configuration:**
|
||||
|
||||
```typescript
|
||||
import { McpServer, CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/server';
|
||||
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
||||
import { CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/sdk/validation/cfworker';
|
||||
|
||||
const server = new McpServer(
|
||||
{ name: 'my-server', version: '1.0.0' },
|
||||
|
|
@ -858,12 +859,15 @@ const server = new McpServer(
|
|||
);
|
||||
```
|
||||
|
||||
You can still explicitly override the validator if needed. The validators are available via the `_shims` export:
|
||||
You can still explicitly override the validator if needed:
|
||||
|
||||
```typescript
|
||||
// Runtime-aware default (auto-selects AjvJsonSchemaValidator or CfWorkerJsonSchemaValidator)
|
||||
import { DefaultJsonSchemaValidator } from '@modelcontextprotocol/server/_shims';
|
||||
// or
|
||||
import { AjvJsonSchemaValidator, CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/server';
|
||||
|
||||
// Specific validators
|
||||
import { AjvJsonSchemaValidator } from '@modelcontextprotocol/server';
|
||||
import { CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/server/validators/cf-worker';
|
||||
```
|
||||
|
||||
## Unchanged APIs
|
||||
|
|
|
|||
|
|
@ -24,6 +24,10 @@
|
|||
"types": "./dist/index.d.mts",
|
||||
"import": "./dist/index.mjs"
|
||||
},
|
||||
"./validators/cf-worker": {
|
||||
"types": "./dist/validators/cfWorker.d.mts",
|
||||
"import": "./dist/validators/cfWorker.mjs"
|
||||
},
|
||||
"./_shims": {
|
||||
"workerd": {
|
||||
"types": "./dist/shimsWorkerd.d.mts",
|
||||
|
|
@ -67,14 +71,6 @@
|
|||
"pkce-challenge": "catalog:runtimeShared",
|
||||
"zod": "catalog:runtimeShared"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@cfworker/json-schema": "catalog:runtimeShared"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@cfworker/json-schema": {
|
||||
"optional": true
|
||||
}
|
||||
},
|
||||
"devDependencies": {
|
||||
"@modelcontextprotocol/core": "workspace:^",
|
||||
"@modelcontextprotocol/tsconfig": "workspace:^",
|
||||
|
|
|
|||
|
|
@ -162,7 +162,7 @@ export type ClientOptions = ProtocolOptions & {
|
|||
* The validator is used to validate structured content returned by tools
|
||||
* against their declared output schemas.
|
||||
*
|
||||
* @default {@linkcode DefaultJsonSchemaValidator} ({@linkcode index.AjvJsonSchemaValidator | AjvJsonSchemaValidator} on Node.js, {@linkcode index.CfWorkerJsonSchemaValidator | CfWorkerJsonSchemaValidator} on Cloudflare Workers)
|
||||
* @default {@linkcode DefaultJsonSchemaValidator} ({@linkcode index.AjvJsonSchemaValidator | AjvJsonSchemaValidator} on Node.js, `CfWorkerJsonSchemaValidator` on Cloudflare Workers)
|
||||
*/
|
||||
jsonSchemaValidator?: jsonSchemaValidator;
|
||||
|
||||
|
|
|
|||
10
packages/client/src/validators/cfWorker.ts
Normal file
10
packages/client/src/validators/cfWorker.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/**
|
||||
* Cloudflare Workers JSON Schema validator, available as a sub-path export.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/client/validators/cf-worker';
|
||||
* ```
|
||||
*/
|
||||
export { CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/core';
|
||||
export type { CfWorkerSchemaDraft } from '@modelcontextprotocol/core';
|
||||
|
|
@ -4,7 +4,7 @@ export default defineConfig({
|
|||
failOnWarn: 'ci-only',
|
||||
// 1. Entry Points
|
||||
// Directly matches package.json include/exclude globs
|
||||
entry: ['src/index.ts', 'src/shimsNode.ts', 'src/shimsWorkerd.ts', 'src/shimsBrowser.ts'],
|
||||
entry: ['src/index.ts', 'src/shimsNode.ts', 'src/shimsWorkerd.ts', 'src/shimsBrowser.ts', 'src/validators/cfWorker.ts'],
|
||||
|
||||
// 2. Output Configuration
|
||||
format: ['esm'],
|
||||
|
|
|
|||
|
|
@ -137,7 +137,6 @@ export { InMemoryTaskMessageQueue, InMemoryTaskStore } from '../../experimental/
|
|||
export type { StandardSchemaWithJSON } from '../../util/standardSchema.js';
|
||||
export { AjvJsonSchemaValidator } from '../../validators/ajvProvider.js';
|
||||
export type { CfWorkerSchemaDraft } from '../../validators/cfWorkerProvider.js';
|
||||
export { CfWorkerJsonSchemaValidator } from '../../validators/cfWorkerProvider.js';
|
||||
// fromJsonSchema is intentionally NOT exported here — the server and client packages
|
||||
// provide runtime-aware wrappers that default to the appropriate validator via _shims.
|
||||
export type { JsonSchemaType, JsonSchemaValidator, jsonSchemaValidator, JsonSchemaValidatorResult } from '../../validators/types.js';
|
||||
|
|
|
|||
|
|
@ -28,12 +28,11 @@ export * from './validators/fromJsonSchema.js';
|
|||
* Choose a validator based on your runtime environment:
|
||||
*
|
||||
* - {@linkcode AjvJsonSchemaValidator}: Best for Node.js (default, fastest)
|
||||
* Import from: @modelcontextprotocol/sdk/validators/ajv
|
||||
* Requires peer dependencies: ajv, ajv-formats
|
||||
* Bundled — no additional dependencies required.
|
||||
*
|
||||
* - {@linkcode CfWorkerJsonSchemaValidator}: Best for edge runtimes
|
||||
* Import from: @modelcontextprotocol/sdk/validators/cfworker
|
||||
* Requires peer dependency: @cfworker/json-schema
|
||||
* Import from: `@modelcontextprotocol/server/validators/cf-worker` or `@modelcontextprotocol/client/validators/cf-worker`
|
||||
* Bundled — no additional dependencies required.
|
||||
*
|
||||
* @example For Node.js with AJV
|
||||
* ```ts source="./index.examples.ts#validation_ajv"
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ function createDefaultAjvInstance(): Ajv {
|
|||
* const validator = new AjvJsonSchemaValidator(ajv);
|
||||
* ```
|
||||
*
|
||||
* @see {@linkcode CfWorkerJsonSchemaValidator} for an edge-runtime-compatible alternative
|
||||
* @see `CfWorkerJsonSchemaValidator` for an edge-runtime-compatible alternative (import from `@modelcontextprotocol/server/validators/cf-worker` or `@modelcontextprotocol/client/validators/cf-worker`)
|
||||
*/
|
||||
export class AjvJsonSchemaValidator implements jsonSchemaValidator {
|
||||
private _ajv: Ajv;
|
||||
|
|
|
|||
|
|
@ -24,6 +24,10 @@
|
|||
"types": "./dist/index.d.mts",
|
||||
"import": "./dist/index.mjs"
|
||||
},
|
||||
"./validators/cf-worker": {
|
||||
"types": "./dist/validators/cfWorker.d.mts",
|
||||
"import": "./dist/validators/cfWorker.mjs"
|
||||
},
|
||||
"./_shims": {
|
||||
"workerd": {
|
||||
"types": "./dist/shimsWorkerd.d.mts",
|
||||
|
|
@ -62,14 +66,6 @@
|
|||
"dependencies": {
|
||||
"zod": "catalog:runtimeShared"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@cfworker/json-schema": "catalog:runtimeShared"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@cfworker/json-schema": {
|
||||
"optional": true
|
||||
}
|
||||
},
|
||||
"devDependencies": {
|
||||
"@cfworker/json-schema": "catalog:runtimeShared",
|
||||
"@eslint/js": "catalog:devTools",
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ export type ServerOptions = ProtocolOptions & {
|
|||
* The validator is used to validate user input returned from elicitation
|
||||
* requests against the requested schema.
|
||||
*
|
||||
* @default {@linkcode DefaultJsonSchemaValidator} ({@linkcode index.AjvJsonSchemaValidator | AjvJsonSchemaValidator} on Node.js, {@linkcode index.CfWorkerJsonSchemaValidator | CfWorkerJsonSchemaValidator} on Cloudflare Workers)
|
||||
* @default {@linkcode DefaultJsonSchemaValidator} ({@linkcode index.AjvJsonSchemaValidator | AjvJsonSchemaValidator} on Node.js, `CfWorkerJsonSchemaValidator` on Cloudflare Workers)
|
||||
*/
|
||||
jsonSchemaValidator?: jsonSchemaValidator;
|
||||
};
|
||||
|
|
|
|||
10
packages/server/src/validators/cfWorker.ts
Normal file
10
packages/server/src/validators/cfWorker.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/**
|
||||
* Cloudflare Workers JSON Schema validator, available as a sub-path export.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/server/validators/cf-worker';
|
||||
* ```
|
||||
*/
|
||||
export { CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/core';
|
||||
export type { CfWorkerSchemaDraft } from '@modelcontextprotocol/core';
|
||||
|
|
@ -4,7 +4,7 @@ export default defineConfig({
|
|||
failOnWarn: 'ci-only',
|
||||
// 1. Entry Points
|
||||
// Directly matches package.json include/exclude globs
|
||||
entry: ['src/index.ts', 'src/shimsNode.ts', 'src/shimsWorkerd.ts'],
|
||||
entry: ['src/index.ts', 'src/shimsNode.ts', 'src/shimsWorkerd.ts', 'src/validators/cfWorker.ts'],
|
||||
|
||||
// 2. Output Configuration
|
||||
format: ['esm'],
|
||||
|
|
|
|||
Loading…
Reference in a new issue