DiscoverAbilitiesAbility.php
1 month ago
ExecuteAbilityAbility.php
1 month ago
GetAbilityInfoAbility.php
1 month ago
McpAbilityHelperTrait.php
1 month ago
McpAbilityHelperTrait.php
88 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Helper trait for WordPress abilities providing MCP-related utilities. |
| 4 | * |
| 5 | * @package McpAdapter |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types=1 ); |
| 9 | |
| 10 | namespace WP\MCP\Abilities; |
| 11 | |
| 12 | use WP_Error; |
| 13 | |
| 14 | /** |
| 15 | * Trait McpAbilityHelperTrait |
| 16 | * |
| 17 | * Provides helper methods for MCP abilities including MCP exposure checking and metadata handling. |
| 18 | */ |
| 19 | trait McpAbilityHelperTrait { |
| 20 | |
| 21 | /** |
| 22 | * Checks if ability is publicly exposed via MCP. |
| 23 | * |
| 24 | * Validates against the ability's mcp.public metadata flag. |
| 25 | * Only abilities with mcp.public=true are accessible via default MCP server. |
| 26 | * |
| 27 | * @param string $ability_name The ability name to check. |
| 28 | * |
| 29 | * @return bool|\WP_Error True if publicly exposed, WP_Error if not. |
| 30 | */ |
| 31 | protected static function check_ability_mcp_exposure( string $ability_name ) { |
| 32 | $ability = wp_get_ability( $ability_name ); |
| 33 | |
| 34 | if ( ! $ability ) { |
| 35 | return new WP_Error( 'ability_not_found', "Ability '{$ability_name}' not found" ); |
| 36 | } |
| 37 | |
| 38 | $meta = $ability->get_meta(); |
| 39 | $is_public_mcp = $meta['mcp']['public'] ?? false; |
| 40 | |
| 41 | if ( ! $is_public_mcp ) { |
| 42 | return new WP_Error( |
| 43 | 'ability_not_public_mcp', |
| 44 | sprintf( 'Ability "%s" is not exposed via MCP (mcp.public!=true)', $ability_name ) |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Checks if ability is publicly exposed via MCP (simple boolean version). |
| 53 | * |
| 54 | * This is a simplified version that returns only boolean values, |
| 55 | * useful for filtering operations where WP_Error handling isn't needed. |
| 56 | * |
| 57 | * @param \WP_Ability $ability The ability object to check. |
| 58 | * |
| 59 | * @return bool True if publicly exposed, false otherwise. |
| 60 | */ |
| 61 | protected static function is_ability_mcp_public( \WP_Ability $ability ): bool { |
| 62 | $meta = $ability->get_meta(); |
| 63 | |
| 64 | return (bool) ( $meta['mcp']['public'] ?? false ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Gets the MCP type of an ability. |
| 69 | * |
| 70 | * Returns the type specified in meta.mcp.type, defaulting to 'tool' if not specified. |
| 71 | * |
| 72 | * @param \WP_Ability $ability The ability object to check. |
| 73 | * |
| 74 | * @return string The MCP type ('tool', 'resource', or 'prompt'). Defaults to 'tool'. |
| 75 | */ |
| 76 | protected static function get_ability_mcp_type( \WP_Ability $ability ): string { |
| 77 | $meta = $ability->get_meta(); |
| 78 | $type = $meta['mcp']['type'] ?? 'tool'; |
| 79 | |
| 80 | // Validate type is one of the allowed values |
| 81 | if ( ! in_array( $type, array( 'tool', 'resource', 'prompt' ), true ) ) { |
| 82 | return 'tool'; |
| 83 | } |
| 84 | |
| 85 | return $type; |
| 86 | } |
| 87 | } |
| 88 |