self::VERSION, 'id' => $id, 'result' => $result, ]; } /** * @param mixed $id * @param int $code * @param string $message * @param mixed $data * @return array */ public static function error( $id, int $code, string $message, $data = null ): array { $error = [ 'code' => $code, 'message' => $message, ]; if ( null !== $data ) { $error['data'] = $data; } return [ 'jsonrpc' => self::VERSION, 'id' => $id, 'error' => $error, ]; } /** * A capability that failed INSIDE its own logic is still a successful * exchange — the agent gets a readable message it can act on, rather than a * protocol error implying it called the server wrongly (FR-005). * * @param mixed $id * @param string $message * @return array */ public static function tool_error( $id, string $message ): array { return self::result( $id, [ 'content' => [ [ 'type' => 'text', 'text' => $message, ], ], 'isError' => true, ] ); } /** * @param mixed $id * @param mixed $payload * @return array */ public static function tool_result( $id, $payload ): array { return self::result( $id, [ 'content' => [ [ 'type' => 'text', 'text' => wp_json_encode( $payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ), ], ], 'isError' => false, ] ); } /** * A message with no `id` is a notification: no reply is ever sent (FR-002). * * @param array $message * @return bool */ public static function is_notification( array $message ): bool { return ! array_key_exists( 'id', $message ); } /** * A structurally valid single request object. * * @param mixed $message * @return bool */ public static function is_valid_request( $message ): bool { return is_array( $message ) && ! empty( $message['method'] ) && is_string( $message['method'] ); } }