1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\Mcp\Server\Resource;
15:
16: use Nexus\Mcp\Core\Schema\Result\ReadResourceResult;
17: use Nexus\Mcp\Server\Discovery\ArgumentBinder;
18: use Nexus\Mcp\Server\ServerContext;
19:
20: /**
21: * Adapts an attribute-discovered handler method to the `ResourceReaderInterface` contract.
22: */
23: final readonly class ReflectedResourceReader implements ResourceReaderInterface
24: {
25: public function __construct(
26: private object $handler,
27: private \ReflectionMethod $method,
28: private ArgumentBinder $binder = new ArgumentBinder(),
29: ) {
30: }
31:
32: #[\Override]
33: public function read(string $uri, ServerContext $context): ReadResourceResult
34: {
35: $bound = $this->binder->bind($this->method, ['uri' => $uri], $context);
36:
37: return ReflectedResourceResult::adapt($this->method->invokeArgs($this->handler, $bound), $uri, $this->method);
38: }
39: }
40: