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\Completion;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\Prompt\PromptReference;
18: use Nexus\Mcp\Core\Schema\Resource\ResourceTemplateReference;
19: use Nexus\Mcp\Core\Schema\Result\CompleteResult;
20: use Nexus\Mcp\Server\ServerContext;
21:
22: /**
23: * In-memory implementation of `CompletionStoreInterface`.
24: *
25: * @phpstan-type ArgumentMap array<non-empty-string, \Closure(string, ?array<string, string>, ServerContext): CompleteResult>
26: */
27: final readonly class CompletionStore implements CompletionStoreInterface
28: {
29: /**
30: * @param array<non-empty-string, ArgumentMap> $promptCompletions
31: * @param array<non-empty-string, ArgumentMap> $templateCompletions
32: */
33: public function __construct(private array $promptCompletions = [], private array $templateCompletions = [])
34: {
35: Assert::that($this->promptCompletions)
36: ->keys()
37: ->isNonEmptyString('Completion store prompt key must be a non-empty string.')
38: ;
39: Assert::that($this->templateCompletions)
40: ->keys()
41: ->isNonEmptyString('Completion store template key must be a non-empty string.')
42: ;
43: }
44:
45: #[\Override]
46: public function complete(
47: PromptReference|ResourceTemplateReference $ref,
48: string $argumentName,
49: string $argumentValue,
50: ?array $contextArguments,
51: ServerContext $context,
52: ): CompleteResult {
53: if ($ref instanceof PromptReference) {
54: $providers = $this->promptCompletions[$ref->name] ?? null;
55: } else {
56: $providers = $this->templateCompletions[$ref->uri] ?? null;
57: }
58:
59: if (null === $providers || ! isset($providers[$argumentName])) {
60: return new CompleteResult(['values' => []]);
61: }
62:
63: return ($providers[$argumentName])($argumentValue, $contextArguments, $context);
64: }
65: }
66: