-
Notifications
You must be signed in to change notification settings - Fork 152
[Client][Server] Add Roots support #395
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
Open
wachterjohannes
wants to merge
3
commits into
modelcontextprotocol:main
Choose a base branch
from
wachterjohannes:eve/roots
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
390aa14
[Client][Server] Add Roots support (roots/list handler + ClientGatewa…
wachterjohannes 792804f
[Client][Server] Move Roots changelog entry to a new 0.7.0 section
wachterjohannes 68354d7
[Client][Server] Address Roots review: capability gating, error forwa…
wachterjohannes 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?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. | ||
| */ | ||
|
|
||
| /** | ||
| * STDIO Roots Example. | ||
| * | ||
| * This example demonstrates the "roots" client capability: | ||
| * - The client advertises the `roots` capability during initialization. | ||
| * - It answers server `roots/list` requests via a RootsCallbackInterface, | ||
| * exposing a couple of `file://` workspace folders. | ||
| * - It can notify the server when its list of roots changes. | ||
| * | ||
| * Usage: php examples/client/stdio_roots.php | ||
| */ | ||
|
|
||
| require_once __DIR__.'/../../vendor/autoload.php'; | ||
|
|
||
| use Mcp\Client; | ||
| use Mcp\Client\Handler\Request\ListRootsRequestHandler; | ||
| use Mcp\Client\Handler\Request\RootsCallbackInterface; | ||
| use Mcp\Client\Transport\StdioTransport; | ||
| use Mcp\Schema\ClientCapabilities; | ||
| use Mcp\Schema\Request\ListRootsRequest; | ||
| use Mcp\Schema\Result\ListRootsResult; | ||
| use Mcp\Schema\Root; | ||
|
|
||
| $rootsRequestHandler = new ListRootsRequestHandler(new class implements RootsCallbackInterface { | ||
| public function __invoke(ListRootsRequest $request): ListRootsResult | ||
| { | ||
| echo "[ROOTS] Server requested the client's list of roots\n"; | ||
|
|
||
| return new ListRootsResult([ | ||
| new Root('file:///home/user/projects/app', 'Application'), | ||
| new Root('file:///home/user/projects/library', 'Library'), | ||
| ]); | ||
| } | ||
| }); | ||
|
|
||
| $client = Client::builder() | ||
| ->setClientInfo('STDIO Roots Test', '1.0.0') | ||
| ->setInitTimeout(30) | ||
| ->setRequestTimeout(120) | ||
| ->setCapabilities(new ClientCapabilities(roots: true, rootsListChanged: true)) | ||
| ->addRequestHandler($rootsRequestHandler) | ||
| ->build(); | ||
|
|
||
| $transport = new StdioTransport( | ||
| command: 'php', | ||
| args: [__DIR__.'/../server/client-communication/server.php'], | ||
| ); | ||
|
|
||
| try { | ||
| echo "Connecting to MCP server...\n"; | ||
| $client->connect($transport); | ||
|
|
||
| $serverInfo = $client->getServerInfo(); | ||
| echo 'Connected to: '.($serverInfo->name ?? 'unknown')."\n\n"; | ||
|
|
||
| echo "Available tools:\n"; | ||
| $toolsResult = $client->listTools(); | ||
| foreach ($toolsResult->tools as $tool) { | ||
| echo " - {$tool->name}\n"; | ||
| } | ||
| echo "\n"; | ||
|
|
||
| // Whenever the client's workspace folders change, notify the server so it can | ||
| // request an updated list via roots/list. | ||
| echo "Notifying the server that the roots list changed...\n"; | ||
| $client->sendRootsListChanged(); | ||
|
|
||
| echo "Client is ready to answer roots/list requests from the server.\n"; | ||
| } catch (Throwable $e) { | ||
| echo "Error: {$e->getMessage()}\n"; | ||
| echo $e->getTraceAsString()."\n"; | ||
| } finally { | ||
| $client->disconnect(); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <?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\Client\Handler\Request; | ||
|
|
||
| use Mcp\Exception\RootsException; | ||
| use Mcp\Schema\JsonRpc\Error; | ||
| use Mcp\Schema\JsonRpc\Request; | ||
| use Mcp\Schema\JsonRpc\Response; | ||
| use Mcp\Schema\Request\ListRootsRequest; | ||
| use Mcp\Schema\Result\ListRootsResult; | ||
| use Psr\Log\LoggerInterface; | ||
| use Psr\Log\NullLogger; | ||
|
|
||
| /** | ||
| * Handler for roots/list requests from the server. | ||
| * | ||
| * The MCP server may ask the client for the list of filesystem roots (its | ||
| * "workspace folders"). This handler wraps a user-provided callback that returns | ||
| * the roots the client wishes to expose. | ||
| * | ||
| * @implements RequestHandlerInterface<ListRootsResult> | ||
| * | ||
| * @author Johannes Wachter <johannes@sulu.io> | ||
| */ | ||
| class ListRootsRequestHandler implements RequestHandlerInterface | ||
| { | ||
| public function __construct( | ||
| private readonly RootsCallbackInterface $callback, | ||
| private readonly LoggerInterface $logger = new NullLogger(), | ||
| ) { | ||
| } | ||
|
|
||
| public function supports(Request $request): bool | ||
| { | ||
| return $request instanceof ListRootsRequest; | ||
| } | ||
|
|
||
| /** | ||
| * @return Response<ListRootsResult>|Error | ||
| */ | ||
| public function handle(Request $request): Response|Error | ||
| { | ||
| \assert($request instanceof ListRootsRequest); | ||
|
|
||
| try { | ||
| $result = $this->callback->__invoke($request); | ||
|
|
||
| return new Response($request->getId(), $result); | ||
| } catch (RootsException $e) { | ||
| $this->logger->error('Listing roots failed: '.$e->getMessage(), ['exception' => $e]); | ||
|
|
||
| return Error::forInternalError($e->getMessage(), $request->getId()); | ||
| } catch (\Throwable $e) { | ||
| $this->logger->error('Unexpected error while listing roots', ['exception' => $e]); | ||
|
|
||
| return Error::forInternalError('Error while listing roots', $request->getId()); | ||
| } | ||
| } | ||
| } |
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,28 @@ | ||
| <?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\Client\Handler\Request; | ||
|
|
||
| use Mcp\Schema\Request\ListRootsRequest; | ||
| use Mcp\Schema\Result\ListRootsResult; | ||
|
|
||
| /** | ||
| * Contract for callbacks used by ListRootsRequestHandler. | ||
| * | ||
| * Implementations return the list of filesystem roots the client exposes when | ||
| * requested by the server. | ||
| * | ||
| * @author Johannes Wachter <johannes@sulu.io> | ||
| */ | ||
| interface RootsCallbackInterface | ||
| { | ||
| public function __invoke(ListRootsRequest $request): ListRootsResult; | ||
| } |
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,24 @@ | ||
| <?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\Exception; | ||
|
|
||
| /** | ||
| * Exception thrown when listing roots fails. | ||
| * | ||
| * When thrown from a roots callback, this exception's message will be | ||
| * included in the error response sent back to the server. | ||
| * | ||
| * @author Johannes Wachter <johannes@sulu.io> | ||
| */ | ||
| final class RootsException extends \RuntimeException implements ExceptionInterface | ||
| { | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
flaky in case
rootsdoesn't carry an array as element =>TypeError