-
Notifications
You must be signed in to change notification settings - Fork 141
[Server] Allow runtime-resolved element handlers #294
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
Merged
soyuka
merged 21 commits into
modelcontextprotocol:main
from
e0ipso:allow-runtime-tools
May 29, 2026
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7a0a5ae
[Server] feat: allow runtime handlers
e0ipso 710c251
feat(server): add runtime handler metadata API
e0ipso 913c5cf
feat(server): expose runtime handlers via Builder
e0ipso 2d9b29b
test(server): cover runtime handler Builder path
e0ipso 68842bf
refactor(server): split runtime handler interface
e0ipso d0cadd6
refactor(server): rename runtime handler interfaces to Runtime casing
e0ipso 0d56bee
chore: please the linting gods
e0ipso acc3100
test(server): use Uuid::v4 for symfony/uid 5.4 compatibility
e0ipso 3876e61
refactor(server): split ArrayLoader registration
e0ipso 09b5667
refactor(server): non-null runtime handler types
e0ipso 59c4163
doc: update docs/server-builder.md
e0ipso a781354
feat: peer revie feedback
e0ipso 7634887
refactor(server): apply PR 294 review feedback
e0ipso 25ad401
doc: address PR 294 review feedback
e0ipso 944059e
refactor: address self-review feedback
e0ipso e89882a
refactor: rename CallableHandler to Handler
e0ipso 7fcbcdc
style: fix phpdoc param alignment
e0ipso 994bd02
refactor: inline $reference->handler access
e0ipso 791b197
Merge branch 'main' into allow-runtime-tools
e0ipso 0bae832
fix: maintainer review feedback
soyuka 21c7c65
style: fix phpdoc param alignment
e0ipso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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
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
105 changes: 105 additions & 0 deletions
105
src/Capability/Registry/Loader/ExplicitElementLoader.php
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the official PHP MCP SDK. | ||
| * | ||
| * A collaboration between Symfony and the PHP Foundation. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Mcp\Capability\Registry\Loader; | ||
|
|
||
| use Mcp\Capability\Completion\ProviderInterface; | ||
| use Mcp\Capability\Registry\ReferenceHandler; | ||
| use Mcp\Capability\RegistryInterface; | ||
| use Mcp\Schema\Prompt; | ||
| use Mcp\Schema\ResourceDefinition; | ||
| use Mcp\Schema\ResourceTemplate; | ||
| use Mcp\Schema\Tool; | ||
| use Mcp\Server\ClientGateway; | ||
| use Mcp\Server\Handler\PromptHandlerInterface; | ||
| use Mcp\Server\Handler\ResourceHandlerInterface; | ||
| use Mcp\Server\Handler\ResourceTemplateHandlerInterface; | ||
| use Mcp\Server\Handler\ToolHandlerInterface; | ||
|
|
||
| /** | ||
| * Translates `Builder::add()` definition+handler pairs into Registry entries. | ||
| * | ||
| * Each registered closure is bound to {@see ReferenceHandler} as its scope so the | ||
| * reference handler short-circuits reflection and invokes it with the raw argument bag. | ||
| * | ||
| * @author Mateu Aguiló Bosch <mateu.aguilo.bosch@gmail.com> | ||
| */ | ||
| final class ExplicitElementLoader implements LoaderInterface | ||
|
e0ipso marked this conversation as resolved.
|
||
| { | ||
| /** | ||
| * @param list<array{definition: Tool, handler: ToolHandlerInterface}> $tools | ||
| * @param list<array{definition: ResourceDefinition, handler: ResourceHandlerInterface}> $resources | ||
| * @param list<array{definition: ResourceTemplate, handler: ResourceTemplateHandlerInterface, completionProviders: array<string, ProviderInterface>}> $resourceTemplates | ||
| * @param list<array{definition: Prompt, handler: PromptHandlerInterface, completionProviders: array<string, ProviderInterface>}> $prompts | ||
| */ | ||
| public function __construct( | ||
| private readonly array $tools = [], | ||
| private readonly array $resources = [], | ||
| private readonly array $resourceTemplates = [], | ||
| private readonly array $prompts = [], | ||
| ) { | ||
| } | ||
|
|
||
| public function load(RegistryInterface $registry): void | ||
| { | ||
| foreach ($this->tools as $entry) { | ||
| $handler = $entry['handler']; | ||
| $registry->registerTool($entry['definition'], $this->boundClosure( | ||
| static function (array $arguments) use ($handler): mixed { | ||
| $gateway = new ClientGateway($arguments['_session']); | ||
| unset($arguments['_session'], $arguments['_request']); | ||
|
|
||
| return $handler->execute($arguments, $gateway); | ||
| }, | ||
| )); | ||
| } | ||
|
|
||
| foreach ($this->resources as $entry) { | ||
| $handler = $entry['handler']; | ||
| $registry->registerResource($entry['definition'], $this->boundClosure( | ||
| static fn (array $arguments): mixed => $handler->read( | ||
| $arguments['uri'], | ||
| new ClientGateway($arguments['_session']), | ||
| ), | ||
| )); | ||
| } | ||
|
|
||
| foreach ($this->resourceTemplates as $entry) { | ||
| $handler = $entry['handler']; | ||
| $registry->registerResourceTemplate($entry['definition'], $this->boundClosure( | ||
| static function (array $arguments) use ($handler): mixed { | ||
| $gateway = new ClientGateway($arguments['_session']); | ||
| $uri = $arguments['uri']; | ||
| unset($arguments['_session'], $arguments['_request'], $arguments['uri']); | ||
|
|
||
| return $handler->read($uri, $arguments, $gateway); | ||
| }, | ||
| ), $entry['completionProviders']); | ||
| } | ||
|
|
||
| foreach ($this->prompts as $entry) { | ||
| $handler = $entry['handler']; | ||
| $registry->registerPrompt($entry['definition'], $this->boundClosure( | ||
| static function (array $arguments) use ($handler): mixed { | ||
| $gateway = new ClientGateway($arguments['_session']); | ||
| unset($arguments['_session'], $arguments['_request']); | ||
|
|
||
| return $handler->get($arguments, $gateway); | ||
| }, | ||
| ), $entry['completionProviders']); | ||
| } | ||
| } | ||
|
|
||
| private function boundClosure(\Closure $closure): \Closure | ||
| { | ||
| return \Closure::bind($closure, null, ReferenceHandler::class); | ||
| } | ||
| } | ||
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
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
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.