Skip to content

Commit e7d9c0f

Browse files
authored
Fix: Add support for notifications/initialized method (#41)
1 parent 8131dc5 commit e7d9c0f

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

src/Data/Resources/JsonRpc/JsonRpcResultResource.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@ class JsonRpcResultResource
1919
* The identifier established by the client for the JSON-RPC request.
2020
* Must be the same as the value of the id member in the Request Object.
2121
* If there was an error in detecting the id in the Request object (e.g. Parse error/Invalid Request), it MUST be Null.
22+
* For notifications, this is null as notifications don't have IDs.
2223
*/
23-
protected string|int $id;
24+
protected string|int|null $id;
2425

2526
/**
2627
* JsonRpcResultResource constructor.
2728
*
28-
* @param string|int $id The identifier established by the client.
29+
* @param string|int|null $id The identifier established by the client. Null for notifications.
2930
* @param array|stdClass $result The result data from the method execution.
3031
*/
31-
public function __construct(string|int $id, array|stdClass $result)
32+
public function __construct(string|int|null $id, array|stdClass $result)
3233
{
3334
$this->result = $result;
3435
$this->id = $id;
@@ -37,7 +38,7 @@ public function __construct(string|int $id, array|stdClass $result)
3738
/**
3839
* Formats the data into a JSON-RPC 2.0 compliant response array.
3940
*
40-
* @return array{jsonrpc: string, id: string|int, result: array|stdClass} The JSON-RPC response array.
41+
* @return array{jsonrpc: string, id: string|int|null, result: array|stdClass} The JSON-RPC response array.
4142
*/
4243
public function toResponse(): array
4344
{

src/Protocol/MCPProtocol.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace OPGG\LaravelMcpServer\Protocol;
44

55
use Exception;
6-
use Illuminate\Support\Str;
76
use OPGG\LaravelMcpServer\Data\ProcessMessageData;
87
use OPGG\LaravelMcpServer\Data\Requests\NotificationData;
98
use OPGG\LaravelMcpServer\Data\Requests\RequestData;
@@ -196,14 +195,13 @@ private function sendSSEMessage(string $clientId, array|JsonRpcResultResource|Js
196195
*/
197196
private function processNotification(string $clientId, NotificationData $notificationData): ProcessMessageData
198197
{
199-
// todo:: processNotification currently not implemented
200198
$method = $notificationData->method;
201-
$handler = $this->requestHandlers[$method] ?? null;
199+
$handler = $this->notificationHandlers[$method] ?? null;
202200
if ($handler) {
203-
$result = $handler->execute(method: $notificationData->method, params: $notificationData->params);
201+
$result = $handler->execute(params: $notificationData->params);
204202
$messageType = $handler->getMessageType($notificationData->params);
205203

206-
$resultResource = new JsonRpcResultResource(id: Str::uuid()->toString(), result: $result);
204+
$resultResource = new JsonRpcResultResource(id: null, result: $result);
207205
$processMessageData = new ProcessMessageData(messageType: $messageType, resource: $resultResource);
208206

209207
if ($processMessageData->isSSEMessage()) {

src/Server/MCPServer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use OPGG\LaravelMcpServer\Protocol\Handlers\NotificationHandler;
1111
use OPGG\LaravelMcpServer\Protocol\Handlers\RequestHandler;
1212
use OPGG\LaravelMcpServer\Protocol\MCPProtocol;
13+
use OPGG\LaravelMcpServer\Server\Notification\InitializedHandler;
1314
use OPGG\LaravelMcpServer\Server\Request\InitializeHandler;
1415
use OPGG\LaravelMcpServer\Server\Request\PingHandler;
1516
use OPGG\LaravelMcpServer\Server\Request\PromptsGetHandler;
@@ -85,6 +86,9 @@ public function __construct(MCPProtocol $protocol, array $serverInfo, ?ServerCap
8586

8687
// Initialize Default Handlers
8788
$this->registerRequestHandler(new PingHandler);
89+
90+
// Register notification handlers
91+
$this->registerNotificationHandler(new InitializedHandler);
8892
}
8993

9094
/**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace OPGG\LaravelMcpServer\Server\Notification;
4+
5+
use OPGG\LaravelMcpServer\Enums\ProcessMessageType;
6+
use OPGG\LaravelMcpServer\Protocol\Handlers\NotificationHandler;
7+
use stdClass;
8+
9+
class InitializedHandler extends NotificationHandler
10+
{
11+
protected const MESSAGE_TYPE = ProcessMessageType::HTTP;
12+
13+
protected const HANDLE_METHOD = 'notifications/initialized';
14+
15+
public function execute(?array $params = null): array|stdClass
16+
{
17+
// The 'notifications/initialized' is sent by the client after the initialization
18+
// handshake is complete. This is purely for acknowledgment - no response is needed.
19+
// We return an empty array to indicate successful processing.
20+
return [];
21+
}
22+
}

0 commit comments

Comments
 (0)