-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathElicitResult.php
More file actions
87 lines (73 loc) · 2.41 KB
/
Copy pathElicitResult.php
File metadata and controls
87 lines (73 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Schema\Result;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Schema\Enum\ElicitAction;
use Mcp\Schema\JsonRpc\ResultInterface;
/**
* The client's response to an elicitation/create request from the server.
*
* Contains the user's action (accept, decline, or cancel) and the content
* they provided when accepting.
*
* @author Johannes Wachter <johannes@sulu.io>
*/
final class ElicitResult implements ResultInterface
{
/**
* @param ElicitAction $action The user's action in response to the elicitation
* @param array<string, mixed>|null $content The content provided by the user (only present when action is "accept")
*/
public function __construct(
public readonly ElicitAction $action,
public readonly ?array $content = null,
) {
}
/**
* @param array{action: string, content?: array<string, mixed>} $data
*/
public static function fromArray(array $data): self
{
if (!isset($data['action']) || !\is_string($data['action'])) {
throw new InvalidArgumentException('Missing or invalid "action" in ElicitResult data.');
}
$action = ElicitAction::from($data['action']);
$content = isset($data['content']) && \is_array($data['content']) ? $data['content'] : null;
if (ElicitAction::Accept === $action && null === $content) {
throw new InvalidArgumentException('Content must be provided when action is "accept".');
}
return new self($action, $content);
}
public function isAccepted(): bool
{
return ElicitAction::Accept === $this->action;
}
public function isDeclined(): bool
{
return ElicitAction::Decline === $this->action;
}
public function isCancelled(): bool
{
return ElicitAction::Cancel === $this->action;
}
/**
* @return array{action: string, content?: array<string, mixed>}
*/
public function jsonSerialize(): array
{
$result = [
'action' => $this->action->value,
];
if (null !== $this->content) {
$result['content'] = $this->content;
}
return $result;
}
}