| 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 resolved mcp.public metadata flag. |
| 25 |
* Only abilities with effective MCP public exposure 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 |
if ( ! McpAbilityExposure::is_public( $ability ) ) { |
| 39 |
return new WP_Error( |
| 40 |
'ability_not_public_mcp', |
| 41 |
sprintf( 'Ability "%s" is not exposed via MCP (mcp.public!=true)', $ability_name ) |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Checks if ability is publicly exposed via MCP (simple boolean version). |
| 50 |
* |
| 51 |
* This is a simplified version that returns only boolean values, |
| 52 |
* useful for filtering operations where WP_Error handling isn't needed. |
| 53 |
* |
| 54 |
* @param \WP_Ability $ability The ability object to check. |
| 55 |
* |
| 56 |
* @return bool True if publicly exposed, false otherwise. |
| 57 |
*/ |
| 58 |
protected static function is_ability_mcp_public( \WP_Ability $ability ): bool { |
| 59 |
return McpAbilityExposure::is_public( $ability ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Gets the MCP type of an ability. |
| 64 |
* |
| 65 |
* Returns the type specified in meta.mcp.type, defaulting to 'tool' if not specified. |
| 66 |
* |
| 67 |
* @param \WP_Ability $ability The ability object to check. |
| 68 |
* |
| 69 |
* @return string The MCP type ('tool', 'resource', or 'prompt'). Defaults to 'tool'. |
| 70 |
*/ |
| 71 |
protected static function get_ability_mcp_type( \WP_Ability $ability ): string { |
| 72 |
$meta = $ability->get_meta(); |
| 73 |
$type = $meta['mcp']['type'] ?? 'tool'; |
| 74 |
|
| 75 |
// Validate type is one of the allowed values |
| 76 |
if ( ! in_array( $type, array( 'tool', 'resource', 'prompt' ), true ) ) { |
| 77 |
return 'tool'; |
| 78 |
} |
| 79 |
|
| 80 |
return $type; |
| 81 |
} |
| 82 |
} |
| 83 |
|