-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathMessageFactory.php
More file actions
168 lines (150 loc) · 5.52 KB
/
MessageFactory.php
File metadata and controls
168 lines (150 loc) · 5.52 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?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\JsonRpc;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Exception\InvalidInputMessageException;
use Mcp\Schema;
use Mcp\Schema\JsonRpc\Error;
use Mcp\Schema\JsonRpc\MessageInterface;
use Mcp\Schema\JsonRpc\Notification;
use Mcp\Schema\JsonRpc\Request;
use Mcp\Schema\JsonRpc\Response;
/**
* Factory for creating JSON-RPC message objects from raw input.
*
* Handles all types of JSON-RPC messages:
* - Requests (have method + id)
* - Notifications (have method, no id)
* - Responses (have result + id)
* - Errors (have error + id)
*
* @author Christopher Hertel <mail@christopher-hertel.de>
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
final class MessageFactory
{
/**
* Registry of all known message classes that have methods.
*
* @var list<class-string<Request>|class-string<Notification>>
*/
private const REGISTERED_MESSAGES = [
Schema\Notification\CancelledNotification::class,
Schema\Notification\InitializedNotification::class,
Schema\Notification\LoggingMessageNotification::class,
Schema\Notification\ProgressNotification::class,
Schema\Notification\PromptListChangedNotification::class,
Schema\Notification\ResourceListChangedNotification::class,
Schema\Notification\ResourceUpdatedNotification::class,
Schema\Notification\RootsListChangedNotification::class,
Schema\Notification\ToolListChangedNotification::class,
Schema\Request\CallToolRequest::class,
Schema\Request\CompletionCompleteRequest::class,
Schema\Request\CreateSamplingMessageRequest::class,
Schema\Request\GetPromptRequest::class,
Schema\Request\InitializeRequest::class,
Schema\Request\ListPromptsRequest::class,
Schema\Request\ListResourcesRequest::class,
Schema\Request\ListResourceTemplatesRequest::class,
Schema\Request\ListRootsRequest::class,
Schema\Request\ListToolsRequest::class,
Schema\Request\PingRequest::class,
Schema\Request\ReadResourceRequest::class,
Schema\Request\ResourceSubscribeRequest::class,
Schema\Request\ResourceUnsubscribeRequest::class,
Schema\Request\SetLogLevelRequest::class,
];
/**
* @param list<class-string<Request>|class-string<Notification>> $registeredMessages
*/
public function __construct(
private readonly array $registeredMessages,
) {
foreach ($this->registeredMessages as $messageClass) {
if (!is_subclass_of($messageClass, Request::class) && !is_subclass_of($messageClass, Notification::class)) {
throw new InvalidArgumentException(\sprintf('Message classes must extend %s or %s.', Request::class, Notification::class));
}
}
}
/**
* Creates a new Factory instance with all the protocol's default messages.
*/
public static function make(): self
{
return new self(self::REGISTERED_MESSAGES);
}
/**
* Creates message objects from JSON input.
*
* Supports both single messages and batch requests. Returns an array containing
* MessageInterface objects or InvalidInputMessageException instances for invalid messages.
*
* @return array<MessageInterface|InvalidInputMessageException>
*
* @throws \JsonException When the input string is not valid JSON
*/
public function create(string $input): array
{
$data = json_decode($input, true, flags: \JSON_THROW_ON_ERROR);
if ('{' === $input[0]) {
$data = [$data];
}
$messages = [];
foreach ($data as $message) {
try {
$messages[] = $this->createMessage($message);
} catch (InvalidInputMessageException $e) {
$messages[] = $e;
}
}
return $messages;
}
/**
* Creates a single message object from parsed JSON data.
*
* @param array<string, mixed> $data
*
* @throws InvalidInputMessageException
*/
private function createMessage(array $data): MessageInterface
{
try {
if (isset($data['error'])) {
return Error::fromArray($data);
}
if (isset($data['result'])) {
return Response::fromArray($data);
}
if (!isset($data['method'])) {
throw new InvalidInputMessageException('Invalid JSON-RPC message: missing "method", "result", or "error" field.');
}
$messageClass = $this->findMessageClassByMethod($data['method']);
return $messageClass::fromArray($data);
} catch (InvalidArgumentException $e) {
throw new InvalidInputMessageException($e->getMessage(), 0, $e);
}
}
/**
* Finds the registered message class for a given method name.
*
* @return class-string<Request>|class-string<Notification>
*
* @throws InvalidInputMessageException
*/
private function findMessageClassByMethod(string $method): string
{
foreach ($this->registeredMessages as $messageClass) {
if ($messageClass::getMethod() === $method) {
return $messageClass;
}
}
throw new InvalidInputMessageException(\sprintf('Unknown method "%s".', $method));
}
}