1111
1212namespace 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