Skip to content

Commit cc96aa0

Browse files
committed
refactor: remove duplicated RS auth helpers from express package
The requireBearerAuth, mcpAuthMetadataRouter, getOAuthProtectedResourceMetadataUrl, and OAuthTokenVerifier exports were added to @modelcontextprotocol/express as backward-compat shims (PR #1907) but are now superseded by @modelcontextprotocol/server-legacy/auth. All consumers updated.
2 parents 71d03ac + 16d13ab commit cc96aa0

67 files changed

Lines changed: 805 additions & 913 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.changeset/cfworker-out-of-barrel.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
'@modelcontextprotocol/client': patch
44
---
55

6-
Stop bundling `@cfworker/json-schema` into the main package barrel. Previously `CfWorkerJsonSchemaValidator` was re-exported from the core internal barrel, so tsdown inlined the `@cfworker/json-schema` dev dependency into every consumer's bundle even when it was never used. The validator is now reachable only via the `_shims` conditional (workerd/browser) and the explicit `@modelcontextprotocol/{server,client}/validators/cf-worker` subpath, so consumers that don't opt into it no longer ship that code. No public API change.
6+
Stop bundling `@cfworker/json-schema` into the main package barrel. Previously `CfWorkerJsonSchemaValidator` was re-exported from the core internal barrel, so tsdown inlined the `@cfworker/json-schema` dependency into every consumer's bundle even when it was never used. The named validator classes are now reachable only via the explicit `@modelcontextprotocol/{client,server}/validators/{ajv,cf-worker}` subpaths and the runtime `_shims` conditional, so consumers that import only from the root entry point no longer ship the validator dep.

.changeset/support-standard-json-schema.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ server.registerTool('greet', {
2121
For raw JSON Schema (e.g. TypeBox output), use the new `fromJsonSchema` adapter:
2222

2323
```typescript
24-
import { fromJsonSchema, AjvJsonSchemaValidator } from '@modelcontextprotocol/core';
24+
import { fromJsonSchema } from '@modelcontextprotocol/server';
2525

2626
server.registerTool('greet', {
27-
inputSchema: fromJsonSchema({ type: 'object', properties: { name: { type: 'string' } } }, new AjvJsonSchemaValidator())
27+
inputSchema: fromJsonSchema({ type: 'object', properties: { name: { type: 'string' } } })
2828
}, handler);
2929
```
3030

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'@modelcontextprotocol/core': minor
3+
'@modelcontextprotocol/client': patch
4+
'@modelcontextprotocol/server': patch
5+
---
6+
7+
Bundle automatic JSON Schema validator defaults in `@modelcontextprotocol/client` and `@modelcontextprotocol/server` runtime shims.
8+
9+
Client and server pick the right validator automatically based on the runtime: the Node shim uses AJV, the browser/workerd shim uses `@cfworker/json-schema`. Both backends are bundled into the shim chunks that select them, so the default code path needs no extra installs — `import { McpServer } from '@modelcontextprotocol/server'` does not pull `ajv` or `@cfworker/json-schema` into the root entry chunk.
10+
11+
The named validator classes remain part of the public surface for consumers who want to customize the built-in backend (pre-register schemas by `$id`, register custom AJV formats, switch dialects, change `@cfworker/json-schema` draft). They are exposed through explicit subpaths so they do not bloat the root index chunk:
12+
13+
- `import { AjvJsonSchemaValidator } from '@modelcontextprotocol/{client,server}/validators/ajv'`
14+
- `import { CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/{client,server}/validators/cf-worker'`
15+
16+
Importing from one of these subpaths means the corresponding peer dep (`ajv` + `ajv-formats`, or `@cfworker/json-schema`) must be in your `package.json`. The shim keeps its own vendored copy for the default path, so a project can use the subpath in some files and rely on the default in others.
17+
18+
The `jsonSchemaValidator` interface remains the public extension point for replacing validation entirely with a custom implementation.

docs/faq.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ The [server quickstart](./server-quickstart.md) walks you through building a wea
7171

7272
### Where are the server auth helpers?
7373

74-
Resource Server helpers (`requireBearerAuth`, `mcpAuthMetadataRouter`, `OAuthTokenVerifier`) are first-class in `@modelcontextprotocol/express`. The Authorization Server helpers (`mcpAuthRouter`, `ProxyOAuthServerProvider`, etc.) have been removed from the core SDK; new code should use a dedicated IdP/OAuth library. Example packages provide a demo with `better-auth`.
74+
All auth helpers (`requireBearerAuth`, `mcpAuthMetadataRouter`, `OAuthTokenVerifier`, `mcpAuthRouter`, `ProxyOAuthServerProvider`, etc.) are available from `@modelcontextprotocol/server-legacy/auth` (deprecated, frozen v1 copy). New code should use a dedicated IdP/OAuth library. Example packages provide a demo with `better-auth`.
7575

7676
### Why did we remove `server` SSE transport?
7777

docs/migration-SKILL.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Replace all `@modelcontextprotocol/sdk/...` imports using this table.
5454
| `@modelcontextprotocol/sdk/server/stdio.js` | `@modelcontextprotocol/server/stdio` |
5555
| `@modelcontextprotocol/sdk/server/streamableHttp.js` | `@modelcontextprotocol/node` (class renamed to `NodeStreamableHTTPServerTransport`) OR `@modelcontextprotocol/server` (web-standard `WebStandardStreamableHTTPServerTransport` for Cloudflare Workers, Deno, etc.) |
5656
| `@modelcontextprotocol/sdk/server/sse.js` | REMOVED (migrate to Streamable HTTP); legacy bridge: `@modelcontextprotocol/server-legacy/sse` |
57-
| `@modelcontextprotocol/sdk/server/auth/*` | RS helpers (`requireBearerAuth`, `mcpAuthMetadataRouter`, `OAuthTokenVerifier`) → `@modelcontextprotocol/express`; AS helpers removed (use external IdP/OAuth library); legacy bridge: `@modelcontextprotocol/server-legacy/auth` |
57+
| `@modelcontextprotocol/sdk/server/auth/*` | RS + AS helpers (`requireBearerAuth`, `mcpAuthMetadataRouter`, `OAuthTokenVerifier`, `mcpAuthRouter`, etc.) → `@modelcontextprotocol/server-legacy/auth` (deprecated, frozen v1 copy); migrate to an external IdP/OAuth library |
5858
| `@modelcontextprotocol/sdk/server/middleware.js` | `@modelcontextprotocol/express` (signature changed, see section 8) |
5959

6060
### Types / shared imports
@@ -332,7 +332,7 @@ new URL(ctx.http?.req?.url).searchParams.get('debug')
332332

333333
### Server-side auth
334334

335-
Resource Server helpers (`requireBearerAuth`, `mcpAuthMetadataRouter`, `getOAuthProtectedResourceMetadataUrl`, `OAuthTokenVerifier`) are first-class in `@modelcontextprotocol/express`. Authorization Server helpers (`mcpAuthRouter`, `OAuthServerProvider`, `ProxyOAuthServerProvider`, `authenticateClient`, `allowedMethods`, etc.) are removed from the core SDK; use an external IdP/OAuth library. See `examples/server/src/` for demos. Legacy bridge: `import { mcpAuthRouter } from '@modelcontextprotocol/server-legacy/auth'` (deprecated, frozen v1 copy).
335+
All auth helpers (`requireBearerAuth`, `mcpAuthMetadataRouter`, `getOAuthProtectedResourceMetadataUrl`, `OAuthTokenVerifier`, `mcpAuthRouter`, `OAuthServerProvider`, `ProxyOAuthServerProvider`, `authenticateClient`, `allowedMethods`, etc.) are available from `@modelcontextprotocol/server-legacy/auth` (deprecated, frozen v1 copy). Migrate to an external IdP/OAuth library for production use. See `examples/server/src/` for demos.
336336

337337
### Host header validation (Express)
338338

@@ -509,8 +509,8 @@ Type changes in handler context:
509509

510510
The SDK now auto-selects the appropriate JSON Schema validator based on runtime:
511511

512-
- Node.js → `AjvJsonSchemaValidator` (no change from v1)
513-
- Cloudflare Workers (workerd) → `CfWorkerJsonSchemaValidator` (previously required manual config)
512+
- Node.js → AJV (no change from v1)
513+
- Cloudflare Workers (workerd) → `@cfworker/json-schema` (previously required manual config)
514514

515515
**No action required** for most users. Cloudflare Workers users can remove explicit `jsonSchemaValidator` configuration:
516516

@@ -527,11 +527,12 @@ new McpServer(
527527
new McpServer({ name: 'server', version: '1.0.0' }, {});
528528
```
529529

530-
Access validators explicitly:
530+
Validator behavior:
531531

532-
- Runtime-aware default: `import { DefaultJsonSchemaValidator } from '@modelcontextprotocol/server/_shims';`
533-
- AJV (Node.js): `import { AjvJsonSchemaValidator } from '@modelcontextprotocol/server';`
534-
- CF Worker: `import { CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/server/validators/cf-worker';`
532+
- Do not add validator imports for normal migrations.
533+
- Do not install `ajv`, `ajv-formats`, or `@cfworker/json-schema` for the default path; client/server bundle the runtime-selected defaults and the root entry point does not pull either dep in.
534+
- To customize the built-in backend (e.g. register custom AJV formats, change `@cfworker/json-schema` draft), import the named class from the package subpath: `@modelcontextprotocol/{client,server}/validators/ajv` for `AjvJsonSchemaValidator`, `@modelcontextprotocol/{client,server}/validators/cf-worker` for `CfWorkerJsonSchemaValidator`. Importing from a subpath means the corresponding peer dep must be in your `package.json`.
535+
- To replace validation entirely, pass `jsonSchemaValidator: myCustomValidator` with your own implementation of the `jsonSchemaValidator` interface.
535536

536537
## 15. Migration Steps (apply in this order)
537538

@@ -543,6 +544,6 @@ Access validators explicitly:
543544
6. Replace plain header objects with `new Headers({...})` and bracket access (`headers['x']`) with `.get()` calls per section 7
544545
7. If using `hostHeaderValidation` from server, update import and signature per section 8
545546
8. If using server SSE transport, migrate to Streamable HTTP
546-
9. If using server auth from the SDK: RS helpers (`requireBearerAuth`, `mcpAuthMetadataRouter`) `@modelcontextprotocol/express`; AS helpers → external IdP/OAuth library
547+
9. If using server auth from the SDK: all auth helpers → `@modelcontextprotocol/server-legacy/auth` (deprecated); migrate to external IdP/OAuth library
547548
10. If relying on `listTools()`/`listPrompts()`/etc. throwing on missing capabilities, set `enforceStrictCapabilities: true`
548549
11. Verify: build with `tsc` / run tests

docs/migration.md

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ const transport = new StreamableHTTPClientTransport(new URL('http://localhost:30
143143

144144
### Server auth split
145145

146-
Resource Server helpers (`requireBearerAuth`, `mcpAuthMetadataRouter`, `getOAuthProtectedResourceMetadataUrl`, `OAuthTokenVerifier`) are now first-class in `@modelcontextprotocol/express`.
146+
Resource Server helpers (`requireBearerAuth`, `mcpAuthMetadataRouter`, `getOAuthProtectedResourceMetadataUrl`, `OAuthTokenVerifier`) are available from `@modelcontextprotocol/server-legacy/auth`. Migrate to a dedicated OAuth provider for production use.
147147

148148
Authorization Server helpers (`mcpAuthRouter`, `OAuthServerProvider`, `ProxyOAuthServerProvider`, `authenticateClient`, `allowedMethods`, etc.) have been removed from the core SDK; new code should use a dedicated IdP/OAuth library. See the [examples](../examples/server/src/) for a working demo with `better-auth`.
149149

@@ -917,8 +917,8 @@ server.setRequestHandler('tools/call', async (request, ctx) => {
917917

918918
The SDK now automatically selects the appropriate JSON Schema validator based on your runtime environment:
919919

920-
- **Node.js**: Uses `AjvJsonSchemaValidator` (same as v1 default)
921-
- **Cloudflare Workers**: Uses `CfWorkerJsonSchemaValidator` (previously required manual configuration)
920+
- **Node.js**: Uses AJV (same as v1 default)
921+
- **Cloudflare Workers**: Uses `@cfworker/json-schema` (previously required manual configuration)
922922

923923
This means Cloudflare Workers users no longer need to explicitly pass the validator:
924924

@@ -949,17 +949,45 @@ const server = new McpServer(
949949
);
950950
```
951951

952-
You can still explicitly override the validator if needed:
952+
You do not need to install or import validator packages for the default behavior. The client and server packages bundle the validator backend selected by the runtime shim, so a normal `import { McpServer } from '@modelcontextprotocol/server'` does not pull `ajv` or `@cfworker/json-schema` into your bundle until you choose to customize.
953+
954+
If you want to customize the **built-in** backend (for example, pre-register schemas by `$id`, register custom AJV formats, or change the `@cfworker/json-schema` draft), import the named class from the explicit subpath and pass an instance through `jsonSchemaValidator`:
953955

954956
```typescript
955-
// Runtime-aware default (auto-selects AjvJsonSchemaValidator or CfWorkerJsonSchemaValidator)
956-
import { DefaultJsonSchemaValidator } from '@modelcontextprotocol/server/_shims';
957+
import { Ajv } from 'ajv';
958+
import addFormats from 'ajv-formats';
959+
import { AjvJsonSchemaValidator } from '@modelcontextprotocol/server/validators/ajv';
960+
961+
const ajv = new Ajv({ strict: true, allErrors: true });
962+
addFormats(ajv);
957963

958-
// Specific validators
959-
import { AjvJsonSchemaValidator } from '@modelcontextprotocol/server';
964+
const server = new McpServer(
965+
{ name: 'my-server', version: '1.0.0' },
966+
{
967+
capabilities: { tools: {} },
968+
jsonSchemaValidator: new AjvJsonSchemaValidator(ajv)
969+
}
970+
);
971+
```
972+
973+
```typescript
960974
import { CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/server/validators/cf-worker';
975+
976+
const server = new McpServer(
977+
{ name: 'my-server', version: '1.0.0' },
978+
{
979+
capabilities: { tools: {} },
980+
jsonSchemaValidator: new CfWorkerJsonSchemaValidator({ draft: '2020-12', shortcircuit: false })
981+
}
982+
);
961983
```
962984

985+
(both subpaths are also available on `@modelcontextprotocol/client/validators/...`)
986+
987+
If you import from one of these subpaths in your own code, the corresponding peer dep (`ajv` + `ajv-formats`, or `@cfworker/json-schema`) needs to be installed in your `package.json`. The runtime shim continues to vendor a copy for the default code path, so you can use the subpath in some files and rely on the default in others.
988+
989+
To replace validation wholesale rather than customizing the built-in classes, implement the `jsonSchemaValidator` interface and pass your own implementation through the option above.
990+
963991
## Unchanged APIs
964992

965993
The following APIs are unchanged between v1 and v2 (only the import paths changed):

0 commit comments

Comments
 (0)