-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathRequest.php
More file actions
136 lines (116 loc) · 3.44 KB
/
Copy pathRequest.php
File metadata and controls
136 lines (116 loc) · 3.44 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?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\JsonRpc;
use Mcp\Exception\InvalidArgumentException;
/**
* @phpstan-type RequestData array{
* jsonrpc: string,
* id: string|int,
* method: string,
* params?: array<string, mixed>,
* }
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
abstract class Request implements HasMethodInterface, MessageInterface
{
protected string|int $id;
/**
* @var array<string, mixed>|null
*/
protected ?array $meta = null;
abstract public static function getMethod(): string;
/**
* @param RequestData $data
*/
public static function fromArray(array $data): static
{
if (($data['jsonrpc'] ?? null) !== MessageInterface::JSONRPC_VERSION) {
throw new InvalidArgumentException('Invalid or missing "jsonrpc" version for Request.');
}
if (!isset($data['id']) || !\is_string($data['id']) && !\is_int($data['id'])) {
throw new InvalidArgumentException('Invalid or missing "id" for Request.');
}
if (!isset($data['method']) || !\is_string($data['method'])) {
throw new InvalidArgumentException('Invalid or missing "method" for Request.');
}
$params = $data['params'] ?? null;
if ($params instanceof \stdClass) {
$params = (array) $params;
}
if (null !== $params && !\is_array($params)) {
throw new InvalidArgumentException('"params" for Request must be an array/object or null.');
}
$request = static::fromParams($params);
$request->id = $data['id'];
if (isset($data['params']['_meta'])) {
$meta = $data['params']['_meta'];
if ($meta instanceof \stdClass) {
$meta = (array) $meta;
}
if (\is_array($meta)) {
$request->meta = $meta;
}
}
return $request;
}
/**
* @param array<string, mixed>|null $params
*/
abstract protected static function fromParams(?array $params): static;
public function getId(): string|int
{
return $this->id;
}
/**
* @return array<string, mixed>|null
*/
public function getMeta(): ?array
{
return $this->meta;
}
public function withId(string|int $id): static
{
$clone = clone $this;
$clone->id = $id;
return $clone;
}
/**
* @param array<string, mixed>|null $meta
*/
public function withMeta(?array $meta): static
{
$clone = clone $this;
$clone->meta = $meta;
return $clone;
}
/**
* @return RequestData
*/
public function jsonSerialize(): array
{
$array = [
'jsonrpc' => MessageInterface::JSONRPC_VERSION,
'id' => $this->id,
'method' => static::getMethod(),
];
if (null !== $params = $this->getParams()) {
$array['params'] = $params;
}
if (null !== $this->meta && !isset($params['_meta'])) {
$array['params']['_meta'] = $this->meta;
}
return $array;
}
/**
* @return array<non-empty-string, mixed>|null
*/
abstract protected function getParams(): ?array;
}