-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Middleware Support for McpServer (Fixes #1238) #1345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Middleware Support for McpServer (Fixes #1238) #1345
Conversation
…ial request processing
…, and error handling
…authentication, and activity aggregation
…d tool handler error propagation
… once and adding a verification test
… middleware timing
|
commit: |
This PR implements a request-lifecycle middleware system for
McpServer(fixes#1238), enabling cross-cutting concerns (logging, authentication, error
handling) to be applied consistently across all Tools, Resources, and Prompts
using a standard "Onion Model".
Motivation and Context
Currently, developers must manually wrap every single tool handler to add logic
like logging or auth checks. This is repetitive and prone to security errors
(forgetting one handler).
This change introduces
server.use(), allowing centralized middlewareregistration that applies to the entire server lifecycle. It solves the need for
reliable, cross-cutting logic execution without boilerplate.
How Has This Been Tested?
packages/server/test/server/mcpServer.test.tscovering:
real-time logging middleware.
pnpm testpassed (348 tests) andpnpm typecheckpassed(strict typing).
Breaking Changes
No. This is a strictly additive change.
_executeRequestpath is optimized to have negligible overheadif no middleware is registered.
Types of changes
change)
Checklist
Additional context
Design Decision:
server.use()freezes the middleware stack upon serverconnection (start). This prevents race conditions where middleware might be
added mid-request, ensuring deterministic behavior.
Example Usage: