refactor(core): extract Dispatcher; Protocol composes it - #2130

Closed
felixweinberger wants to merge 4 commits into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher
Closed

refactor(core): extract Dispatcher; Protocol composes it#2130
felixweinberger wants to merge 4 commits into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher

Conversation

@felixweinberger

@felixweinbergerfelixweinberger commented May 20, 2026

Copy link
Copy Markdown
Contributor

2026-06 stateless stack (v2-stateless label):

#PR
1#2128tasks-delete (mechanical)
2#2129schema-sync (mechanical)
3#2130Dispatcher extraction (zero-Δ refactor)
4#2131HTTP-stateless (the substantive review)
5#2132stdio/InMemory transports (additive)
6#2133docs + changeset
7#2134LegacyServer/LegacyClient extraction

Extracts Dispatcher<Ctx> (handler map + middleware chain + dispatch()) from Protocol. Protocol now composes one; setRequestHandler/_onrequest/fallbackRequestHandler delegate. The _wrapHandler override hook becomes dispatcher.use(middleware).

Zero behavior change.dispatch()'s catch block preserves _onrequest's error mapping verbatim. No existing test assertions changed.

Motivation and Context

Enabling refactor for SEP-2575: _dispatchStateless (next PR) needs to invoke the same handler map + middleware that legacy _onrequest uses, without going through Protocol's session machinery.

How Has This Been Tested?

pnpm test:all — count delta is dispatcher.test.ts (+11) and wrapHandler.test.ts (−1, redundant). No existing assertion changed.

Breaking Changes

Protocol._wrapHandler override hook removed (was protected). Subclasses use this.dispatcher.use(mw) instead.

Types of changes

  • Breaking change

Checklist

  • My code follows the repository's style guidelines
  • New and existing tests pass locally

@changeset-bot

changeset-botBot commented May 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 628f0e1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
NameType
@modelcontextprotocol/coreMajor

Not sure what this means? Click here to learn what changesets are.

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

@pkg-pr-new

pkg-pr-newBot commented May 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@2130

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@2130

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@2130

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@2130

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@2130

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@2130

commit: 628f0e1

@felixweinberger

Copy link
Copy Markdown
ContributorAuthor

@claude review

@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-schema-sync branch from c67522f to ce732aeCompareMay 21, 2026 10:42
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-dispatcher branch from 09fc142 to 315684dCompareMay 21, 2026 10:42
Comment on lines +159 to +161
* `Protocol._onrequest` prior to extraction.
*/
async dispatch(request: JSONRPCRequest, ctx: ContextT): Promise<JSONRPCResponse | JSONRPCErrorResponse> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Type-precision nit: Dispatcher.dispatch() declares Promise<JSONRPCResponse | JSONRPCErrorResponse>, but JSONRPCResponse is already the union JSONRPCResultResponse | JSONRPCErrorResponse (schemas.ts:170), so the trailing | JSONRPCErrorResponse is redundant. Likewise okResponse() always builds a success response but is typed as returning the wider JSONRPCResponse union instead of JSONRPCResultResponse. No runtime impact — purely a clarity/precision improvement.

Extended reasoning...

The redundant union.Dispatcher.dispatch() (packages/core/src/shared/dispatcher.ts:159) declares its return type as Promise<JSONRPCResponse | JSONRPCErrorResponse>. But JSONRPCResponse is itself already a union: packages/core/src/types/schemas.ts:170 defines JSONRPCResponseSchema = z.union([JSONRPCResultResponseSchema, JSONRPCErrorResponseSchema]), and spec.types.ts:257 confirms type JSONRPCResponse = JSONRPCResultResponse | JSONRPCErrorResponse. TypeScript collapses A | B where B ⊆ A to just A, so the declared type is exactly equivalent to Promise<JSONRPCResponse> — the | JSONRPCErrorResponse adds nothing.

The over-wide okResponse() type.okResponse() (packages/core/src/shared/dispatcher.ts:195) unconditionally builds { jsonrpc, id, result } — always a success-shaped response. Yet it's typed as returning JSONRPCResponse (the success-or-error union) when the precise type is JSONRPCResultResponse. A caller that wanted to access .result directly would be forced to narrow with a type guard even though the function can never produce an error variant.

Why this isn't a behavior bug. Both signatures are correct in the sense that the actual return value is assignable to the declared type — they're just wider than necessary. Every current caller (Protocol._onrequest in protocol.ts) immediately passes the result to transport.send(), which accepts the full JSONRPCMessage union, so nothing observable changes. The pre-PR _onrequest code also typed its locally-built success object as const response: JSONRPCResponse, so this carries over a pre-existing widening — but since the PR is introducing these new exported helpers in a brand-new file, it's a good moment to get the types exactly right.

Why fix it.okResponse and errorResponse are now exported from packages/core (export * from './shared/dispatcher.js' in index.ts), so they become public-ish API for sibling packages and the upcoming _dispatchStateless path. Tighter return types give downstream callers better narrowing for free and document the contract ("this always succeeds") without needing a comment.

Concrete walk-through.

  1. A future consumer calls const r = okResponse(1, { tools: [] }).
  2. They want to read r.result. With the current signature r: JSONRPCResponse, TypeScript reports Property 'result' does not exist on type 'JSONRPCErrorResponse' and forces an if ('result' in r) guard.
  3. With r: JSONRPCResultResponse, r.result type-checks directly — matching the function's actual guaranteed behavior.

Suggested fix.

asyncdispatch(request: JSONRPCRequest,ctx: ContextT): Promise<JSONRPCResponse>{}exportfunctionokResponse(id: JSONRPCRequest['id'],result: Result): JSONRPCResultResponse{}

errorResponse is already correctly narrowed to JSONRPCErrorResponse, so only these two annotations need to change. Filed as a nit since there's zero runtime impact.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving as-is for this PR. The wider types match the pre-existing convention and every caller passes the result straight to transport.send(); will tighten if a downstream caller needs the narrowing.

Comment threadCLAUDE.md Outdated
…ences)
Removes the 2025-11 experimental tasks side-channel through Protocol:
TaskManager, processInbound*/processOutbound*, task interception, the
experimental.tasks.* client/server accessors, and all task-augmented
request handling.
Also extends beyond the implementation deletion to scrub remaining
references in examples, docs, and comments. The only task-related
symbol remaining in packages/*.ts is `taskSupport` in ToolExecutionSchema,
kept solely to match spec.types.ts (which still declares it); both are
removed together in the next commit (spec regen).
CHANGELOG entries are preserved (historical record). Migration docs
retain a brief removal note. `microtask`/`platformBackgroundTask` are
JS/platform terminology, not MCP tasks.
Satisfies: SEP-2663 (core-removal half; tasks are now Extensions Track).
pnpm fetch:spec-types. Spec moved 44 commits since last regen.
Brings:
- RequestMetaObject with namespaced io.modelcontextprotocol/* keys (required protocolVersion/clientInfo/clientCapabilities)
- Result.resultType: "complete" | "input_required" (required)
- DiscoverRequest/Result, InputRequiredResult, InputRequest/Response/s, InputResponseRequestParams
- SubscriptionsListenRequest, SubscriptionFilter, SubscriptionsAcknowledgedNotification
- UnsupportedProtocolVersionError, MissingRequiredClientCapabilityError (-32003)
Removes (moved to extension or removed from spec):
- InitializeRequest/Result, InitializedNotification, PingRequest, SetLevelRequest
- SubscribeRequest, UnsubscribeRequest, RootsListChangedNotification
- All Task* types, taskSupport, TaskAugmentedRequestParams
Typecheck broken pending next commit (schemas.ts/guards.ts reference removed types).
Isolated for diff clarity per plan §10.
…emas
schemas.ts:
- Result/Notification _meta now use plain MetaObject (not RequestMetaSchema)
- ResultTypeSchema, resultType optional on ResultSchema (BC: pre-2026 omits it)
- RequestMetaSchema kept simple {progressToken?}; namespaced io.modelcontextprotocol/*
keys validated by parseClientMeta in P2, not here (TS2589 depth budget)
- DiscoverRequest/Result, UnsupportedProtocolVersionErrorData,
MissingRequiredClientCapabilityErrorData
- SubscriptionFilter, SubscriptionsListenRequest/Params,
SubscriptionsAcknowledgedNotification/Params
- InputRequest/Response/s, InputRequiredResult, InputResponseRequestParams
- CacheableResult/CacheScope (SEP-2243), PaginatedResult extends CacheableResult,
ReadResourceResult extends CacheableResult
- structuredContent: unknown (was Record<string,unknown>)
- Tool inputSchema/outputSchema loosened to match spec (open record + type:object on input)
- ToolExecution removed (only had taskSupport)
- ClientRequest/ServerNotification/etc unions = 2026 spec methods only
- registerLegacySchemas() lets legacyWireSchemas merge into runtime lookup maps
- Dropped Initialize*/Ping/SetLevel/Subscribe/Unsubscribe/RootsListChanged
NEW legacyWireSchemas.ts:
- LegacyRequestParams/Result/RequestMetaObject base types (no namespaced keys, no resultType)
- TS interfaces + zod for Initialize/Ping/SetLevel/Subscribe/Unsubscribe/RootsListChanged
- legacyRequest/Result/NotificationSchemas maps; registerLegacySchemas() side-effect on import
types.ts: re-exports legacy types; RequestTypeMap/NotificationTypeMap/ResultTypeMap
merge legacy methods. Adds Discover*/InputRequired*/Subscriptions*/Cache* types.
guards.ts: isInitializeRequest/isInitializedNotification import from legacyWireSchemas.
specTypeSchema.ts: allowlist updated (drop legacy, add 2026).
spec.types.test.ts: Relax<T> helper makes spec-required PermissiveKeys
(_meta/resultType/ttlMs/cacheScope/inputResponses/requestState/io.modelcontextprotocol/*)
optional so mutual assignability holds for everything else. Dropped checks for
removed types; added 17 checks for 2026 types. Count 176→150.
types.capabilities.test.ts: import InitializeRequestParamsSchema from legacyWireSchemas.
types.test.ts: outputSchema no longer requires type:object.
mcp.ts: drop ToolExecution/execution (removed from spec).
client.ts: assertCapabilityForMethod switches on RequestMethod (includes legacy).
…or change)
Introduce `Dispatcher<ContextT>` (`packages/core/src/shared/dispatcher.ts`)
holding the request-handler registry, middleware chain, and `dispatch()`
entry point. `Protocol` now composes a `protected readonly dispatcher`
and delegates:
- `setRequestHandler` / `removeRequestHandler` / `assertCanSetRequestHandler`
- `fallbackRequestHandler` (now `dispatcher.fallbackHandler`)
- `_onrequest` invokes `dispatcher.dispatch(request, ctx)` for lookup,
middleware onion, and JSON-RPC response wrapping
The previous `_wrapHandler` override hook is replaced by `dispatcher.use()`:
- `Server` registers `_callToolResultMiddleware` (was `_wrapHandler` for tools/call)
- `Client` registers `_validationMiddleware` (was `_wrapHandler` for
elicitation/create + sampling/createMessage)
Same validation runs on the same paths; the observation point moves from
per-registration wrapping to per-dispatch middleware.
`dispatch()`'s catch block preserves `_onrequest`'s error behavior verbatim:
thrown errors surface their `code` (if a safe integer), `message`, and `data`.
No sanitization is introduced.
Tests:
- `dispatcher.test.ts` added (11 tests for setHandler/dispatch/middleware/error)
- `wrapHandler.test.ts` deleted (covered by rewritten customMethods test)
- `customMethods.test.ts` adapts one test from spying on `_wrapHandler` to
spying via middleware; same assertion (both 2-arg and 3-arg routes through)
No existing test assertions changed.
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-schema-sync branch from ce732ae to 29c4991CompareMay 21, 2026 11:14
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-dispatcher branch from 315684d to 628f0e1CompareMay 21, 2026 11:15
@felixweinberger

Copy link
Copy Markdown
ContributorAuthor

@claude review

Comment on lines +279 to +280
/** The handler registry. Both `_onrequest` and `_dispatchStateless` route through it. */
protected readonly dispatcher = new Dispatcher<ContextT>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Several new comments and JSDoc blocks reference _dispatchStateless and "the 2026-06 dispatch path" — neither of which exists in this PR or the codebase yet (they are planned for #2131). A reader who greps for _dispatchStateless after merge will find nothing. Consider qualifying the references (e.g. "and the forthcoming _dispatchStateless path (#2131)") or deferring them to the PR that introduces the second path. Affects protocol.ts:279, dispatcher.ts:55, client.ts:288, and server.ts:182.

Extended reasoning...

What the issue is. Four new comments added by this PR reference code that does not exist yet:

  • packages/core/src/shared/protocol.ts:279/** The handler registry. Both \_onrequest` and `_dispatchStateless` route through it. */`
  • packages/core/src/shared/dispatcher.ts (class JSDoc, ~line 55) — "Both the legacy connect/_onrequest path and the 2026-06 stateless dispatch path route through dispatch."
  • packages/client/src/client/client.ts:288 — "…so it applies to both the legacy `_onrequest` path and the 2026-06 dispatch path."
  • packages/server/src/server/server.ts:182 — "…so it applies to both the legacy `_onrequest` path and the 2026-06 dispatch path."

_dispatchStateless appears nowhere in the codebase except inside the comment at protocol.ts:279, and there is no "2026-06 dispatch path" implemented anywhere. These are forward references to PR #2131.

Why it matters. The repo's own review guidance flags "prose that promises behavior the code no longer ships" and "any claim the diff doesn't back." Once this PR merges, a contributor reading Protocol who sees "Both `_onrequest` and `_dispatchStateless` route through it" and greps for _dispatchStateless will find zero hits. They have no way to know whether the symbol was renamed, removed, or never landed. _onrequest already calls _onrequest legacy — there is no "legacy" vs. "new" distinction observable in the merged code, so the dichotomy the comments draw doesn't exist for a reader of the codebase at this commit.

Step-by-step proof.

  1. Merge refactor(core): extract Dispatcher; Protocol composes it #2130 in isolation.
  2. Open packages/core/src/shared/protocol.ts at line 279: /** The handler registry. Both \_onrequest` and `_dispatchStateless` route through it. */`
  3. Run grep -rn _dispatchStateless packages/ → only the comment itself matches. There is no method, no export, no test.
  4. Open packages/server/src/server/server.ts:182: "…applies to both the legacy `_onrequest` path and the 2026-06 dispatch path." → there is exactly one path, _onrequest. Nothing distinguishes it as "legacy".

Why this is a nit and not a blocker. The PR description openly states this is PR 3 of a 7-PR v2-stateless stack, and that #2131 ("HTTP-stateless") introduces _dispatchStateless. The author and reviewers have full stack context, the comments are accurate as a description of intent, and they will become literally true once #2131 merges. There is zero runtime impact. But comments live in the merged tree, not in the PR description — and PRs in a stack don't always all land, or land in order.

How to fix. Either qualify the references so they are true at merge time, e.g.:

/** The handler registry. \`_onrequest\` routes through it (and the forthcoming \`_dispatchStateless\` path, #2131). */

or describe only what exists today and let #2131 update the comments when it adds the second path:

/** The handler registry. \`_onrequest\` routes through it. */

The first option preserves the useful forward signal for reviewers of this stack; the second is the most conservative.

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

Labels

v2-stateless2026-06 SDK: Protocol decomposition + SEP alignment (request-first / stateless)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@felixweinberger
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

refactor(core): extract Dispatcher; Protocol composes it - #2130

Closed
felixweinberger wants to merge 4 commits into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher
Closed

refactor(core): extract Dispatcher; Protocol composes it#2130
felixweinberger wants to merge 4 commits into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher

Conversation

@felixweinberger

@felixweinbergerfelixweinberger commented May 20, 2026

Copy link
Copy Markdown
Contributor

2026-06 stateless stack (v2-stateless label):

#PR
1#2128tasks-delete (mechanical)
2#2129schema-sync (mechanical)
3#2130Dispatcher extraction (zero-Δ refactor)
4#2131HTTP-stateless (the substantive review)
5#2132stdio/InMemory transports (additive)
6#2133docs + changeset
7#2134LegacyServer/LegacyClient extraction

Extracts Dispatcher<Ctx> (handler map + middleware chain + dispatch()) from Protocol. Protocol now composes one; setRequestHandler/_onrequest/fallbackRequestHandler delegate. The _wrapHandler override hook becomes dispatcher.use(middleware).

Zero behavior change.dispatch()'s catch block preserves _onrequest's error mapping verbatim. No existing test assertions changed.

Motivation and Context

Enabling refactor for SEP-2575: _dispatchStateless (next PR) needs to invoke the same handler map + middleware that legacy _onrequest uses, without going through Protocol's session machinery.

How Has This Been Tested?

pnpm test:all — count delta is dispatcher.test.ts (+11) and wrapHandler.test.ts (−1, redundant). No existing assertion changed.

Breaking Changes

Protocol._wrapHandler override hook removed (was protected). Subclasses use this.dispatcher.use(mw) instead.

Types of changes

  • Breaking change

Checklist

  • My code follows the repository's style guidelines
  • New and existing tests pass locally

@changeset-bot

changeset-botBot commented May 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 628f0e1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
NameType
@modelcontextprotocol/coreMajor

Not sure what this means? Click here to learn what changesets are.

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

@pkg-pr-new

pkg-pr-newBot commented May 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@2130

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@2130

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@2130

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@2130

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@2130

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@2130

commit: 628f0e1

@felixweinberger

Copy link
Copy Markdown
ContributorAuthor

@claude review

@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-schema-sync branch from c67522f to ce732aeCompareMay 21, 2026 10:42
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-dispatcher branch from 09fc142 to 315684dCompareMay 21, 2026 10:42
Comment on lines +159 to +161
* `Protocol._onrequest` prior to extraction.
*/
async dispatch(request: JSONRPCRequest, ctx: ContextT): Promise<JSONRPCResponse | JSONRPCErrorResponse> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Type-precision nit: Dispatcher.dispatch() declares Promise<JSONRPCResponse | JSONRPCErrorResponse>, but JSONRPCResponse is already the union JSONRPCResultResponse | JSONRPCErrorResponse (schemas.ts:170), so the trailing | JSONRPCErrorResponse is redundant. Likewise okResponse() always builds a success response but is typed as returning the wider JSONRPCResponse union instead of JSONRPCResultResponse. No runtime impact — purely a clarity/precision improvement.

Extended reasoning...

The redundant union.Dispatcher.dispatch() (packages/core/src/shared/dispatcher.ts:159) declares its return type as Promise<JSONRPCResponse | JSONRPCErrorResponse>. But JSONRPCResponse is itself already a union: packages/core/src/types/schemas.ts:170 defines JSONRPCResponseSchema = z.union([JSONRPCResultResponseSchema, JSONRPCErrorResponseSchema]), and spec.types.ts:257 confirms type JSONRPCResponse = JSONRPCResultResponse | JSONRPCErrorResponse. TypeScript collapses A | B where B ⊆ A to just A, so the declared type is exactly equivalent to Promise<JSONRPCResponse> — the | JSONRPCErrorResponse adds nothing.

The over-wide okResponse() type.okResponse() (packages/core/src/shared/dispatcher.ts:195) unconditionally builds { jsonrpc, id, result } — always a success-shaped response. Yet it's typed as returning JSONRPCResponse (the success-or-error union) when the precise type is JSONRPCResultResponse. A caller that wanted to access .result directly would be forced to narrow with a type guard even though the function can never produce an error variant.

Why this isn't a behavior bug. Both signatures are correct in the sense that the actual return value is assignable to the declared type — they're just wider than necessary. Every current caller (Protocol._onrequest in protocol.ts) immediately passes the result to transport.send(), which accepts the full JSONRPCMessage union, so nothing observable changes. The pre-PR _onrequest code also typed its locally-built success object as const response: JSONRPCResponse, so this carries over a pre-existing widening — but since the PR is introducing these new exported helpers in a brand-new file, it's a good moment to get the types exactly right.

Why fix it.okResponse and errorResponse are now exported from packages/core (export * from './shared/dispatcher.js' in index.ts), so they become public-ish API for sibling packages and the upcoming _dispatchStateless path. Tighter return types give downstream callers better narrowing for free and document the contract ("this always succeeds") without needing a comment.

Concrete walk-through.

  1. A future consumer calls const r = okResponse(1, { tools: [] }).
  2. They want to read r.result. With the current signature r: JSONRPCResponse, TypeScript reports Property 'result' does not exist on type 'JSONRPCErrorResponse' and forces an if ('result' in r) guard.
  3. With r: JSONRPCResultResponse, r.result type-checks directly — matching the function's actual guaranteed behavior.

Suggested fix.

asyncdispatch(request: JSONRPCRequest,ctx: ContextT): Promise<JSONRPCResponse>{}exportfunctionokResponse(id: JSONRPCRequest['id'],result: Result): JSONRPCResultResponse{}

errorResponse is already correctly narrowed to JSONRPCErrorResponse, so only these two annotations need to change. Filed as a nit since there's zero runtime impact.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving as-is for this PR. The wider types match the pre-existing convention and every caller passes the result straight to transport.send(); will tighten if a downstream caller needs the narrowing.

Comment threadCLAUDE.md Outdated
…ences)
Removes the 2025-11 experimental tasks side-channel through Protocol:
TaskManager, processInbound*/processOutbound*, task interception, the
experimental.tasks.* client/server accessors, and all task-augmented
request handling.
Also extends beyond the implementation deletion to scrub remaining
references in examples, docs, and comments. The only task-related
symbol remaining in packages/*.ts is `taskSupport` in ToolExecutionSchema,
kept solely to match spec.types.ts (which still declares it); both are
removed together in the next commit (spec regen).
CHANGELOG entries are preserved (historical record). Migration docs
retain a brief removal note. `microtask`/`platformBackgroundTask` are
JS/platform terminology, not MCP tasks.
Satisfies: SEP-2663 (core-removal half; tasks are now Extensions Track).
pnpm fetch:spec-types. Spec moved 44 commits since last regen.
Brings:
- RequestMetaObject with namespaced io.modelcontextprotocol/* keys (required protocolVersion/clientInfo/clientCapabilities)
- Result.resultType: "complete" | "input_required" (required)
- DiscoverRequest/Result, InputRequiredResult, InputRequest/Response/s, InputResponseRequestParams
- SubscriptionsListenRequest, SubscriptionFilter, SubscriptionsAcknowledgedNotification
- UnsupportedProtocolVersionError, MissingRequiredClientCapabilityError (-32003)
Removes (moved to extension or removed from spec):
- InitializeRequest/Result, InitializedNotification, PingRequest, SetLevelRequest
- SubscribeRequest, UnsubscribeRequest, RootsListChangedNotification
- All Task* types, taskSupport, TaskAugmentedRequestParams
Typecheck broken pending next commit (schemas.ts/guards.ts reference removed types).
Isolated for diff clarity per plan §10.
…emas
schemas.ts:
- Result/Notification _meta now use plain MetaObject (not RequestMetaSchema)
- ResultTypeSchema, resultType optional on ResultSchema (BC: pre-2026 omits it)
- RequestMetaSchema kept simple {progressToken?}; namespaced io.modelcontextprotocol/*
keys validated by parseClientMeta in P2, not here (TS2589 depth budget)
- DiscoverRequest/Result, UnsupportedProtocolVersionErrorData,
MissingRequiredClientCapabilityErrorData
- SubscriptionFilter, SubscriptionsListenRequest/Params,
SubscriptionsAcknowledgedNotification/Params
- InputRequest/Response/s, InputRequiredResult, InputResponseRequestParams
- CacheableResult/CacheScope (SEP-2243), PaginatedResult extends CacheableResult,
ReadResourceResult extends CacheableResult
- structuredContent: unknown (was Record<string,unknown>)
- Tool inputSchema/outputSchema loosened to match spec (open record + type:object on input)
- ToolExecution removed (only had taskSupport)
- ClientRequest/ServerNotification/etc unions = 2026 spec methods only
- registerLegacySchemas() lets legacyWireSchemas merge into runtime lookup maps
- Dropped Initialize*/Ping/SetLevel/Subscribe/Unsubscribe/RootsListChanged
NEW legacyWireSchemas.ts:
- LegacyRequestParams/Result/RequestMetaObject base types (no namespaced keys, no resultType)
- TS interfaces + zod for Initialize/Ping/SetLevel/Subscribe/Unsubscribe/RootsListChanged
- legacyRequest/Result/NotificationSchemas maps; registerLegacySchemas() side-effect on import
types.ts: re-exports legacy types; RequestTypeMap/NotificationTypeMap/ResultTypeMap
merge legacy methods. Adds Discover*/InputRequired*/Subscriptions*/Cache* types.
guards.ts: isInitializeRequest/isInitializedNotification import from legacyWireSchemas.
specTypeSchema.ts: allowlist updated (drop legacy, add 2026).
spec.types.test.ts: Relax<T> helper makes spec-required PermissiveKeys
(_meta/resultType/ttlMs/cacheScope/inputResponses/requestState/io.modelcontextprotocol/*)
optional so mutual assignability holds for everything else. Dropped checks for
removed types; added 17 checks for 2026 types. Count 176→150.
types.capabilities.test.ts: import InitializeRequestParamsSchema from legacyWireSchemas.
types.test.ts: outputSchema no longer requires type:object.
mcp.ts: drop ToolExecution/execution (removed from spec).
client.ts: assertCapabilityForMethod switches on RequestMethod (includes legacy).
…or change)
Introduce `Dispatcher<ContextT>` (`packages/core/src/shared/dispatcher.ts`)
holding the request-handler registry, middleware chain, and `dispatch()`
entry point. `Protocol` now composes a `protected readonly dispatcher`
and delegates:
- `setRequestHandler` / `removeRequestHandler` / `assertCanSetRequestHandler`
- `fallbackRequestHandler` (now `dispatcher.fallbackHandler`)
- `_onrequest` invokes `dispatcher.dispatch(request, ctx)` for lookup,
middleware onion, and JSON-RPC response wrapping
The previous `_wrapHandler` override hook is replaced by `dispatcher.use()`:
- `Server` registers `_callToolResultMiddleware` (was `_wrapHandler` for tools/call)
- `Client` registers `_validationMiddleware` (was `_wrapHandler` for
elicitation/create + sampling/createMessage)
Same validation runs on the same paths; the observation point moves from
per-registration wrapping to per-dispatch middleware.
`dispatch()`'s catch block preserves `_onrequest`'s error behavior verbatim:
thrown errors surface their `code` (if a safe integer), `message`, and `data`.
No sanitization is introduced.
Tests:
- `dispatcher.test.ts` added (11 tests for setHandler/dispatch/middleware/error)
- `wrapHandler.test.ts` deleted (covered by rewritten customMethods test)
- `customMethods.test.ts` adapts one test from spying on `_wrapHandler` to
spying via middleware; same assertion (both 2-arg and 3-arg routes through)
No existing test assertions changed.
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-schema-sync branch from ce732ae to 29c4991CompareMay 21, 2026 11:14
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-dispatcher branch from 315684d to 628f0e1CompareMay 21, 2026 11:15
@felixweinberger

Copy link
Copy Markdown
ContributorAuthor

@claude review

Comment on lines +279 to +280
/** The handler registry. Both `_onrequest` and `_dispatchStateless` route through it. */
protected readonly dispatcher = new Dispatcher<ContextT>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Several new comments and JSDoc blocks reference _dispatchStateless and "the 2026-06 dispatch path" — neither of which exists in this PR or the codebase yet (they are planned for #2131). A reader who greps for _dispatchStateless after merge will find nothing. Consider qualifying the references (e.g. "and the forthcoming _dispatchStateless path (#2131)") or deferring them to the PR that introduces the second path. Affects protocol.ts:279, dispatcher.ts:55, client.ts:288, and server.ts:182.

Extended reasoning...

What the issue is. Four new comments added by this PR reference code that does not exist yet:

  • packages/core/src/shared/protocol.ts:279/** The handler registry. Both \_onrequest` and `_dispatchStateless` route through it. */`
  • packages/core/src/shared/dispatcher.ts (class JSDoc, ~line 55) — "Both the legacy connect/_onrequest path and the 2026-06 stateless dispatch path route through dispatch."
  • packages/client/src/client/client.ts:288 — "…so it applies to both the legacy `_onrequest` path and the 2026-06 dispatch path."
  • packages/server/src/server/server.ts:182 — "…so it applies to both the legacy `_onrequest` path and the 2026-06 dispatch path."

_dispatchStateless appears nowhere in the codebase except inside the comment at protocol.ts:279, and there is no "2026-06 dispatch path" implemented anywhere. These are forward references to PR #2131.

Why it matters. The repo's own review guidance flags "prose that promises behavior the code no longer ships" and "any claim the diff doesn't back." Once this PR merges, a contributor reading Protocol who sees "Both `_onrequest` and `_dispatchStateless` route through it" and greps for _dispatchStateless will find zero hits. They have no way to know whether the symbol was renamed, removed, or never landed. _onrequest already calls _onrequest legacy — there is no "legacy" vs. "new" distinction observable in the merged code, so the dichotomy the comments draw doesn't exist for a reader of the codebase at this commit.

Step-by-step proof.

  1. Merge refactor(core): extract Dispatcher; Protocol composes it #2130 in isolation.
  2. Open packages/core/src/shared/protocol.ts at line 279: /** The handler registry. Both \_onrequest` and `_dispatchStateless` route through it. */`
  3. Run grep -rn _dispatchStateless packages/ → only the comment itself matches. There is no method, no export, no test.
  4. Open packages/server/src/server/server.ts:182: "…applies to both the legacy `_onrequest` path and the 2026-06 dispatch path." → there is exactly one path, _onrequest. Nothing distinguishes it as "legacy".

Why this is a nit and not a blocker. The PR description openly states this is PR 3 of a 7-PR v2-stateless stack, and that #2131 ("HTTP-stateless") introduces _dispatchStateless. The author and reviewers have full stack context, the comments are accurate as a description of intent, and they will become literally true once #2131 merges. There is zero runtime impact. But comments live in the merged tree, not in the PR description — and PRs in a stack don't always all land, or land in order.

How to fix. Either qualify the references so they are true at merge time, e.g.:

/** The handler registry. \`_onrequest\` routes through it (and the forthcoming \`_dispatchStateless\` path, #2131). */

or describe only what exists today and let #2131 update the comments when it adds the second path:

/** The handler registry. \`_onrequest\` routes through it. */

The first option preserves the useful forward signal for reviewers of this stack; the second is the most conservative.

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

Labels

v2-stateless2026-06 SDK: Protocol decomposition + SEP alignment (request-first / stateless)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@felixweinberger
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

refactor(core): extract Dispatcher; Protocol composes it - #2130

Closed
felixweinberger wants to merge 4 commits into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher
Closed

refactor(core): extract Dispatcher; Protocol composes it#2130
felixweinberger wants to merge 4 commits into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher

Conversation

@felixweinberger

@felixweinbergerfelixweinberger commented May 20, 2026

Copy link
Copy Markdown
Contributor

2026-06 stateless stack (v2-stateless label):

#PR
1#2128tasks-delete (mechanical)
2#2129schema-sync (mechanical)
3#2130Dispatcher extraction (zero-Δ refactor)
4#2131HTTP-stateless (the substantive review)
5#2132stdio/InMemory transports (additive)
6#2133docs + changeset
7#2134LegacyServer/LegacyClient extraction

Extracts Dispatcher<Ctx> (handler map + middleware chain + dispatch()) from Protocol. Protocol now composes one; setRequestHandler/_onrequest/fallbackRequestHandler delegate. The _wrapHandler override hook becomes dispatcher.use(middleware).

Zero behavior change.dispatch()'s catch block preserves _onrequest's error mapping verbatim. No existing test assertions changed.

Motivation and Context

Enabling refactor for SEP-2575: _dispatchStateless (next PR) needs to invoke the same handler map + middleware that legacy _onrequest uses, without going through Protocol's session machinery.

How Has This Been Tested?

pnpm test:all — count delta is dispatcher.test.ts (+11) and wrapHandler.test.ts (−1, redundant). No existing assertion changed.

Breaking Changes

Protocol._wrapHandler override hook removed (was protected). Subclasses use this.dispatcher.use(mw) instead.

Types of changes

  • Breaking change

Checklist

  • My code follows the repository's style guidelines
  • New and existing tests pass locally

@changeset-bot

changeset-botBot commented May 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 628f0e1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
NameType
@modelcontextprotocol/coreMajor

Not sure what this means? Click here to learn what changesets are.

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

@pkg-pr-new

pkg-pr-newBot commented May 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@2130

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@2130

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@2130

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@2130

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@2130

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@2130

commit: 628f0e1

@felixweinberger

Copy link
Copy Markdown
ContributorAuthor

@claude review

@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-schema-sync branch from c67522f to ce732aeCompareMay 21, 2026 10:42
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-dispatcher branch from 09fc142 to 315684dCompareMay 21, 2026 10:42
Comment on lines +159 to +161
* `Protocol._onrequest` prior to extraction.
*/
async dispatch(request: JSONRPCRequest, ctx: ContextT): Promise<JSONRPCResponse | JSONRPCErrorResponse> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Type-precision nit: Dispatcher.dispatch() declares Promise<JSONRPCResponse | JSONRPCErrorResponse>, but JSONRPCResponse is already the union JSONRPCResultResponse | JSONRPCErrorResponse (schemas.ts:170), so the trailing | JSONRPCErrorResponse is redundant. Likewise okResponse() always builds a success response but is typed as returning the wider JSONRPCResponse union instead of JSONRPCResultResponse. No runtime impact — purely a clarity/precision improvement.

Extended reasoning...

The redundant union.Dispatcher.dispatch() (packages/core/src/shared/dispatcher.ts:159) declares its return type as Promise<JSONRPCResponse | JSONRPCErrorResponse>. But JSONRPCResponse is itself already a union: packages/core/src/types/schemas.ts:170 defines JSONRPCResponseSchema = z.union([JSONRPCResultResponseSchema, JSONRPCErrorResponseSchema]), and spec.types.ts:257 confirms type JSONRPCResponse = JSONRPCResultResponse | JSONRPCErrorResponse. TypeScript collapses A | B where B ⊆ A to just A, so the declared type is exactly equivalent to Promise<JSONRPCResponse> — the | JSONRPCErrorResponse adds nothing.

The over-wide okResponse() type.okResponse() (packages/core/src/shared/dispatcher.ts:195) unconditionally builds { jsonrpc, id, result } — always a success-shaped response. Yet it's typed as returning JSONRPCResponse (the success-or-error union) when the precise type is JSONRPCResultResponse. A caller that wanted to access .result directly would be forced to narrow with a type guard even though the function can never produce an error variant.

Why this isn't a behavior bug. Both signatures are correct in the sense that the actual return value is assignable to the declared type — they're just wider than necessary. Every current caller (Protocol._onrequest in protocol.ts) immediately passes the result to transport.send(), which accepts the full JSONRPCMessage union, so nothing observable changes. The pre-PR _onrequest code also typed its locally-built success object as const response: JSONRPCResponse, so this carries over a pre-existing widening — but since the PR is introducing these new exported helpers in a brand-new file, it's a good moment to get the types exactly right.

Why fix it.okResponse and errorResponse are now exported from packages/core (export * from './shared/dispatcher.js' in index.ts), so they become public-ish API for sibling packages and the upcoming _dispatchStateless path. Tighter return types give downstream callers better narrowing for free and document the contract ("this always succeeds") without needing a comment.

Concrete walk-through.

  1. A future consumer calls const r = okResponse(1, { tools: [] }).
  2. They want to read r.result. With the current signature r: JSONRPCResponse, TypeScript reports Property 'result' does not exist on type 'JSONRPCErrorResponse' and forces an if ('result' in r) guard.
  3. With r: JSONRPCResultResponse, r.result type-checks directly — matching the function's actual guaranteed behavior.

Suggested fix.

asyncdispatch(request: JSONRPCRequest,ctx: ContextT): Promise<JSONRPCResponse>{}exportfunctionokResponse(id: JSONRPCRequest['id'],result: Result): JSONRPCResultResponse{}

errorResponse is already correctly narrowed to JSONRPCErrorResponse, so only these two annotations need to change. Filed as a nit since there's zero runtime impact.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving as-is for this PR. The wider types match the pre-existing convention and every caller passes the result straight to transport.send(); will tighten if a downstream caller needs the narrowing.

Comment threadCLAUDE.md Outdated
…ences)
Removes the 2025-11 experimental tasks side-channel through Protocol:
TaskManager, processInbound*/processOutbound*, task interception, the
experimental.tasks.* client/server accessors, and all task-augmented
request handling.
Also extends beyond the implementation deletion to scrub remaining
references in examples, docs, and comments. The only task-related
symbol remaining in packages/*.ts is `taskSupport` in ToolExecutionSchema,
kept solely to match spec.types.ts (which still declares it); both are
removed together in the next commit (spec regen).
CHANGELOG entries are preserved (historical record). Migration docs
retain a brief removal note. `microtask`/`platformBackgroundTask` are
JS/platform terminology, not MCP tasks.
Satisfies: SEP-2663 (core-removal half; tasks are now Extensions Track).
pnpm fetch:spec-types. Spec moved 44 commits since last regen.
Brings:
- RequestMetaObject with namespaced io.modelcontextprotocol/* keys (required protocolVersion/clientInfo/clientCapabilities)
- Result.resultType: "complete" | "input_required" (required)
- DiscoverRequest/Result, InputRequiredResult, InputRequest/Response/s, InputResponseRequestParams
- SubscriptionsListenRequest, SubscriptionFilter, SubscriptionsAcknowledgedNotification
- UnsupportedProtocolVersionError, MissingRequiredClientCapabilityError (-32003)
Removes (moved to extension or removed from spec):
- InitializeRequest/Result, InitializedNotification, PingRequest, SetLevelRequest
- SubscribeRequest, UnsubscribeRequest, RootsListChangedNotification
- All Task* types, taskSupport, TaskAugmentedRequestParams
Typecheck broken pending next commit (schemas.ts/guards.ts reference removed types).
Isolated for diff clarity per plan §10.
…emas
schemas.ts:
- Result/Notification _meta now use plain MetaObject (not RequestMetaSchema)
- ResultTypeSchema, resultType optional on ResultSchema (BC: pre-2026 omits it)
- RequestMetaSchema kept simple {progressToken?}; namespaced io.modelcontextprotocol/*
keys validated by parseClientMeta in P2, not here (TS2589 depth budget)
- DiscoverRequest/Result, UnsupportedProtocolVersionErrorData,
MissingRequiredClientCapabilityErrorData
- SubscriptionFilter, SubscriptionsListenRequest/Params,
SubscriptionsAcknowledgedNotification/Params
- InputRequest/Response/s, InputRequiredResult, InputResponseRequestParams
- CacheableResult/CacheScope (SEP-2243), PaginatedResult extends CacheableResult,
ReadResourceResult extends CacheableResult
- structuredContent: unknown (was Record<string,unknown>)
- Tool inputSchema/outputSchema loosened to match spec (open record + type:object on input)
- ToolExecution removed (only had taskSupport)
- ClientRequest/ServerNotification/etc unions = 2026 spec methods only
- registerLegacySchemas() lets legacyWireSchemas merge into runtime lookup maps
- Dropped Initialize*/Ping/SetLevel/Subscribe/Unsubscribe/RootsListChanged
NEW legacyWireSchemas.ts:
- LegacyRequestParams/Result/RequestMetaObject base types (no namespaced keys, no resultType)
- TS interfaces + zod for Initialize/Ping/SetLevel/Subscribe/Unsubscribe/RootsListChanged
- legacyRequest/Result/NotificationSchemas maps; registerLegacySchemas() side-effect on import
types.ts: re-exports legacy types; RequestTypeMap/NotificationTypeMap/ResultTypeMap
merge legacy methods. Adds Discover*/InputRequired*/Subscriptions*/Cache* types.
guards.ts: isInitializeRequest/isInitializedNotification import from legacyWireSchemas.
specTypeSchema.ts: allowlist updated (drop legacy, add 2026).
spec.types.test.ts: Relax<T> helper makes spec-required PermissiveKeys
(_meta/resultType/ttlMs/cacheScope/inputResponses/requestState/io.modelcontextprotocol/*)
optional so mutual assignability holds for everything else. Dropped checks for
removed types; added 17 checks for 2026 types. Count 176→150.
types.capabilities.test.ts: import InitializeRequestParamsSchema from legacyWireSchemas.
types.test.ts: outputSchema no longer requires type:object.
mcp.ts: drop ToolExecution/execution (removed from spec).
client.ts: assertCapabilityForMethod switches on RequestMethod (includes legacy).
…or change)
Introduce `Dispatcher<ContextT>` (`packages/core/src/shared/dispatcher.ts`)
holding the request-handler registry, middleware chain, and `dispatch()`
entry point. `Protocol` now composes a `protected readonly dispatcher`
and delegates:
- `setRequestHandler` / `removeRequestHandler` / `assertCanSetRequestHandler`
- `fallbackRequestHandler` (now `dispatcher.fallbackHandler`)
- `_onrequest` invokes `dispatcher.dispatch(request, ctx)` for lookup,
middleware onion, and JSON-RPC response wrapping
The previous `_wrapHandler` override hook is replaced by `dispatcher.use()`:
- `Server` registers `_callToolResultMiddleware` (was `_wrapHandler` for tools/call)
- `Client` registers `_validationMiddleware` (was `_wrapHandler` for
elicitation/create + sampling/createMessage)
Same validation runs on the same paths; the observation point moves from
per-registration wrapping to per-dispatch middleware.
`dispatch()`'s catch block preserves `_onrequest`'s error behavior verbatim:
thrown errors surface their `code` (if a safe integer), `message`, and `data`.
No sanitization is introduced.
Tests:
- `dispatcher.test.ts` added (11 tests for setHandler/dispatch/middleware/error)
- `wrapHandler.test.ts` deleted (covered by rewritten customMethods test)
- `customMethods.test.ts` adapts one test from spying on `_wrapHandler` to
spying via middleware; same assertion (both 2-arg and 3-arg routes through)
No existing test assertions changed.
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-schema-sync branch from ce732ae to 29c4991CompareMay 21, 2026 11:14
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-dispatcher branch from 315684d to 628f0e1CompareMay 21, 2026 11:15
@felixweinberger

Copy link
Copy Markdown
ContributorAuthor

@claude review

Comment on lines +279 to +280
/** The handler registry. Both `_onrequest` and `_dispatchStateless` route through it. */
protected readonly dispatcher = new Dispatcher<ContextT>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Several new comments and JSDoc blocks reference _dispatchStateless and "the 2026-06 dispatch path" — neither of which exists in this PR or the codebase yet (they are planned for #2131). A reader who greps for _dispatchStateless after merge will find nothing. Consider qualifying the references (e.g. "and the forthcoming _dispatchStateless path (#2131)") or deferring them to the PR that introduces the second path. Affects protocol.ts:279, dispatcher.ts:55, client.ts:288, and server.ts:182.

Extended reasoning...

What the issue is. Four new comments added by this PR reference code that does not exist yet:

  • packages/core/src/shared/protocol.ts:279/** The handler registry. Both \_onrequest` and `_dispatchStateless` route through it. */`
  • packages/core/src/shared/dispatcher.ts (class JSDoc, ~line 55) — "Both the legacy connect/_onrequest path and the 2026-06 stateless dispatch path route through dispatch."
  • packages/client/src/client/client.ts:288 — "…so it applies to both the legacy `_onrequest` path and the 2026-06 dispatch path."
  • packages/server/src/server/server.ts:182 — "…so it applies to both the legacy `_onrequest` path and the 2026-06 dispatch path."

_dispatchStateless appears nowhere in the codebase except inside the comment at protocol.ts:279, and there is no "2026-06 dispatch path" implemented anywhere. These are forward references to PR #2131.

Why it matters. The repo's own review guidance flags "prose that promises behavior the code no longer ships" and "any claim the diff doesn't back." Once this PR merges, a contributor reading Protocol who sees "Both `_onrequest` and `_dispatchStateless` route through it" and greps for _dispatchStateless will find zero hits. They have no way to know whether the symbol was renamed, removed, or never landed. _onrequest already calls _onrequest legacy — there is no "legacy" vs. "new" distinction observable in the merged code, so the dichotomy the comments draw doesn't exist for a reader of the codebase at this commit.

Step-by-step proof.

  1. Merge refactor(core): extract Dispatcher; Protocol composes it #2130 in isolation.
  2. Open packages/core/src/shared/protocol.ts at line 279: /** The handler registry. Both \_onrequest` and `_dispatchStateless` route through it. */`
  3. Run grep -rn _dispatchStateless packages/ → only the comment itself matches. There is no method, no export, no test.
  4. Open packages/server/src/server/server.ts:182: "…applies to both the legacy `_onrequest` path and the 2026-06 dispatch path." → there is exactly one path, _onrequest. Nothing distinguishes it as "legacy".

Why this is a nit and not a blocker. The PR description openly states this is PR 3 of a 7-PR v2-stateless stack, and that #2131 ("HTTP-stateless") introduces _dispatchStateless. The author and reviewers have full stack context, the comments are accurate as a description of intent, and they will become literally true once #2131 merges. There is zero runtime impact. But comments live in the merged tree, not in the PR description — and PRs in a stack don't always all land, or land in order.

How to fix. Either qualify the references so they are true at merge time, e.g.:

/** The handler registry. \`_onrequest\` routes through it (and the forthcoming \`_dispatchStateless\` path, #2131). */

or describe only what exists today and let #2131 update the comments when it adds the second path:

/** The handler registry. \`_onrequest\` routes through it. */

The first option preserves the useful forward signal for reviewers of this stack; the second is the most conservative.

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

Labels

v2-stateless2026-06 SDK: Protocol decomposition + SEP alignment (request-first / stateless)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@felixweinberger
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

refactor(core): extract Dispatcher; Protocol composes it - #2130

Closed
felixweinberger wants to merge 4 commits into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher
Closed

refactor(core): extract Dispatcher; Protocol composes it#2130
felixweinberger wants to merge 4 commits into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher

Conversation

@felixweinberger

@felixweinbergerfelixweinberger commented May 20, 2026

Copy link
Copy Markdown
Contributor

2026-06 stateless stack (v2-stateless label):

#PR
1#2128tasks-delete (mechanical)
2#2129schema-sync (mechanical)
3#2130Dispatcher extraction (zero-Δ refactor)
4#2131HTTP-stateless (the substantive review)
5#2132stdio/InMemory transports (additive)
6#2133docs + changeset
7#2134LegacyServer/LegacyClient extraction

Extracts Dispatcher<Ctx> (handler map + middleware chain + dispatch()) from Protocol. Protocol now composes one; setRequestHandler/_onrequest/fallbackRequestHandler delegate. The _wrapHandler override hook becomes dispatcher.use(middleware).

Zero behavior change.dispatch()'s catch block preserves _onrequest's error mapping verbatim. No existing test assertions changed.

Motivation and Context

Enabling refactor for SEP-2575: _dispatchStateless (next PR) needs to invoke the same handler map + middleware that legacy _onrequest uses, without going through Protocol's session machinery.

How Has This Been Tested?

pnpm test:all — count delta is dispatcher.test.ts (+11) and wrapHandler.test.ts (−1, redundant). No existing assertion changed.

Breaking Changes

Protocol._wrapHandler override hook removed (was protected). Subclasses use this.dispatcher.use(mw) instead.

Types of changes

  • Breaking change

Checklist

  • My code follows the repository's style guidelines
  • New and existing tests pass locally

@changeset-bot

changeset-botBot commented May 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 628f0e1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
NameType
@modelcontextprotocol/coreMajor

Not sure what this means? Click here to learn what changesets are.

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

@pkg-pr-new

pkg-pr-newBot commented May 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@2130

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@2130

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@2130

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@2130

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@2130

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@2130

commit: 628f0e1

@felixweinberger

Copy link
Copy Markdown
ContributorAuthor

@claude review

@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-schema-sync branch from c67522f to ce732aeCompareMay 21, 2026 10:42
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-dispatcher branch from 09fc142 to 315684dCompareMay 21, 2026 10:42
Comment on lines +159 to +161
* `Protocol._onrequest` prior to extraction.
*/
async dispatch(request: JSONRPCRequest, ctx: ContextT): Promise<JSONRPCResponse | JSONRPCErrorResponse> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Type-precision nit: Dispatcher.dispatch() declares Promise<JSONRPCResponse | JSONRPCErrorResponse>, but JSONRPCResponse is already the union JSONRPCResultResponse | JSONRPCErrorResponse (schemas.ts:170), so the trailing | JSONRPCErrorResponse is redundant. Likewise okResponse() always builds a success response but is typed as returning the wider JSONRPCResponse union instead of JSONRPCResultResponse. No runtime impact — purely a clarity/precision improvement.

Extended reasoning...

The redundant union.Dispatcher.dispatch() (packages/core/src/shared/dispatcher.ts:159) declares its return type as Promise<JSONRPCResponse | JSONRPCErrorResponse>. But JSONRPCResponse is itself already a union: packages/core/src/types/schemas.ts:170 defines JSONRPCResponseSchema = z.union([JSONRPCResultResponseSchema, JSONRPCErrorResponseSchema]), and spec.types.ts:257 confirms type JSONRPCResponse = JSONRPCResultResponse | JSONRPCErrorResponse. TypeScript collapses A | B where B ⊆ A to just A, so the declared type is exactly equivalent to Promise<JSONRPCResponse> — the | JSONRPCErrorResponse adds nothing.

The over-wide okResponse() type.okResponse() (packages/core/src/shared/dispatcher.ts:195) unconditionally builds { jsonrpc, id, result } — always a success-shaped response. Yet it's typed as returning JSONRPCResponse (the success-or-error union) when the precise type is JSONRPCResultResponse. A caller that wanted to access .result directly would be forced to narrow with a type guard even though the function can never produce an error variant.

Why this isn't a behavior bug. Both signatures are correct in the sense that the actual return value is assignable to the declared type — they're just wider than necessary. Every current caller (Protocol._onrequest in protocol.ts) immediately passes the result to transport.send(), which accepts the full JSONRPCMessage union, so nothing observable changes. The pre-PR _onrequest code also typed its locally-built success object as const response: JSONRPCResponse, so this carries over a pre-existing widening — but since the PR is introducing these new exported helpers in a brand-new file, it's a good moment to get the types exactly right.

Why fix it.okResponse and errorResponse are now exported from packages/core (export * from './shared/dispatcher.js' in index.ts), so they become public-ish API for sibling packages and the upcoming _dispatchStateless path. Tighter return types give downstream callers better narrowing for free and document the contract ("this always succeeds") without needing a comment.

Concrete walk-through.

  1. A future consumer calls const r = okResponse(1, { tools: [] }).
  2. They want to read r.result. With the current signature r: JSONRPCResponse, TypeScript reports Property 'result' does not exist on type 'JSONRPCErrorResponse' and forces an if ('result' in r) guard.
  3. With r: JSONRPCResultResponse, r.result type-checks directly — matching the function's actual guaranteed behavior.

Suggested fix.

asyncdispatch(request: JSONRPCRequest,ctx: ContextT): Promise<JSONRPCResponse>{}exportfunctionokResponse(id: JSONRPCRequest['id'],result: Result): JSONRPCResultResponse{}

errorResponse is already correctly narrowed to JSONRPCErrorResponse, so only these two annotations need to change. Filed as a nit since there's zero runtime impact.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving as-is for this PR. The wider types match the pre-existing convention and every caller passes the result straight to transport.send(); will tighten if a downstream caller needs the narrowing.

Comment threadCLAUDE.md Outdated
…ences)
Removes the 2025-11 experimental tasks side-channel through Protocol:
TaskManager, processInbound*/processOutbound*, task interception, the
experimental.tasks.* client/server accessors, and all task-augmented
request handling.
Also extends beyond the implementation deletion to scrub remaining
references in examples, docs, and comments. The only task-related
symbol remaining in packages/*.ts is `taskSupport` in ToolExecutionSchema,
kept solely to match spec.types.ts (which still declares it); both are
removed together in the next commit (spec regen).
CHANGELOG entries are preserved (historical record). Migration docs
retain a brief removal note. `microtask`/`platformBackgroundTask` are
JS/platform terminology, not MCP tasks.
Satisfies: SEP-2663 (core-removal half; tasks are now Extensions Track).
pnpm fetch:spec-types. Spec moved 44 commits since last regen.
Brings:
- RequestMetaObject with namespaced io.modelcontextprotocol/* keys (required protocolVersion/clientInfo/clientCapabilities)
- Result.resultType: "complete" | "input_required" (required)
- DiscoverRequest/Result, InputRequiredResult, InputRequest/Response/s, InputResponseRequestParams
- SubscriptionsListenRequest, SubscriptionFilter, SubscriptionsAcknowledgedNotification
- UnsupportedProtocolVersionError, MissingRequiredClientCapabilityError (-32003)
Removes (moved to extension or removed from spec):
- InitializeRequest/Result, InitializedNotification, PingRequest, SetLevelRequest
- SubscribeRequest, UnsubscribeRequest, RootsListChangedNotification
- All Task* types, taskSupport, TaskAugmentedRequestParams
Typecheck broken pending next commit (schemas.ts/guards.ts reference removed types).
Isolated for diff clarity per plan §10.
…emas
schemas.ts:
- Result/Notification _meta now use plain MetaObject (not RequestMetaSchema)
- ResultTypeSchema, resultType optional on ResultSchema (BC: pre-2026 omits it)
- RequestMetaSchema kept simple {progressToken?}; namespaced io.modelcontextprotocol/*
keys validated by parseClientMeta in P2, not here (TS2589 depth budget)
- DiscoverRequest/Result, UnsupportedProtocolVersionErrorData,
MissingRequiredClientCapabilityErrorData
- SubscriptionFilter, SubscriptionsListenRequest/Params,
SubscriptionsAcknowledgedNotification/Params
- InputRequest/Response/s, InputRequiredResult, InputResponseRequestParams
- CacheableResult/CacheScope (SEP-2243), PaginatedResult extends CacheableResult,
ReadResourceResult extends CacheableResult
- structuredContent: unknown (was Record<string,unknown>)
- Tool inputSchema/outputSchema loosened to match spec (open record + type:object on input)
- ToolExecution removed (only had taskSupport)
- ClientRequest/ServerNotification/etc unions = 2026 spec methods only
- registerLegacySchemas() lets legacyWireSchemas merge into runtime lookup maps
- Dropped Initialize*/Ping/SetLevel/Subscribe/Unsubscribe/RootsListChanged
NEW legacyWireSchemas.ts:
- LegacyRequestParams/Result/RequestMetaObject base types (no namespaced keys, no resultType)
- TS interfaces + zod for Initialize/Ping/SetLevel/Subscribe/Unsubscribe/RootsListChanged
- legacyRequest/Result/NotificationSchemas maps; registerLegacySchemas() side-effect on import
types.ts: re-exports legacy types; RequestTypeMap/NotificationTypeMap/ResultTypeMap
merge legacy methods. Adds Discover*/InputRequired*/Subscriptions*/Cache* types.
guards.ts: isInitializeRequest/isInitializedNotification import from legacyWireSchemas.
specTypeSchema.ts: allowlist updated (drop legacy, add 2026).
spec.types.test.ts: Relax<T> helper makes spec-required PermissiveKeys
(_meta/resultType/ttlMs/cacheScope/inputResponses/requestState/io.modelcontextprotocol/*)
optional so mutual assignability holds for everything else. Dropped checks for
removed types; added 17 checks for 2026 types. Count 176→150.
types.capabilities.test.ts: import InitializeRequestParamsSchema from legacyWireSchemas.
types.test.ts: outputSchema no longer requires type:object.
mcp.ts: drop ToolExecution/execution (removed from spec).
client.ts: assertCapabilityForMethod switches on RequestMethod (includes legacy).
…or change)
Introduce `Dispatcher<ContextT>` (`packages/core/src/shared/dispatcher.ts`)
holding the request-handler registry, middleware chain, and `dispatch()`
entry point. `Protocol` now composes a `protected readonly dispatcher`
and delegates:
- `setRequestHandler` / `removeRequestHandler` / `assertCanSetRequestHandler`
- `fallbackRequestHandler` (now `dispatcher.fallbackHandler`)
- `_onrequest` invokes `dispatcher.dispatch(request, ctx)` for lookup,
middleware onion, and JSON-RPC response wrapping
The previous `_wrapHandler` override hook is replaced by `dispatcher.use()`:
- `Server` registers `_callToolResultMiddleware` (was `_wrapHandler` for tools/call)
- `Client` registers `_validationMiddleware` (was `_wrapHandler` for
elicitation/create + sampling/createMessage)
Same validation runs on the same paths; the observation point moves from
per-registration wrapping to per-dispatch middleware.
`dispatch()`'s catch block preserves `_onrequest`'s error behavior verbatim:
thrown errors surface their `code` (if a safe integer), `message`, and `data`.
No sanitization is introduced.
Tests:
- `dispatcher.test.ts` added (11 tests for setHandler/dispatch/middleware/error)
- `wrapHandler.test.ts` deleted (covered by rewritten customMethods test)
- `customMethods.test.ts` adapts one test from spying on `_wrapHandler` to
spying via middleware; same assertion (both 2-arg and 3-arg routes through)
No existing test assertions changed.
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-schema-sync branch from ce732ae to 29c4991CompareMay 21, 2026 11:14
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-dispatcher branch from 315684d to 628f0e1CompareMay 21, 2026 11:15
@felixweinberger

Copy link
Copy Markdown
ContributorAuthor

@claude review

Comment on lines +279 to +280
/** The handler registry. Both `_onrequest` and `_dispatchStateless` route through it. */
protected readonly dispatcher = new Dispatcher<ContextT>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Several new comments and JSDoc blocks reference _dispatchStateless and "the 2026-06 dispatch path" — neither of which exists in this PR or the codebase yet (they are planned for #2131). A reader who greps for _dispatchStateless after merge will find nothing. Consider qualifying the references (e.g. "and the forthcoming _dispatchStateless path (#2131)") or deferring them to the PR that introduces the second path. Affects protocol.ts:279, dispatcher.ts:55, client.ts:288, and server.ts:182.

Extended reasoning...

What the issue is. Four new comments added by this PR reference code that does not exist yet:

  • packages/core/src/shared/protocol.ts:279/** The handler registry. Both \_onrequest` and `_dispatchStateless` route through it. */`
  • packages/core/src/shared/dispatcher.ts (class JSDoc, ~line 55) — "Both the legacy connect/_onrequest path and the 2026-06 stateless dispatch path route through dispatch."
  • packages/client/src/client/client.ts:288 — "…so it applies to both the legacy `_onrequest` path and the 2026-06 dispatch path."
  • packages/server/src/server/server.ts:182 — "…so it applies to both the legacy `_onrequest` path and the 2026-06 dispatch path."

_dispatchStateless appears nowhere in the codebase except inside the comment at protocol.ts:279, and there is no "2026-06 dispatch path" implemented anywhere. These are forward references to PR #2131.

Why it matters. The repo's own review guidance flags "prose that promises behavior the code no longer ships" and "any claim the diff doesn't back." Once this PR merges, a contributor reading Protocol who sees "Both `_onrequest` and `_dispatchStateless` route through it" and greps for _dispatchStateless will find zero hits. They have no way to know whether the symbol was renamed, removed, or never landed. _onrequest already calls _onrequest legacy — there is no "legacy" vs. "new" distinction observable in the merged code, so the dichotomy the comments draw doesn't exist for a reader of the codebase at this commit.

Step-by-step proof.

  1. Merge refactor(core): extract Dispatcher; Protocol composes it #2130 in isolation.
  2. Open packages/core/src/shared/protocol.ts at line 279: /** The handler registry. Both \_onrequest` and `_dispatchStateless` route through it. */`
  3. Run grep -rn _dispatchStateless packages/ → only the comment itself matches. There is no method, no export, no test.
  4. Open packages/server/src/server/server.ts:182: "…applies to both the legacy `_onrequest` path and the 2026-06 dispatch path." → there is exactly one path, _onrequest. Nothing distinguishes it as "legacy".

Why this is a nit and not a blocker. The PR description openly states this is PR 3 of a 7-PR v2-stateless stack, and that #2131 ("HTTP-stateless") introduces _dispatchStateless. The author and reviewers have full stack context, the comments are accurate as a description of intent, and they will become literally true once #2131 merges. There is zero runtime impact. But comments live in the merged tree, not in the PR description — and PRs in a stack don't always all land, or land in order.

How to fix. Either qualify the references so they are true at merge time, e.g.:

/** The handler registry. \`_onrequest\` routes through it (and the forthcoming \`_dispatchStateless\` path, #2131). */

or describe only what exists today and let #2131 update the comments when it adds the second path:

/** The handler registry. \`_onrequest\` routes through it. */

The first option preserves the useful forward signal for reviewers of this stack; the second is the most conservative.

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

Labels

v2-stateless2026-06 SDK: Protocol decomposition + SEP alignment (request-first / stateless)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@felixweinberger
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

refactor(core): extract Dispatcher; Protocol composes it - #2130

Closed
felixweinberger wants to merge 4 commits into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher
Closed

refactor(core): extract Dispatcher; Protocol composes it#2130
felixweinberger wants to merge 4 commits into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher

Conversation

@felixweinberger

@felixweinbergerfelixweinberger commented May 20, 2026

Copy link
Copy Markdown
Contributor

2026-06 stateless stack (v2-stateless label):

#PR
1#2128tasks-delete (mechanical)
2#2129schema-sync (mechanical)
3#2130Dispatcher extraction (zero-Δ refactor)
4#2131HTTP-stateless (the substantive review)
5#2132stdio/InMemory transports (additive)
6#2133docs + changeset
7#2134LegacyServer/LegacyClient extraction

Extracts Dispatcher<Ctx> (handler map + middleware chain + dispatch()) from Protocol. Protocol now composes one; setRequestHandler/_onrequest/fallbackRequestHandler delegate. The _wrapHandler override hook becomes dispatcher.use(middleware).

Zero behavior change.dispatch()'s catch block preserves _onrequest's error mapping verbatim. No existing test assertions changed.

Motivation and Context

Enabling refactor for SEP-2575: _dispatchStateless (next PR) needs to invoke the same handler map + middleware that legacy _onrequest uses, without going through Protocol's session machinery.

How Has This Been Tested?

pnpm test:all — count delta is dispatcher.test.ts (+11) and wrapHandler.test.ts (−1, redundant). No existing assertion changed.

Breaking Changes

Protocol._wrapHandler override hook removed (was protected). Subclasses use this.dispatcher.use(mw) instead.

Types of changes

  • Breaking change

Checklist

  • My code follows the repository's style guidelines
  • New and existing tests pass locally

@changeset-bot

changeset-botBot commented May 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 628f0e1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
NameType
@modelcontextprotocol/coreMajor

Not sure what this means? Click here to learn what changesets are.

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

@pkg-pr-new

pkg-pr-newBot commented May 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@2130

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@2130

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@2130

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@2130

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@2130

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@2130

commit: 628f0e1

@felixweinberger

Copy link
Copy Markdown
ContributorAuthor

@claude review

@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-schema-sync branch from c67522f to ce732aeCompareMay 21, 2026 10:42
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-dispatcher branch from 09fc142 to 315684dCompareMay 21, 2026 10:42
Comment on lines +159 to +161
* `Protocol._onrequest` prior to extraction.
*/
async dispatch(request: JSONRPCRequest, ctx: ContextT): Promise<JSONRPCResponse | JSONRPCErrorResponse> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Type-precision nit: Dispatcher.dispatch() declares Promise<JSONRPCResponse | JSONRPCErrorResponse>, but JSONRPCResponse is already the union JSONRPCResultResponse | JSONRPCErrorResponse (schemas.ts:170), so the trailing | JSONRPCErrorResponse is redundant. Likewise okResponse() always builds a success response but is typed as returning the wider JSONRPCResponse union instead of JSONRPCResultResponse. No runtime impact — purely a clarity/precision improvement.

Extended reasoning...

The redundant union.Dispatcher.dispatch() (packages/core/src/shared/dispatcher.ts:159) declares its return type as Promise<JSONRPCResponse | JSONRPCErrorResponse>. But JSONRPCResponse is itself already a union: packages/core/src/types/schemas.ts:170 defines JSONRPCResponseSchema = z.union([JSONRPCResultResponseSchema, JSONRPCErrorResponseSchema]), and spec.types.ts:257 confirms type JSONRPCResponse = JSONRPCResultResponse | JSONRPCErrorResponse. TypeScript collapses A | B where B ⊆ A to just A, so the declared type is exactly equivalent to Promise<JSONRPCResponse> — the | JSONRPCErrorResponse adds nothing.

The over-wide okResponse() type.okResponse() (packages/core/src/shared/dispatcher.ts:195) unconditionally builds { jsonrpc, id, result } — always a success-shaped response. Yet it's typed as returning JSONRPCResponse (the success-or-error union) when the precise type is JSONRPCResultResponse. A caller that wanted to access .result directly would be forced to narrow with a type guard even though the function can never produce an error variant.

Why this isn't a behavior bug. Both signatures are correct in the sense that the actual return value is assignable to the declared type — they're just wider than necessary. Every current caller (Protocol._onrequest in protocol.ts) immediately passes the result to transport.send(), which accepts the full JSONRPCMessage union, so nothing observable changes. The pre-PR _onrequest code also typed its locally-built success object as const response: JSONRPCResponse, so this carries over a pre-existing widening — but since the PR is introducing these new exported helpers in a brand-new file, it's a good moment to get the types exactly right.

Why fix it.okResponse and errorResponse are now exported from packages/core (export * from './shared/dispatcher.js' in index.ts), so they become public-ish API for sibling packages and the upcoming _dispatchStateless path. Tighter return types give downstream callers better narrowing for free and document the contract ("this always succeeds") without needing a comment.

Concrete walk-through.

  1. A future consumer calls const r = okResponse(1, { tools: [] }).
  2. They want to read r.result. With the current signature r: JSONRPCResponse, TypeScript reports Property 'result' does not exist on type 'JSONRPCErrorResponse' and forces an if ('result' in r) guard.
  3. With r: JSONRPCResultResponse, r.result type-checks directly — matching the function's actual guaranteed behavior.

Suggested fix.

asyncdispatch(request: JSONRPCRequest,ctx: ContextT): Promise<JSONRPCResponse>{}exportfunctionokResponse(id: JSONRPCRequest['id'],result: Result): JSONRPCResultResponse{}

errorResponse is already correctly narrowed to JSONRPCErrorResponse, so only these two annotations need to change. Filed as a nit since there's zero runtime impact.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving as-is for this PR. The wider types match the pre-existing convention and every caller passes the result straight to transport.send(); will tighten if a downstream caller needs the narrowing.

Comment threadCLAUDE.md Outdated
…ences)
Removes the 2025-11 experimental tasks side-channel through Protocol:
TaskManager, processInbound*/processOutbound*, task interception, the
experimental.tasks.* client/server accessors, and all task-augmented
request handling.
Also extends beyond the implementation deletion to scrub remaining
references in examples, docs, and comments. The only task-related
symbol remaining in packages/*.ts is `taskSupport` in ToolExecutionSchema,
kept solely to match spec.types.ts (which still declares it); both are
removed together in the next commit (spec regen).
CHANGELOG entries are preserved (historical record). Migration docs
retain a brief removal note. `microtask`/`platformBackgroundTask` are
JS/platform terminology, not MCP tasks.
Satisfies: SEP-2663 (core-removal half; tasks are now Extensions Track).
pnpm fetch:spec-types. Spec moved 44 commits since last regen.
Brings:
- RequestMetaObject with namespaced io.modelcontextprotocol/* keys (required protocolVersion/clientInfo/clientCapabilities)
- Result.resultType: "complete" | "input_required" (required)
- DiscoverRequest/Result, InputRequiredResult, InputRequest/Response/s, InputResponseRequestParams
- SubscriptionsListenRequest, SubscriptionFilter, SubscriptionsAcknowledgedNotification
- UnsupportedProtocolVersionError, MissingRequiredClientCapabilityError (-32003)
Removes (moved to extension or removed from spec):
- InitializeRequest/Result, InitializedNotification, PingRequest, SetLevelRequest
- SubscribeRequest, UnsubscribeRequest, RootsListChangedNotification
- All Task* types, taskSupport, TaskAugmentedRequestParams
Typecheck broken pending next commit (schemas.ts/guards.ts reference removed types).
Isolated for diff clarity per plan §10.
…emas
schemas.ts:
- Result/Notification _meta now use plain MetaObject (not RequestMetaSchema)
- ResultTypeSchema, resultType optional on ResultSchema (BC: pre-2026 omits it)
- RequestMetaSchema kept simple {progressToken?}; namespaced io.modelcontextprotocol/*
keys validated by parseClientMeta in P2, not here (TS2589 depth budget)
- DiscoverRequest/Result, UnsupportedProtocolVersionErrorData,
MissingRequiredClientCapabilityErrorData
- SubscriptionFilter, SubscriptionsListenRequest/Params,
SubscriptionsAcknowledgedNotification/Params
- InputRequest/Response/s, InputRequiredResult, InputResponseRequestParams
- CacheableResult/CacheScope (SEP-2243), PaginatedResult extends CacheableResult,
ReadResourceResult extends CacheableResult
- structuredContent: unknown (was Record<string,unknown>)
- Tool inputSchema/outputSchema loosened to match spec (open record + type:object on input)
- ToolExecution removed (only had taskSupport)
- ClientRequest/ServerNotification/etc unions = 2026 spec methods only
- registerLegacySchemas() lets legacyWireSchemas merge into runtime lookup maps
- Dropped Initialize*/Ping/SetLevel/Subscribe/Unsubscribe/RootsListChanged
NEW legacyWireSchemas.ts:
- LegacyRequestParams/Result/RequestMetaObject base types (no namespaced keys, no resultType)
- TS interfaces + zod for Initialize/Ping/SetLevel/Subscribe/Unsubscribe/RootsListChanged
- legacyRequest/Result/NotificationSchemas maps; registerLegacySchemas() side-effect on import
types.ts: re-exports legacy types; RequestTypeMap/NotificationTypeMap/ResultTypeMap
merge legacy methods. Adds Discover*/InputRequired*/Subscriptions*/Cache* types.
guards.ts: isInitializeRequest/isInitializedNotification import from legacyWireSchemas.
specTypeSchema.ts: allowlist updated (drop legacy, add 2026).
spec.types.test.ts: Relax<T> helper makes spec-required PermissiveKeys
(_meta/resultType/ttlMs/cacheScope/inputResponses/requestState/io.modelcontextprotocol/*)
optional so mutual assignability holds for everything else. Dropped checks for
removed types; added 17 checks for 2026 types. Count 176→150.
types.capabilities.test.ts: import InitializeRequestParamsSchema from legacyWireSchemas.
types.test.ts: outputSchema no longer requires type:object.
mcp.ts: drop ToolExecution/execution (removed from spec).
client.ts: assertCapabilityForMethod switches on RequestMethod (includes legacy).
…or change)
Introduce `Dispatcher<ContextT>` (`packages/core/src/shared/dispatcher.ts`)
holding the request-handler registry, middleware chain, and `dispatch()`
entry point. `Protocol` now composes a `protected readonly dispatcher`
and delegates:
- `setRequestHandler` / `removeRequestHandler` / `assertCanSetRequestHandler`
- `fallbackRequestHandler` (now `dispatcher.fallbackHandler`)
- `_onrequest` invokes `dispatcher.dispatch(request, ctx)` for lookup,
middleware onion, and JSON-RPC response wrapping
The previous `_wrapHandler` override hook is replaced by `dispatcher.use()`:
- `Server` registers `_callToolResultMiddleware` (was `_wrapHandler` for tools/call)
- `Client` registers `_validationMiddleware` (was `_wrapHandler` for
elicitation/create + sampling/createMessage)
Same validation runs on the same paths; the observation point moves from
per-registration wrapping to per-dispatch middleware.
`dispatch()`'s catch block preserves `_onrequest`'s error behavior verbatim:
thrown errors surface their `code` (if a safe integer), `message`, and `data`.
No sanitization is introduced.
Tests:
- `dispatcher.test.ts` added (11 tests for setHandler/dispatch/middleware/error)
- `wrapHandler.test.ts` deleted (covered by rewritten customMethods test)
- `customMethods.test.ts` adapts one test from spying on `_wrapHandler` to
spying via middleware; same assertion (both 2-arg and 3-arg routes through)
No existing test assertions changed.
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-schema-sync branch from ce732ae to 29c4991CompareMay 21, 2026 11:14
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-dispatcher branch from 315684d to 628f0e1CompareMay 21, 2026 11:15
@felixweinberger

Copy link
Copy Markdown
ContributorAuthor

@claude review

Comment on lines +279 to +280
/** The handler registry. Both `_onrequest` and `_dispatchStateless` route through it. */
protected readonly dispatcher = new Dispatcher<ContextT>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Several new comments and JSDoc blocks reference _dispatchStateless and "the 2026-06 dispatch path" — neither of which exists in this PR or the codebase yet (they are planned for #2131). A reader who greps for _dispatchStateless after merge will find nothing. Consider qualifying the references (e.g. "and the forthcoming _dispatchStateless path (#2131)") or deferring them to the PR that introduces the second path. Affects protocol.ts:279, dispatcher.ts:55, client.ts:288, and server.ts:182.

Extended reasoning...

What the issue is. Four new comments added by this PR reference code that does not exist yet:

  • packages/core/src/shared/protocol.ts:279/** The handler registry. Both \_onrequest` and `_dispatchStateless` route through it. */`
  • packages/core/src/shared/dispatcher.ts (class JSDoc, ~line 55) — "Both the legacy connect/_onrequest path and the 2026-06 stateless dispatch path route through dispatch."
  • packages/client/src/client/client.ts:288 — "…so it applies to both the legacy `_onrequest` path and the 2026-06 dispatch path."
  • packages/server/src/server/server.ts:182 — "…so it applies to both the legacy `_onrequest` path and the 2026-06 dispatch path."

_dispatchStateless appears nowhere in the codebase except inside the comment at protocol.ts:279, and there is no "2026-06 dispatch path" implemented anywhere. These are forward references to PR #2131.

Why it matters. The repo's own review guidance flags "prose that promises behavior the code no longer ships" and "any claim the diff doesn't back." Once this PR merges, a contributor reading Protocol who sees "Both `_onrequest` and `_dispatchStateless` route through it" and greps for _dispatchStateless will find zero hits. They have no way to know whether the symbol was renamed, removed, or never landed. _onrequest already calls _onrequest legacy — there is no "legacy" vs. "new" distinction observable in the merged code, so the dichotomy the comments draw doesn't exist for a reader of the codebase at this commit.

Step-by-step proof.

  1. Merge refactor(core): extract Dispatcher; Protocol composes it #2130 in isolation.
  2. Open packages/core/src/shared/protocol.ts at line 279: /** The handler registry. Both \_onrequest` and `_dispatchStateless` route through it. */`
  3. Run grep -rn _dispatchStateless packages/ → only the comment itself matches. There is no method, no export, no test.
  4. Open packages/server/src/server/server.ts:182: "…applies to both the legacy `_onrequest` path and the 2026-06 dispatch path." → there is exactly one path, _onrequest. Nothing distinguishes it as "legacy".

Why this is a nit and not a blocker. The PR description openly states this is PR 3 of a 7-PR v2-stateless stack, and that #2131 ("HTTP-stateless") introduces _dispatchStateless. The author and reviewers have full stack context, the comments are accurate as a description of intent, and they will become literally true once #2131 merges. There is zero runtime impact. But comments live in the merged tree, not in the PR description — and PRs in a stack don't always all land, or land in order.

How to fix. Either qualify the references so they are true at merge time, e.g.:

/** The handler registry. \`_onrequest\` routes through it (and the forthcoming \`_dispatchStateless\` path, #2131). */

or describe only what exists today and let #2131 update the comments when it adds the second path:

/** The handler registry. \`_onrequest\` routes through it. */

The first option preserves the useful forward signal for reviewers of this stack; the second is the most conservative.

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

Labels

v2-stateless2026-06 SDK: Protocol decomposition + SEP alignment (request-first / stateless)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@felixweinberger
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

refactor(core): extract Dispatcher; Protocol composes it - #2130

Closed
felixweinberger wants to merge 4 commits into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher
Closed

refactor(core): extract Dispatcher; Protocol composes it#2130
felixweinberger wants to merge 4 commits into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher

Conversation

@felixweinberger

@felixweinbergerfelixweinberger commented May 20, 2026

Copy link
Copy Markdown
Contributor

2026-06 stateless stack (v2-stateless label):

#PR
1#2128tasks-delete (mechanical)
2#2129schema-sync (mechanical)
3#2130Dispatcher extraction (zero-Δ refactor)
4#2131HTTP-stateless (the substantive review)
5#2132stdio/InMemory transports (additive)
6#2133docs + changeset
7#2134LegacyServer/LegacyClient extraction

Extracts Dispatcher<Ctx> (handler map + middleware chain + dispatch()) from Protocol. Protocol now composes one; setRequestHandler/_onrequest/fallbackRequestHandler delegate. The _wrapHandler override hook becomes dispatcher.use(middleware).

Zero behavior change.dispatch()'s catch block preserves _onrequest's error mapping verbatim. No existing test assertions changed.

Motivation and Context

Enabling refactor for SEP-2575: _dispatchStateless (next PR) needs to invoke the same handler map + middleware that legacy _onrequest uses, without going through Protocol's session machinery.

How Has This Been Tested?

pnpm test:all — count delta is dispatcher.test.ts (+11) and wrapHandler.test.ts (−1, redundant). No existing assertion changed.

Breaking Changes

Protocol._wrapHandler override hook removed (was protected). Subclasses use this.dispatcher.use(mw) instead.

Types of changes

  • Breaking change

Checklist

  • My code follows the repository's style guidelines
  • New and existing tests pass locally

@changeset-bot

changeset-botBot commented May 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 628f0e1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
NameType
@modelcontextprotocol/coreMajor

Not sure what this means? Click here to learn what changesets are.

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

@pkg-pr-new

pkg-pr-newBot commented May 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@2130

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@2130

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@2130

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@2130

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@2130

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@2130

commit: 628f0e1

@felixweinberger

Copy link
Copy Markdown
ContributorAuthor

@claude review

@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-schema-sync branch from c67522f to ce732aeCompareMay 21, 2026 10:42
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-dispatcher branch from 09fc142 to 315684dCompareMay 21, 2026 10:42
Comment on lines +159 to +161
* `Protocol._onrequest` prior to extraction.
*/
async dispatch(request: JSONRPCRequest, ctx: ContextT): Promise<JSONRPCResponse | JSONRPCErrorResponse> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Type-precision nit: Dispatcher.dispatch() declares Promise<JSONRPCResponse | JSONRPCErrorResponse>, but JSONRPCResponse is already the union JSONRPCResultResponse | JSONRPCErrorResponse (schemas.ts:170), so the trailing | JSONRPCErrorResponse is redundant. Likewise okResponse() always builds a success response but is typed as returning the wider JSONRPCResponse union instead of JSONRPCResultResponse. No runtime impact — purely a clarity/precision improvement.

Extended reasoning...

The redundant union.Dispatcher.dispatch() (packages/core/src/shared/dispatcher.ts:159) declares its return type as Promise<JSONRPCResponse | JSONRPCErrorResponse>. But JSONRPCResponse is itself already a union: packages/core/src/types/schemas.ts:170 defines JSONRPCResponseSchema = z.union([JSONRPCResultResponseSchema, JSONRPCErrorResponseSchema]), and spec.types.ts:257 confirms type JSONRPCResponse = JSONRPCResultResponse | JSONRPCErrorResponse. TypeScript collapses A | B where B ⊆ A to just A, so the declared type is exactly equivalent to Promise<JSONRPCResponse> — the | JSONRPCErrorResponse adds nothing.

The over-wide okResponse() type.okResponse() (packages/core/src/shared/dispatcher.ts:195) unconditionally builds { jsonrpc, id, result } — always a success-shaped response. Yet it's typed as returning JSONRPCResponse (the success-or-error union) when the precise type is JSONRPCResultResponse. A caller that wanted to access .result directly would be forced to narrow with a type guard even though the function can never produce an error variant.

Why this isn't a behavior bug. Both signatures are correct in the sense that the actual return value is assignable to the declared type — they're just wider than necessary. Every current caller (Protocol._onrequest in protocol.ts) immediately passes the result to transport.send(), which accepts the full JSONRPCMessage union, so nothing observable changes. The pre-PR _onrequest code also typed its locally-built success object as const response: JSONRPCResponse, so this carries over a pre-existing widening — but since the PR is introducing these new exported helpers in a brand-new file, it's a good moment to get the types exactly right.

Why fix it.okResponse and errorResponse are now exported from packages/core (export * from './shared/dispatcher.js' in index.ts), so they become public-ish API for sibling packages and the upcoming _dispatchStateless path. Tighter return types give downstream callers better narrowing for free and document the contract ("this always succeeds") without needing a comment.

Concrete walk-through.

  1. A future consumer calls const r = okResponse(1, { tools: [] }).
  2. They want to read r.result. With the current signature r: JSONRPCResponse, TypeScript reports Property 'result' does not exist on type 'JSONRPCErrorResponse' and forces an if ('result' in r) guard.
  3. With r: JSONRPCResultResponse, r.result type-checks directly — matching the function's actual guaranteed behavior.

Suggested fix.

asyncdispatch(request: JSONRPCRequest,ctx: ContextT): Promise<JSONRPCResponse>{}exportfunctionokResponse(id: JSONRPCRequest['id'],result: Result): JSONRPCResultResponse{}

errorResponse is already correctly narrowed to JSONRPCErrorResponse, so only these two annotations need to change. Filed as a nit since there's zero runtime impact.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving as-is for this PR. The wider types match the pre-existing convention and every caller passes the result straight to transport.send(); will tighten if a downstream caller needs the narrowing.

Comment threadCLAUDE.md Outdated
…ences)
Removes the 2025-11 experimental tasks side-channel through Protocol:
TaskManager, processInbound*/processOutbound*, task interception, the
experimental.tasks.* client/server accessors, and all task-augmented
request handling.
Also extends beyond the implementation deletion to scrub remaining
references in examples, docs, and comments. The only task-related
symbol remaining in packages/*.ts is `taskSupport` in ToolExecutionSchema,
kept solely to match spec.types.ts (which still declares it); both are
removed together in the next commit (spec regen).
CHANGELOG entries are preserved (historical record). Migration docs
retain a brief removal note. `microtask`/`platformBackgroundTask` are
JS/platform terminology, not MCP tasks.
Satisfies: SEP-2663 (core-removal half; tasks are now Extensions Track).
pnpm fetch:spec-types. Spec moved 44 commits since last regen.
Brings:
- RequestMetaObject with namespaced io.modelcontextprotocol/* keys (required protocolVersion/clientInfo/clientCapabilities)
- Result.resultType: "complete" | "input_required" (required)
- DiscoverRequest/Result, InputRequiredResult, InputRequest/Response/s, InputResponseRequestParams
- SubscriptionsListenRequest, SubscriptionFilter, SubscriptionsAcknowledgedNotification
- UnsupportedProtocolVersionError, MissingRequiredClientCapabilityError (-32003)
Removes (moved to extension or removed from spec):
- InitializeRequest/Result, InitializedNotification, PingRequest, SetLevelRequest
- SubscribeRequest, UnsubscribeRequest, RootsListChangedNotification
- All Task* types, taskSupport, TaskAugmentedRequestParams
Typecheck broken pending next commit (schemas.ts/guards.ts reference removed types).
Isolated for diff clarity per plan §10.
…emas
schemas.ts:
- Result/Notification _meta now use plain MetaObject (not RequestMetaSchema)
- ResultTypeSchema, resultType optional on ResultSchema (BC: pre-2026 omits it)
- RequestMetaSchema kept simple {progressToken?}; namespaced io.modelcontextprotocol/*
keys validated by parseClientMeta in P2, not here (TS2589 depth budget)
- DiscoverRequest/Result, UnsupportedProtocolVersionErrorData,
MissingRequiredClientCapabilityErrorData
- SubscriptionFilter, SubscriptionsListenRequest/Params,
SubscriptionsAcknowledgedNotification/Params
- InputRequest/Response/s, InputRequiredResult, InputResponseRequestParams
- CacheableResult/CacheScope (SEP-2243), PaginatedResult extends CacheableResult,
ReadResourceResult extends CacheableResult
- structuredContent: unknown (was Record<string,unknown>)
- Tool inputSchema/outputSchema loosened to match spec (open record + type:object on input)
- ToolExecution removed (only had taskSupport)
- ClientRequest/ServerNotification/etc unions = 2026 spec methods only
- registerLegacySchemas() lets legacyWireSchemas merge into runtime lookup maps
- Dropped Initialize*/Ping/SetLevel/Subscribe/Unsubscribe/RootsListChanged
NEW legacyWireSchemas.ts:
- LegacyRequestParams/Result/RequestMetaObject base types (no namespaced keys, no resultType)
- TS interfaces + zod for Initialize/Ping/SetLevel/Subscribe/Unsubscribe/RootsListChanged
- legacyRequest/Result/NotificationSchemas maps; registerLegacySchemas() side-effect on import
types.ts: re-exports legacy types; RequestTypeMap/NotificationTypeMap/ResultTypeMap
merge legacy methods. Adds Discover*/InputRequired*/Subscriptions*/Cache* types.
guards.ts: isInitializeRequest/isInitializedNotification import from legacyWireSchemas.
specTypeSchema.ts: allowlist updated (drop legacy, add 2026).
spec.types.test.ts: Relax<T> helper makes spec-required PermissiveKeys
(_meta/resultType/ttlMs/cacheScope/inputResponses/requestState/io.modelcontextprotocol/*)
optional so mutual assignability holds for everything else. Dropped checks for
removed types; added 17 checks for 2026 types. Count 176→150.
types.capabilities.test.ts: import InitializeRequestParamsSchema from legacyWireSchemas.
types.test.ts: outputSchema no longer requires type:object.
mcp.ts: drop ToolExecution/execution (removed from spec).
client.ts: assertCapabilityForMethod switches on RequestMethod (includes legacy).
…or change)
Introduce `Dispatcher<ContextT>` (`packages/core/src/shared/dispatcher.ts`)
holding the request-handler registry, middleware chain, and `dispatch()`
entry point. `Protocol` now composes a `protected readonly dispatcher`
and delegates:
- `setRequestHandler` / `removeRequestHandler` / `assertCanSetRequestHandler`
- `fallbackRequestHandler` (now `dispatcher.fallbackHandler`)
- `_onrequest` invokes `dispatcher.dispatch(request, ctx)` for lookup,
middleware onion, and JSON-RPC response wrapping
The previous `_wrapHandler` override hook is replaced by `dispatcher.use()`:
- `Server` registers `_callToolResultMiddleware` (was `_wrapHandler` for tools/call)
- `Client` registers `_validationMiddleware` (was `_wrapHandler` for
elicitation/create + sampling/createMessage)
Same validation runs on the same paths; the observation point moves from
per-registration wrapping to per-dispatch middleware.
`dispatch()`'s catch block preserves `_onrequest`'s error behavior verbatim:
thrown errors surface their `code` (if a safe integer), `message`, and `data`.
No sanitization is introduced.
Tests:
- `dispatcher.test.ts` added (11 tests for setHandler/dispatch/middleware/error)
- `wrapHandler.test.ts` deleted (covered by rewritten customMethods test)
- `customMethods.test.ts` adapts one test from spying on `_wrapHandler` to
spying via middleware; same assertion (both 2-arg and 3-arg routes through)
No existing test assertions changed.
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-schema-sync branch from ce732ae to 29c4991CompareMay 21, 2026 11:14
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-dispatcher branch from 315684d to 628f0e1CompareMay 21, 2026 11:15
@felixweinberger

Copy link
Copy Markdown
ContributorAuthor

@claude review

Comment on lines +279 to +280
/** The handler registry. Both `_onrequest` and `_dispatchStateless` route through it. */
protected readonly dispatcher = new Dispatcher<ContextT>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Several new comments and JSDoc blocks reference _dispatchStateless and "the 2026-06 dispatch path" — neither of which exists in this PR or the codebase yet (they are planned for #2131). A reader who greps for _dispatchStateless after merge will find nothing. Consider qualifying the references (e.g. "and the forthcoming _dispatchStateless path (#2131)") or deferring them to the PR that introduces the second path. Affects protocol.ts:279, dispatcher.ts:55, client.ts:288, and server.ts:182.

Extended reasoning...

What the issue is. Four new comments added by this PR reference code that does not exist yet:

  • packages/core/src/shared/protocol.ts:279/** The handler registry. Both \_onrequest` and `_dispatchStateless` route through it. */`
  • packages/core/src/shared/dispatcher.ts (class JSDoc, ~line 55) — "Both the legacy connect/_onrequest path and the 2026-06 stateless dispatch path route through dispatch."
  • packages/client/src/client/client.ts:288 — "…so it applies to both the legacy `_onrequest` path and the 2026-06 dispatch path."
  • packages/server/src/server/server.ts:182 — "…so it applies to both the legacy `_onrequest` path and the 2026-06 dispatch path."

_dispatchStateless appears nowhere in the codebase except inside the comment at protocol.ts:279, and there is no "2026-06 dispatch path" implemented anywhere. These are forward references to PR #2131.

Why it matters. The repo's own review guidance flags "prose that promises behavior the code no longer ships" and "any claim the diff doesn't back." Once this PR merges, a contributor reading Protocol who sees "Both `_onrequest` and `_dispatchStateless` route through it" and greps for _dispatchStateless will find zero hits. They have no way to know whether the symbol was renamed, removed, or never landed. _onrequest already calls _onrequest legacy — there is no "legacy" vs. "new" distinction observable in the merged code, so the dichotomy the comments draw doesn't exist for a reader of the codebase at this commit.

Step-by-step proof.

  1. Merge refactor(core): extract Dispatcher; Protocol composes it #2130 in isolation.
  2. Open packages/core/src/shared/protocol.ts at line 279: /** The handler registry. Both \_onrequest` and `_dispatchStateless` route through it. */`
  3. Run grep -rn _dispatchStateless packages/ → only the comment itself matches. There is no method, no export, no test.
  4. Open packages/server/src/server/server.ts:182: "…applies to both the legacy `_onrequest` path and the 2026-06 dispatch path." → there is exactly one path, _onrequest. Nothing distinguishes it as "legacy".

Why this is a nit and not a blocker. The PR description openly states this is PR 3 of a 7-PR v2-stateless stack, and that #2131 ("HTTP-stateless") introduces _dispatchStateless. The author and reviewers have full stack context, the comments are accurate as a description of intent, and they will become literally true once #2131 merges. There is zero runtime impact. But comments live in the merged tree, not in the PR description — and PRs in a stack don't always all land, or land in order.

How to fix. Either qualify the references so they are true at merge time, e.g.:

/** The handler registry. \`_onrequest\` routes through it (and the forthcoming \`_dispatchStateless\` path, #2131). */

or describe only what exists today and let #2131 update the comments when it adds the second path:

/** The handler registry. \`_onrequest\` routes through it. */

The first option preserves the useful forward signal for reviewers of this stack; the second is the most conservative.

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

Labels

v2-stateless2026-06 SDK: Protocol decomposition + SEP alignment (request-first / stateless)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@felixweinberger
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

refactor(core): extract Dispatcher; Protocol composes it - #2130

Closed
felixweinberger wants to merge 4 commits into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher
Closed

refactor(core): extract Dispatcher; Protocol composes it#2130
felixweinberger wants to merge 4 commits into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher

Conversation

@felixweinberger

@felixweinbergerfelixweinberger commented May 20, 2026

Copy link
Copy Markdown
Contributor

2026-06 stateless stack (v2-stateless label):

#PR
1#2128tasks-delete (mechanical)
2#2129schema-sync (mechanical)
3#2130Dispatcher extraction (zero-Δ refactor)
4#2131HTTP-stateless (the substantive review)
5#2132stdio/InMemory transports (additive)
6#2133docs + changeset
7#2134LegacyServer/LegacyClient extraction

Extracts Dispatcher<Ctx> (handler map + middleware chain + dispatch()) from Protocol. Protocol now composes one; setRequestHandler/_onrequest/fallbackRequestHandler delegate. The _wrapHandler override hook becomes dispatcher.use(middleware).

Zero behavior change.dispatch()'s catch block preserves _onrequest's error mapping verbatim. No existing test assertions changed.

Motivation and Context

Enabling refactor for SEP-2575: _dispatchStateless (next PR) needs to invoke the same handler map + middleware that legacy _onrequest uses, without going through Protocol's session machinery.

How Has This Been Tested?

pnpm test:all — count delta is dispatcher.test.ts (+11) and wrapHandler.test.ts (−1, redundant). No existing assertion changed.

Breaking Changes

Protocol._wrapHandler override hook removed (was protected). Subclasses use this.dispatcher.use(mw) instead.

Types of changes

  • Breaking change

Checklist

  • My code follows the repository's style guidelines
  • New and existing tests pass locally

@changeset-bot

changeset-botBot commented May 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 628f0e1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
NameType
@modelcontextprotocol/coreMajor

Not sure what this means? Click here to learn what changesets are.

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

@pkg-pr-new

pkg-pr-newBot commented May 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@2130

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@2130

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@2130

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@2130

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@2130

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@2130

commit: 628f0e1

@felixweinberger

Copy link
Copy Markdown
ContributorAuthor

@claude review

@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-schema-sync branch from c67522f to ce732aeCompareMay 21, 2026 10:42
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-dispatcher branch from 09fc142 to 315684dCompareMay 21, 2026 10:42
Comment on lines +159 to +161
* `Protocol._onrequest` prior to extraction.
*/
async dispatch(request: JSONRPCRequest, ctx: ContextT): Promise<JSONRPCResponse | JSONRPCErrorResponse> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Type-precision nit: Dispatcher.dispatch() declares Promise<JSONRPCResponse | JSONRPCErrorResponse>, but JSONRPCResponse is already the union JSONRPCResultResponse | JSONRPCErrorResponse (schemas.ts:170), so the trailing | JSONRPCErrorResponse is redundant. Likewise okResponse() always builds a success response but is typed as returning the wider JSONRPCResponse union instead of JSONRPCResultResponse. No runtime impact — purely a clarity/precision improvement.

Extended reasoning...

The redundant union.Dispatcher.dispatch() (packages/core/src/shared/dispatcher.ts:159) declares its return type as Promise<JSONRPCResponse | JSONRPCErrorResponse>. But JSONRPCResponse is itself already a union: packages/core/src/types/schemas.ts:170 defines JSONRPCResponseSchema = z.union([JSONRPCResultResponseSchema, JSONRPCErrorResponseSchema]), and spec.types.ts:257 confirms type JSONRPCResponse = JSONRPCResultResponse | JSONRPCErrorResponse. TypeScript collapses A | B where B ⊆ A to just A, so the declared type is exactly equivalent to Promise<JSONRPCResponse> — the | JSONRPCErrorResponse adds nothing.

The over-wide okResponse() type.okResponse() (packages/core/src/shared/dispatcher.ts:195) unconditionally builds { jsonrpc, id, result } — always a success-shaped response. Yet it's typed as returning JSONRPCResponse (the success-or-error union) when the precise type is JSONRPCResultResponse. A caller that wanted to access .result directly would be forced to narrow with a type guard even though the function can never produce an error variant.

Why this isn't a behavior bug. Both signatures are correct in the sense that the actual return value is assignable to the declared type — they're just wider than necessary. Every current caller (Protocol._onrequest in protocol.ts) immediately passes the result to transport.send(), which accepts the full JSONRPCMessage union, so nothing observable changes. The pre-PR _onrequest code also typed its locally-built success object as const response: JSONRPCResponse, so this carries over a pre-existing widening — but since the PR is introducing these new exported helpers in a brand-new file, it's a good moment to get the types exactly right.

Why fix it.okResponse and errorResponse are now exported from packages/core (export * from './shared/dispatcher.js' in index.ts), so they become public-ish API for sibling packages and the upcoming _dispatchStateless path. Tighter return types give downstream callers better narrowing for free and document the contract ("this always succeeds") without needing a comment.

Concrete walk-through.

  1. A future consumer calls const r = okResponse(1, { tools: [] }).
  2. They want to read r.result. With the current signature r: JSONRPCResponse, TypeScript reports Property 'result' does not exist on type 'JSONRPCErrorResponse' and forces an if ('result' in r) guard.
  3. With r: JSONRPCResultResponse, r.result type-checks directly — matching the function's actual guaranteed behavior.

Suggested fix.

asyncdispatch(request: JSONRPCRequest,ctx: ContextT): Promise<JSONRPCResponse>{}exportfunctionokResponse(id: JSONRPCRequest['id'],result: Result): JSONRPCResultResponse{}

errorResponse is already correctly narrowed to JSONRPCErrorResponse, so only these two annotations need to change. Filed as a nit since there's zero runtime impact.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving as-is for this PR. The wider types match the pre-existing convention and every caller passes the result straight to transport.send(); will tighten if a downstream caller needs the narrowing.

Comment threadCLAUDE.md Outdated
…ences)
Removes the 2025-11 experimental tasks side-channel through Protocol:
TaskManager, processInbound*/processOutbound*, task interception, the
experimental.tasks.* client/server accessors, and all task-augmented
request handling.
Also extends beyond the implementation deletion to scrub remaining
references in examples, docs, and comments. The only task-related
symbol remaining in packages/*.ts is `taskSupport` in ToolExecutionSchema,
kept solely to match spec.types.ts (which still declares it); both are
removed together in the next commit (spec regen).
CHANGELOG entries are preserved (historical record). Migration docs
retain a brief removal note. `microtask`/`platformBackgroundTask` are
JS/platform terminology, not MCP tasks.
Satisfies: SEP-2663 (core-removal half; tasks are now Extensions Track).
pnpm fetch:spec-types. Spec moved 44 commits since last regen.
Brings:
- RequestMetaObject with namespaced io.modelcontextprotocol/* keys (required protocolVersion/clientInfo/clientCapabilities)
- Result.resultType: "complete" | "input_required" (required)
- DiscoverRequest/Result, InputRequiredResult, InputRequest/Response/s, InputResponseRequestParams
- SubscriptionsListenRequest, SubscriptionFilter, SubscriptionsAcknowledgedNotification
- UnsupportedProtocolVersionError, MissingRequiredClientCapabilityError (-32003)
Removes (moved to extension or removed from spec):
- InitializeRequest/Result, InitializedNotification, PingRequest, SetLevelRequest
- SubscribeRequest, UnsubscribeRequest, RootsListChangedNotification
- All Task* types, taskSupport, TaskAugmentedRequestParams
Typecheck broken pending next commit (schemas.ts/guards.ts reference removed types).
Isolated for diff clarity per plan §10.
…emas
schemas.ts:
- Result/Notification _meta now use plain MetaObject (not RequestMetaSchema)
- ResultTypeSchema, resultType optional on ResultSchema (BC: pre-2026 omits it)
- RequestMetaSchema kept simple {progressToken?}; namespaced io.modelcontextprotocol/*
keys validated by parseClientMeta in P2, not here (TS2589 depth budget)
- DiscoverRequest/Result, UnsupportedProtocolVersionErrorData,
MissingRequiredClientCapabilityErrorData
- SubscriptionFilter, SubscriptionsListenRequest/Params,
SubscriptionsAcknowledgedNotification/Params
- InputRequest/Response/s, InputRequiredResult, InputResponseRequestParams
- CacheableResult/CacheScope (SEP-2243), PaginatedResult extends CacheableResult,
ReadResourceResult extends CacheableResult
- structuredContent: unknown (was Record<string,unknown>)
- Tool inputSchema/outputSchema loosened to match spec (open record + type:object on input)
- ToolExecution removed (only had taskSupport)
- ClientRequest/ServerNotification/etc unions = 2026 spec methods only
- registerLegacySchemas() lets legacyWireSchemas merge into runtime lookup maps
- Dropped Initialize*/Ping/SetLevel/Subscribe/Unsubscribe/RootsListChanged
NEW legacyWireSchemas.ts:
- LegacyRequestParams/Result/RequestMetaObject base types (no namespaced keys, no resultType)
- TS interfaces + zod for Initialize/Ping/SetLevel/Subscribe/Unsubscribe/RootsListChanged
- legacyRequest/Result/NotificationSchemas maps; registerLegacySchemas() side-effect on import
types.ts: re-exports legacy types; RequestTypeMap/NotificationTypeMap/ResultTypeMap
merge legacy methods. Adds Discover*/InputRequired*/Subscriptions*/Cache* types.
guards.ts: isInitializeRequest/isInitializedNotification import from legacyWireSchemas.
specTypeSchema.ts: allowlist updated (drop legacy, add 2026).
spec.types.test.ts: Relax<T> helper makes spec-required PermissiveKeys
(_meta/resultType/ttlMs/cacheScope/inputResponses/requestState/io.modelcontextprotocol/*)
optional so mutual assignability holds for everything else. Dropped checks for
removed types; added 17 checks for 2026 types. Count 176→150.
types.capabilities.test.ts: import InitializeRequestParamsSchema from legacyWireSchemas.
types.test.ts: outputSchema no longer requires type:object.
mcp.ts: drop ToolExecution/execution (removed from spec).
client.ts: assertCapabilityForMethod switches on RequestMethod (includes legacy).
…or change)
Introduce `Dispatcher<ContextT>` (`packages/core/src/shared/dispatcher.ts`)
holding the request-handler registry, middleware chain, and `dispatch()`
entry point. `Protocol` now composes a `protected readonly dispatcher`
and delegates:
- `setRequestHandler` / `removeRequestHandler` / `assertCanSetRequestHandler`
- `fallbackRequestHandler` (now `dispatcher.fallbackHandler`)
- `_onrequest` invokes `dispatcher.dispatch(request, ctx)` for lookup,
middleware onion, and JSON-RPC response wrapping
The previous `_wrapHandler` override hook is replaced by `dispatcher.use()`:
- `Server` registers `_callToolResultMiddleware` (was `_wrapHandler` for tools/call)
- `Client` registers `_validationMiddleware` (was `_wrapHandler` for
elicitation/create + sampling/createMessage)
Same validation runs on the same paths; the observation point moves from
per-registration wrapping to per-dispatch middleware.
`dispatch()`'s catch block preserves `_onrequest`'s error behavior verbatim:
thrown errors surface their `code` (if a safe integer), `message`, and `data`.
No sanitization is introduced.
Tests:
- `dispatcher.test.ts` added (11 tests for setHandler/dispatch/middleware/error)
- `wrapHandler.test.ts` deleted (covered by rewritten customMethods test)
- `customMethods.test.ts` adapts one test from spying on `_wrapHandler` to
spying via middleware; same assertion (both 2-arg and 3-arg routes through)
No existing test assertions changed.
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-schema-sync branch from ce732ae to 29c4991CompareMay 21, 2026 11:14
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-dispatcher branch from 315684d to 628f0e1CompareMay 21, 2026 11:15
@felixweinberger

Copy link
Copy Markdown
ContributorAuthor

@claude review

Comment on lines +279 to +280
/** The handler registry. Both `_onrequest` and `_dispatchStateless` route through it. */
protected readonly dispatcher = new Dispatcher<ContextT>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Several new comments and JSDoc blocks reference _dispatchStateless and "the 2026-06 dispatch path" — neither of which exists in this PR or the codebase yet (they are planned for #2131). A reader who greps for _dispatchStateless after merge will find nothing. Consider qualifying the references (e.g. "and the forthcoming _dispatchStateless path (#2131)") or deferring them to the PR that introduces the second path. Affects protocol.ts:279, dispatcher.ts:55, client.ts:288, and server.ts:182.

Extended reasoning...

What the issue is. Four new comments added by this PR reference code that does not exist yet:

  • packages/core/src/shared/protocol.ts:279/** The handler registry. Both \_onrequest` and `_dispatchStateless` route through it. */`
  • packages/core/src/shared/dispatcher.ts (class JSDoc, ~line 55) — "Both the legacy connect/_onrequest path and the 2026-06 stateless dispatch path route through dispatch."
  • packages/client/src/client/client.ts:288 — "…so it applies to both the legacy `_onrequest` path and the 2026-06 dispatch path."
  • packages/server/src/server/server.ts:182 — "…so it applies to both the legacy `_onrequest` path and the 2026-06 dispatch path."

_dispatchStateless appears nowhere in the codebase except inside the comment at protocol.ts:279, and there is no "2026-06 dispatch path" implemented anywhere. These are forward references to PR #2131.

Why it matters. The repo's own review guidance flags "prose that promises behavior the code no longer ships" and "any claim the diff doesn't back." Once this PR merges, a contributor reading Protocol who sees "Both `_onrequest` and `_dispatchStateless` route through it" and greps for _dispatchStateless will find zero hits. They have no way to know whether the symbol was renamed, removed, or never landed. _onrequest already calls _onrequest legacy — there is no "legacy" vs. "new" distinction observable in the merged code, so the dichotomy the comments draw doesn't exist for a reader of the codebase at this commit.

Step-by-step proof.

  1. Merge refactor(core): extract Dispatcher; Protocol composes it #2130 in isolation.
  2. Open packages/core/src/shared/protocol.ts at line 279: /** The handler registry. Both \_onrequest` and `_dispatchStateless` route through it. */`
  3. Run grep -rn _dispatchStateless packages/ → only the comment itself matches. There is no method, no export, no test.
  4. Open packages/server/src/server/server.ts:182: "…applies to both the legacy `_onrequest` path and the 2026-06 dispatch path." → there is exactly one path, _onrequest. Nothing distinguishes it as "legacy".

Why this is a nit and not a blocker. The PR description openly states this is PR 3 of a 7-PR v2-stateless stack, and that #2131 ("HTTP-stateless") introduces _dispatchStateless. The author and reviewers have full stack context, the comments are accurate as a description of intent, and they will become literally true once #2131 merges. There is zero runtime impact. But comments live in the merged tree, not in the PR description — and PRs in a stack don't always all land, or land in order.

How to fix. Either qualify the references so they are true at merge time, e.g.:

/** The handler registry. \`_onrequest\` routes through it (and the forthcoming \`_dispatchStateless\` path, #2131). */

or describe only what exists today and let #2131 update the comments when it adds the second path:

/** The handler registry. \`_onrequest\` routes through it. */

The first option preserves the useful forward signal for reviewers of this stack; the second is the most conservative.

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

Labels

v2-stateless2026-06 SDK: Protocol decomposition + SEP alignment (request-first / stateless)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@felixweinberger
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

refactor(core): extract Dispatcher; Protocol composes it - #2130

Closed
felixweinberger wants to merge 4 commits into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher
Closed

refactor(core): extract Dispatcher; Protocol composes it#2130
felixweinberger wants to merge 4 commits into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher

Conversation

@felixweinberger

@felixweinbergerfelixweinberger commented May 20, 2026

Copy link
Copy Markdown
Contributor

2026-06 stateless stack (v2-stateless label):

#PR
1#2128tasks-delete (mechanical)
2#2129schema-sync (mechanical)
3#2130Dispatcher extraction (zero-Δ refactor)
4#2131HTTP-stateless (the substantive review)
5#2132stdio/InMemory transports (additive)
6#2133docs + changeset
7#2134LegacyServer/LegacyClient extraction

Extracts Dispatcher<Ctx> (handler map + middleware chain + dispatch()) from Protocol. Protocol now composes one; setRequestHandler/_onrequest/fallbackRequestHandler delegate. The _wrapHandler override hook becomes dispatcher.use(middleware).

Zero behavior change.dispatch()'s catch block preserves _onrequest's error mapping verbatim. No existing test assertions changed.

Motivation and Context

Enabling refactor for SEP-2575: _dispatchStateless (next PR) needs to invoke the same handler map + middleware that legacy _onrequest uses, without going through Protocol's session machinery.

How Has This Been Tested?

pnpm test:all — count delta is dispatcher.test.ts (+11) and wrapHandler.test.ts (−1, redundant). No existing assertion changed.

Breaking Changes

Protocol._wrapHandler override hook removed (was protected). Subclasses use this.dispatcher.use(mw) instead.

Types of changes

  • Breaking change

Checklist

  • My code follows the repository's style guidelines
  • New and existing tests pass locally

@changeset-bot

changeset-botBot commented May 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 628f0e1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
NameType
@modelcontextprotocol/coreMajor

Not sure what this means? Click here to learn what changesets are.

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

@pkg-pr-new

pkg-pr-newBot commented May 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@2130

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@2130

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@2130

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@2130

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@2130

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@2130

commit: 628f0e1

@felixweinberger

Copy link
Copy Markdown
ContributorAuthor

@claude review

@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-schema-sync branch from c67522f to ce732aeCompareMay 21, 2026 10:42
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-dispatcher branch from 09fc142 to 315684dCompareMay 21, 2026 10:42
Comment on lines +159 to +161
* `Protocol._onrequest` prior to extraction.
*/
async dispatch(request: JSONRPCRequest, ctx: ContextT): Promise<JSONRPCResponse | JSONRPCErrorResponse> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Type-precision nit: Dispatcher.dispatch() declares Promise<JSONRPCResponse | JSONRPCErrorResponse>, but JSONRPCResponse is already the union JSONRPCResultResponse | JSONRPCErrorResponse (schemas.ts:170), so the trailing | JSONRPCErrorResponse is redundant. Likewise okResponse() always builds a success response but is typed as returning the wider JSONRPCResponse union instead of JSONRPCResultResponse. No runtime impact — purely a clarity/precision improvement.

Extended reasoning...

The redundant union.Dispatcher.dispatch() (packages/core/src/shared/dispatcher.ts:159) declares its return type as Promise<JSONRPCResponse | JSONRPCErrorResponse>. But JSONRPCResponse is itself already a union: packages/core/src/types/schemas.ts:170 defines JSONRPCResponseSchema = z.union([JSONRPCResultResponseSchema, JSONRPCErrorResponseSchema]), and spec.types.ts:257 confirms type JSONRPCResponse = JSONRPCResultResponse | JSONRPCErrorResponse. TypeScript collapses A | B where B ⊆ A to just A, so the declared type is exactly equivalent to Promise<JSONRPCResponse> — the | JSONRPCErrorResponse adds nothing.

The over-wide okResponse() type.okResponse() (packages/core/src/shared/dispatcher.ts:195) unconditionally builds { jsonrpc, id, result } — always a success-shaped response. Yet it's typed as returning JSONRPCResponse (the success-or-error union) when the precise type is JSONRPCResultResponse. A caller that wanted to access .result directly would be forced to narrow with a type guard even though the function can never produce an error variant.

Why this isn't a behavior bug. Both signatures are correct in the sense that the actual return value is assignable to the declared type — they're just wider than necessary. Every current caller (Protocol._onrequest in protocol.ts) immediately passes the result to transport.send(), which accepts the full JSONRPCMessage union, so nothing observable changes. The pre-PR _onrequest code also typed its locally-built success object as const response: JSONRPCResponse, so this carries over a pre-existing widening — but since the PR is introducing these new exported helpers in a brand-new file, it's a good moment to get the types exactly right.

Why fix it.okResponse and errorResponse are now exported from packages/core (export * from './shared/dispatcher.js' in index.ts), so they become public-ish API for sibling packages and the upcoming _dispatchStateless path. Tighter return types give downstream callers better narrowing for free and document the contract ("this always succeeds") without needing a comment.

Concrete walk-through.

  1. A future consumer calls const r = okResponse(1, { tools: [] }).
  2. They want to read r.result. With the current signature r: JSONRPCResponse, TypeScript reports Property 'result' does not exist on type 'JSONRPCErrorResponse' and forces an if ('result' in r) guard.
  3. With r: JSONRPCResultResponse, r.result type-checks directly — matching the function's actual guaranteed behavior.

Suggested fix.

asyncdispatch(request: JSONRPCRequest,ctx: ContextT): Promise<JSONRPCResponse>{}exportfunctionokResponse(id: JSONRPCRequest['id'],result: Result): JSONRPCResultResponse{}

errorResponse is already correctly narrowed to JSONRPCErrorResponse, so only these two annotations need to change. Filed as a nit since there's zero runtime impact.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving as-is for this PR. The wider types match the pre-existing convention and every caller passes the result straight to transport.send(); will tighten if a downstream caller needs the narrowing.

Comment threadCLAUDE.md Outdated
…ences)
Removes the 2025-11 experimental tasks side-channel through Protocol:
TaskManager, processInbound*/processOutbound*, task interception, the
experimental.tasks.* client/server accessors, and all task-augmented
request handling.
Also extends beyond the implementation deletion to scrub remaining
references in examples, docs, and comments. The only task-related
symbol remaining in packages/*.ts is `taskSupport` in ToolExecutionSchema,
kept solely to match spec.types.ts (which still declares it); both are
removed together in the next commit (spec regen).
CHANGELOG entries are preserved (historical record). Migration docs
retain a brief removal note. `microtask`/`platformBackgroundTask` are
JS/platform terminology, not MCP tasks.
Satisfies: SEP-2663 (core-removal half; tasks are now Extensions Track).
pnpm fetch:spec-types. Spec moved 44 commits since last regen.
Brings:
- RequestMetaObject with namespaced io.modelcontextprotocol/* keys (required protocolVersion/clientInfo/clientCapabilities)
- Result.resultType: "complete" | "input_required" (required)
- DiscoverRequest/Result, InputRequiredResult, InputRequest/Response/s, InputResponseRequestParams
- SubscriptionsListenRequest, SubscriptionFilter, SubscriptionsAcknowledgedNotification
- UnsupportedProtocolVersionError, MissingRequiredClientCapabilityError (-32003)
Removes (moved to extension or removed from spec):
- InitializeRequest/Result, InitializedNotification, PingRequest, SetLevelRequest
- SubscribeRequest, UnsubscribeRequest, RootsListChangedNotification
- All Task* types, taskSupport, TaskAugmentedRequestParams
Typecheck broken pending next commit (schemas.ts/guards.ts reference removed types).
Isolated for diff clarity per plan §10.
…emas
schemas.ts:
- Result/Notification _meta now use plain MetaObject (not RequestMetaSchema)
- ResultTypeSchema, resultType optional on ResultSchema (BC: pre-2026 omits it)
- RequestMetaSchema kept simple {progressToken?}; namespaced io.modelcontextprotocol/*
keys validated by parseClientMeta in P2, not here (TS2589 depth budget)
- DiscoverRequest/Result, UnsupportedProtocolVersionErrorData,
MissingRequiredClientCapabilityErrorData
- SubscriptionFilter, SubscriptionsListenRequest/Params,
SubscriptionsAcknowledgedNotification/Params
- InputRequest/Response/s, InputRequiredResult, InputResponseRequestParams
- CacheableResult/CacheScope (SEP-2243), PaginatedResult extends CacheableResult,
ReadResourceResult extends CacheableResult
- structuredContent: unknown (was Record<string,unknown>)
- Tool inputSchema/outputSchema loosened to match spec (open record + type:object on input)
- ToolExecution removed (only had taskSupport)
- ClientRequest/ServerNotification/etc unions = 2026 spec methods only
- registerLegacySchemas() lets legacyWireSchemas merge into runtime lookup maps
- Dropped Initialize*/Ping/SetLevel/Subscribe/Unsubscribe/RootsListChanged
NEW legacyWireSchemas.ts:
- LegacyRequestParams/Result/RequestMetaObject base types (no namespaced keys, no resultType)
- TS interfaces + zod for Initialize/Ping/SetLevel/Subscribe/Unsubscribe/RootsListChanged
- legacyRequest/Result/NotificationSchemas maps; registerLegacySchemas() side-effect on import
types.ts: re-exports legacy types; RequestTypeMap/NotificationTypeMap/ResultTypeMap
merge legacy methods. Adds Discover*/InputRequired*/Subscriptions*/Cache* types.
guards.ts: isInitializeRequest/isInitializedNotification import from legacyWireSchemas.
specTypeSchema.ts: allowlist updated (drop legacy, add 2026).
spec.types.test.ts: Relax<T> helper makes spec-required PermissiveKeys
(_meta/resultType/ttlMs/cacheScope/inputResponses/requestState/io.modelcontextprotocol/*)
optional so mutual assignability holds for everything else. Dropped checks for
removed types; added 17 checks for 2026 types. Count 176→150.
types.capabilities.test.ts: import InitializeRequestParamsSchema from legacyWireSchemas.
types.test.ts: outputSchema no longer requires type:object.
mcp.ts: drop ToolExecution/execution (removed from spec).
client.ts: assertCapabilityForMethod switches on RequestMethod (includes legacy).
…or change)
Introduce `Dispatcher<ContextT>` (`packages/core/src/shared/dispatcher.ts`)
holding the request-handler registry, middleware chain, and `dispatch()`
entry point. `Protocol` now composes a `protected readonly dispatcher`
and delegates:
- `setRequestHandler` / `removeRequestHandler` / `assertCanSetRequestHandler`
- `fallbackRequestHandler` (now `dispatcher.fallbackHandler`)
- `_onrequest` invokes `dispatcher.dispatch(request, ctx)` for lookup,
middleware onion, and JSON-RPC response wrapping
The previous `_wrapHandler` override hook is replaced by `dispatcher.use()`:
- `Server` registers `_callToolResultMiddleware` (was `_wrapHandler` for tools/call)
- `Client` registers `_validationMiddleware` (was `_wrapHandler` for
elicitation/create + sampling/createMessage)
Same validation runs on the same paths; the observation point moves from
per-registration wrapping to per-dispatch middleware.
`dispatch()`'s catch block preserves `_onrequest`'s error behavior verbatim:
thrown errors surface their `code` (if a safe integer), `message`, and `data`.
No sanitization is introduced.
Tests:
- `dispatcher.test.ts` added (11 tests for setHandler/dispatch/middleware/error)
- `wrapHandler.test.ts` deleted (covered by rewritten customMethods test)
- `customMethods.test.ts` adapts one test from spying on `_wrapHandler` to
spying via middleware; same assertion (both 2-arg and 3-arg routes through)
No existing test assertions changed.
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-schema-sync branch from ce732ae to 29c4991CompareMay 21, 2026 11:14
@felixweinberger
felixweinbergerforce-pushed the fweinberger/v2-dispatcher branch from 315684d to 628f0e1CompareMay 21, 2026 11:15
@felixweinberger

Copy link
Copy Markdown
ContributorAuthor

@claude review

Comment on lines +279 to +280
/** The handler registry. Both `_onrequest` and `_dispatchStateless` route through it. */
protected readonly dispatcher = new Dispatcher<ContextT>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Several new comments and JSDoc blocks reference _dispatchStateless and "the 2026-06 dispatch path" — neither of which exists in this PR or the codebase yet (they are planned for #2131). A reader who greps for _dispatchStateless after merge will find nothing. Consider qualifying the references (e.g. "and the forthcoming _dispatchStateless path (#2131)") or deferring them to the PR that introduces the second path. Affects protocol.ts:279, dispatcher.ts:55, client.ts:288, and server.ts:182.

Extended reasoning...

What the issue is. Four new comments added by this PR reference code that does not exist yet:

  • packages/core/src/shared/protocol.ts:279/** The handler registry. Both \_onrequest` and `_dispatchStateless` route through it. */`
  • packages/core/src/shared/dispatcher.ts (class JSDoc, ~line 55) — "Both the legacy connect/_onrequest path and the 2026-06 stateless dispatch path route through dispatch."
  • packages/client/src/client/client.ts:288 — "…so it applies to both the legacy `_onrequest` path and the 2026-06 dispatch path."
  • packages/server/src/server/server.ts:182 — "…so it applies to both the legacy `_onrequest` path and the 2026-06 dispatch path."

_dispatchStateless appears nowhere in the codebase except inside the comment at protocol.ts:279, and there is no "2026-06 dispatch path" implemented anywhere. These are forward references to PR #2131.

Why it matters. The repo's own review guidance flags "prose that promises behavior the code no longer ships" and "any claim the diff doesn't back." Once this PR merges, a contributor reading Protocol who sees "Both `_onrequest` and `_dispatchStateless` route through it" and greps for _dispatchStateless will find zero hits. They have no way to know whether the symbol was renamed, removed, or never landed. _onrequest already calls _onrequest legacy — there is no "legacy" vs. "new" distinction observable in the merged code, so the dichotomy the comments draw doesn't exist for a reader of the codebase at this commit.

Step-by-step proof.

  1. Merge refactor(core): extract Dispatcher; Protocol composes it #2130 in isolation.
  2. Open packages/core/src/shared/protocol.ts at line 279: /** The handler registry. Both \_onrequest` and `_dispatchStateless` route through it. */`
  3. Run grep -rn _dispatchStateless packages/ → only the comment itself matches. There is no method, no export, no test.
  4. Open packages/server/src/server/server.ts:182: "…applies to both the legacy `_onrequest` path and the 2026-06 dispatch path." → there is exactly one path, _onrequest. Nothing distinguishes it as "legacy".

Why this is a nit and not a blocker. The PR description openly states this is PR 3 of a 7-PR v2-stateless stack, and that #2131 ("HTTP-stateless") introduces _dispatchStateless. The author and reviewers have full stack context, the comments are accurate as a description of intent, and they will become literally true once #2131 merges. There is zero runtime impact. But comments live in the merged tree, not in the PR description — and PRs in a stack don't always all land, or land in order.

How to fix. Either qualify the references so they are true at merge time, e.g.:

/** The handler registry. \`_onrequest\` routes through it (and the forthcoming \`_dispatchStateless\` path, #2131). */

or describe only what exists today and let #2131 update the comments when it adds the second path:

/** The handler registry. \`_onrequest\` routes through it. */

The first option preserves the useful forward signal for reviewers of this stack; the second is the most conservative.

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

Labels

v2-stateless2026-06 SDK: Protocol decomposition + SEP alignment (request-first / stateless)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

@felixweinberger