feat(mcp): Add McpServerInterceptor with scoped hooks#1174
Merged
Conversation
eff5f6a to
33e2eb0
Compare
adwsingh
approved these changes
May 18, 2026
Add hook-based server interceptor for McpService, following the established ClientInterceptor pattern. Interceptors observe or modify requests at specific pipeline stages without exposing async dispatch internals. Hooks: readBefore/modifyBefore Execution and ToolCall, readAfter/modifyAfter Execution and ToolCall. Also makes McpServerProxy's rpc(), start(), and shutdown() protected to allow custom proxy transport implementations.
33e2eb0 to
e1dc33d
Compare
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.
Summary
Server interceptor hooks:
McpServerInterceptorinterface with scoped read/modify hooks at specific pipeline stages, following theClientInterceptorpatternreadBefore/modifyBeforeandreadAfter/modifyAfterfor both execution-level and tool-call-level stagesMcpExecutionHookandMcpToolCallHook(typed context, request, protocol version, tool metadata)McpServerInterceptor.chain()for composing multiple interceptors, with error handling matchingClientInterceptorChain(swapError, last-write-wins for read hooks, immediate propagation for modify hooks)McpService.Builder.interceptor()andMcpServerBuilder.interceptor()Extensible proxy transport:
McpServerProxy.rpc(),start(),shutdown()protected to allow custom proxy transport implementations outside the packageMotivation
There's currently no way to observe, augment, or short-circuit requests at the
McpServicelevel. Consumers usingMcpServicedirectly can wrap it, but that customization is lost when usingMcpServer, which creates and owns theMcpServiceinstance internally. Placing the interceptor onMcpService.Buildersolves both cases — directMcpServiceusers add interceptors at construction, andMcpServerBuilderplumbs them through.The hook-based approach follows the established
ClientInterceptorpattern — scoped hooks with typed context rather than a generic request handler chain.McpServiceowns when hooks fire, so interceptors never need to understand async dispatch internals.Separately, custom MCP proxy transports beyond the provided stdio and HTTP implementations are not possible today because
McpServerProxy's abstract methods are package-private. Making them protected allows consumers to implement their own proxy transports.