| 1 |
<?php |
| 2 |
/** |
| 3 |
* Kit MCP Resource base class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Abstract base for all Kit Plugin resources exposed via the WordPress Abilities |
| 11 |
* API and MCP Adapter. |
| 12 |
* |
| 13 |
* @package ConvertKit |
| 14 |
* @author ConvertKit |
| 15 |
*/ |
| 16 |
abstract class ConvertKit_MCP_Resource { |
| 17 |
|
| 18 |
/** |
| 19 |
* Returns the resource name, prefixed with `kit/` (e.g. `kit/forms`). |
| 20 |
* |
| 21 |
* @since 3.4.2 |
| 22 |
* |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
abstract public function get_name(); |
| 26 |
|
| 27 |
/** |
| 28 |
* Returns the resource URI that MCP clients read (e.g. `kit://forms`). |
| 29 |
* |
| 30 |
* @since 3.4.2 |
| 31 |
* |
| 32 |
* @return string |
| 33 |
*/ |
| 34 |
abstract public function get_uri(); |
| 35 |
|
| 36 |
/** |
| 37 |
* Returns the resource's human-readable label. |
| 38 |
* |
| 39 |
* @since 3.4.2 |
| 40 |
* |
| 41 |
* @return string |
| 42 |
*/ |
| 43 |
abstract public function get_label(); |
| 44 |
|
| 45 |
/** |
| 46 |
* Returns the resource's human-readable description. |
| 47 |
* |
| 48 |
* @since 3.4.2 |
| 49 |
* |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
abstract public function get_description(); |
| 53 |
|
| 54 |
/** |
| 55 |
* Returns the resource content's MIME type. |
| 56 |
* |
| 57 |
* Sub classes can override this to return a different MIME type. |
| 58 |
* |
| 59 |
* @since 3.4.2 |
| 60 |
* |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
public function get_mime_type() { |
| 64 |
|
| 65 |
return 'text/markdown'; |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Returns the resource's category. |
| 71 |
* |
| 72 |
* @since 3.4.2 |
| 73 |
* |
| 74 |
* @return string |
| 75 |
*/ |
| 76 |
public function get_category() { |
| 77 |
|
| 78 |
return 'kit'; |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns the resource's input JSON Schema. |
| 84 |
* |
| 85 |
* Resources are read by URI with no input, so this is an empty object |
| 86 |
* schema. `null` is allowed as well as an object, so input validation |
| 87 |
* doesn't fail on the empty read. |
| 88 |
* |
| 89 |
* @since 3.4.2 |
| 90 |
* |
| 91 |
* @return array |
| 92 |
*/ |
| 93 |
public function get_input_schema() { |
| 94 |
|
| 95 |
return array( |
| 96 |
'type' => array( 'object', 'null' ), |
| 97 |
); |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Returns the resource's output JSON Schema. |
| 103 |
* |
| 104 |
* Resource content is returned as a single string, wrapped as text content |
| 105 |
* by the MCP Adapter. |
| 106 |
* |
| 107 |
* @since 3.4.2 |
| 108 |
* |
| 109 |
* @return array |
| 110 |
*/ |
| 111 |
public function get_output_schema() { |
| 112 |
|
| 113 |
return array( |
| 114 |
'type' => 'string', |
| 115 |
); |
| 116 |
|
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Permission callback. |
| 121 |
* |
| 122 |
* Sub classes can override this to implement their own permission callback. |
| 123 |
* |
| 124 |
* @since 3.4.2 |
| 125 |
* |
| 126 |
* @param array $input Resource input (unused). |
| 127 |
* @return bool|WP_Error |
| 128 |
*/ |
| 129 |
public function permission_callback( $input ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found |
| 130 |
|
| 131 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 132 |
return new WP_Error( |
| 133 |
'convertkit_mcp_resource_permission_callback', |
| 134 |
__( 'You do not have permission for this operation.', 'convertkit' ) |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 138 |
return true; |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Returns the arguments array passed to wp_register_ability(). |
| 144 |
* |
| 145 |
* @since 3.4.2 |
| 146 |
* |
| 147 |
* @return array |
| 148 |
*/ |
| 149 |
public function get_ability_args() { |
| 150 |
|
| 151 |
return array( |
| 152 |
'label' => $this->get_label(), |
| 153 |
'description' => $this->get_description(), |
| 154 |
'category' => $this->get_category(), |
| 155 |
'input_schema' => $this->get_input_schema(), |
| 156 |
'output_schema' => $this->get_output_schema(), |
| 157 |
'permission_callback' => array( $this, 'permission_callback' ), |
| 158 |
'execute_callback' => array( $this, 'execute_callback' ), |
| 159 |
'meta' => array( |
| 160 |
'mcp' => array( |
| 161 |
'uri' => $this->get_uri(), |
| 162 |
'mimeType' => $this->get_mime_type(), |
| 163 |
), |
| 164 |
), |
| 165 |
); |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Execute callback for this resource, returning its content. |
| 171 |
* |
| 172 |
* @since 3.4.2 |
| 173 |
* |
| 174 |
* @param array $input Resource input. |
| 175 |
* @return string|WP_Error |
| 176 |
*/ |
| 177 |
abstract public function execute_callback( $input ); |
| 178 |
|
| 179 |
} |
| 180 |
|