Skip to content

Instantly share code, notes, and snippets.

@rajeshsingh520
Last active February 25, 2026 10:01
Show Gist options
  • Select an option

  • Save rajeshsingh520/5c6e64499f3be486ade1557bd4f05284 to your computer and use it in GitHub Desktop.

Select an option

Save rajeshsingh520/5c6e64499f3be486ade1557bd4f05284 to your computer and use it in GitHub Desktop.
register custom tool in the list of tools under woocommerce mcp server
class WCAbilitiesDemo {
/**
* Plugin version.
*/
public const VERSION = '1.0.0';
/**
* Initialize the plugin.
*/
public static function init(): void {
// Register our store info ability when the abilities API is ready
//add_action( 'init', array( __CLASS__, 'maybe_register_store_info_ability' ), 20 );
add_action( 'wp_abilities_api_init', array( __CLASS__, 'register_store_info_ability' ) );
/**
* This is needed as we are using our plugin initial in tool pisol-date-time if we have used woocommerce then we did not use this
*/
add_filter( 'woocommerce_mcp_include_ability', function( $include, $ability_id ) {
if ( str_starts_with( $ability_id, 'pisol-date-time/' ) ) {
return true;
}
return $include;
}, 10, 2 );
/**
* since we are registering out custom category of tools we need this
* if we would have use predefined categoru like 'store' then we need not use this
*/
add_action( 'wp_abilities_api_categories_init', function() {
wp_register_ability_category(
'pisol-date-time',
array(
'label' => __( 'Pisol Date Time plugin' ),
'description' => __( 'Date and time related abilities for the Pisol Date Time plugin.' ),
)
);
} );
}
/**
* Register the store info ability with the WordPress Abilities API.
*
* This demonstrates how third-party plugins can register abilities that
* will be automatically discovered and made available through the MCP server.
*/
public static function register_store_info_ability(): void {
// Only proceed if wp_register_ability function exists (from WordPress Abilities API)
if ( ! function_exists( 'wp_register_ability' ) ) {
return;
}
wp_register_ability(
'pisol-date-time/store-infoo',
array(
'label' => __( 'Get Store Information (Demo)', 'wc-mcp-ability' ),
'description' => __( 'Demo implementation: Retrieves basic information about the WooCommerce store including name, URL, version, and basic statistics.', 'wc-mcp-ability' ),
'category' => 'pisol-date-time', //this should be some existing category if it is new we must register it using wp_register_ability_category
'input_schema' => array(
'type' => 'object',
'properties' => array(
'include_stats' => array(
'type' => 'boolean',
'description' => 'Whether to include basic store statistics (product count, order count, etc.)',
'default' => false,
),
),
),
'output_schema' => array(
'type' => 'object',
'properties' => array(
'store_name' => array( 'type' => 'string' ),
'store_url' => array( 'type' => 'string' ),
'admin_email' => array( 'type' => 'string' ),
'woocommerce_version' => array( 'type' => 'string' ),
'wordpress_version' => array( 'type' => 'string' ),
'currency' => array( 'type' => 'string' ),
'country' => array( 'type' => 'string' ),
'plugin_source' => array( 'type' => 'string' ),
'stats' => array(
'type' => 'object',
'properties' => array(
'product_count' => array( 'type' => 'integer' ),
'order_count' => array( 'type' => 'integer' ),
'order_breakdown' => array(
'type' => 'object',
'properties' => array(
'completed' => array( 'type' => 'integer' ),
'processing' => array( 'type' => 'integer' ),
'pending' => array( 'type' => 'integer' ),
'on-hold' => array( 'type' => 'integer' ),
'cancelled' => array( 'type' => 'integer' ),
'refunded' => array( 'type' => 'integer' ),
'failed' => array( 'type' => 'integer' ),
),
),
'customer_count' => array( 'type' => 'integer' ),
),
),
),
'required' => array( 'store_name', 'store_url', 'woocommerce_version', 'plugin_source' ),
),
'execute_callback' => array( __CLASS__, 'execute_store_info_ability' ),
'permission_callback' => array( __CLASS__, 'check_store_info_permission' ),
)
);
}
/**
* Execute the store info ability.
*
* @param array $input Input parameters.
* @return array Store information.
*/
public static function execute_store_info_ability( array $input ): array {
// Build the basic store information
$result = array(
'store_name' => get_bloginfo( 'name' ),
'store_url' => get_site_url(),
'admin_email' => get_bloginfo( 'admin_email' ),
'woocommerce_version' => WC()->version,
'wordpress_version' => get_bloginfo( 'version' ),
'currency' => get_woocommerce_currency(),
'country' => WC()->countries->get_base_country(),
'plugin_source' => 'WooCommerce Abilities Demo Plugin v' . self::VERSION,
);
// Include statistics if requested
if ( ! empty( $input['include_stats'] ) ) {
$result['stats'] = self::get_store_statistics();
}
return $result;
}
/**
* Get store statistics.
*
* @return array Store statistics.
*/
private static function get_store_statistics(): array {
// Products use 'publish' status
$product_count = (int) wp_count_posts( 'product' )->publish;
// Orders - using WooCommerce order status constants
$completed_count = wc_orders_count( 'completed' );
$processing_count = wc_orders_count( 'processing' );
$pending_count = wc_orders_count( 'pending' );
$on_hold_count = wc_orders_count( 'on-hold' );
$cancelled_count = wc_orders_count( 'cancelled' );
$refunded_count = wc_orders_count( 'refunded' );
$failed_count = wc_orders_count( 'failed' );
$order_breakdown = array(
'completed' => $completed_count,
'processing' => $processing_count,
'pending' => $pending_count,
'on-hold' => $on_hold_count,
'cancelled' => $cancelled_count,
'refunded' => $refunded_count,
'failed' => $failed_count,
);
$order_count = array_sum( $order_breakdown );
// Customers - count users with 'customer' role
$users_counts = count_users();
$customer_count = isset( $users_counts['avail_roles']['customer'] ) ? (int) $users_counts['avail_roles']['customer'] : 0;
return array(
'product_count' => $product_count,
'order_count' => $order_count,
'order_breakdown' => $order_breakdown,
'customer_count' => $customer_count,
);
}
/**
* Check permission for the store info ability.
*
* @return bool Whether user has permission.
*/
public static function check_store_info_permission(): bool {
// Allow users who can manage WooCommerce (same as system_status endpoint)
return current_user_can( 'manage_woocommerce' );
}
}
// Initialize the plugin
WCAbilitiesDemo::init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment