array( 'product', 'create' ), 'wc_update_product' => array( 'product', 'update', 'id' ), 'wc_create_variation' => array( 'product_variation', 'create' ), 'wc_update_variation' => array( 'product_variation', 'update', 'variation_id' ), 'wc_delete_variation' => array( 'product_variation', 'delete', 'variation_id', 'force' ), 'wc_create_coupon' => array( 'coupon', 'create' ), 'wc_update_coupon' => array( 'coupon', 'update', 'id' ), 'wc_delete_coupon' => array( 'coupon', 'delete', 'id', 'force' ), 'wc_create_order' => array( 'order', 'create' ), 'wc_update_order' => array( 'order', 'update', 'order_id' ), 'wc_update_order_status' => array( 'order', 'update', 'id' ), 'wc_add_order_note' => array( 'order_note', 'create', 'order_id', 'customer_note' ), ); /** * Store and content reads; its option, meta, user and file reads are * not listed. */ private const READS = array( 'wc_get_attribute_terms', 'wc_get_coupon', 'wc_get_coupon_count', 'wc_get_coupons', 'wc_get_customers', 'wc_get_order', 'wc_get_orders', 'wc_get_product', 'wc_get_product_attributes', 'wc_get_products', 'wc_get_product_variations', 'wc_get_store_stats', 'wc_get_variation', 'wp_get_post', 'wp_get_posts', 'wp_get_post_terms', 'wp_get_post_types', 'wp_get_site_info', ); /** * {@inheritDoc} */ public function id(): string { return 'royal-mcp'; } /** * {@inheritDoc} */ public function name(): string { return 'Royal MCP'; } /** * {@inheritDoc} */ public function plugin_file(): string { return 'royal-mcp/royal-mcp.php'; } /** * {@inheritDoc} */ public function tested(): array { return array( '1.5.10', '1.6.0' ); } /** * {@inheritDoc} * * @param string $route The route. */ public function serves( string $route ): bool { return 1 === preg_match( '#^/royal-mcp/v1(/mcp|/messages)?/?$#i', $route ); } /** * {@inheritDoc} * * @param WP_REST_Request $request The request. */ public function presents_credential( WP_REST_Request $request ): bool { return null !== self::bearer( $request ) || '' !== (string) $request->get_header( 'X-Royal-MCP-API-Key' ); } /** * {@inheritDoc} * * @param WP_REST_Request $request The request. */ public function principal( WP_REST_Request $request ): ?Principal { $settings = get_option( 'royal_mcp_settings', array() ); if ( ! is_array( $settings ) || empty( $settings['enabled'] ) ) { return null; } $bearer = self::bearer( $request ); if ( null !== $bearer && class_exists( \Royal_MCP\OAuth\Token_Store::class ) ) { $token = \Royal_MCP\OAuth\Token_Store::validate_token( $bearer ); if ( is_array( $token ) && '' !== (string) ( $token['client_id'] ?? '' ) ) { return new Principal( $this->id() . ':oauth:' . $token['client_id'], (string) $token['client_id'], (int) ( $token['user_id'] ?? 0 ) ); } } $key = $bearer ?? (string) $request->get_header( 'X-Royal-MCP-API-Key' ); if ( '' === $key || ! self::key_matches( $key, $settings ) ) { return null; } // Royal keeps a signed-in user, else runs the key as its saved user. $user = is_user_logged_in() ? get_current_user_id() : (int) ( $settings['api_key_user_id'] ?? 0 ); return $user > 0 ? new Principal( $this->id() . ':api-key', __( 'Site API key', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), $user ) : null; } /** * {@inheritDoc} */ public function principals(): array { global $wpdb; $found = array(); $settings = get_option( 'royal_mcp_settings', array() ); if ( is_array( $settings ) && ( ! empty( $settings['api_key_hash'] ) || ! empty( $settings['api_key'] ) ) ) { $user = (int) ( $settings['api_key_user_id'] ?? 0 ); $found[] = array( 'id' => $this->id() . ':api-key', 'label' => __( 'Site API key', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), 'runs_as_admin' => $user > 0 ? user_can( $user, 'manage_options' ) : null, ); } $table = $wpdb->prefix . 'royal_mcp_oauth_clients'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Royal MCP's own table, read for the binding screen. if ( $table === $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- The plugin's own table, read for the binding screen. foreach ( (array) $wpdb->get_results( $wpdb->prepare( 'SELECT client_id, client_name FROM %i LIMIT 200', $table ) ) as $client ) { $found[] = array( 'id' => $this->id() . ':oauth:' . $client->client_id, /* translators: %s: OAuth client name. */ 'label' => sprintf( __( 'Connected app "%s"', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), (string) $client->client_name ), // Whoever authorized it: an administrator or not. 'runs_as_admin' => null, ); } } return $found; } /** * {@inheritDoc} * * @param string $tool Tool name. * @param array $arguments Arguments. */ public function classify( string $tool, array $arguments ): ?Classification { return ToolMap::classify( self::TOOLS, $tool, $arguments ) ?? ToolMap::read( self::READS, $tool ); } /** * The Bearer credential, or null. * * @param WP_REST_Request $request The request. */ private static function bearer( WP_REST_Request $request ): ?string { $header = (string) $request->get_header( 'Authorization' ); return 0 === stripos( $header, 'Bearer ' ) ? substr( $header, 7 ) : null; } /** * Whether a key is the site's, compared as Royal compares it. * * @param string $key The key. * @param array $settings Royal's settings. */ private static function key_matches( string $key, array $settings ): bool { $hash = (string) ( $settings['api_key_hash'] ?? '' ); $plain = (string) ( $settings['api_key'] ?? '' ); if ( '' !== $hash ) { return hash_equals( $hash, hash( 'sha256', $key ) ); } return '' !== $plain && hash_equals( $plain, $key ); } }