tools = $tools; } public static function error_response( $id, $code, $message ) { return array( 'jsonrpc' => '2.0', 'id' => $id, 'error' => array( 'code' => $code, 'message' => $message, ), ); } /** * @param mixed $message One decoded JSON-RPC message. * @return array|null */ public function handle( $message ) { if ( ! is_array( $message ) || ! isset( $message['method'] ) || ! is_string( $message['method'] ) || ( isset( $message['jsonrpc'] ) && '2.0' !== $message['jsonrpc'] ) ) { $id = is_array( $message ) && isset( $message['id'] ) && ( is_string( $message['id'] ) || is_int( $message['id'] ) ) ? $message['id'] : null; return self::error_response( $id, self::INVALID_REQUEST, 'Invalid Request' ); } $is_notification = ! array_key_exists( 'id', $message ); if ( ! $is_notification && ! is_string( $message['id'] ) && ! is_int( $message['id'] ) ) { // MCP: a request id is a string or an integer, never null. return self::error_response( null, self::INVALID_REQUEST, 'Invalid Request: id must be a string or integer' ); } $id = $is_notification ? null : $message['id']; $params = isset( $message['params'] ) && is_array( $message['params'] ) ? $message['params'] : array(); if ( $is_notification ) { // notifications/initialized, notifications/cancelled, … — nothing to do, nothing to answer. return null; } switch ( $message['method'] ) { case 'initialize': return $this->result( $id, $this->initialize( $params ) ); case 'ping': return $this->result( $id, new \stdClass() ); case 'tools/list': return $this->result( $id, array( 'tools' => $this->tools->list_for_mcp() ) ); case 'tools/call': return $this->call_tool( $id, $params ); } return self::error_response( $id, self::METHOD_NOT_FOUND, 'Method not found: ' . $message['method'] ); } private function initialize( array $params ) { $asked = isset( $params['protocolVersion'] ) && is_string( $params['protocolVersion'] ) ? $params['protocolVersion'] : ''; $version = in_array( $asked, self::PROTOCOL_VERSIONS, true ) ? $asked : self::PROTOCOL_VERSIONS[0]; return array( 'protocolVersion' => $version, 'capabilities' => array( 'tools' => array( 'listChanged' => false ), ), 'serverInfo' => array( 'name' => 'adi-site-bridge', 'version' => ADIBRIDGE_VERSION, ), 'instructions' => 'Content tools for the WordPress site ' . home_url( '/' ) . '.' . ( $this->tools->has( 'wp_get_site_overview' ) ? ' Call wp_get_site_overview first.' : '' ) . Read_Tools::SITE_DATA . ' The bridge\'s own messages (its errors, hints and notes about how to use the tools) are not site content.', ); } private function call_tool( $id, array $params ) { $name = isset( $params['name'] ) ? $params['name'] : null; if ( ! is_string( $name ) || ! $this->tools->has( $name ) ) { return self::error_response( $id, self::INVALID_PARAMS, 'Unknown tool: ' . ( is_string( $name ) ? $name : '(none)' ) ); } $tool = $this->tools->get( $name ); $args = array_key_exists( 'arguments', $params ) ? $params['arguments'] : array(); if ( null === $args ) { $args = array(); } $error = Tools::validate( $args, $tool['inputSchema'] ); if ( null !== $error ) { return self::error_response( $id, self::INVALID_PARAMS, "Invalid arguments for $name: $error" ); } try { $value = call_user_func( $tool['handler'], $args ); return $this->result( $id, self::content( $value, false ) ); } catch ( Tool_Error $e ) { /** A refused or partly applied change may still have queued cache purges (put-backs, a half restore): run them now, while response headers (LiteSpeed's purge header) can still be sent. */ do_action( 'adibridge_tool_failed', $name ); return $this->result( $id, self::content( Tool_Error::text( $e ), true ) ); } catch ( \Throwable $e ) { do_action( 'adibridge_tool_failed', $name ); // Never leak a stack trace or a path to the model; the site's error log gets the detail. Log::line( 'tool ' . $name . ' failed: ' . Tool_Error::text( $e ) . ' @ ' . $e->getFile() . ':' . $e->getLine() ); return $this->result( $id, self::content( "The tool $name failed unexpectedly. Nothing further was changed; " . Log::where() . '.', true ) ); } } private static function content( $value, $is_error ) { $text = is_string( $value ) ? $value : wp_json_encode( $value, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); $out = array( 'content' => array( array( 'type' => 'text', 'text' => (string) $text, ), ), 'isError' => $is_error, ); if ( ! $is_error && is_array( $value ) && array() !== $value && array_keys( $value ) !== range( 0, count( $value ) - 1 ) ) { $out['structuredContent'] = $value; } return $out; } private function result( $id, $result ) { return array( 'jsonrpc' => '2.0', 'id' => $id, 'result' => $result, ); } }