-
Notifications
You must be signed in to change notification settings - Fork 103
Add missing handler for resource subscribe/unsubscribe handlers #220
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
bigdevlarry
wants to merge
1
commit into
modelcontextprotocol:main
Choose a base branch
from
bigdevlarry:Add-missing-handler-for-Resource-Subscribe-Unsubscribe
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.
+568
−3
Open
Changes from all commits
Commits
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
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,66 @@ | ||
| <?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\Server\Handler\Request; | ||
|
|
||
| use Mcp\Capability\RegistryInterface; | ||
| use Mcp\Exception\ResourceNotFoundException; | ||
| use Mcp\Schema\JsonRpc\Error; | ||
| use Mcp\Schema\JsonRpc\Request; | ||
| use Mcp\Schema\JsonRpc\Response; | ||
| use Mcp\Schema\Request\ResourceSubscribeRequest; | ||
| use Mcp\Schema\Result\EmptyResult; | ||
| use Mcp\Server\Session\SessionInterface; | ||
| use Psr\Log\LoggerInterface; | ||
| use Psr\Log\NullLogger; | ||
|
|
||
| /** | ||
| * @implements RequestHandlerInterface<EmptyResult> | ||
| * | ||
| * @author Larry Sule-balogun <suleabimbola@gmail.com> | ||
| */ | ||
| final class ResourceSubscribeHandler implements RequestHandlerInterface | ||
| { | ||
| public function __construct( | ||
| private readonly RegistryInterface $registry, | ||
| private readonly LoggerInterface $logger = new NullLogger(), | ||
| ) { | ||
| } | ||
|
|
||
| public function supports(Request $request): bool | ||
| { | ||
| return $request instanceof ResourceSubscribeRequest; | ||
| } | ||
|
|
||
| public function handle(Request $request, SessionInterface $session): Response|Error | ||
| { | ||
| \assert($request instanceof ResourceSubscribeRequest); | ||
|
|
||
| $uri = $request->uri; | ||
|
|
||
| try { | ||
| $this->registry->getResource($uri); | ||
| } catch (ResourceNotFoundException $e) { | ||
| $this->logger->error('Resource not found', ['uri' => $uri]); | ||
|
|
||
| return Error::forResourceNotFound($e->getMessage(), $request->getId()); | ||
| } | ||
|
|
||
| $this->logger->debug('Subscribing to resource', ['uri' => $uri]); | ||
|
|
||
| $this->registry->subscribe($session, $uri); | ||
|
|
||
| return new Response( | ||
| $request->getId(), | ||
| new EmptyResult(), | ||
| ); | ||
| } | ||
| } |
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,66 @@ | ||
| <?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\Server\Handler\Request; | ||
|
|
||
| use Mcp\Capability\RegistryInterface; | ||
| use Mcp\Exception\ResourceNotFoundException; | ||
| use Mcp\Schema\JsonRpc\Error; | ||
| use Mcp\Schema\JsonRpc\Request; | ||
| use Mcp\Schema\JsonRpc\Response; | ||
| use Mcp\Schema\Request\ResourceUnsubscribeRequest; | ||
| use Mcp\Schema\Result\EmptyResult; | ||
| use Mcp\Server\Session\SessionInterface; | ||
| use Psr\Log\LoggerInterface; | ||
| use Psr\Log\NullLogger; | ||
|
|
||
| /** | ||
| * @implements RequestHandlerInterface<EmptyResult> | ||
| * | ||
| * @author Larry Sule-balogun <suleabimbola@gmail.com> | ||
| */ | ||
| final class ResourceUnsubscribeHandler implements RequestHandlerInterface | ||
| { | ||
| public function __construct( | ||
| private readonly RegistryInterface $registry, | ||
| private readonly LoggerInterface $logger = new NullLogger(), | ||
| ) { | ||
| } | ||
|
|
||
| public function supports(Request $request): bool | ||
| { | ||
| return $request instanceof ResourceUnsubscribeRequest; | ||
| } | ||
|
|
||
| public function handle(Request $request, SessionInterface $session): Response|Error | ||
| { | ||
| \assert($request instanceof ResourceUnsubscribeRequest); | ||
|
|
||
| $uri = $request->uri; | ||
|
|
||
| try { | ||
| $this->registry->getResource($uri); | ||
| } catch (ResourceNotFoundException $e) { | ||
| $this->logger->error('Resource not found', ['uri' => $uri]); | ||
|
|
||
| return Error::forResourceNotFound($e->getMessage(), $request->getId()); | ||
| } | ||
|
|
||
| $this->logger->debug('Unsubscribing from resource', ['uri' => $uri]); | ||
|
|
||
| $this->registry->unsubscribe($session, $uri); | ||
|
|
||
| return new Response( | ||
| $request->getId(), | ||
| new EmptyResult(), | ||
| ); | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -9,10 +9,13 @@ | |
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| ini_set('display_errors', '0'); | ||
|
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. This prevents PHP from printing errors to the response body fixes the JSON-RPC error of outputting html |
||
|
|
||
| require_once dirname(__DIR__, 2).'/vendor/autoload.php'; | ||
|
|
||
| use Http\Discovery\Psr17Factory; | ||
| use Laminas\HttpHandlerRunner\Emitter\SapiEmitter; | ||
| use Mcp\Capability\Registry; | ||
| use Mcp\Schema\Content\AudioContent; | ||
| use Mcp\Schema\Content\EmbeddedResource; | ||
| use Mcp\Schema\Content\ImageContent; | ||
|
|
@@ -32,6 +35,7 @@ | |
| $request = $psr17Factory->createServerRequestFromGlobals(); | ||
|
|
||
| $transport = new StreamableHttpTransport($request, logger: $logger); | ||
| $registry = new Registry(null, $logger); | ||
|
|
||
| $server = Server::builder() | ||
| ->setServerInfo('mcp-conformance-test-server', '1.0.0') | ||
|
|
@@ -51,7 +55,6 @@ | |
| ->addResource(fn () => 'This is the content of the static text resource.', 'test://static-text', 'static-text', 'A static text resource for testing') | ||
| ->addResource(fn () => fopen('data://image/png;base64,'.Elements::TEST_IMAGE_BASE64, 'r'), 'test://static-binary', 'static-binary', 'A static binary resource (image) for testing') | ||
| ->addResourceTemplate([Elements::class, 'resourceTemplate'], 'test://template/{id}/data', 'template', 'A resource template with parameter substitution', 'application/json') | ||
| // TODO: Handler for resources/subscribe and resources/unsubscribe | ||
| ->addResource(fn () => 'Watched resource content', 'test://watched-resource', 'watched-resource', 'A resource that can be watched') | ||
| // Prompts | ||
| ->addPrompt(fn () => [['role' => 'user', 'content' => 'This is a simple prompt for testing.']], 'test_simple_prompt', 'A simple prompt without arguments') | ||
|
|
||
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.
How do we make the subscription persistent to make it work across multiple request?