Skip to content

Commit 23249ca

Browse files
committed
Add JsonRpcNotification and abstract Notification
1 parent 1ecb3d7 commit 23249ca

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the Nexus MCP SDK package.
7+
*
8+
* (c) 2025 John Paul E. Balandan, CPA <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Nexus\Mcp\Message;
15+
16+
/**
17+
* A notification which does not expect a response.
18+
*/
19+
final readonly class JsonRpcNotification extends Notification
20+
{
21+
public function __construct(string $jsonrpc, string $method, ?array $params = null)
22+
{
23+
parent::__construct($jsonrpc, $method, $params);
24+
}
25+
26+
/**
27+
* @return array{
28+
* jsonrpc: non-empty-string,
29+
* method: non-empty-string,
30+
* params?: null|array<string, mixed>,
31+
* }
32+
*/
33+
#[\Override]
34+
public function toArray(): array
35+
{
36+
return array_filter([
37+
'jsonrpc' => $this->jsonrpc,
38+
'method' => $this->method,
39+
'params' => $this->params,
40+
], static fn(mixed $value): bool => null !== $value);
41+
}
42+
}

src/Message/Notification.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the Nexus MCP SDK package.
7+
*
8+
* (c) 2025 John Paul E. Balandan, CPA <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Nexus\Mcp\Message;
15+
16+
/**
17+
* The base JSON-RPC notification object.
18+
*/
19+
abstract readonly class Notification extends Message
20+
{
21+
/**
22+
* @param non-empty-string $jsonrpc
23+
* @param non-empty-string $method
24+
* @param null|array<string, mixed> $params
25+
*/
26+
public function __construct(
27+
string $jsonrpc,
28+
public string $method,
29+
public ?array $params = null,
30+
) {
31+
parent::__construct($jsonrpc);
32+
}
33+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the Nexus MCP SDK package.
7+
*
8+
* (c) 2025 John Paul E. Balandan, CPA <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Nexus\Mcp\Tests\Message;
15+
16+
use Nexus\Mcp\Message\JsonRpcNotification;
17+
use Nexus\Mcp\Message\Notification;
18+
use PHPUnit\Framework\Attributes\CoversClass;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* @internal
23+
*/
24+
#[CoversClass(JsonRpcNotification::class)]
25+
#[CoversClass(Notification::class)]
26+
final class JsonRpcNotificationTest extends TestCase
27+
{
28+
public function testNotificationCanBeJsonSerialized(): void
29+
{
30+
$notification = new JsonRpcNotification('2.0', 'initialized');
31+
self::assertSame([
32+
'jsonrpc' => '2.0',
33+
'method' => 'initialized',
34+
], $notification->toArray());
35+
self::assertSame('{"jsonrpc":"2.0","method":"initialized"}', json_encode($notification));
36+
}
37+
38+
public function testNotificationWithParamsCanBeJsonSerialized(): void
39+
{
40+
$notification = new JsonRpcNotification(
41+
'2.0',
42+
'initialized',
43+
['param1' => 'value1', 'param2' => 'value2'],
44+
);
45+
self::assertSame([
46+
'jsonrpc' => '2.0',
47+
'method' => 'initialized',
48+
'params' => ['param1' => 'value1', 'param2' => 'value2'],
49+
], $notification->toArray());
50+
self::assertSame(
51+
'{"jsonrpc":"2.0","method":"initialized","params":{"param1":"value1","param2":"value2"}}',
52+
json_encode($notification),
53+
);
54+
}
55+
}

0 commit comments

Comments
 (0)