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\Cursor;
17: use Nexus\Mcp\Core\Schema\Result\ListResourcesResult;
18: use Nexus\Mcp\Core\Schema\Result\ReadResourceResult;
19: use Nexus\Mcp\Server\Exception\ResourceNotFoundException;
20: use Nexus\Mcp\Server\ServerContext;
21:
22: /**
23: * Chains a primary `ResourceStoreInterface` (exact-URI matches) with a fallback
24: * `ResourceTemplateStoreInterface` (URI-template matches). `read()` tries the
25: * primary first. On `ResourceNotFoundException` it falls through to the template
26: * store, only re-raising the not-found when neither side matches. `list()`
27: * delegates to the primary unchanged.
28: */
29: final readonly class CompositeResourceStore implements ResourceStoreInterface
30: {
31: public function __construct(private ResourceStoreInterface $resourceStore, private ResourceTemplateStoreInterface $resourceTemplateStore)
32: {
33: }
34:
35: #[\Override]
36: public function list(?Cursor $cursor): ListResourcesResult
37: {
38: return $this->resourceStore->list($cursor);
39: }
40:
41: #[\Override]
42: public function read(string $uri, ServerContext $context): ReadResourceResult
43: {
44: try {
45: return $this->resourceStore->read($uri, $context);
46: } catch (ResourceNotFoundException) {
47: return $this->resourceTemplateStore->read($uri, $context);
48: }
49: }
50: }
51: