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. Only Posts blocks key by post_type (so // distinct post types in the same tool get distinct keys); other block // types key by block_type, ignoring any phantom post_type from legacy // UI-saved configs. $result_key = ( 'posts' === $block_type && ! empty( $block_config['post_type'] ) ) ? $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; } // Extract arguments scoped to this block by stripping the result_key prefix. $prefix = $result_key . '_'; $prefix_len = strlen( $prefix ); $block_args = array(); foreach ( $arguments as $key => $value ) { if ( str_starts_with( $key, $prefix ) ) { $block_args[ substr( $key, $prefix_len ) ] = $value; } } // If joins are configured, use the query builder. if ( ! empty( $block_config['joins'] ) ) { $results[ $result_key ] = $this->query_builder->execute( $block_entry, $block_args ); } else { $results[ $result_key ] = $block->execute( $block_config, $block_args ); } } return array( 'content' => array( array( 'type' => 'text', 'text' => wp_json_encode( $results ), ), ), ); } }