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\Core\Schema\Request;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\JsonRpc\JsonRpcRequest;
18: use Nexus\Mcp\Core\Schema\RequestId;
19: use Nexus\Mcp\Core\Schema\RequestParams\CreateMessageRequestParams;
20:
21: /**
22: * A request from the server to sample an LLM via the client. The client has full discretion over
23: * which model to select. The client should also inform the user before beginning sampling, to
24: * allow them to inspect the request (human in the loop) and decide whether to approve it.
25: *
26: * @property-read CreateMessageRequestParams $params
27: *
28: * @extends JsonRpcRequest<'sampling/createMessage'>
29: *
30: * @see https://modelcontextprotocol.io/specification/2025-11-25/schema#createmessagerequest
31: */
32: final readonly class CreateMessageRequest extends JsonRpcRequest implements ServerRequest
33: {
34: public function __construct(RequestId $id, CreateMessageRequestParams $params)
35: {
36: parent::__construct($id, $params);
37: }
38:
39: #[\Override]
40: public static function getMethod(): string
41: {
42: return 'sampling/createMessage';
43: }
44:
45: #[\Override]
46: public static function fromArray(array $data): static
47: {
48: Assert::that($data)->hasOffset('id', 'missing the required "id" key.');
49: $id = $data['id'];
50: Assert::that($id)->isArrayKey('"id" must be an int or string, {type} given.');
51:
52: Assert::that($data)->hasOffset('params', 'missing the required "params" key.');
53: Assert::that($data['params'])
54: ->isArray('"params" must be an object, {type} given.')
55: ->isMap('"params" must be a string-keyed object.')
56: ;
57:
58: return new self(new RequestId($id), CreateMessageRequestParams::fromArray($data['params']));
59: }
60: }
61: