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\Content;
15:
16: use Nexus\Mcp\Schema\Annotations;
17: use Nexus\Mcp\Schema\Arrayable;
18:
19: /**
20: * Text provided to or from an LLM.
21: *
22: * @implements Arrayable<array{
23: * _meta?: array<string, mixed>,
24: * type: 'text',
25: * text: string,
26: * annotations?: template-type<Annotations, Arrayable, 'T'>,
27: * }>
28: */
29: final readonly class TextContent implements \JsonSerializable, Arrayable, ContentBlock
30: {
31: /**
32: * @var non-empty-string
33: */
34: public string $type;
35:
36: /**
37: * @param string $text The text content of the message.
38: * @param null|Annotations $annotations Optional annotations for the client.
39: * @param null|array<string, mixed> $meta Reserved by MCP to allow clients and servers to attach
40: * additional metadata to their interactions.
41: */
42: public function __construct(
43: public string $text,
44: public ?Annotations $annotations = null,
45: public ?array $meta = null,
46: ) {
47: $this->type = 'text';
48: }
49:
50: #[\Override]
51: public function toArray(): array
52: {
53: return array_filter([
54: '_meta' => $this->meta,
55: 'type' => $this->type,
56: 'text' => $this->text,
57: 'annotations' => $this->annotations?->toArray(),
58: ], static fn(mixed $value): bool => null !== $value);
59: }
60:
61: /**
62: * @return template-type<self, Arrayable, 'T'>
63: */
64: #[\Override]
65: public function jsonSerialize(): array
66: {
67: return $this->toArray();
68: }
69: }
70: