Skip to content

Commit 0158b0e

Browse files
committed
Add JsonRpcRequest and abstract Request
1 parent f54805f commit 0158b0e

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

src/Message/JsonRpcRequest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 request that expects a response.
18+
*/
19+
final readonly class JsonRpcRequest extends Request
20+
{
21+
public function __construct(
22+
public RequestId $requestId,
23+
string $jsonrpc,
24+
string $method,
25+
?array $params = null,
26+
) {
27+
parent::__construct($jsonrpc, $method, $params);
28+
}
29+
30+
/**
31+
* @return array{
32+
* jsonrpc: non-empty-string,
33+
* id: int|non-empty-string,
34+
* method: non-empty-string,
35+
* params?: null|array<string, mixed>,
36+
* }
37+
*/
38+
#[\Override]
39+
public function toArray(): array
40+
{
41+
return array_filter([
42+
'jsonrpc' => $this->jsonrpc,
43+
'id' => $this->requestId->id,
44+
'method' => $this->method,
45+
'params' => $this->params,
46+
], static fn(mixed $value): bool => null !== $value);
47+
}
48+
}

src/Message/Request.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 request object.
18+
*/
19+
abstract readonly class Request 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: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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\JsonRpcRequest;
17+
use Nexus\Mcp\Message\Request;
18+
use Nexus\Mcp\Message\RequestId;
19+
use PHPUnit\Framework\Attributes\CoversClass;
20+
use PHPUnit\Framework\TestCase;
21+
22+
/**
23+
* @internal
24+
*/
25+
#[CoversClass(JsonRpcRequest::class)]
26+
#[CoversClass(Request::class)]
27+
final class JsonRpcRequestTest extends TestCase
28+
{
29+
public function testRequestCanBeJsonSerialized(): void
30+
{
31+
$request = new JsonRpcRequest(RequestId::fromId('123'), '2.0', 'initialize');
32+
self::assertSame([
33+
'jsonrpc' => '2.0',
34+
'id' => '123',
35+
'method' => 'initialize',
36+
], $request->toArray());
37+
self::assertSame('{"jsonrpc":"2.0","id":"123","method":"initialize"}', json_encode($request));
38+
}
39+
40+
public function testRequestWithParamsCanBeJsonSerialized(): void
41+
{
42+
$request = new JsonRpcRequest(
43+
RequestId::fromId('123'),
44+
'2.0',
45+
'initialize',
46+
['param1' => 'value1', 'param2' => 'value2']
47+
);
48+
self::assertSame([
49+
'jsonrpc' => '2.0',
50+
'id' => '123',
51+
'method' => 'initialize',
52+
'params' => ['param1' => 'value1', 'param2' => 'value2'],
53+
], $request->toArray());
54+
self::assertSame(
55+
'{"jsonrpc":"2.0","id":"123","method":"initialize","params":{"param1":"value1","param2":"value2"}}',
56+
json_encode($request)
57+
);
58+
}
59+
}

0 commit comments

Comments
 (0)