| @@ -2,8 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Elementor\Modules\Mcp\Abilities; |
| 4 | 4 | |
| 5 | 5 | use Elementor\Modules\Mcp\Utils\Editor_Sync_State; |
| 6 | +use Elementor\Modules\Mcp\Utils\Mcp_V4_Gate; | |
| 6 | 7 | |
| 7 | 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | 9 | exit; |
| 9 | 10 | } |
| @@ -22,8 +23,13 @@ | ||
| 22 | 23 | |
| 23 | 24 | abstract public function execute( $input = [] ); |
| 24 | 25 | |
| 25 | 26 | final public function execute_guarded( $input = [] ) { |
| 27 | + $availability = $this->is_available(); | |
| 28 | + if ( is_wp_error( $availability ) ) { | |
| 29 | + return $availability; | |
| 30 | + } | |
| 31 | + | |
| 26 | 32 | $is_mutating = self::KIND_TOOL === $this->get_kind() && $this->is_destructive(); |
| 27 | 33 | |
| 28 | 34 | if ( $is_mutating ) { |
| 29 | 35 | $guard = apply_filters( 'elementor/mcp/pre_execute_guard', null, $input ); |
| @@ -50,11 +56,16 @@ | ||
| 50 | 56 | |
| 51 | 57 | public function register(): void { |
| 52 | 58 | $definition = $this->definition()->to_array(); |
| 53 | 59 | $definition['execute_callback'] = [ $this, 'execute_guarded' ]; |
| 60 | + $definition['description'] = $this->get_description_for_llm(); | |
| 61 | + | |
| 54 | 62 | $meta = is_array( $definition['meta'] ?? null ) ? $definition['meta'] : []; |
| 55 | 63 | $mcp = is_array( $meta['mcp'] ?? null ) ? $meta['mcp'] : []; |
| 56 | 64 | $mcp['public'] = true; |
| 65 | + if ( isset( $mcp['description'] ) ) { | |
| 66 | + $mcp['description'] = $this->maybe_append_unavailable_notice( (string) $mcp['description'] ); | |
| 67 | + } | |
| 57 | 68 | $meta['mcp'] = $mcp; |
| 58 | 69 | $meta['show_in_rest'] = true; |
| 59 | 70 | $definition['meta'] = $meta; |
| 60 | 71 | wp_register_ability( $this->get_id(), $definition ); |
| @@ -76,11 +87,17 @@ | ||
| 76 | 87 | return $this->mcp_meta()['mimeType'] ?? null; |
| 77 | 88 | } |
| 78 | 89 | |
| 79 | 90 | public function get_resource_description(): ?string { |
| 80 | - return $this->mcp_meta()['description'] ?? $this->definition()->description; | |
| 91 | + $base = $this->mcp_meta()['description'] ?? $this->definition()->description; | |
| 92 | + | |
| 93 | + return $this->maybe_append_unavailable_notice( (string) $base ); | |
| 81 | 94 | } |
| 82 | 95 | |
| 96 | + public function get_description_for_llm(): string { | |
| 97 | + return $this->maybe_append_unavailable_notice( (string) $this->definition()->description ); | |
| 98 | + } | |
| 99 | + | |
| 83 | 100 | public function get_display_name(): string { |
| 84 | 101 | return (string) ( $this->mcp_meta()['name'] ?? $this->definition()->label ); |
| 85 | 102 | } |
| 86 | 103 | |
| @@ -91,8 +108,23 @@ | ||
| 91 | 108 | |
| 92 | 109 | return substr( $this->get_id(), strlen( self::ABILITY_ID_PREFIX ) ); |
| 93 | 110 | } |
| 94 | 111 | |
| 112 | + /** | |
| 113 | + * Whether this ability may currently run. | |
| 114 | + * | |
| 115 | + * Subclasses may override to add reasons beyond the default Atomic Editor gate | |
| 116 | + * (missing plugin, licence tier, post-type support, ...). Return `true` | |
| 117 | + * when available, or a `\WP_Error` explaining why not. Include a | |
| 118 | + * `description_notice` entry in the error data to add a short hint to | |
| 119 | + * this ability's description in `tools/list` / `elementor/list-resources`. | |
| 120 | + * | |
| 121 | + * @return true|\WP_Error | |
| 122 | + */ | |
| 123 | + public function is_available() { | |
| 124 | + return Mcp_V4_Gate::is_available( $this->get_id() ); | |
| 125 | + } | |
| 126 | + | |
| 95 | 127 | public function is_exposed_via_proxy(): bool { |
| 96 | 128 | return true; |
| 97 | 129 | } |
| 98 | 130 | |
| @@ -119,6 +151,24 @@ | ||
| 119 | 151 | private function mcp_meta(): array { |
| 120 | 152 | $meta = $this->definition()->meta; |
| 121 | 153 | |
| 122 | 154 | return is_array( $meta['mcp'] ?? null ) ? $meta['mcp'] : []; |
| 155 | + } | |
| 156 | + | |
| 157 | + private function maybe_append_unavailable_notice( string $description ): string { | |
| 158 | + $availability = $this->is_available(); | |
| 159 | + if ( ! is_wp_error( $availability ) ) { | |
| 160 | + return $description; | |
| 161 | + } | |
| 162 | + | |
| 163 | + $data = $availability->get_error_data(); | |
| 164 | + $notice = is_array( $data ) && isset( $data['description_notice'] ) | |
| 165 | + ? (string) $data['description_notice'] | |
| 166 | + : $availability->get_error_message(); | |
| 167 | + | |
| 168 | + if ( '' === $notice ) { | |
| 169 | + return $description; | |
| 170 | + } | |
| 171 | + | |
| 172 | + return '' === $description ? $notice : rtrim( $description ) . ' ' . $notice; | |
| 123 | 173 | } |
| 124 | 174 | } |