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\Prompt;
15:
16: use Nexus\Mcp\Core\Schema\Cursor;
17: use Nexus\Mcp\Core\Schema\Enum\CacheScope;
18: use Nexus\Mcp\Core\Schema\Prompt\Prompt;
19: use Nexus\Mcp\Core\Schema\Result\GetPromptResult;
20: use Nexus\Mcp\Core\Schema\Result\ListPromptsResult;
21: use Nexus\Mcp\Server\AbstractPaginatedStore;
22: use Nexus\Mcp\Server\Exception\PromptNotFoundException;
23: use Nexus\Mcp\Server\ServerContext;
24:
25: /**
26: * In-memory implementation of `PromptStoreInterface`.
27: *
28: * @extends AbstractPaginatedStore<PromptEntry>
29: */
30: final readonly class PromptStore extends AbstractPaginatedStore implements PromptStoreInterface
31: {
32: protected const string STORE_LABEL = 'Prompt store';
33:
34: #[\Override]
35: public function list(?Cursor $cursor): ListPromptsResult
36: {
37: return $this->paginate(
38: $cursor,
39: static fn(PromptEntry $entry): Prompt => $entry->prompt,
40: self::buildResult(...),
41: );
42: }
43:
44: #[\Override]
45: public function get(string $name, ?array $arguments, ServerContext $context): GetPromptResult
46: {
47: if (! \array_key_exists($name, $this->entries)) {
48: throw new PromptNotFoundException($name, $context->requestId);
49: }
50:
51: return $this->entries[$name]->renderer->render($arguments, $context);
52: }
53:
54: /**
55: * @param list<Prompt> $prompts
56: */
57: private static function buildResult(array $prompts, ?Cursor $nextCursor, int $ttlMs, CacheScope $cacheScope): ListPromptsResult
58: {
59: return new ListPromptsResult(prompts: $prompts, ttlMs: $ttlMs, cacheScope: $cacheScope, nextCursor: $nextCursor);
60: }
61: }
62: