Skip to content

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

Draft
felixweinberger wants to merge 1 commit into
fweinberger/v2-schema-syncfrom
fweinberger/v2-dispatcher
Draft

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

Conversation

@felixweinberger
Copy link
Copy Markdown
Contributor

@felixweinberger felixweinberger commented May 20, 2026

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

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

changeset-bot Bot commented May 20, 2026

⚠️ No Changeset found

Latest commit: 09fc142

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

This PR includes no changesets

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

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

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 20, 2026

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: 09fc142

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant