forked from modelcontextprotocol/php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResourceSubscribeHandler.php
More file actions
72 lines (60 loc) · 2.04 KB
/
ResourceSubscribeHandler.php
File metadata and controls
72 lines (60 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?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\Resource\SubscriptionManagerInterface;
use Mcp\Server\Session\SessionInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Psr\SimpleCache\InvalidArgumentException;
/**
* @implements RequestHandlerInterface<EmptyResult>
*
* @author Larry Sule-balogun <suleabimbola@gmail.com>
*/
final class ResourceSubscribeHandler implements RequestHandlerInterface
{
public function __construct(
private readonly RegistryInterface $registry,
private readonly SubscriptionManagerInterface $subscriptionManager,
private readonly LoggerInterface $logger = new NullLogger(),
) {
}
public function supports(Request $request): bool
{
return $request instanceof ResourceSubscribeRequest;
}
/**
* @throws InvalidArgumentException
*/
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->subscriptionManager->subscribe($session, $uri);
return new Response(
$request->getId(),
new EmptyResult(),
);
}
}