PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / vendor / wordpress / mcp-adapter / includes / Abilities / McpAbilityHelperTrait.php

McpAbilityHelperTrait.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF trunk, at vendor/wordpress/mcp-adapter/includes/Abilities/McpAbilityHelperTrait.php

88 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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