Skip to content

Commit 912cec1

Browse files
committed
fix: improve exception handling in MCPProtocol with enhanced logging and Throwable support
1 parent 52ae48e commit 912cec1

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/Protocol/MCPProtocol.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use Illuminate\Support\Facades\Log;
7+
use Throwable;
78
use OPGG\LaravelMcpServer\Data\ProcessMessageData;
89
use OPGG\LaravelMcpServer\Data\Requests\NotificationData;
910
use OPGG\LaravelMcpServer\Data\Requests\RequestData;
@@ -127,13 +128,22 @@ public function handleMessage(string $clientId, array $message): ProcessMessageD
127128

128129
throw new JsonRpcErrorException(message: 'Invalid Request: Message format not recognized', code: JsonRpcErrorCode::INVALID_REQUEST);
129130
} catch (JsonRpcErrorException $e) {
131+
report($e);
130132
$jsonErrorResource = new JsonRpcErrorResource(exception: $e, id: $messageId);
131133
$this->sendSSEMessage(clientId: $clientId, message: $jsonErrorResource);
132134

133135
return new ProcessMessageData(messageType: ProcessMessageType::HTTP, resource: $jsonErrorResource, isNotification: false);
134-
} catch (Exception $e) {
136+
} catch (Throwable $e) {
137+
report($e);
138+
135139
$jsonErrorResource = new JsonRpcErrorResource(
136-
exception: new JsonRpcErrorException(message: 'INTERNAL_ERROR', code: JsonRpcErrorCode::INTERNAL_ERROR),
140+
exception: new JsonRpcErrorException(
141+
message: $e->getMessage() ?: 'INTERNAL_ERROR',
142+
code: JsonRpcErrorCode::INTERNAL_ERROR,
143+
data: [
144+
'exception' => $e::class,
145+
]
146+
),
137147
id: $messageId
138148
);
139149
$this->sendSSEMessage(clientId: $clientId, message: $jsonErrorResource);

0 commit comments

Comments
 (0)