Middleware Support for McpServer (Fixes #1238)#1345
Open
ahammednibras8 wants to merge 22 commits intomodelcontextprotocol:mainfrom
Open
Middleware Support for McpServer (Fixes #1238)#1345ahammednibras8 wants to merge 22 commits intomodelcontextprotocol:mainfrom
ahammednibras8 wants to merge 22 commits intomodelcontextprotocol:mainfrom
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: