Skip to content

Fix: Correctly parse JSON-RPC batch requests#47

Merged
james2037 merged 1 commit into
masterfrom
fix/json-batch-parsing
Jun 5, 2025
Merged

Fix: Correctly parse JSON-RPC batch requests#47
james2037 merged 1 commit into
masterfrom
fix/json-batch-parsing

Conversation

@james2037

Copy link
Copy Markdown
Owner

The previous implementation incorrectly handled JSON-RPC batch requests, leading to parsing errors when an array of requests was received.

This commit addresses the issue by:

  1. Modifying StdioTransport::receive() to ensure that when a batch request (an array of JSON objects) is detected, it's correctly identified. The raw JSON string is then passed to JsonRpcMessage.

  2. Modifying JsonRpcMessage::fromJsonArray() to first decode the incoming JSON string into an array of stdClass objects. It then iterates through this array, calling a new method fromJsonObject() for each item. This avoids inefficient re-encoding/decoding.

  3. Introducing a new public static method JsonRpcMessage::fromJsonObject(). This method takes a stdClass object (a single decoded JSON-RPC message) and performs the same validation and object construction logic as the original fromJson() method, but operates directly on the object's properties. This includes casting nested structures like params, result, and error to arrays where appropriate to maintain type consistency.

  4. Adding comprehensive unit tests for the modified StdioTransport::receive() and for the new and modified methods in JsonRpcMessage. These tests cover various scenarios, including valid and invalid batch requests, requests, notifications, and responses.

These changes ensure that the server can now correctly parse and process JSON-RPC batch requests according to the specification, resolving the "Expected object, received array" ZodError. All linters and unit tests pass with these changes.

The previous implementation incorrectly handled JSON-RPC batch requests,
leading to parsing errors when an array of requests was received.

This commit addresses the issue by:

1. Modifying `StdioTransport::receive()` to ensure that when a batch
   request (an array of JSON objects) is detected, it's correctly
   identified. The raw JSON string is then passed to `JsonRpcMessage`.

2. Modifying `JsonRpcMessage::fromJsonArray()` to first decode the
   incoming JSON string into an array of `stdClass` objects. It then
   iterates through this array, calling a new method `fromJsonObject()`
   for each item. This avoids inefficient re-encoding/decoding.

3. Introducing a new public static method `JsonRpcMessage::fromJsonObject()`.
   This method takes a `stdClass` object (a single decoded JSON-RPC message)
   and performs the same validation and object construction logic as the
   original `fromJson()` method, but operates directly on the object's
   properties. This includes casting nested structures like `params`,
   `result`, and `error` to arrays where appropriate to maintain type
   consistency.

4. Adding comprehensive unit tests for the modified `StdioTransport::receive()`
   and for the new and modified methods in `JsonRpcMessage`. These tests
   cover various scenarios, including valid and invalid batch requests,
   requests, notifications, and responses.

These changes ensure that the server can now correctly parse and process
JSON-RPC batch requests according to the specification, resolving the
"Expected object, received array" ZodError. All linters and unit tests
pass with these changes.
@github-actions

github-actions Bot commented Jun 5, 2025

Copy link
Copy Markdown

🧪 Test Results Summary

PHPUnit Tests

✅ All tests passed!

Details: OK (260 tests, 1037 assertions)

  • Total Tests: 260
  • Total Assertions: 1037

Code Quality

  • PHPCS: ✅ No issues
  • PHPStan: ✅ No errors found

📁 Detailed Reports

  • Coverage Report: Download the test-outputs artifact for detailed HTML coverage report
  • Test Results: Check the "Tests" tab above for detailed test results
  • Raw Logs: View the "Actions" tab for complete output logs

This comment will update automatically when you push new commits.

Code Coverage

Code Coverage

Package Line Rate Health
Capability/CapabilityInterface.php 0%
Capability/ResourcesCapability.php 100%
Capability/ToolsCapability.php 100%
Exception/InvalidParamsException.php 0%
Exception/InvalidRequestException.php 0%
Exception/MethodNotSupportedException.php 0%
Exception/TransportException.php 0%
Message/JsonRpcMessage.php 99%
Registry/Registry.php 97%
Resource/Attribute/ResourceUri.php 100%
Resource/BlobResourceContents.php 100%
Resource/Resource.php 100%
Resource/ResourceContents.php 100%
Resource/ResourceRegistry.php 100%
Resource/TextResourceContents.php 100%
Server.php 69%
Tool/Attribute/Parameter.php 100%
Tool/Attribute/Tool.php 100%
Tool/Attribute/ToolAnnotations.php 100%
Tool/Content/AbstractContent.php 100%
Tool/Content/AbstractMediaContent.php 100%
Tool/Content/Annotations.php 100%
Tool/Content/AudioContent.php 100%
Tool/Content/ContentItemInterface.php 0%
Tool/Content/EmbeddedResource.php 100%
Tool/Content/ImageContent.php 100%
Tool/Content/TextContent.php 100%
Tool/Tool.php 100%
Tool/ToolRegistry.php 100%
Transport/HttpTransport.php 100%
Transport/StdioTransport.php 88%
Transport/TransportInterface.php 0%
Summary 91% (901 / 992)

@codecov

codecov Bot commented Jun 5, 2025

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 88.33333% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Transport/StdioTransport.php 73.91% 6 Missing ⚠️
src/Message/JsonRpcMessage.php 97.29% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@james2037 james2037 merged commit 28f11fe into master Jun 5, 2025
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant