array( 'refund', 'create', 'order_id' ), 'wp_woo_create_product' => array( 'product', 'create' ), 'wp_woo_update_product' => array( 'product', 'update', 'product_id' ), 'wp_woo_delete_product' => array( 'product', 'delete', 'product_id', 'force' ), 'wp_woo_create_variation' => array( 'product_variation', 'create' ), 'wp_woo_update_variation' => array( 'product_variation', 'update', 'variation_id' ), 'wp_woo_delete_variation' => array( 'product_variation', 'delete', 'variation_id', 'force' ), 'wp_woo_create_coupon' => array( 'coupon', 'create' ), 'wp_woo_update_coupon' => array( 'coupon', 'update', 'coupon_id' ), 'wp_woo_delete_coupon' => array( 'coupon', 'delete', 'coupon_id', 'force' ), 'wp_woo_create_order' => array( 'order', 'create' ), 'wp_woo_update_order' => array( 'order', 'update', 'order_id' ), 'wp_woo_delete_order' => array( 'order', 'delete', 'order_id', 'force' ), 'wp_woo_add_order_note' => array( 'order_note', 'create', 'order_id', 'is_customer' ), 'wp_woo_create_customer' => array( 'customer', 'create' ), 'wp_woo_update_customer' => array( 'customer', 'update', 'customer_id' ), 'wp_woo_create_tax_rate' => array( 'tax', 'create' ), 'wp_woo_update_setting' => array( 'wc_setting', 'update' ), ); /** * Store and content reads (Cowboy marks each read-only); its settings, * gateway, file, log and database reads are not listed. */ private const READS = array( 'wp_woo_get_coupon', 'wp_woo_get_customer', 'wp_woo_get_customer_orders', 'wp_woo_get_order', 'wp_woo_get_product', 'wp_woo_get_tax_rates', 'wp_woo_list_coupons', 'wp_woo_list_customers', 'wp_woo_list_order_notes', 'wp_woo_list_orders', 'wp_woo_list_products', 'wp_woo_list_shipping_zones', 'wp_woo_list_variations', 'wp_woo_report_customer_stats', 'wp_woo_report_orders_by_status', 'wp_woo_report_revenue_by_date', 'wp_woo_report_sales', 'wp_woo_report_top_products', 'wp_get_post', 'wp_list_posts', 'wp_list_media', 'wp_list_terms', 'wp_list_comments', 'wp_list_menus', 'wp_list_menu_items', 'wp_site_info', ); /** * {@inheritDoc} */ public function id(): string { return 'cowboy-mcp'; } /** * {@inheritDoc} */ public function name(): string { return 'Cowboy MCP'; } /** * {@inheritDoc} */ public function plugin_file(): string { return 'cowboy-mcp/cowboy-mcp.php'; } /** * {@inheritDoc} */ public function tested(): array { return array( '1.7.4', '1.8.0' ); } /** * {@inheritDoc} * * @param string $route The route. */ public function serves( string $route ): bool { return 1 === preg_match( '#^/cowboy-mcp/v1/endpoint/?$#i', $route ); } /** * {@inheritDoc} * * @param WP_REST_Request $request The request. */ public function presents_credential( WP_REST_Request $request ): bool { return str_starts_with( (string) $request->get_header( 'Authorization' ), 'Bearer ' ); } /** * {@inheritDoc} * * @param WP_REST_Request $request The request. */ public function principal( WP_REST_Request $request ): ?Principal { if ( ! class_exists( \Cowboy_MCP_Auth::class ) || true !== \Cowboy_MCP_Auth::validate_request( $request ) ) { return null; } $context = \Cowboy_MCP_Auth::$current_key_context; $key = (string) ( $context['key_id'] ?? '' ); $label = (string) ( $context['key_label'] ?? '' ); if ( '' === $key ) { return null; } if ( str_starts_with( $key, 'oauth_' ) ) { $tokens = get_option( 'cowboy_mcp_oauth_tokens', array() ); $client = is_array( $tokens ) ? (string) ( $tokens[ substr( $key, 6 ) ]['client_id'] ?? '' ) : ''; return '' === $client ? null : new Principal( $this->id() . ':oauth:' . $client, $label, get_current_user_id() ); } return new Principal( $this->id() . ':key:' . $key, $label, get_current_user_id() ); } /** * {@inheritDoc} */ public function principals(): array { $found = array(); $keys = get_option( 'cowboy_mcp_api_keys', array() ); foreach ( is_array( $keys ) ? $keys : array() as $id => $key ) { $found[] = array( 'id' => $this->id() . ':key:' . $id, /* translators: 1: key label, 2: the key's visible prefix. */ 'label' => sprintf( __( 'API key "%1$s" (%2$s…)', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), (string) ( $key['label'] ?? '' ), (string) ( $key['prefix'] ?? '' ) ), // Cowboy runs every API key as the site's first administrator. 'runs_as_admin' => true, ); } $clients = get_option( 'cowboy_mcp_oauth_clients', array() ); foreach ( is_array( $clients ) ? $clients : array() as $id => $client ) { $found[] = array( 'id' => $this->id() . ':oauth:' . $id, /* translators: %s: OAuth client name. */ 'label' => sprintf( __( 'Connected app "%s"', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), (string) ( $client['client_name'] ?? $id ) ), // Cowboy lets only administrators authorize an app. 'runs_as_admin' => true, ); } return $found; } /** * {@inheritDoc} * * @param string $tool Tool name. * @param array $arguments Arguments. */ public function classify( string $tool, array $arguments ): ?Classification { // The gateway meta-tool runs another tool by name. if ( 'cowboy_run' === $tool && is_string( $arguments['tool'] ?? null ) ) { $inner = is_array( $arguments['arguments'] ?? null ) ? $arguments['arguments'] : array(); return ToolMap::classify( self::TOOLS, $arguments['tool'], $inner ) ?? ToolMap::read( self::READS, $arguments['tool'] ); } return ToolMap::classify( self::TOOLS, $tool, $arguments ) ?? ToolMap::read( self::READS, $tool ); } }