| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace WP\McpSchema\Server\Prompts\DTO; |
| 6 |
|
| 7 |
use WP\McpSchema\Common\AbstractDataTransferObject; |
| 8 |
use WP\McpSchema\Common\Protocol\Factory\ContentBlockFactory; |
| 9 |
use WP\McpSchema\Common\Protocol\Union\ContentBlockInterface; |
| 10 |
use WP\McpSchema\Common\Traits\ValidatesRequiredFields; |
| 11 |
|
| 12 |
/** |
| 13 |
* Describes a message returned as part of a prompt. |
| 14 |
* |
| 15 |
* This is similar to `SamplingMessage`, but also supports the embedding of |
| 16 |
* resources from the MCP server. |
| 17 |
* |
| 18 |
* @since 2024-11-05 |
| 19 |
* @last-updated 2025-11-25 (modified properties: content, role) |
| 20 |
* |
| 21 |
* @mcp-domain Server |
| 22 |
* @mcp-subdomain Prompts |
| 23 |
* @mcp-version 2025-11-25 |
| 24 |
*/ |
| 25 |
class PromptMessage extends AbstractDataTransferObject |
| 26 |
{ |
| 27 |
use ValidatesRequiredFields; |
| 28 |
|
| 29 |
/** |
| 30 |
* @since 2024-11-05 |
| 31 |
* |
| 32 |
* @var 'user'|'assistant' |
| 33 |
*/ |
| 34 |
protected string $role; |
| 35 |
|
| 36 |
/** |
| 37 |
* @since 2024-11-05 |
| 38 |
* |
| 39 |
* @var \WP\McpSchema\Common\Protocol\Union\ContentBlockInterface |
| 40 |
*/ |
| 41 |
protected ContentBlockInterface $content; |
| 42 |
|
| 43 |
/** |
| 44 |
* @param 'user'|'assistant' $role @since 2024-11-05 |
| 45 |
* @param \WP\McpSchema\Common\Protocol\Union\ContentBlockInterface $content @since 2024-11-05 |
| 46 |
*/ |
| 47 |
public function __construct( |
| 48 |
string $role, |
| 49 |
ContentBlockInterface $content |
| 50 |
) { |
| 51 |
$this->role = $role; |
| 52 |
$this->content = $content; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Creates an instance from an array. |
| 57 |
* |
| 58 |
* @param array{ |
| 59 |
* role: 'user'|'assistant', |
| 60 |
* content: array<string, mixed>|\WP\McpSchema\Common\Protocol\Union\ContentBlockInterface |
| 61 |
* } $data |
| 62 |
* @phpstan-param array<string, mixed> $data |
| 63 |
* @return self |
| 64 |
*/ |
| 65 |
public static function fromArray(array $data): self |
| 66 |
{ |
| 67 |
self::assertRequired($data, ['role', 'content']); |
| 68 |
|
| 69 |
/** @var 'user'|'assistant' $role */ |
| 70 |
$role = self::asString($data['role']); |
| 71 |
|
| 72 |
/** @var \WP\McpSchema\Common\Protocol\Union\ContentBlockInterface $content */ |
| 73 |
$content = is_array($data['content']) |
| 74 |
? ContentBlockFactory::fromArray(self::asArray($data['content'])) |
| 75 |
: $data['content']; |
| 76 |
|
| 77 |
return new self( |
| 78 |
$role, |
| 79 |
$content |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Converts the instance to an array. |
| 85 |
* |
| 86 |
* @return array<string, mixed> |
| 87 |
*/ |
| 88 |
public function toArray(): array |
| 89 |
{ |
| 90 |
$result = []; |
| 91 |
|
| 92 |
$result['role'] = $this->role; |
| 93 |
$result['content'] = $this->content->toArray(); |
| 94 |
|
| 95 |
return $result; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @return 'user'|'assistant' |
| 100 |
*/ |
| 101 |
public function getRole(): string |
| 102 |
{ |
| 103 |
return $this->role; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* @return \WP\McpSchema\Common\Protocol\Union\ContentBlockInterface |
| 108 |
*/ |
| 109 |
public function getContent(): ContentBlockInterface |
| 110 |
{ |
| 111 |
return $this->content; |
| 112 |
} |
| 113 |
} |
| 114 |
|