| 1 |
<?php |
| 2 |
/** |
| 3 |
* Kit MCP Prompt base class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Abstract base for all Kit Plugin prompts exposed via the WordPress Abilities |
| 11 |
* API and MCP Adapter. |
| 12 |
* |
| 13 |
* @package ConvertKit |
| 14 |
* @author ConvertKit |
| 15 |
*/ |
| 16 |
abstract class ConvertKit_MCP_Prompt { |
| 17 |
|
| 18 |
/** |
| 19 |
* Returns the prompt's short name (e.g. `setup`), without the `kit/` prefix. |
| 20 |
* |
| 21 |
* @since 3.4.2 |
| 22 |
* |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
abstract protected function get_prompt_name(); |
| 26 |
|
| 27 |
/** |
| 28 |
* Returns the fully qualified prompt name, prefixed with `kit/` |
| 29 |
* (e.g. `kit/setup`). |
| 30 |
* |
| 31 |
* @since 3.4.2 |
| 32 |
* |
| 33 |
* @return string |
| 34 |
*/ |
| 35 |
public function get_name() { |
| 36 |
|
| 37 |
return 'kit/' . $this->get_prompt_name(); |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Returns the prompt's human-readable label / title. |
| 43 |
* |
| 44 |
* @since 3.4.2 |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
abstract public function get_label(); |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns the prompt's human-readable description. |
| 52 |
* |
| 53 |
* @since 3.4.2 |
| 54 |
* |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
abstract public function get_description(); |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns the prompt's arguments, keyed by argument name. Each value is an |
| 61 |
* array with a `description` and an optional `required` flag. MCP prompt |
| 62 |
* arguments are always strings. |
| 63 |
* |
| 64 |
* @since 3.4.2 |
| 65 |
* |
| 66 |
* @return array |
| 67 |
*/ |
| 68 |
protected function get_arguments() { |
| 69 |
|
| 70 |
return array(); |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Returns the prompt's category. |
| 76 |
* |
| 77 |
* @since 3.4.2 |
| 78 |
* |
| 79 |
* @return string |
| 80 |
*/ |
| 81 |
public function get_category() { |
| 82 |
|
| 83 |
return 'kit'; |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Returns the prompt's input JSON Schema, built from get_arguments(). The MCP |
| 89 |
* Adapter converts each property to a prompt argument. |
| 90 |
* |
| 91 |
* @since 3.4.2 |
| 92 |
* |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
public function get_input_schema() { |
| 96 |
|
| 97 |
$properties = array(); |
| 98 |
$required = array(); |
| 99 |
|
| 100 |
foreach ( $this->get_arguments() as $name => $argument ) { |
| 101 |
$properties[ $name ] = array( |
| 102 |
'type' => 'string', |
| 103 |
'description' => isset( $argument['description'] ) ? $argument['description'] : '', |
| 104 |
); |
| 105 |
|
| 106 |
if ( ! empty( $argument['required'] ) ) { |
| 107 |
$required[] = $name; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
$schema = array( |
| 112 |
'type' => 'object', |
| 113 |
'properties' => $properties, |
| 114 |
); |
| 115 |
|
| 116 |
if ( count( $required ) ) { |
| 117 |
$schema['required'] = $required; |
| 118 |
} |
| 119 |
|
| 120 |
return $schema; |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Returns the prompt's output JSON Schema. Prompt content is returned as a |
| 126 |
* text string, wrapped as a user message by the MCP Adapter. |
| 127 |
* |
| 128 |
* @since 3.4.2 |
| 129 |
* |
| 130 |
* @return array |
| 131 |
*/ |
| 132 |
public function get_output_schema() { |
| 133 |
|
| 134 |
return array( |
| 135 |
'type' => 'object', |
| 136 |
); |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Permission callback. |
| 142 |
* |
| 143 |
* @since 3.4.2 |
| 144 |
* |
| 145 |
* @param array $input Prompt input (unused). |
| 146 |
* @return bool|WP_Error |
| 147 |
*/ |
| 148 |
public function permission_callback( $input ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found |
| 149 |
|
| 150 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 151 |
return new WP_Error( |
| 152 |
'convertkit_mcp_prompt_permission_callback', |
| 153 |
__( 'You do not have permission for this operation.', 'convertkit' ) |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
return true; |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Returns the arguments array passed to wp_register_ability(). |
| 163 |
* |
| 164 |
* @since 3.4.2 |
| 165 |
* |
| 166 |
* @return array |
| 167 |
*/ |
| 168 |
public function get_ability_args() { |
| 169 |
|
| 170 |
return array( |
| 171 |
'label' => $this->get_label(), |
| 172 |
'description' => $this->get_description(), |
| 173 |
'category' => $this->get_category(), |
| 174 |
'input_schema' => $this->get_input_schema(), |
| 175 |
'output_schema' => $this->get_output_schema(), |
| 176 |
'permission_callback' => array( $this, 'permission_callback' ), |
| 177 |
'execute_callback' => array( $this, 'execute_callback' ), |
| 178 |
'meta' => array( |
| 179 |
'mcp' => array( |
| 180 |
'type' => 'prompt', |
| 181 |
'public' => true, |
| 182 |
), |
| 183 |
), |
| 184 |
); |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Execute callback for this prompt, returning its prompt text. |
| 190 |
* |
| 191 |
* Sub classes build their prompt and return $this->render( array( ...sections... ) ). |
| 192 |
* |
| 193 |
* @since 3.4.2 |
| 194 |
* |
| 195 |
* @param array $input Prompt arguments. |
| 196 |
* @return array|WP_Error |
| 197 |
*/ |
| 198 |
abstract public function execute_callback( $input ); |
| 199 |
|
| 200 |
/** |
| 201 |
* Assembles the given text sections into a single prompt message, skipping |
| 202 |
* empty sections. |
| 203 |
* |
| 204 |
* @since 3.4.2 |
| 205 |
* |
| 206 |
* @param array $sections Text sections. |
| 207 |
* @return array Prompt result ( text ). |
| 208 |
*/ |
| 209 |
protected function render( $sections ) { |
| 210 |
|
| 211 |
$sections = array_filter( |
| 212 |
array_map( 'trim', (array) $sections ), |
| 213 |
function ( $section ) { |
| 214 |
return $section !== ''; |
| 215 |
} |
| 216 |
); |
| 217 |
|
| 218 |
return array( |
| 219 |
'text' => implode( "\n\n", $sections ), |
| 220 |
); |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
} |
| 225 |
|