[Server] add a reference handler setter to server builder#265
Open
simonchrz wants to merge 5 commits intomodelcontextprotocol:mainfrom
Open
[Server] add a reference handler setter to server builder#265simonchrz wants to merge 5 commits intomodelcontextprotocol:mainfrom
simonchrz wants to merge 5 commits intomodelcontextprotocol:mainfrom
Conversation
Allow consumers to provide a custom ReferenceHandlerInterface implementation (e.g. a security-aware decorator) instead of always using the default ReferenceHandler.
- Fluent API returns builder instance - build() succeeds with and without custom handler - Integration test verifies custom handler intercepts tool calls
chr-hertel
requested changes
Mar 11, 2026
Member
There was a problem hiding this comment.
Thanks @simonchrz - just one minor comment.
edit: plus
- pipeline failure looks real
- can you please add a changelog entry for 0.5.0 here
tests/Unit/Server/BuilderTest.php
Outdated
| $requestHandlers = (new \ReflectionClass($protocol))->getProperty('requestHandlers')->getValue($protocol); | ||
|
|
||
| foreach ($requestHandlers as $handler) { | ||
| if ($handler instanceof \Mcp\Server\Handler\Request\CallToolHandler) { |
Member
There was a problem hiding this comment.
please use imports for the FQCNs instead of fully inline
Suggested change
| if ($handler instanceof \Mcp\Server\Handler\Request\CallToolHandler) { | |
| if ($handler instanceof CallToolHandler) { |
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.
Added a setReferenceHandler method to Mcp\Server\Builder.
Motivation and Context
The MCP SDK's Builder hardcodes new ReferenceHandler($container) internally — there's no way to customize how tool/resource/prompt handlers are invoked. This prevents consumers from adding cross-cutting concerns like authorization checks before tool execution.
We want to decorate the ReferenceHandler with a SecurityReferenceHandler that reads Symfony's #[IsGranted] attribute from tool methods and checks permissions via the AuthorizationCheckerInterface before delegating to the actual handler. Without setReferenceHandler(), the only alternatives are fragile workarounds (event listener hacks, replacing internal request handlers via reflection, or duplicating authorization logic inside every tool method).
How Has This Been Tested?
I've added Mcp\Tests\Unit\Server\BuilderTest and tested it manually agains librechat mcp client.
Breaking Changes
no, it's optional
Types of changes
Checklist