Skip to content

Commit 37c6aa3

Browse files
committed
Add abstract Message
1 parent 924d9d3 commit 37c6aa3

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

src/Message/Message.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 JSON-RPC message.
18+
*/
19+
abstract readonly class Message implements \JsonSerializable
20+
{
21+
final public const string JSON_RPC_VERSION = '2.0';
22+
23+
/**
24+
* @var non-empty-string
25+
*/
26+
public string $jsonrpc;
27+
28+
public function __construct(string $jsonrpc)
29+
{
30+
if ('' === $jsonrpc) {
31+
throw new \InvalidArgumentException('JSON-RPC version cannot be empty.');
32+
}
33+
34+
if (self::JSON_RPC_VERSION !== $jsonrpc) {
35+
throw new \InvalidArgumentException('Invalid JSON-RPC version.');
36+
}
37+
38+
$this->jsonrpc = $jsonrpc;
39+
}
40+
41+
/**
42+
* Get the array representation of the message.
43+
*
44+
* @return array<string, mixed>
45+
*/
46+
abstract public function toArray(): array;
47+
48+
/**
49+
* @return array<string, mixed>
50+
*/
51+
#[\Override]
52+
public function jsonSerialize(): array
53+
{
54+
return $this->toArray();
55+
}
56+
}

tests/Message/MessageTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\Message;
17+
use PHPUnit\Framework\Attributes\CoversClass;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* @internal
22+
*/
23+
#[CoversClass(Message::class)]
24+
final class MessageTest extends TestCase
25+
{
26+
public function testMessageDoesNotAcceptInvalidJsonRpcVersion(): void
27+
{
28+
$this->expectException(\InvalidArgumentException::class);
29+
$this->expectExceptionMessage('Invalid JSON-RPC version.');
30+
$this->getMockBuilder(Message::class)->setConstructorArgs(['1.1'])->getMock();
31+
}
32+
33+
public function testMessageDoesNotAcceptEmptyJsonRpcVersion(): void
34+
{
35+
$this->expectException(\InvalidArgumentException::class);
36+
$this->expectExceptionMessage('JSON-RPC version cannot be empty.');
37+
$this->getMockBuilder(Message::class)->setConstructorArgs([''])->getMock();
38+
}
39+
40+
public function testMessageCanBeJsonSerialized(): void
41+
{
42+
$message = $this->getMockBuilder(Message::class)
43+
->setConstructorArgs([Message::JSON_RPC_VERSION])
44+
->onlyMethods(['toArray'])
45+
->getMock()
46+
;
47+
$message->method('toArray')->willReturn(['jsonrpc' => Message::JSON_RPC_VERSION]);
48+
49+
self::assertSame('{"jsonrpc":"2.0"}', json_encode($message));
50+
}
51+
}

0 commit comments

Comments
 (0)