-
Notifications
You must be signed in to change notification settings - Fork 155
[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
base: main
Are you sure you want to change the base?
Changes from all commits
9ec4189
b325b88
2386faf
3e4eade
4c21047
5d278a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. | ||
| * - Calling the server's `inspect_workspace_roots` tool makes the server issue | ||
| * a `roots/list` request, so the handler below actually runs. | ||
| * - It notifies 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\Content\TextContent; | ||
| 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'], | ||
| ); | ||
|
|
||
| echo "Connecting to MCP server...\n"; | ||
| $client->connect($transport); | ||
|
|
||
| $serverInfo = $client->getServerInfo(); | ||
| echo 'Connected to: '.($serverInfo->name ?? 'unknown')."\n\n"; | ||
|
|
||
| // The server tool asks the client for its roots, which triggers the handler above. | ||
| echo "Calling 'inspect_workspace_roots'...\n"; | ||
| $result = $client->callTool(name: 'inspect_workspace_roots'); | ||
|
|
||
| echo "\nResult:\n"; | ||
| foreach ($result->content as $content) { | ||
| if ($content instanceof TextContent) { | ||
| echo $content->text."\n"; | ||
| } | ||
| } | ||
|
|
||
| // Whenever the client's workspace folders change, notify the server so it can | ||
| // request an updated list via roots/list. | ||
| echo "\nNotifying the server that the roots list changed...\n"; | ||
| $client->sendRootsListChanged(); | ||
|
|
||
| $client->disconnect(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,11 +17,13 @@ | |
| use Mcp\Client\Transport\TransportInterface; | ||
| use Mcp\Exception\ConnectionException; | ||
| use Mcp\Exception\RequestException; | ||
| use Mcp\Exception\RuntimeException; | ||
| use Mcp\Schema\Enum\LoggingLevel; | ||
| use Mcp\Schema\Implementation; | ||
| use Mcp\Schema\JsonRpc\Error; | ||
| use Mcp\Schema\JsonRpc\Request; | ||
| use Mcp\Schema\JsonRpc\Response; | ||
| use Mcp\Schema\Notification\RootsListChangedNotification; | ||
| use Mcp\Schema\PromptReference; | ||
| use Mcp\Schema\Request\CallToolRequest; | ||
| use Mcp\Schema\Request\CompletionCompleteRequest; | ||
|
|
@@ -244,6 +246,27 @@ public function setLoggingLevel(LoggingLevel $level): void | |
| $this->sendRequest($request); | ||
| } | ||
|
|
||
| /** | ||
| * Notify the server that the client's list of roots has changed. | ||
| * | ||
| * The server should react by requesting an updated list via roots/list. | ||
| * | ||
| * @throws RuntimeException if the client did not advertise the `roots.listChanged` capability | ||
| * @throws ConnectionException if the client is not connected | ||
| */ | ||
| public function sendRootsListChanged(): void | ||
| { | ||
| if (true !== $this->config->capabilities->rootsListChanged) { | ||
| throw new RuntimeException('Cannot send a "roots/list_changed" notification without advertising the "roots.listChanged" capability. Build the client with new ClientCapabilities(roots: true, rootsListChanged: true).'); | ||
| } | ||
|
|
||
| if (!$this->isConnected()) { | ||
| throw new ConnectionException('Client is not connected. Call connect() first.'); | ||
| } | ||
|
|
||
| $this->protocol->sendNotification(new RootsListChangedNotification()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i get why you're not using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch — it was worse than just skipping the check: |
||
| } | ||
|
|
||
| /** | ||
| * Send a request to the server and wait for response. | ||
| * | ||
|
|
||
| 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()); | ||
| } | ||
| } | ||
| } |
| 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; | ||
| } |
| 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 | ||
| { | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
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.
i don't think we need the try-catch block here
and, not sure, but can we demo it the other way around so that the list roots request actually is executed?
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.
Both done in 5d278a9. try/catch is gone, and the demo is turned around: the client-communication server now has an
inspect_workspace_rootstool usingClientGateway::supportsRoots()/listRoots(), and the example calls it — so the server really issuesroots/listand the client's handler answers. Output when run:The
sendRootsListChanged()notification is kept at the end as a secondary demo.