| 1 |
<?php |
| 2 |
/** |
| 3 |
* Resolves whether an ability is exposed through the default MCP server. |
| 4 |
* |
| 5 |
* @package McpAdapter |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace WP\MCP\Abilities; |
| 11 |
|
| 12 |
use WP_Ability; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class McpAbilityExposure |
| 16 |
* |
| 17 |
* Single source of truth for MCP exposure. Every read of the exposure flag must |
| 18 |
* go through this class. Reading `meta.mcp.public` directly reintroduces the bug |
| 19 |
* this class exists to prevent: exposure cannot be resolved while an ability is |
| 20 |
* being registered, because `wp_register_ability_args` callbacks that run later |
| 21 |
* can still change `meta.public`, and no filter priority is guaranteed to run |
| 22 |
* last. Resolving from the stored ability instead happens after registration is |
| 23 |
* complete, so the metadata is final. |
| 24 |
* |
| 25 |
* @since 0.6.0 |
| 26 |
*/ |
| 27 |
final class McpAbilityExposure { |
| 28 |
|
| 29 |
/** |
| 30 |
* Determines whether an ability is exposed through the default MCP server. |
| 31 |
* |
| 32 |
* An explicit `meta.mcp.public` wins. When it is absent or null, exposure is |
| 33 |
* inherited from the high-level `meta.public` flag. Malformed `meta.mcp` |
| 34 |
* fails closed. |
| 35 |
* |
| 36 |
* @since 0.6.0 |
| 37 |
* |
| 38 |
* @param \WP_Ability $ability The ability to check. |
| 39 |
* |
| 40 |
* @return bool True when the ability is exposed through MCP, false otherwise. |
| 41 |
*/ |
| 42 |
public static function is_public( WP_Ability $ability ): bool { |
| 43 |
return self::is_meta_public( $ability->get_meta() ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Determines whether ability metadata resolves to MCP exposure. |
| 48 |
* |
| 49 |
* @since 0.6.0 |
| 50 |
* |
| 51 |
* @param array<string, mixed> $meta Ability metadata. |
| 52 |
* |
| 53 |
* @return bool True when the metadata resolves to MCP exposure, false otherwise. |
| 54 |
*/ |
| 55 |
public static function is_meta_public( array $meta ): bool { |
| 56 |
$mcp_meta = $meta['mcp'] ?? array(); |
| 57 |
|
| 58 |
// Fail closed when `meta.mcp` is malformed. |
| 59 |
if ( ! is_array( $mcp_meta ) ) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
if ( isset( $mcp_meta['public'] ) ) { |
| 64 |
return (bool) $mcp_meta['public']; |
| 65 |
} |
| 66 |
|
| 67 |
return true === ( $meta['public'] ?? false ); |
| 68 |
} |
| 69 |
} |
| 70 |
|