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\Result;
15:
16: use Nexus\Mcp\Schema\Arrayable;
17: use Nexus\Mcp\Schema\Content\AudioContent;
18: use Nexus\Mcp\Schema\Content\ImageContent;
19: use Nexus\Mcp\Schema\Content\TextContent;
20: use Nexus\Mcp\Schema\Enum\Role;
21:
22: /**
23: * The client's response to a `sampling/create_message` request from the server. The client should inform the
24: * user before returning the sampled message, to allow them to inspect the response (human in the loop) and
25: * decide whether to allow the server to see it.
26: *
27: * @extends Result<array{
28: * _meta?: array<string, mixed>,
29: * role: 'assistant'|'user',
30: * content: template-type<AudioContent|ImageContent|TextContent, Arrayable, 'T'>,
31: * model: non-empty-string,
32: * stopReason?: non-empty-string,
33: * }>
34: */
35: final readonly class CreateMessageResult extends Result implements ClientResult
36: {
37: /**
38: * @param non-empty-string $model The name of the model that generated the message.
39: * @param null|non-empty-string $stopReason The reason why sampling stopped, if known.
40: */
41: public function __construct(
42: public Role $role,
43: public AudioContent|ImageContent|TextContent $content,
44: public string $model,
45: public ?string $stopReason = null,
46: ?array $meta = null,
47: ) {
48: parent::__construct($meta);
49: }
50:
51: #[\Override]
52: public function toArray(): array
53: {
54: return array_filter([
55: '_meta' => $this->meta,
56: 'role' => $this->role->value,
57: 'content' => $this->content->toArray(),
58: 'model' => $this->model,
59: 'stopReason' => $this->stopReason,
60: ], static fn(mixed $value): bool => null !== $value);
61: }
62: }
63: