Skip to content

Commit 24e36df

Browse files
author
klapaudius
committed
Enhance error handling with additional context data
Updated JsonRpcErrorException instances to include relevant context data for better debugging. Adjusted test cases accordingly to validate the new data fields. Added detailed comments to improve code clarity for message handling methods.
1 parent aa4359a commit 24e36df

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

src/Protocol/MCPProtocol.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function handleMessage(string $clientId, array $message): void
122122
$messageId = $message['id'] ?? null;
123123
try {
124124
if (! isset($message['jsonrpc']) || $message['jsonrpc'] !== '2.0') {
125-
throw new JsonRpcErrorException(message: 'Invalid Request: Not a valid JSON-RPC 2.0 message', code: JsonRpcErrorCode::INVALID_REQUEST);
125+
throw new JsonRpcErrorException(message: 'Invalid Request: Not a valid JSON-RPC 2.0 message', code: JsonRpcErrorCode::INVALID_REQUEST, data: $message);
126126
}
127127

128128
$requestData = DataUtil::makeRequestData(message: $message);
@@ -137,7 +137,7 @@ public function handleMessage(string $clientId, array $message): void
137137
return;
138138
}
139139

140-
throw new JsonRpcErrorException(message: 'Invalid Request: Message format not recognized', code: JsonRpcErrorCode::INVALID_REQUEST);
140+
throw new JsonRpcErrorException(message: 'Invalid Request: Message format not recognized', code: JsonRpcErrorCode::INVALID_REQUEST, data: $message);
141141
} catch (JsonRpcErrorException $e) {
142142
$this->pushMessage(clientId: $clientId, message: new JsonRpcErrorResource(exception: $e, id: $messageId));
143143
} catch (Exception $e) {
@@ -168,7 +168,7 @@ private function handleRequestProcess(string $clientId, RequestData $requestData
168168
}
169169
}
170170

171-
throw new JsonRpcErrorException("Method not found: {$requestData->method}", JsonRpcErrorCode::METHOD_NOT_FOUND);
171+
throw new JsonRpcErrorException("Method not found: {$requestData->method}", JsonRpcErrorCode::METHOD_NOT_FOUND, data: $requestData->toArray());
172172
} catch (JsonRpcErrorException $e) {
173173
$this->pushMessage(clientId: $clientId, message: new JsonRpcErrorResource(exception: $e, id: $messageId));
174174
} catch (ToolParamsValidatorException $e) {
@@ -199,7 +199,7 @@ private function handleNotificationProcess(string $clientId, NotificationData $n
199199
}
200200
}
201201

202-
throw new JsonRpcErrorException("Method not found: {$notificationData->method}", JsonRpcErrorCode::METHOD_NOT_FOUND);
202+
throw new JsonRpcErrorException("Method not found: {$notificationData->method}", JsonRpcErrorCode::METHOD_NOT_FOUND, data: $notificationData->toArray());
203203
} catch (JsonRpcErrorException $e) {
204204
$this->pushMessage(clientId: $clientId, message: new JsonRpcErrorResource(exception: $e, id: null));
205205
} catch (Exception $e) {
@@ -209,7 +209,12 @@ private function handleNotificationProcess(string $clientId, NotificationData $n
209209
}
210210

211211
/**
212-
* @throws Exception
212+
* Pushes a message to a specified client.
213+
*
214+
* @param string $clientId The unique identifier of the client to push the message to.
215+
* @param array|JsonRpcResultResource|JsonRpcErrorResource $message The message to be pushed to the client, either as an array or an instance of JsonRpcResultResource/JsonRpcErrorResource.
216+
*
217+
* @throws Exception If transport is unable to push the message to client
213218
*/
214219
private function pushMessage(string $clientId, array|JsonRpcResultResource|JsonRpcErrorResource $message): void
215220
{
@@ -222,6 +227,14 @@ private function pushMessage(string $clientId, array|JsonRpcResultResource|JsonR
222227
$this->transport->pushMessage(clientId: $clientId, message: $message);
223228
}
224229

230+
/**
231+
* Process an incoming message from a specified client.
232+
*
233+
* @param string $clientId The unique identifier of the client.
234+
* @param array $message The message data to be processed.
235+
*
236+
* @throws Exception If the message cannot be processed
237+
*/
225238
public function requestMessage(string $clientId, array $message): void
226239
{
227240
$this->transport->processMessage(clientId: $clientId, message: $message);

src/Server/MCPServer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function registerNotificationHandler(NotificationHandler $handler): void
164164
public function initialize(InitializeData $data): InitializeResource
165165
{
166166
if ($this->initialized) {
167-
throw new JsonRpcErrorException(message: 'Server already initialized', code: JsonRpcErrorCode::INVALID_REQUEST);
167+
throw new JsonRpcErrorException(message: 'Server already initialized', code: JsonRpcErrorCode::INVALID_REQUEST, data: $data);
168168
}
169169

170170
$this->initialized = true;

src/Server/Request/ToolsCallHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public function execute(string $method, ?array $params = null): array
3737
{
3838
$name = $params['name'] ?? null;
3939
if ($name === null) {
40-
throw new JsonRpcErrorException(message: 'Tool name is required', code: JsonRpcErrorCode::INVALID_REQUEST);
40+
throw new JsonRpcErrorException(message: 'Tool name is required', code: JsonRpcErrorCode::INVALID_REQUEST, data: $params);
4141
}
4242

4343
$tool = $this->toolRepository->getTool($name);
4444
if (! $tool) {
45-
throw new JsonRpcErrorException(message: "Tool '{$name}' not found", code: JsonRpcErrorCode::METHOD_NOT_FOUND);
45+
throw new JsonRpcErrorException(message: "Tool '{$name}' not found", code: JsonRpcErrorCode::METHOD_NOT_FOUND, data:$params);
4646
}
4747

4848
$arguments = $params['arguments'] ?? [];

tests/Server/MCPServerTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,16 @@ public function test_initialize_throws_when_already_initialized(): void
249249
$server->initialize($initializeData);
250250

251251
// Expect
252-
$this->expectException(JsonRpcErrorException::class);
253-
$this->expectExceptionMessage('Server already initialized');
254252

255253
// Act
256-
$server->initialize($initializeData);
254+
try {
255+
$server->initialize($initializeData);
256+
$this->fail('Expected exception not thrown');
257+
} catch (JsonRpcErrorException $e) {
258+
$this->assertEquals(-32600, $e->getJsonRpcErrorCode());
259+
$this->assertEquals('Server already initialized', $e->getMessage());
260+
$this->assertEquals($initializeData, $e->getErrorData());
261+
}
257262
}
258263

259264
/**

0 commit comments

Comments
 (0)