1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2025 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\Schema\Request;
15:
16: use Nexus\Mcp\Schema\Arrayable;
17: use Nexus\Mcp\Schema\Completion\PromptReference;
18: use Nexus\Mcp\Schema\Completion\ResourceTemplateReference;
19: use Nexus\Mcp\Schema\Message\JsonRpcRequest;
20: use Nexus\Mcp\Schema\Message\RequestId;
21:
22: /**
23: * A request from the client to the server, to ask for completion options.
24: *
25: * @extends JsonRpcRequest<array{
26: * jsonrpc: '2.0',
27: * id: int|non-empty-string,
28: * method: 'completion/complete',
29: * params: array{
30: * _meta?: array<string, mixed>,
31: * ref: template-type<PromptReference|ResourceTemplateReference, Arrayable, 'T'>,
32: * argument: array{name: non-empty-string, value: non-empty-string},
33: * context?: array{arguments: array<string, string>}
34: * }
35: * }>
36: */
37: final readonly class CompleteRequest extends JsonRpcRequest implements ClientRequest
38: {
39: /**
40: * @param array{name: string, value: string} $argument The argument’s information.
41: * @param null|array<string, string> $context Additional, optional context for completions.
42: * Contains the previously-resolved variables in a URI template or prompt.
43: * @param null|array<string, mixed> $meta Reserved by MCP to allow clients and servers to attach
44: * additional metadata to their interactions.
45: */
46: public function __construct(
47: RequestId $id,
48: PromptReference|ResourceTemplateReference $ref,
49: array $argument,
50: ?array $context = null,
51: ?array $meta = null,
52: ) {
53: if (! \array_key_exists('name', $argument) || ! \is_string($argument['name']) || '' === $argument['name']) {
54: throw new \InvalidArgumentException('The "name" key in $argument must be a non-empty string.');
55: }
56:
57: if (! \array_key_exists('value', $argument) || ! \is_string($argument['value']) || '' === $argument['value']) {
58: throw new \InvalidArgumentException('The "value" key in $argument must be a non-empty string.');
59: }
60:
61: $context = isset($context) ? ['arguments' => $context] : null;
62:
63: parent::__construct(self::JSON_RPC_VERSION, $id, 'completion/complete', array_filter([
64: '_meta' => $meta,
65: 'ref' => $ref->toArray(),
66: 'argument' => ['name' => $argument['name'], 'value' => $argument['value']],
67: 'context' => $context,
68: ], static fn(mixed $value): bool => null !== $value));
69: }
70: }
71: