query_builder = $query_builder; } /** * Execute a tool configuration. * * Iterates through the tool's blocks, executes each one. * If a block has joins configured, delegates to the query builder. * * @since 1.0.0 * * @param array $tool_config The full tool configuration from CPT meta. * @param array $arguments The LLM-provided input arguments. * @return array MCP content response array. */ public function execute( array $tool_config, array $arguments ): array { $blocks = $tool_config['blocks'] ?? array(); $results = array(); $selected_blocks = $arguments['_blocks'] ?? null; foreach ( $blocks as $index => $block_entry ) { $block_type = $block_entry['block_type'] ?? ''; $block = Wlconduit_Block_Registry::get( $block_type ); if ( ! $block ) { continue; } $block_config = $block_entry['config'] ?? array(); // Build a unique result key using post_type if available, otherwise block_type + index. $result_key = $block_config['post_type'] ?? $block_type; if ( isset( $results[ $result_key ] ) ) { $result_key = $result_key . '_' . $index; } // If _blocks is specified, skip blocks not in the list. if ( is_array( $selected_blocks ) && ! in_array( $result_key, $selected_blocks, true ) ) { continue; } // If joins are configured, use the query builder. if ( ! empty( $block_config['joins'] ) ) { $results[ $result_key ] = $this->query_builder->execute( $block_entry, $arguments ); } else { $results[ $result_key ] = $block->execute( $block_config, $arguments ); } } return array( 'content' => array( array( 'type' => 'text', 'text' => wp_json_encode( $results ), ), ), ); } }