Skip to content

Commit cd10995

Browse files
committed
Adopt Schema component in server handlers
1 parent 477cdb1 commit cd10995

36 files changed

+177
-602
lines changed

src/Message/Error.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

src/Message/Factory.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Mcp\Message;
1313

1414
use Mcp\Exception\InvalidInputMessageException;
15+
use Mcp\Schema\JsonRpc\Notification;
16+
use Mcp\Schema\JsonRpc\Request;
1517

1618
/**
1719
* @author Christopher Hertel <[email protected]>
@@ -35,9 +37,9 @@ public function create(string $input): iterable
3537
if (!isset($message['method'])) {
3638
yield new InvalidInputMessageException('Invalid JSON-RPC request, missing "method".');
3739
} elseif (str_starts_with((string) $message['method'], 'notifications/')) {
38-
yield Notification::from($message);
40+
yield Notification::fromArray($message);
3941
} else {
40-
yield Request::from($message);
42+
yield Request::fromArray($message);
4143
}
4244
}
4345
}

src/Message/Notification.php

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/Message/Request.php

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/Message/Response.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/Schema/Constants.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,8 @@
1414
/**
1515
* @author Kyrian Obikwelu <[email protected]>
1616
*/
17-
final class Constants
17+
interface Constants
1818
{
1919
public const LATEST_PROTOCOL_VERSION = '2025-03-26';
2020
public const JSONRPC_VERSION = '2.0';
21-
22-
public const PARSE_ERROR = -32700;
23-
public const INVALID_REQUEST = -32600;
24-
public const METHOD_NOT_FOUND = -32601;
25-
public const INVALID_PARAMS = -32602;
26-
public const INTERNAL_ERROR = -32603;
27-
28-
public const SERVER_ERROR = -32000;
29-
30-
private function __construct()
31-
{
32-
}
3321
}

src/Schema/JsonRpc/BatchRequest.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,29 @@
1111

1212
namespace Mcp\Schema\JsonRpc;
1313

14+
use Mcp\Exception\InvalidArgumentException;
15+
1416
/**
1517
* A JSON-RPC batch request, as described in https://www.jsonrpc.org/specification#batch.
1618
*
1719
* @author Kyrian Obikwelu <[email protected]>
1820
*/
19-
class BatchRequest extends Message implements \Countable
21+
readonly class BatchRequest implements MessageInterface, \Countable
2022
{
2123
/**
2224
* Create a new JSON-RPC 2.0 batch of requests/notifications.
2325
*/
24-
public function __construct(public readonly array $items)
25-
{
26+
public function __construct(
27+
public array $items,
28+
) {
2629
foreach ($items as $item) {
2730
if (!($item instanceof Request || $item instanceof Notification)) {
28-
throw new \InvalidArgumentException('All items in BatchRequest must be instances of Request or Notification.');
31+
throw new InvalidArgumentException('All items in BatchRequest must be instances of Request or Notification.');
2932
}
3033
}
3134
}
3235

33-
public function getId(): string|int|null
36+
public function getId(): string|int
3437
{
3538
foreach ($this->items as $item) {
3639
if ($item instanceof Request) {
@@ -41,41 +44,28 @@ public function getId(): string|int|null
4144
return '';
4245
}
4346

44-
/**
45-
* Create a new JSON-RPC 2.0 batch of requests/notifications.
46-
*/
47-
public static function make(array $items): static
48-
{
49-
return new static($items);
50-
}
51-
52-
public function toArray(): array
53-
{
54-
return array_map(fn ($item) => $item->toArray(), $this->items);
55-
}
56-
5747
public function jsonSerialize(): array
5848
{
59-
return $this->toArray();
49+
return $this->items;
6050
}
6151

6252
public static function fromArray(array $data): static
6353
{
6454
if (empty($data)) {
65-
throw new \InvalidArgumentException('BatchRequest data array must not be empty.');
55+
throw new InvalidArgumentException('BatchRequest data array must not be empty.');
6656
}
6757

6858
$items = [];
6959
foreach ($data as $itemData) {
7060
if (!\is_array($itemData)) {
71-
throw new \InvalidArgumentException('BatchRequest item data must be an array.');
61+
throw new InvalidArgumentException('BatchRequest item data must be an array.');
7262
}
7363
if (isset($itemData['id']) && null !== $itemData['id']) {
7464
$items[] = Request::fromArray($itemData);
7565
} elseif (isset($itemData['method'])) {
7666
$items[] = Notification::fromArray($itemData);
7767
} else {
78-
throw new \InvalidArgumentException("Invalid item in BatchRequest data: missing 'method' or 'id'.");
68+
throw new InvalidArgumentException("Invalid item in BatchRequest data: missing 'method' or 'id'.");
7969
}
8070
}
8171

0 commit comments

Comments
 (0)