芝麻web文件管理V1.00
编辑当前文件:/home/ezdajrnh/public_html/wp-content/plugins/woocommerce/src/Internal/MCP/MCPAdapterProvider.php
initialized ) { return; } $this->initialize_mcp_adapter(); $this->register_hooks(); $this->initialized = true; } /** * Initialize the MCP adapter. */ private function initialize_mcp_adapter(): void { // Check if MCP adapter class exists (should be autoloaded by WooCommerce's composer). if ( ! class_exists( 'WP\MCP\Core\McpAdapter' ) ) { if ( function_exists( 'wc_get_logger' ) ) { wc_get_logger()->warning( 'MCP adapter class not found. Skipping MCP initialization.', array( 'source' => 'woocommerce-mcp' ) ); } return; } // Initialize the MCP adapter instance - this triggers the rest_api_init hook registration. \WP\MCP\Core\McpAdapter::instance(); } /** * Register WordPress hooks for MCP adapter. */ private function register_hooks(): void { // Initialize MCP server when MCP adapter is ready. add_action( 'mcp_adapter_init', array( $this, 'initialize_mcp_server' ) ); } /** * Initialize MCP server. * * @param object $adapter MCP adapter instance. */ public function initialize_mcp_server( $adapter ): void { // Get filtered abilities for MCP server. $abilities_ids = $this->get_woocommerce_mcp_abilities(); // Bail if no abilities are available. if ( empty( $abilities_ids ) ) { return; } /* * Temporarily disable MCP validation during server creation. * Workaround for validator bug with union types (e.g., ["integer", "null"]). * This will be removed once the mcp-adapter validator bug is fixed. * * @see https://github.com/WordPress/mcp-adapter/issues/47 */ add_filter( 'mcp_validation_enabled', array( __CLASS__, 'disable_mcp_validation' ), 999 ); try { // Create MCP server. $adapter->create_server( 'woocommerce-mcp', self::MCP_NAMESPACE, self::MCP_ROUTE, __( 'WooCommerce MCP Server', 'woocommerce' ), __( 'AI-accessible WooCommerce operations via MCP', 'woocommerce' ), '1.0.0', array( WooCommerceRestTransport::class ), \WP\MCP\Infrastructure\ErrorHandling\ErrorLogMcpErrorHandler::class, \WP\MCP\Infrastructure\Observability\NullMcpObservabilityHandler::class, $abilities_ids, ); } catch ( \Throwable $e ) { if ( function_exists( 'wc_get_logger' ) ) { wc_get_logger()->error( 'MCP server initialization failed: ' . $e->getMessage(), array( 'source' => 'woocommerce-mcp' ) ); } } finally { // Re-enable MCP validation immediately after server creation. remove_filter( 'mcp_validation_enabled', array( __CLASS__, 'disable_mcp_validation' ), 999 ); } } /** * Get WooCommerce abilities for MCP server. * * Filters abilities to include only those with 'woocommerce/' namespace by default, * with a filter to allow inclusion of abilities from other namespaces. * * @return array Array of ability IDs for MCP server. */ private function get_woocommerce_mcp_abilities(): array { // Get all abilities from the registry. $abilities_registry = wc_get_container()->get( AbilitiesRegistry::class ); $all_abilities_ids = $abilities_registry->get_abilities_ids(); // Filter abilities based on namespace and custom filter. $mcp_abilities = array_filter( $all_abilities_ids, function ( $ability_id ) { // Include WooCommerce abilities by default. $include = str_starts_with( $ability_id, 'woocommerce/' ); // Allow filter to override inclusion decision. /** * Filter to override MCP ability inclusion decision. * * @since 10.3.0 * @param bool $include Whether to include the ability. * @param string $ability_id The ability ID. */ return apply_filters( 'woocommerce_mcp_include_ability', $include, $ability_id ); } ); // Re-index array. return array_values( $mcp_abilities ); } /** * Temporarily disable MCP validation. * * Used as a callback for the mcp_validation_enabled filter to work around * validator bugs with union types. * * @return bool Always returns false to disable validation. */ public static function disable_mcp_validation(): bool { return false; } /** * Check if MCP adapter is initialized. * * @return bool Whether MCP adapter is initialized. */ public function is_initialized(): bool { return $this->initialized; } /** * Check if the current request is for the MCP endpoint. * * @return bool True if this is an MCP endpoint request. */ public static function is_mcp_request(): bool { // Check if this is a REST request. if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) { return false; } // Get the request URI. $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; // Build the MCP endpoint path dynamically from constants. $mcp_endpoint = '/' . self::MCP_NAMESPACE . '/' . self::MCP_ROUTE; // Check if the request is for the MCP endpoint. return false !== strpos( $request_uri, $mcp_endpoint ); } }