PluginProbe ʕ •ᴥ•ʔ
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). / trunk
CommerceBird – AI Command Center, ERP Integrations & B2B for WooCommerce (Zoho, Exact Online). vtrunk
3.0.3 3.0.2 3.0.1 trunk 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.3.0 2.3.1 2.3.10 2.3.11 2.3.12 2.3.13 2.3.14 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.7.91 2.7.92 2.7.93 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0
commercebird / vendor / wordpress / mcp-adapter / includes / Transport / Infrastructure / JsonRpcResponseBuilder.php
commercebird / vendor / wordpress / mcp-adapter / includes / Transport / Infrastructure Last commit date
HttpRequestContext.php 2 months ago HttpRequestHandler.php 2 months ago HttpSessionValidator.php 2 months ago JsonRpcResponseBuilder.php 2 months ago McpTransportContext.php 2 months ago McpTransportHelperTrait.php 2 months ago RequestRouter.php 2 months ago SessionManager.php 2 months ago
JsonRpcResponseBuilder.php
111 lines
1 <?php
2 /**
3 * JSON-RPC Response Builder for MCP Transport
4 *
5 * @package McpAdapter
6 */
7
8 declare( strict_types=1 );
9
10 namespace WP\MCP\Transport\Infrastructure;
11
12 use WP\McpSchema\Common\McpConstants;
13
14 /**
15 * Builds standardized JSON-RPC 2.0 responses for MCP transport.
16 *
17 * Centralizes response creation logic to eliminate duplication and ensure
18 * consistent response formatting across all MCP transport implementations.
19 */
20 class JsonRpcResponseBuilder {
21
22 /**
23 * Create a JSON-RPC 2.0 success response.
24 *
25 * @param mixed $request_id The request ID from the original JSON-RPC request (string, number, or null).
26 * @param mixed $result The result data to return.
27 *
28 * @return array The formatted JSON-RPC response.
29 */
30 public static function create_success_response( $request_id, $result ): array {
31 return array(
32 'jsonrpc' => McpConstants::JSONRPC_VERSION,
33 'id' => $request_id,
34 // Make sure the result is an object (not an array)
35 'result' => (object) $result,
36 );
37 }
38
39 /**
40 * Create a JSON-RPC 2.0 error response.
41 *
42 * @param mixed $request_id The request ID from the original JSON-RPC request (string, number, or null).
43 * @param array $error The error array with 'code', 'message', and optional 'data'.
44 *
45 * @return array The formatted JSON-RPC error response.
46 */
47 public static function create_error_response( $request_id, array $error ): array {
48 return array(
49 'jsonrpc' => McpConstants::JSONRPC_VERSION,
50 'id' => $request_id,
51 'error' => $error,
52 );
53 }
54
55 /**
56 * Process multiple MCP messages and format the response correctly.
57 *
58 * Handles both batch requests (array of messages) and single requests,
59 * returning the appropriate response format per JSON-RPC 2.0 specification.
60 *
61 * @param array $messages Array of JSON-RPC messages to process.
62 * @param bool $is_batch_request Whether the original request was a batch.
63 * @param callable $processor Callback function to process each individual message.
64 * Should accept (array $message) and return array $response.
65 *
66 * @return array|null The formatted response (array for batch, single response for non-batch).
67 */
68 public static function process_messages( array $messages, bool $is_batch_request, callable $processor ): ?array {
69 $results = array();
70
71 foreach ( $messages as $message ) {
72 $response = call_user_func( $processor, $message );
73 if ( null === $response ) {
74 continue;
75 }
76
77 $results[] = $response;
78 }
79
80 // Return response format based on original request format (JSON-RPC 2.0 spec)
81 // If the request was a batch, response MUST be an array, even if only one result
82 return $is_batch_request ? $results : ( $results[0] ?? null );
83 }
84
85 /**
86 * Normalize request body to an array of messages.
87 *
88 * Converts single messages to an array for uniform processing.
89 *
90 * @param mixed $body The decoded request body.
91 *
92 * @return array Array of messages for processing.
93 */
94 public static function normalize_messages( $body ): array {
95 return self::is_batch_request( $body ) ? $body : array( $body );
96 }
97
98 /**
99 * Determine if a request body represents a batch request.
100 *
101 * Per JSON-RPC 2.0 specification, a batch request is an array with at least one element.
102 *
103 * @param mixed $body The decoded request body.
104 *
105 * @return bool True if this is a batch request.
106 */
107 public static function is_batch_request( $body ): bool {
108 return is_array( $body ) && isset( $body[0] );
109 }
110 }
111