PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / trunk
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages vtrunk
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / includes / mcp / class-convertkit-mcp.php

class-convertkit-mcp.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages trunk, at includes/mcp/class-convertkit-mcp.php

303 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Kit MCP class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Registers Plugin abilities (tools) using the WordPress Abilities API, and exposes
11 * those abilities as MCP tools via the WordPress MCP Adapter (if installed).
12 *
13 * The Abilities API ships with WordPress 6.9 and later.
14 *
15 * The WordPress MCP Adapter is a separate plugin, and not (yet) part of WordPress
16 * core. If it is not active on the site, abilities are still registered and callable
17 * in PHP, but nothing is exposed over the MCP protocol.
18 *
19 * @package ConvertKit
20 * @author ConvertKit
21 */
22 class ConvertKit_MCP {
23
24 /**
25 * The ability category slug used to group all Kit abilities.
26 *
27 * @since 3.4.0
28 *
29 * @var string
30 */
31 const CATEGORY_SLUG = 'kit';
32
33 /**
34 * The MCP server ID.
35 *
36 * @since 3.4.0
37 *
38 * @var string
39 */
40 const SERVER_ID = 'kit/mcp';
41
42 /**
43 * The REST namespace used by the MCP server.
44 *
45 * @since 3.4.0
46 *
47 * @var string
48 */
49 const SERVER_NAMESPACE = 'kit/mcp';
50
51 /**
52 * The REST version number used by the MCP server.
53 *
54 * @since 3.4.0
55 *
56 * @var string
57 */
58 const SERVER_ROUTE = 'v1';
59
60 /**
61 * Returns the absolute URL that MCP clients connect to.
62 *
63 * @since 3.4.0
64 *
65 * @return string
66 */
67 public static function get_server_url() {
68
69 return rest_url( self::SERVER_NAMESPACE . '/' . self::SERVER_ROUTE );
70
71 }
72
73 /**
74 * Constructor.
75 *
76 * @since 3.4.0
77 */
78 public function __construct() {
79
80 // Register the ability category.
81 add_action( 'wp_abilities_api_categories_init', array( $this, 'register_abilities_category' ) );
82
83 // Register abilities.
84 add_action( 'wp_abilities_api_init', array( $this, 'register_abilities' ) );
85
86 // Register resource-list abilities (Forms, Tags, Landing Pages, Products).
87 // These are owned by the Plugin (not by any single block or feature),
88 // so they're added here rather than via a per-class register_abilities().
89 add_filter( 'convertkit_abilities', array( $this, 'register_resource_abilities' ) );
90
91 // Register settings get / update abilities for each Plugin settings
92 // These are owned by the Plugin (not by any single feature),
93 // so they're added here rather than via a per-class register_abilities().
94 add_filter( 'convertkit_abilities', array( $this, 'register_settings_abilities' ) );
95
96 // Register the per-Post Kit settings get / update abilities. These
97 // operate on the `_wp_convertkit_post_meta` post meta (form,
98 // landing_page, tag, restrict_content) rather than any Plugin-wide
99 // settings group.
100 add_filter( 'convertkit_abilities', array( $this, 'register_post_settings_abilities' ) );
101
102 // Register the per-Category Kit settings get / update abilities.
103 // These operate on the `_wp_convertkit_term_meta` term meta (form,
104 // form_position) for the WordPress `category` taxonomy.
105 add_filter( 'convertkit_abilities', array( $this, 'register_category_settings_abilities' ) );
106
107 // Register the MCP server.
108 add_action( 'mcp_adapter_init', array( $this, 'register_mcp_server' ) );
109
110 }
111
112 /**
113 * Appends the settings get / update abilities for each Plugin settings
114 * group to the convertkit_abilities filter, so they are registered with
115 * the Abilities API and exposed via the MCP server.
116 *
117 * @since 3.4.0
118 *
119 * @param array $abilities Abilities to register.
120 * @return array
121 */
122 public function register_settings_abilities( $abilities ) {
123
124 // Settings instances to register with MCP.
125 $groups = array(
126 new ConvertKit_Settings(),
127 new ConvertKit_Settings_Broadcasts(),
128 new ConvertKit_Settings_Restrict_Content(),
129 );
130
131 // Iterate through settings groups, registering the get and update abilities.
132 foreach ( $groups as $settings ) {
133 $get = new ConvertKit_MCP_Ability_Settings_Get( $settings );
134 $update = new ConvertKit_MCP_Ability_Settings_Update( $settings );
135
136 $abilities[ $get->get_name() ] = $get;
137 $abilities[ $update->get_name() ] = $update;
138 }
139
140 return $abilities;
141
142 }
143
144 /**
145 * Appends the per-Post Kit settings abilities to the convertkit_abilities
146 * filter, so they are registered with the Abilities API and exposed via
147 * the MCP server.
148 *
149 * @since 3.4.0
150 *
151 * @param array $abilities Abilities to register.
152 * @return array
153 */
154 public function register_post_settings_abilities( $abilities ) {
155
156 $abilities['kit/post-settings-get'] = new ConvertKit_MCP_Ability_Post_Settings_Get();
157 $abilities['kit/post-settings-update'] = new ConvertKit_MCP_Ability_Post_Settings_Update();
158
159 return $abilities;
160
161 }
162
163 /**
164 * Appends the per-Category Kit settings abilities to the convertkit_abilities
165 * filter, so they are registered with the Abilities API and exposed via
166 * the MCP server.
167 *
168 * @since 3.4.0
169 *
170 * @param array $abilities Abilities to register.
171 * @return array
172 */
173 public function register_category_settings_abilities( $abilities ) {
174
175 $abilities['kit/category-settings-get'] = new ConvertKit_MCP_Ability_Category_Settings_Get();
176 $abilities['kit/category-settings-update'] = new ConvertKit_MCP_Ability_Category_Settings_Update();
177
178 return $abilities;
179
180 }
181
182 /**
183 * Appends the resource-list abilities (Forms, Tags, Landing Pages,
184 * Products) to the convertkit_abilities filter, so they are registered
185 * with the Abilities API and exposed via the MCP server.
186 *
187 * @since 3.4.0
188 *
189 * @param array $abilities Abilities to register.
190 * @return array
191 */
192 public function register_resource_abilities( $abilities ) {
193
194 return array_merge(
195 $abilities,
196 array(
197 'kit/forms-list' => new ConvertKit_MCP_Ability_Resource_Forms(),
198 'kit/tags-list' => new ConvertKit_MCP_Ability_Resource_Tags(),
199 'kit/landing-pages-list' => new ConvertKit_MCP_Ability_Resource_Landing_Pages(),
200 'kit/products-list' => new ConvertKit_MCP_Ability_Resource_Products(),
201 )
202 );
203
204 }
205
206 /**
207 * Register the 'kit' ability category.
208 *
209 * @since 3.4.0
210 */
211 public function register_abilities_category() {
212
213 wp_register_ability_category(
214 self::CATEGORY_SLUG,
215 array(
216 'label' => __( 'Kit', 'convertkit' ),
217 'description' => __( 'Abilities exposed by the Kit Plugin.', 'convertkit' ),
218 )
219 );
220
221 }
222
223 /**
224 * Register abilities with the WordPress Abilities API.
225 *
226 * @since 3.4.0
227 */
228 public function register_abilities() {
229
230 // Get abilities.
231 $abilities = convertkit_get_abilities();
232
233 // Bail if no abilities are available.
234 if ( ! count( $abilities ) ) {
235 return;
236 }
237
238 // Iterate through abilities, registering them.
239 foreach ( $abilities as $ability ) {
240
241 // Skip if this ability is not an instance of ConvertKit_MCP_Ability.
242 if ( ! ( $ability instanceof ConvertKit_MCP_Ability ) ) {
243 continue;
244 }
245
246 // Register ability.
247 wp_register_ability( $ability->get_name(), $ability->get_ability_args() );
248 }
249
250 }
251
252 /**
253 * Register an MCP server that exposes Kit abilities as MCP tools.
254 *
255 * @since 3.4.0
256 *
257 * @param object $adapter The MCP Adapter instance.
258 * @return void
259 */
260 public function register_mcp_server( $adapter ) {
261
262 // Bail if the MCP server isn't enabled.
263 $settings = new ConvertKit_Settings();
264 $mcp_settings = new ConvertKit_Settings_MCP();
265 if ( ! $mcp_settings->enabled() ) {
266 return;
267 }
268
269 // Get abilities.
270 $abilities = convertkit_get_abilities();
271
272 // Build array of ability names.
273 $ability_names = array();
274 foreach ( $abilities as $ability ) {
275 $ability_names[] = $ability->get_name();
276 }
277
278 // Create the MCP server.
279 $result = $adapter->create_server(
280 self::SERVER_ID,
281 self::SERVER_NAMESPACE,
282 self::SERVER_ROUTE,
283 __( 'Kit WordPress Plugin MCP', 'convertkit' ),
284 __( 'Exposes Kit Plugin abilities over the Model Context Protocol.', 'convertkit' ),
285 '1.0.0',
286 array( 'WP\\MCP\\Transport\\HttpTransport' ),
287 'WP\\MCP\\Infrastructure\\ErrorHandling\\ErrorLogMcpErrorHandler',
288 'WP\\MCP\\Infrastructure\\Observability\\NullMcpObservabilityHandler',
289 $ability_names, // Abilities (Tools).
290 array(), // Resources.
291 array() // Prompts.
292 );
293
294 // If an error occured when creating the server, log it.
295 if ( is_wp_error( $result ) && $settings->debug_enabled() ) {
296 $log = new ConvertKit_Log( CONVERTKIT_PLUGIN_PATH );
297 $log->add( 'MCP: create_server(): Error: ' . $result->get_error_message() );
298 }
299
300 }
301
302 }
303