Skip to content

[Server] add a reference handler setter to server builder#265

Open
simonchrz wants to merge 5 commits intomodelcontextprotocol:mainfrom
simonchrz:feat/set-reference-handler
Open

[Server] add a reference handler setter to server builder#265
simonchrz wants to merge 5 commits intomodelcontextprotocol:mainfrom
simonchrz:feat/set-reference-handler

Conversation

@simonchrz
Copy link

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

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
Copy link
Member

@chr-hertel chr-hertel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @simonchrz - just one minor comment.

edit: plus

  • pipeline failure looks real
  • can you please add a changelog entry for 0.5.0 here

$requestHandlers = (new \ReflectionClass($protocol))->getProperty('requestHandlers')->getValue($protocol);

foreach ($requestHandlers as $handler) {
if ($handler instanceof \Mcp\Server\Handler\Request\CallToolHandler) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use imports for the FQCNs instead of fully inline

Suggested change
if ($handler instanceof \Mcp\Server\Handler\Request\CallToolHandler) {
if ($handler instanceof CallToolHandler) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and below

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants