PluginProbe
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN / 1.1.6
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN v1.1.6
1.3.3 1.3.2 1.3.1 1.3.0 1.2.4 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 All 29 releases
xspeed / includes / modules / Mcp / Mcp_Tools.php

Mcp_Tools.php in xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN 1.1.6, at includes/modules/Mcp/Mcp_Tools.php

1,812 lines 67.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MCP tool registry — the single source of truth for the tools xSpeed
4 * exposes to AI assistants.
5 *
6 * Each tool declares an MCP-style descriptor (name, description, JSON
7 * Schema inputSchema) and a handler that runs against the Free engine.
8 * Consumed by BOTH:
9 * - Mcp_Server (the per-site JSON-RPC endpoint at /xspeed/mcp), and
10 * - McpModule's REST tool routes (the optional hosted-broker path),
11 * so the two transports can never drift.
12 *
13 * Handlers take an associative array of already-decoded arguments and
14 * return either a plain array (serialized to JSON in the MCP result) or
15 * a WP_Error (surfaced as an MCP tool error).
16 *
17 * The plugin adds ZERO cache logic here — every handler is a thin proxy
18 * to Cache / Settings / Settings_Manager / Server / Admin / Pro_Audit /
19 * Cache_Benchmark.
20 *
21 * @package XSpeed
22 */
23
24 declare(strict_types=1);
25
26 namespace XSpeed\Modules\Mcp;
27
28 use XSpeed\Cache;
29 use XSpeed\Server;
30 use XSpeed\Admin;
31 use XSpeed\Settings;
32 use XSpeed\Settings_Manager;
33 use XSpeed\Pro_Audit;
34 use XSpeed\Cache_Benchmark;
35 use XSpeed\Tier_Registry;
36
37 defined( 'ABSPATH' ) || exit;
38
39 final class Mcp_Tools {
40
41 /** Valid cache purge types. */
42 public const PURGE_TYPES = array( 'all', 'page', 'assets', 'object', 'rest' );
43
44 /**
45 * Per-call read-only override. Null means "defer to the pairing token's
46 * scope" (the JSON-RPC path that predates OAuth). true/false is set by
47 * Mcp_Server when an OAuth access token (with its own scope) authorized
48 * the request, so a read-only OAuth grant is enforced even though the
49 * pairing token may be read-write (or absent).
50 *
51 * @var bool|null
52 */
53 private static $read_only_override = null;
54
55 /**
56 * Set the active credential's read-only state for the current request.
57 * Passing null clears the override (back to the pairing-token default).
58 *
59 * @param bool|null $read_only Whether the active credential is read-only.
60 */
61 public static function set_read_only_override( ?bool $read_only ): void {
62 self::$read_only_override = $read_only;
63 }
64
65 /**
66 * Whether the active MCP credential is limited to read-only tools. Uses
67 * the per-call override when set, else the pairing token's scope.
68 */
69 private static function is_read_only(): bool {
70 if ( null !== self::$read_only_override ) {
71 return self::$read_only_override;
72 }
73 return Mcp_Pairing::is_read_only();
74 }
75
76 /**
77 * Per-call `configure` grant. Writing credential/secret fields over MCP is
78 * gated on this and it is OFF by default — even a write-scoped connection
79 * cannot rewrite an API token or password unless it was granted the
80 * explicit `configure` scope. Null means "no per-call grant" (the pairing
81 * token / JSON-RPC path), where it falls back to a filter. (#116)
82 *
83 * @var bool|null
84 */
85 private static $configure_override = null;
86
87 /**
88 * Set whether the active credential may write secret fields (the OAuth
89 * `configure` scope). Passing null clears it back to the filter default.
90 *
91 * @param bool|null $can_configure Whether the credential carries `configure`.
92 */
93 public static function set_configure_override( ?bool $can_configure ): void {
94 self::$configure_override = $can_configure;
95 }
96
97 /**
98 * Whether the active MCP credential may write credential/secret fields.
99 * Uses the per-call override (OAuth `configure` scope) when set; otherwise
100 * the `xspeed_mcp_allow_credential_writes` filter, which defaults to false
101 * so credential writes are off by default on every connection — including
102 * the pairing token. A site owner who wants an agent to manage credentials
103 * opts in by returning true from that filter. (#116)
104 */
105 public static function can_configure(): bool {
106 if ( null !== self::$configure_override ) {
107 return self::$configure_override;
108 }
109 /**
110 * Allow MCP connections to write credential (secret) fields. Off by
111 * default; see docs/MCP-SERVER.md. Applies to pairing-token connections
112 * and any OAuth grant lacking the `configure` scope.
113 *
114 * @param bool $allow Whether credential writes over MCP are permitted.
115 */
116 return (bool) apply_filters( 'xspeed_mcp_allow_credential_writes', false );
117 }
118
119 /**
120 * Full tool catalog: name => descriptor. `handler` is a callable
121 * ( array $args ) : array|\WP_Error. `write` marks tools that mutate
122 * state (used for read-only scope enforcement).
123 *
124 * @return array<string, array{description:string, inputSchema:array, handler:callable, write:bool}>
125 */
126 public static function catalog(): array {
127 $catalog = array(
128 'get_cache_status' => array(
129 'description' => 'Get cache status for this WordPress site: whether caching is enabled, cache stats (cached pages, size, hit ratio, last purge), and the detected web server.',
130 'inputSchema' => self::object_schema( array(), array() ),
131 'write' => false,
132 'handler' => array( self::class, 'get_cache_status' ),
133 ),
134 'list_modules' => array(
135 'description' => 'List all xSpeed modules (free and Pro) with their settings schema and status.',
136 'inputSchema' => self::object_schema( array(), array() ),
137 'write' => false,
138 'handler' => array( self::class, 'list_modules' ),
139 ),
140 'get_site_info' => array(
141 'description' => 'Get facts about this site and install: whether xSpeed Pro is active and licensed, plugin/WordPress/PHP versions, and the detected web server. Use this rather than inferring the tier from the module list.',
142 'inputSchema' => self::object_schema( array(), array() ),
143 'write' => false,
144 'handler' => array( self::class, 'get_site_info' ),
145 ),
146 'run_benchmark' => array(
147 'description' => 'Run a before/after cache benchmark on the home page and return the timings. Each side reports bytes (decoded payload) and bytes_transferred (compressed wire size).',
148 'inputSchema' => self::object_schema( array(), array() ),
149 'write' => false,
150 'handler' => array( self::class, 'run_benchmark' ),
151 ),
152 'get_pro_audit' => array(
153 'description' => 'Personalized list of Pro features that would benefit THIS site, from its current settings and cache stats.',
154 'inputSchema' => self::object_schema( array(), array() ),
155 'write' => false,
156 'handler' => array( self::class, 'get_pro_audit' ),
157 ),
158 'purge_cache' => array(
159 'description' => 'Purge the site cache. "type" selects what to purge: all, page, assets, object, or rest. Defaults to all.',
160 'inputSchema' => self::object_schema(
161 array(
162 'type' => array(
163 'type' => 'string',
164 'enum' => self::PURGE_TYPES,
165 'description' => 'What to purge. Defaults to "all".',
166 ),
167 ),
168 array()
169 ),
170 'write' => true,
171 'handler' => array( self::class, 'purge_cache' ),
172 ),
173 'toggle_cache' => array(
174 'description' => 'Enable or disable page caching. Installs/removes the cache drop-in and WP_CACHE constant as needed.',
175 'inputSchema' => self::object_schema(
176 array(
177 'enabled' => array(
178 'type' => 'boolean',
179 'description' => 'true to enable caching, false to disable.',
180 ),
181 ),
182 array( 'enabled' )
183 ),
184 'write' => true,
185 'handler' => array( self::class, 'toggle_cache' ),
186 ),
187 'get_settings' => array(
188 'description' => 'Read the settings for a given xSpeed module (e.g. "minify", "gzip"). Returns schema-validated values.',
189 'inputSchema' => self::object_schema(
190 array(
191 'module' => array(
192 'type' => 'string',
193 'description' => 'The module slug, e.g. "minify".',
194 ),
195 ),
196 array( 'module' )
197 ),
198 'write' => false,
199 'handler' => array( self::class, 'get_settings' ),
200 ),
201 'update_settings' => array(
202 'description' => 'Update settings for a given xSpeed module. "values" is an object of setting keys to new values; unknown keys are stripped and invalid values rejected by the module schema.',
203 'inputSchema' => self::object_schema(
204 array(
205 'module' => array(
206 'type' => 'string',
207 'description' => 'The module slug, e.g. "minify".',
208 ),
209 'values' => array(
210 'type' => 'object',
211 'description' => 'Map of setting keys to new values.',
212 ),
213 ),
214 array( 'module', 'values' )
215 ),
216 'write' => true,
217 'handler' => array( self::class, 'update_settings' ),
218 ),
219 // --- Promoted high-value actions: dedicated typed tools so the AI
220 // calls them directly (no run_command hop). Each is a thin wrapper
221 // over Cli_Bridge, so Free tools can drive Pro actions (psi, ccss)
222 // without a cross-repo class reference, and none can drift from the
223 // CLI. ---
224 'purge_cloudflare' => array(
225 'description' => 'Purge the Cloudflare edge cache for this site (requires Cloudflare connected in the Cloudflare module).',
226 'inputSchema' => self::object_schema( array(), array() ),
227 'write' => true,
228 'handler' => array( self::class, 'purge_cloudflare' ),
229 ),
230 'scan_database' => array(
231 'description' => 'Scan the database for bloat (post revisions, auto-drafts, trashed posts, spam comments, expired transients, orphaned meta) without deleting anything.',
232 'inputSchema' => self::object_schema( array(), array() ),
233 'write' => false,
234 'handler' => array( self::class, 'scan_database' ),
235 ),
236 'clean_database' => array(
237 'description' => 'Clean database bloat. Removes the categories currently enabled in the Database module settings. Destructive — run scan_database first to preview.',
238 'inputSchema' => self::object_schema( array(), array() ),
239 'write' => true,
240 'handler' => array( self::class, 'clean_database' ),
241 ),
242 'flush_object_cache' => array(
243 'description' => 'Flush the persistent object cache (Redis / Memcached), if enabled.',
244 'inputSchema' => self::object_schema( array(), array() ),
245 'write' => true,
246 'handler' => array( self::class, 'flush_object_cache' ),
247 ),
248 'start_preloader' => array(
249 'description' => 'Start the cache preloader — crawls the sitemap to warm the page cache in the background.',
250 'inputSchema' => self::object_schema( array(), array() ),
251 'write' => true,
252 'handler' => array( self::class, 'start_preloader' ),
253 ),
254 'run_score' => array(
255 'description' => 'Run an external performance audit (PageSpeed Insights, or GTmetrix when configured) against this site and return the score plus Core Web Vitals. Available on every install. Spends the site\'s own configured API quota. Use get_score_history to read past runs without starting a new one.',
256 'inputSchema' => self::object_schema(
257 array(
258 'target' => array(
259 'type' => 'string',
260 'description' => 'URL to audit. Defaults to the configured URL, then the home page.',
261 ),
262 'strategy' => array(
263 'type' => 'string',
264 'enum' => array( 'mobile', 'desktop' ),
265 'description' => 'mobile (default) or desktop. PageSpeed Insights only.',
266 ),
267 'provider' => array(
268 'type' => 'string',
269 'description' => 'Audit provider, when the site has more than one configured.',
270 ),
271 'force' => array(
272 'type' => 'boolean',
273 'description' => 'Re-run even when a recent cached result exists. Use after a change you want measured immediately.',
274 ),
275 ),
276 array()
277 ),
278 // Classified `write`, deliberately. #147 asked whether a
279 // read-only grant should be able to call this, since a run
280 // changes no site CONFIGURATION. But it spends the site's own
281 // metered PSI/GTmetrix quota and persists a Score_Store row,
282 // and "read-only" should mean a call cannot cost the owner
283 // anything. The gap this closes is that Free had NO typed
284 // trigger at all: run_pagespeed is conditional on the Pro-only
285 // `xspeed psi` command and silently drops off tools/list here,
286 // leaving only run_command — also write, and a gateway to the
287 // entire CLI surface. A write-scoped Hub connection now gets a
288 // first-class trigger instead of the blunt instrument.
289 'write' => true,
290 'handler' => array( self::class, 'run_score' ),
291 ),
292 'run_pagespeed' => array(
293 'description' => 'Run an external performance audit (PageSpeed Insights, or GTmetrix when configured) and return the score + Core Web Vitals. Defaults to the site home page, mobile strategy. Requires external scores to be enabled in settings — the plugin makes no outbound calls otherwise.',
294 'inputSchema' => self::object_schema(
295 array(
296 'url' => array(
297 'type' => 'string',
298 'description' => 'URL to audit. Defaults to the site home page.',
299 ),
300 'strategy' => array(
301 'type' => 'string',
302 'enum' => array( 'mobile', 'desktop' ),
303 'description' => 'Audit strategy. Defaults to "mobile".',
304 ),
305 'force' => array(
306 'type' => 'boolean',
307 'description' => 'Re-run even when a recent cached result exists. Use after a change you want measured immediately.',
308 ),
309 'provider' => array(
310 'type' => 'string',
311 'description' => 'Audit provider, when the site has more than one configured.',
312 ),
313 ),
314 array()
315 ),
316 // `write`, matching run_score — the two dispatch to the same
317 // `xspeed psi` and cost the owner the same metered quota, so
318 // classifying them oppositely let a read-only grant make a real
319 // outbound audit through this one while the other refused it.
320 // Aligned toward write rather than read: generate_critical_css
321 // sets the precedent that spending an external quota is a write
322 // even when no site configuration changes. (QA B2 on #162)
323 'write' => true,
324 'handler' => array( self::class, 'run_pagespeed' ),
325 ),
326 'generate_critical_css' => array(
327 'description' => 'Generate above-the-fold Critical CSS for the site (Pro). Calls the external generator and stores the result.',
328 'inputSchema' => self::object_schema( array(), array() ),
329 'write' => true,
330 'handler' => array( self::class, 'generate_critical_css' ),
331 ),
332 'get_health' => array(
333 'description' => 'Full health diagnostics: every Health check (drop-in, WP_CACHE, server rewrite, expiry-vs-preload, Set-Cookie poisoning, conflicts), cache stats, hourly hit/miss buckets, the daily hit-ratio series, and recent activity. The single best first call when diagnosing a low hit ratio.',
334 'inputSchema' => self::object_schema( array(), array() ),
335 'write' => false,
336 'handler' => array( self::class, 'get_health' ),
337 ),
338 'get_benchmark_history' => array(
339 'description' => 'Stored benchmark runs (oldest to newest: timestamps, uncached/cached ms, savings, transfer bytes) plus recent settings-change events for correlating a change with its performance effect.',
340 'inputSchema' => self::object_schema(
341 array(
342 'limit' => array(
343 'type' => 'integer',
344 'description' => 'Max runs to return (default 100).',
345 ),
346 ),
347 array()
348 ),
349 'write' => false,
350 'handler' => array( self::class, 'get_benchmark_history' ),
351 ),
352 'get_score_history' => array(
353 'description' => 'Stored EXTERNAL audit runs (PageSpeed Insights / GTmetrix): score, Core Web Vitals (LCP/FCP/CLS/TBT/SI/TTFB), which tool ran it, and the report link where one exists. Read-only — returns what this site already measured and never starts a new audit. Use run_score to actually run one.',
354 'inputSchema' => self::object_schema(
355 array(
356 'limit' => array(
357 'type' => 'integer',
358 'description' => 'Max runs to return, newest first (default 100).',
359 ),
360 ),
361 array()
362 ),
363 'write' => false,
364 'handler' => array( self::class, 'get_score_history' ),
365 ),
366 // --- Actions promoted out of the generated `xspeed_*` aliases.
367 // Each was previously reachable ONLY as an `action` string on a
368 // coarse generated tool that was marked write regardless, so a
369 // read-only connection lost the read ones. Typed here with an
370 // honest kind so the AI stops guessing and the deny-list has one
371 // name per action. ---
372 'get_cache_inventory' => array(
373 'description' => 'Inspect what is actually in the page cache: which pages are cached and how old they are, or where the disk usage goes. Read-only.',
374 'inputSchema' => self::object_schema(
375 array(
376 'detail' => array(
377 'type' => 'string',
378 'enum' => array( 'pages', 'size' ),
379 'description' => '"pages" lists cached pages and their age; "size" breaks down disk usage. Defaults to "pages".',
380 ),
381 'limit' => array(
382 'type' => 'string',
383 'description' => 'Max rows to return (pages only).',
384 ),
385 ),
386 array()
387 ),
388 'write' => false,
389 'handler' => array( self::class, 'get_cache_inventory' ),
390 ),
391 'get_purge_log' => array(
392 'description' => 'Recent cache purges and what triggered each one. Use it to explain why a page stopped being cached. Read-only.',
393 'inputSchema' => self::object_schema(
394 array(
395 'limit' => array(
396 'type' => 'string',
397 'description' => 'Max entries to return.',
398 ),
399 ),
400 array()
401 ),
402 'write' => false,
403 'handler' => array( self::class, 'get_purge_log' ),
404 ),
405 'recheck_rewrite_rules' => array(
406 'description' => 'Re-verify the server rewrite rules that route requests to the cache, and repair them if they drifted.',
407 'inputSchema' => self::object_schema( array(), array() ),
408 'write' => true,
409 'handler' => array( self::class, 'recheck_rewrite_rules' ),
410 ),
411 'set_cloudflare_dev_mode' => array(
412 'description' => 'Turn Cloudflare development mode on or off. On bypasses the edge cache for ~3 hours so origin changes show immediately.',
413 'inputSchema' => self::object_schema(
414 array(
415 'enabled' => array(
416 'type' => 'boolean',
417 'description' => 'true turns development mode on, false turns it off.',
418 ),
419 ),
420 array( 'enabled' )
421 ),
422 'write' => true,
423 'handler' => array( self::class, 'set_cloudflare_dev_mode' ),
424 ),
425 'optimize_database' => array(
426 'description' => 'Run table optimization on the WordPress database (reclaims space after cleanup). Separate from clean_database, which deletes bloat rows.',
427 'inputSchema' => self::object_schema( array(), array() ),
428 'write' => true,
429 'handler' => array( self::class, 'optimize_database' ),
430 ),
431 'get_object_cache_status' => array(
432 'description' => 'Object cache state: whether the drop-in is installed, which backend is configured, and the server snippet needed to enable it. Read-only.',
433 'inputSchema' => self::object_schema(
434 array(
435 'detail' => array(
436 'type' => 'string',
437 'enum' => array( 'status', 'snippet' ),
438 'description' => '"status" reports the current state; "snippet" returns the server config to enable it. Defaults to "status".',
439 ),
440 ),
441 array()
442 ),
443 'write' => false,
444 'handler' => array( self::class, 'get_object_cache_status' ),
445 ),
446 'toggle_object_cache' => array(
447 'description' => 'Enable or disable the object cache drop-in. Verify the backend with test_object_cache first — enabling against an unreachable server slows every request.',
448 'inputSchema' => self::object_schema(
449 array(
450 'enabled' => array(
451 'type' => 'boolean',
452 'description' => 'true installs the drop-in, false removes it.',
453 ),
454 ),
455 array( 'enabled' )
456 ),
457 'write' => true,
458 'handler' => array( self::class, 'toggle_object_cache' ),
459 ),
460 'manage_critical_css' => array(
461 'description' => 'List the stored Critical CSS entries, or clear them so they regenerate. Use generate_critical_css to create them.',
462 'inputSchema' => self::object_schema(
463 array(
464 'action' => array(
465 'type' => 'string',
466 'enum' => array( 'list', 'clear' ),
467 'description' => '"list" returns what is stored; "clear" deletes it.',
468 ),
469 ),
470 array( 'action' )
471 ),
472 'write' => true,
473 'handler' => array( self::class, 'manage_critical_css' ),
474 ),
475 'get_preloader_status' => array(
476 'description' => 'Cache preloader progress: whether a run is active, how far through the URL list it is. Read-only.',
477 'inputSchema' => self::object_schema( array(), array() ),
478 'write' => false,
479 'handler' => array( self::class, 'get_preloader_status' ),
480 ),
481 'stop_preloader' => array(
482 'description' => 'Stop a running cache preload. Safe mid-run — already-warmed pages stay cached.',
483 'inputSchema' => self::object_schema( array(), array() ),
484 'write' => true,
485 'handler' => array( self::class, 'stop_preloader' ),
486 ),
487 'purge_url' => array(
488 'description' => 'Purge the cache for ONE URL only (all its variants: device buckets, trailing-slash forms, static-tree copy). Surgical alternative to purge_cache when a single page changed.',
489 'inputSchema' => self::object_schema(
490 array(
491 'url' => array(
492 'type' => 'string',
493 'description' => 'Absolute URL or site-relative path, e.g. "https://site.com/about/" or "/about/".',
494 ),
495 ),
496 array( 'url' )
497 ),
498 'write' => true,
499 'handler' => array( self::class, 'purge_url' ),
500 ),
501 'test_object_cache' => array(
502 'description' => 'Live connect + read/write probe of the configured Redis/Memcached backend using the saved Object Cache settings. Verifies the credentials actually work — writing settings alone does not.',
503 'inputSchema' => self::object_schema( array(), array() ),
504 'write' => false,
505 'handler' => array( self::class, 'test_object_cache' ),
506 ),
507 'cloudflare_verify' => array(
508 'description' => 'Verify the saved Cloudflare credentials against the Cloudflare API (token/zone check). Read-only — use purge_cloudflare to purge the edge.',
509 'inputSchema' => self::object_schema( array(), array() ),
510 'write' => false,
511 'handler' => array( self::class, 'cloudflare_verify' ),
512 ),
513 'list_commands' => array(
514 'description' => 'List every xSpeed command that run_command can invoke (name, description, module, options). Use this to discover the full action surface beyond the curated + dedicated tools.',
515 'inputSchema' => self::object_schema( array(), array() ),
516 'write' => false,
517 'handler' => array( self::class, 'list_commands' ),
518 ),
519 'run_command' => array(
520 'description' => 'Run any xSpeed command — the full CLI surface (~50 commands across every module: cache, cloudflare, database, critical/unused CSS, pagespeed, images, migration, preloader, object cache, analytics, RUM, smart-* and more). Call list_commands first to discover names + options. Examples: run_command("cloudflare purge"), run_command("database clean"), run_command("psi", {}, {"url":"https://site.com","strategy":"mobile"}).',
521 'inputSchema' => self::object_schema(
522 array(
523 'command' => array(
524 'type' => 'string',
525 'description' => 'Command name, e.g. "cloudflare purge" or "database scan" (the "xspeed " prefix is optional).',
526 ),
527 'args' => array(
528 'type' => 'array',
529 'description' => 'Positional arguments, if the command takes any.',
530 'items' => array( 'type' => 'string' ),
531 ),
532 'options' => array(
533 'type' => 'object',
534 'description' => 'Named options / flags, e.g. { "url": "https://site.com", "strategy": "mobile", "force": true }.',
535 ),
536 ),
537 array( 'command' )
538 ),
539 'write' => true,
540 'handler' => array( self::class, 'run_command' ),
541 ),
542 );
543
544 // Dedicated tools that wrap a command only present when a given
545 // module is active (e.g. Pro): drop them if the command isn't
546 // registered, so we never advertise a tool that always fails. The
547 // action stays reachable via run_command if the command exists.
548 $conditional = array(
549 'generate_critical_css' => 'xspeed ccss',
550 'purge_cloudflare' => 'xspeed cf',
551 'cloudflare_verify' => 'xspeed cf',
552 'flush_object_cache' => 'xspeed objcache',
553 'test_object_cache' => 'xspeed objcache',
554 'start_preloader' => 'xspeed preloader',
555 'scan_database' => 'xspeed db',
556 'clean_database' => 'xspeed db',
557 'purge_url' => 'xspeed cache',
558 'get_cache_inventory' => 'xspeed cache',
559 'get_purge_log' => 'xspeed cache',
560 'recheck_rewrite_rules' => 'xspeed cache',
561 'set_cloudflare_dev_mode' => 'xspeed cf',
562 'optimize_database' => 'xspeed db',
563 'get_object_cache_status' => 'xspeed objcache',
564 'toggle_object_cache' => 'xspeed objcache',
565 'manage_critical_css' => 'xspeed ccss',
566 'get_preloader_status' => 'xspeed preloader',
567 'stop_preloader' => 'xspeed preloader',
568 );
569
570 /*
571 * Tools that are ALWAYS in the catalog, mapped to the command they
572 * cover. These are listed separately from $conditional because the
573 * two roles are different and used to be conflated in one map: this
574 * set only tells the alias generator "don't emit an alias for this
575 * command, a typed tool already covers it" — it must never drop a
576 * tool.
577 *
578 * That conflation is exactly what broke get_settings/update_settings:
579 * they were mapped to `xspeed settings`, a command that did not exist,
580 * so the drop loop unset them on every request and they never reached
581 * tools/list. `xspeed settings` now exists (SettingsModule), but the
582 * split is what stops the class of bug recurring — an unconditional
583 * tool can no longer be removed by a command going away. (#149/#153)
584 */
585 $always = array(
586 'get_settings' => 'xspeed settings',
587 'update_settings' => 'xspeed settings',
588 'run_pagespeed' => 'xspeed psi',
589 'get_health' => 'xspeed health',
590 'get_score_history' => 'xspeed score',
591 );
592
593 $commands = Cli_Bridge::commands();
594 foreach ( $conditional as $tool => $command ) {
595 if ( ! isset( $commands[ $command ] ) ) {
596 unset( $catalog[ $tool ] );
597 }
598 }
599 // Alias generation reads both maps; the drop loop above reads only
600 // $conditional.
601 $conditional = array_merge( $conditional, $always );
602
603 /*
604 * One dedicated tool per xSpeed CLI command, generated from the same
605 * Cli_Bridge catalog the CLI registers from — so the AI can reach the
606 * long tail without the list_commands -> run_command hop, and the
607 * generated set can never drift from the CLI.
608 *
609 * Commands already covered by a typed tool above are SKIPPED. The
610 * `isset()` guard below only catches NAME collisions, and a generated
611 * name never collides — `xspeed cf` becomes `xspeed_cf`, which is not
612 * `purge_cloudflare`. So both used to ship: two tools for one action,
613 * with the generated one marked write even when it wrapped a read,
614 * and a per-tool permission on one name silently bypassable via the
615 * other. $conditional already maps every typed tool to its command;
616 * inverted, that IS the skip list.
617 */
618 foreach ( self::cli_generated_tools( array_flip( $conditional ) ) as $name => $spec ) {
619 if ( ! isset( $catalog[ $name ] ) ) {
620 $catalog[ $name ] = $spec;
621 }
622 }
623
624 return $catalog;
625 }
626
627 /**
628 * Generate one MCP tool per registered xSpeed CLI command. Each wraps
629 * Cli_Bridge::run(): the tool's `action` (the command's first positional,
630 * e.g. `verify`/`purge` for `xspeed cf`) plus any named options are passed
631 * straight through. Tool names are the command with the `xspeed ` prefix
632 * dropped and spaces -> underscores (`xspeed cf` -> `xspeed_cf`).
633 *
634 * @return array<string, array{description:string, inputSchema:array, write:bool, handler:callable}>
635 */
636 private static function cli_generated_tools( array $covered = array() ): array {
637 $tools = array();
638 foreach ( Cli_Bridge::commands() as $command => $spec ) {
639 // Already exposed as typed tools with real schemas and honest
640 // read/write kinds — generating a coarse alias too would give the
641 // AI two ways to do one thing and make a per-tool permission on
642 // the typed name bypassable via the generated one.
643 if ( isset( $covered[ $command ] ) ) {
644 continue;
645 }
646 $tool_name = self::cli_tool_name( $command );
647 if ( '' === $tool_name ) {
648 continue;
649 }
650
651 // Build the input schema from the command's synopsis: positional
652 // args become string properties (the first is usually the action,
653 // exposed with its allowed values as an enum); assoc args become
654 // named options.
655 $properties = array();
656 $required = array();
657 foreach ( $spec['synopsis'] as $arg ) {
658 if ( ! isset( $arg['name'] ) ) {
659 continue;
660 }
661 $arg_name = (string) $arg['name'];
662 $prop = array(
663 'type' => 'string',
664 'description' => isset( $arg['description'] ) ? (string) $arg['description'] : '',
665 );
666 if ( isset( $arg['options'] ) && is_array( $arg['options'] ) && ! empty( $arg['options'] ) ) {
667 $prop['enum'] = array_values( array_map( 'strval', $arg['options'] ) );
668 }
669 $properties[ $arg_name ] = $prop;
670 $is_optional = ! empty( $arg['optional'] );
671 $is_flag = isset( $arg['type'] ) && 'flag' === $arg['type'];
672 if ( ! $is_optional && ! $is_flag ) {
673 $required[] = $arg_name;
674 }
675 }
676
677 $description = '' !== $spec['shortdesc']
678 ? $spec['shortdesc']
679 : sprintf( 'Run the "%s" xSpeed command.', $command );
680
681 list( $write, $write_actions, $read_actions ) = self::cli_write_profile( $command, $spec['synopsis'] );
682
683 $tools[ $tool_name ] = array(
684 'description' => $description,
685 'inputSchema' => self::object_schema( $properties, $required ),
686 'write' => $write,
687 // The action values that mutate state. When set, read-only
688 // enforcement is per-ACTION (a read-only grant may still call
689 // the tool with a read action like "status"/"scan").
690 'write_actions' => $write_actions,
691 // The complement — actions positively classified as reads.
692 // action_writes() allowlists against THIS rather than negating
693 // write_actions, so an action added to a command later is
694 // refused under a read-only grant until it has been
695 // classified, instead of silently becoming callable.
696 'read_actions' => $read_actions,
697 'handler' => self::cli_handler_for( $command, $spec['synopsis'] ),
698 );
699 }
700 return $tools;
701 }
702
703 /** Derive an MCP tool name from a CLI command ("xspeed cf" -> "xspeed_cf"). */
704 private static function cli_tool_name( string $command ): string {
705 $command = trim( preg_replace( '/\s+/', ' ', $command ) ?? '' );
706 if ( '' === $command ) {
707 return '';
708 }
709 return str_replace( ' ', '_', $command );
710 }
711
712 /** Action verbs that only inspect state (never mutate). */
713 private const CLI_READ_VERBS = array( 'status', 'scan', 'list', 'verify', 'get', 'show', 'info', 'export', 'preview', 'check', 'snippet', 'test' );
714
715 /** Commands with NO action enum that are nonetheless pure inspection. */
716 private const CLI_READ_ONLY_COMMANDS = array( 'xspeed health', 'xspeed support' );
717
718 /**
719 * Compute the write profile for a generated command tool:
720 * [ $write_bool, $write_actions ]
721 * where $write_actions is the list of action values that mutate state
722 * (empty when the tool has no action enum). $write_bool is the tool-level
723 * flag: true if ANY action writes (so read-only clients see it flagged),
724 * but per-action enforcement in invoke() still lets a read-only grant run
725 * the tool's read actions (e.g. `minify status` while `minify purge` is
726 * refused).
727 *
728 * @param string $command Full command name.
729 * @param array $synopsis Command synopsis.
730 * @return array{0:bool,1:string[],2:string[]} write flag, write actions, read actions
731 */
732 /**
733 * Does THIS call mutate state, given the action the caller submitted?
734 *
735 * The tool-level `write` flag is true when ANY of a command's actions
736 * write, so read-only clients can see the tool is capable of mutating.
737 * Enforcing on that flag alone refuses the whole tool — which is how a
738 * read-only grant lost the ability to run `xspeed_minify status` even
739 * though only `purge` writes. `write_actions` records exactly which
740 * action values mutate; this is what reads it.
741 *
742 * Fails CLOSED in every ambiguous case. An action that isn't in the
743 * schema, an absent action, or a tool with no per-action profile all fall
744 * back to the coarse flag and are refused. A read-only grant may end up
745 * with less access than strictly necessary; it must never end up with
746 * more.
747 *
748 * @param array $tool The catalog entry.
749 * @param array $args The submitted arguments.
750 */
751 private static function action_writes( array $tool, array $args ): bool {
752 $write_actions = isset( $tool['write_actions'] ) && is_array( $tool['write_actions'] )
753 ? $tool['write_actions']
754 : array();
755
756 // No per-action profile — the coarse flag is all we have.
757 if ( empty( $write_actions ) ) {
758 return true;
759 }
760
761 $action = isset( $args['action'] ) && is_scalar( $args['action'] )
762 ? strtolower( trim( (string) $args['action'] ) )
763 : '';
764
765 // No action supplied: the command's own default is unknown here, so
766 // treat it as a write rather than guessing.
767 if ( '' === $action ) {
768 return true;
769 }
770
771 // Only an action we positively recognise as read is allowed through.
772 // Anything unknown is refused, so a future action added to a command
773 // can't silently become callable under a read-only grant before it has
774 // been classified.
775 $known = array_map(
776 static function ( $a ) {
777 return strtolower( trim( (string) $a ) );
778 },
779 isset( $tool['read_actions'] ) && is_array( $tool['read_actions'] ) ? $tool['read_actions'] : array()
780 );
781
782 return ! in_array( $action, $known, true );
783 }
784
785 private static function cli_write_profile( string $command, array $synopsis ): array {
786 // Command with an action enum → classify each action.
787 foreach ( $synopsis as $arg ) {
788 if ( isset( $arg['type'], $arg['options'] ) && 'positional' === $arg['type'] && is_array( $arg['options'] ) ) {
789 $write_actions = array();
790 $read_actions = array();
791 foreach ( $arg['options'] as $opt ) {
792 if ( in_array( strtolower( (string) $opt ), self::CLI_READ_VERBS, true ) ) {
793 $read_actions[] = (string) $opt;
794 } else {
795 $write_actions[] = (string) $opt;
796 }
797 }
798 return array( ! empty( $write_actions ), $write_actions, $read_actions );
799 }
800 }
801
802 // No action enum: a small allow-list of pure-inspection commands is
803 // read-only; everything else defaults to write (safe — a read-only
804 // grant never mutates).
805 $is_read = in_array( trim( $command ), self::CLI_READ_ONLY_COMMANDS, true );
806 return array( ! $is_read, array(), array() );
807 }
808
809 /**
810 * Build the handler for a generated command tool. It maps the tool's
811 * arguments back to Cli_Bridge::run(): positional synopsis args (in order)
812 * become $args; everything else is passed as named options.
813 *
814 * @param string $command Full command name.
815 * @param array $synopsis Command synopsis.
816 * @return callable
817 */
818 private static function cli_handler_for( string $command, array $synopsis ): callable {
819 // Names of the positional args, in declared order.
820 $positionals = array();
821 foreach ( $synopsis as $arg ) {
822 if ( isset( $arg['name'] ) && ( ! isset( $arg['type'] ) || 'positional' === $arg['type'] ) ) {
823 $positionals[] = (string) $arg['name'];
824 }
825 }
826
827 return static function ( array $tool_args ) use ( $command, $positionals ) {
828 $args = array();
829 $assoc = $tool_args;
830 // Pull positionals out (in order) into $args; the rest are options.
831 foreach ( $positionals as $pname ) {
832 if ( array_key_exists( $pname, $assoc ) && '' !== (string) $assoc[ $pname ] ) {
833 $args[] = (string) $assoc[ $pname ];
834 }
835 unset( $assoc[ $pname ] );
836 }
837 return Cli_Bridge::run( $command, $args, $assoc );
838 };
839 }
840
841 /**
842 * The tool list in MCP `tools/list` shape.
843 *
844 * @return array<int, array{name:string, description:string, inputSchema:array}>
845 */
846 public static function list(): array {
847 $out = array();
848 foreach ( self::catalog() as $name => $spec ) {
849 $out[] = array(
850 'name' => $name,
851 'description' => $spec['description'],
852 'inputSchema' => $spec['inputSchema'],
853 );
854 }
855 return $out;
856 }
857
858 /**
859 * Invoke a tool by name with decoded arguments.
860 *
861 * @param string $name Tool name.
862 * @param array $args Decoded arguments.
863 * @return array|\WP_Error Result payload or error.
864 */
865 public static function invoke( string $name, array $args ) {
866 $catalog = self::catalog();
867 if ( ! isset( $catalog[ $name ] ) ) {
868 $error = new \WP_Error(
869 'xspeed_mcp_unknown_tool',
870 sprintf(
871 /* translators: %s: tool name. */
872 __( 'Unknown tool: %s', 'xspeed' ),
873 $name
874 ),
875 array( 'status' => 404 )
876 );
877
878 // A call for a tool that doesn't exist is still something that
879 // happened to this site, and a run of them is the shape of a
880 // probe. Recording it is the difference between a trail that
881 // shows what was ATTEMPTED and one that only shows what
882 // succeeded. Scope is unknowable here, so log the conservative
883 // one rather than implying the attempt was read-only.
884 Mcp_Activity_Log::record( $name, $args, false, $error->get_error_message(), 'write', self::$channel );
885
886 return $error;
887 }
888
889 // Scope enforcement: a read-only connection cannot invoke a tool that
890 // mutates state. run_command is a gateway to the full CLI surface, so
891 // it's treated as write regardless of the wrapped command. The active
892 // credential's scope (pairing token OR OAuth access token) is carried
893 // in self::$scope_override; it falls back to the pairing global for
894 // callers that don't set a per-call scope.
895 if ( ! empty( $catalog[ $name ]['write'] ) && self::is_read_only() && self::action_writes( $catalog[ $name ], $args ) ) {
896 return new \WP_Error(
897 'xspeed_mcp_read_only',
898 sprintf(
899 /* translators: %s: tool name. */
900 __( 'This MCP connection is read-only; the "%s" tool changes state and is not permitted. Reconnect with write access to use it.', 'xspeed' ),
901 $name
902 ),
903 array( 'status' => 403 )
904 );
905 }
906
907 self::$dispatching = true;
908 try {
909 $result = call_user_func( $catalog[ $name ]['handler'], $args );
910
911 // Audit every dispatched call — this is the record the admin
912 // reads to answer "what did the assistant do to my site?".
913 // Recorded here (not per-handler) so a new tool is covered the
914 // moment it joins the catalog.
915 [ $ok, $error ] = self::outcome( $result );
916
917 $scope = empty( $catalog[ $name ]['write'] ) ? 'read' : 'write';
918
919 Mcp_Activity_Log::record( $name, $args, $ok, $error, $scope, self::$channel );
920
921 return $result;
922 } finally {
923 self::$dispatching = false;
924 }
925 }
926
927 /**
928 * Read success/failure out of a handler result.
929 *
930 * Two failure shapes reach here. A handler that validates its own
931 * input returns WP_Error. A handler that delegates to Cli_Bridge gets
932 * back an ARRAY carrying `ok => false` plus `error`, because a
933 * `WP_CLI::error()` inside the shim is a controlled failure rather
934 * than an exception. Reading only the first shape logged every failed
935 * command — a refused purge, a Cloudflare call with no credentials —
936 * as a success.
937 *
938 * @param mixed $result Handler return value.
939 * @return array{0:bool,1:string}
940 */
941 private static function outcome( $result ): array {
942 if ( is_wp_error( $result ) ) {
943 return array( false, $result->get_error_message() );
944 }
945
946 if ( is_array( $result ) && array_key_exists( 'ok', $result ) && ! $result['ok'] ) {
947 $error = isset( $result['error'] ) ? (string) $result['error'] : '';
948 return array( false, '' === $error ? 'Command reported failure.' : $error );
949 }
950
951 return array( true, '' );
952 }
953
954 /** @var string Transport that carried the current call (for the audit log). */
955 private static $channel = 'mcp';
956
957 /**
958 * Name the transport for subsequent invokes — the JSON-RPC endpoint and
959 * the hosted-broker REST routes share this catalog, and the audit trail
960 * should say which one a call arrived on.
961 */
962 public static function set_channel( string $channel ): void {
963 self::$channel = '' === $channel ? 'mcp' : $channel;
964 }
965
966 /** @var bool True while an MCP tool handler is executing. */
967 private static $dispatching = false;
968
969 /**
970 * True while a tool call is being dispatched — lets deeper layers
971 * (e.g. the settings change-log) attribute a mutation to MCP.
972 */
973 public static function in_dispatch(): bool {
974 return self::$dispatching;
975 }
976
977 /*
978 * Handlers — thin proxies to the Free engine. Each takes decoded tool
979 * arguments and returns an array payload (or WP_Error on bad input).
980 */
981
982 /**
983 * Cache status, stats, and detected server.
984 *
985 * @param array $args Unused.
986 * @return array
987 */
988 public static function get_cache_status( array $args ) {
989 unset( $args );
990 $opts = Settings::get();
991 return array(
992 'cache_enabled' => (bool) ( $opts['cache_enabled'] ?? false ),
993 'stats' => Cache::get_stats(),
994 'server' => Server::type(),
995 );
996 }
997
998 /**
999 * Facts about this site and install, stated explicitly.
1000 *
1001 * The Hub's fleet dashboard needed two things no tool reported directly.
1002 * It had been INFERRING Pro's presence from `list_modules` — "any entry
1003 * with tier: pro" — which works only because the registry returns just
1004 * available modules. That is an inference riding an implementation
1005 * detail, and it breaks the day a Pro install registers zero Pro modules.
1006 *
1007 * `pro_active` is "the Pro plugin is loaded and API-compatible";
1008 * `licensed` is a separate question, since Pro can be active but
1009 * unlicensed (its modules then boot but their settings are locked). Both
1010 * are reported so a consumer never has to guess which one it wanted.
1011 * (#146)
1012 *
1013 * @param array $args Unused.
1014 * @return array
1015 */
1016 /**
1017 * Is Pro licensed right now?
1018 *
1019 * Resolved through the `xspeed_module_descriptor` filter — the one Pro
1020 * actually registers — by running a minimal Pro descriptor through it and
1021 * reading back the `locked` flag Pro sets when the licence is inactive.
1022 *
1023 * `license` is deliberately not used as the probe slug: Pro exempts that
1024 * module from locking so an expired site can still reach the screen where
1025 * a new key is entered, so it would always come back unlocked.
1026 */
1027 private static function pro_licensed(): bool {
1028 $probe = apply_filters(
1029 'xspeed_module_descriptor',
1030 array(
1031 'slug' => '__license_probe__',
1032 'tier' => 'pro',
1033 ),
1034 null
1035 );
1036
1037 return empty( $probe['locked'] );
1038 }
1039
1040 public static function get_site_info( array $args ) {
1041 unset( $args );
1042
1043 $pro_active = Tier_Registry::pro_active();
1044
1045 return array(
1046 'pro_active' => $pro_active,
1047 'pro_version' => defined( 'XSPEED_PRO_VERSION' ) ? (string) constant( 'XSPEED_PRO_VERSION' ) : null,
1048 // Distinct from pro_active: Pro can be installed and running
1049 // while its license is expired or absent.
1050 //
1051 // NOT `apply_filters( 'xspeed_pro_licensed', true )`. That hook is
1052 // only ever APPLIED by Pro as an override point — no released
1053 // version registers it — so with nothing listening the `true`
1054 // default stood and this reported `licensed: true` on a fully
1055 // revoked licence: the exact misreport the tool exists to
1056 // eliminate. (QA blocker on #158)
1057 //
1058 // Ask the question the dashboard asks instead. Pro DOES register
1059 // `xspeed_module_descriptor` and stamps `locked => 'license'` on
1060 // every Pro entry when the licence is inactive, so reading that
1061 // back is a real signal, and it cannot drift from what the panel
1062 // shows because it IS what the panel shows.
1063 'licensed' => $pro_active ? self::pro_licensed() : false,
1064 'plugin_version' => defined( 'XSPEED_VERSION' ) ? (string) constant( 'XSPEED_VERSION' ) : null,
1065 'wp_version' => get_bloginfo( 'version' ),
1066 'php_version' => PHP_VERSION,
1067 'server' => Server::type(),
1068 'multisite' => is_multisite(),
1069 );
1070 }
1071
1072 /**
1073 * All registered module descriptors.
1074 *
1075 * @param array $args Unused.
1076 * @return array
1077 */
1078 public static function list_modules( array $args ) {
1079 unset( $args );
1080 return Admin::modules_payload();
1081 }
1082
1083 /**
1084 * Before/after cache benchmark timings.
1085 *
1086 * @param array $args Unused.
1087 * @return array
1088 */
1089 public static function run_benchmark( array $args ) {
1090 unset( $args );
1091 return Cache_Benchmark::run();
1092 }
1093
1094 /**
1095 * Personalized Pro-feature suggestions for this site.
1096 *
1097 * @param array $args Unused.
1098 * @return array
1099 */
1100 public static function get_pro_audit( array $args ) {
1101 unset( $args );
1102 return array( 'suggestions' => Pro_Audit::run() );
1103 }
1104
1105 /**
1106 * Purge the cache by type.
1107 *
1108 * @param array $args { type?:string } — one of PURGE_TYPES; default all.
1109 * @return array|\WP_Error
1110 */
1111 public static function purge_cache( array $args ) {
1112 $type = isset( $args['type'] ) ? (string) $args['type'] : 'all';
1113 if ( '' === $type ) {
1114 $type = 'all';
1115 }
1116 if ( ! in_array( $type, self::PURGE_TYPES, true ) ) {
1117 return new \WP_Error(
1118 'xspeed_mcp_bad_type',
1119 sprintf(
1120 /* translators: %s: comma-separated list of valid purge types. */
1121 __( 'Invalid purge type. Expected one of: %s', 'xspeed' ),
1122 implode( ', ', self::PURGE_TYPES )
1123 ),
1124 array( 'status' => 400 )
1125 );
1126 }
1127 // Named source, not the default "manual": the purge log's whole job
1128 // is to let an admin see that the cache cleared because an assistant
1129 // asked, not because someone clicked.
1130 $count = Cache::purge_type( $type, __( 'AI assistant', 'xspeed' ) );
1131 return array(
1132 'purged' => $type,
1133 'count' => $count,
1134 'stats' => Cache::get_stats(),
1135 );
1136 }
1137
1138 /**
1139 * Enable or disable page caching.
1140 *
1141 * @param array $args { enabled:bool }.
1142 * @return array|\WP_Error
1143 */
1144 public static function toggle_cache( array $args ) {
1145 if ( ! array_key_exists( 'enabled', $args ) ) {
1146 return new \WP_Error(
1147 'xspeed_mcp_missing_enabled',
1148 __( 'The "enabled" parameter is required (true or false).', 'xspeed' ),
1149 array( 'status' => 400 )
1150 );
1151 }
1152 $enabled = rest_sanitize_boolean( $args['enabled'] );
1153 $install = Cache::toggle( $enabled );
1154
1155 // Persist cache_enabled the same way the Free /cache/toggle route
1156 // does (class-rest-api.php:235) — Cache::toggle handles the drop-in
1157 // + wp-config; Settings owns the option flag.
1158 Settings::update( array( 'cache_enabled' => $enabled ) );
1159
1160 return array(
1161 'cache_enabled' => $enabled,
1162 'install_state' => $install,
1163 'stats' => Cache::get_stats(),
1164 );
1165 }
1166
1167 /**
1168 * Is this module reachable over MCP right now?
1169 *
1170 * Mirrors SettingsModule::module_reachable(). Registration is not
1171 * enough: Module_Registry::available() only asks whether Pro is LOADED,
1172 * not whether it is LICENSED, so an unlicensed Pro site had every Pro
1173 * module readable and writable over MCP while the dashboard showed it
1174 * locked — reachable by any agent holding a write token. (QA M2)
1175 *
1176 * The licence answer comes through the `xspeed_module_descriptor` filter
1177 * Pro registers, so Free never names a Pro class. (NOT
1178 * `xspeed_pro_licensed` — Pro only ever APPLIES that one as an override
1179 * and nothing listens to it, so gating on it silently passed everything.)
1180 * `license` is exempt for the same reason Pro exempts it: locking it
1181 * would remove the only surface that can fix an expired licence.
1182 */
1183 private static function settings_module_reachable( string $slug ): bool {
1184 $module = \XSpeed\Module_Registry::available()[ $slug ] ?? null;
1185 if ( ! $module ) {
1186 return false;
1187 }
1188 if ( \XSpeed\Module::TIER_PRO !== $module->tier() || 'license' === $slug ) {
1189 return true;
1190 }
1191
1192 // Ask the SAME question the dashboard asks. `xspeed_pro_licensed` is
1193 // only ever APPLIED by Pro as an override hook — nothing registers it
1194 // — so calling it here returned the default `true` and gated nothing.
1195 // Pro DOES register `xspeed_module_descriptor`, and sets
1196 // `locked => 'license'` on every Pro entry when the licence is
1197 // inactive. Reusing that keeps one definition of "locked" instead of
1198 // a second one in Free that can drift from the panel. (QA M2)
1199 $entry = apply_filters(
1200 'xspeed_module_descriptor',
1201 array(
1202 'slug' => $slug,
1203 'tier' => $module->tier(),
1204 ),
1205 $module
1206 );
1207
1208 return empty( $entry['locked'] );
1209 }
1210
1211 /**
1212 * Read a module's schema-validated settings.
1213 *
1214 * @param array $args { module:string }.
1215 * @return array|\WP_Error
1216 */
1217 public static function get_settings( array $args ) {
1218 $module = isset( $args['module'] ) ? (string) $args['module'] : '';
1219 if ( '' === $module ) {
1220 return new \WP_Error(
1221 'xspeed_mcp_missing_module',
1222 __( 'The "module" parameter is required.', 'xspeed' ),
1223 array( 'status' => 400 )
1224 );
1225 }
1226 if ( ! self::settings_module_reachable( $module ) ) {
1227 return new \WP_Error(
1228 'xspeed_mcp_unknown_module',
1229 sprintf(
1230 /* translators: %s: module slug. */
1231 __( 'Unknown module "%s".', 'xspeed' ),
1232 $module
1233 ),
1234 array( 'status' => 404 )
1235 );
1236 }
1237 return array(
1238 'module' => $module,
1239 // Public view — secret fields masked. An MCP agent must never be able
1240 // to read stored credentials back in plaintext. (#115)
1241 'settings' => Settings_Manager::get_public( $module ),
1242 );
1243 }
1244
1245 /**
1246 * Update a module's settings (schema-validated).
1247 *
1248 * @param array $args { module:string, values:array }.
1249 * @return array|\WP_Error
1250 */
1251 public static function update_settings( array $args ) {
1252 $module = isset( $args['module'] ) ? (string) $args['module'] : '';
1253 $values = $args['values'] ?? null;
1254 if ( '' === $module ) {
1255 return new \WP_Error(
1256 'xspeed_mcp_missing_module',
1257 __( 'The "module" parameter is required.', 'xspeed' ),
1258 array( 'status' => 400 )
1259 );
1260 }
1261 if ( ! is_array( $values ) ) {
1262 return new \WP_Error(
1263 'xspeed_mcp_bad_values',
1264 __( 'The "values" parameter must be an object of setting keys.', 'xspeed' ),
1265 array( 'status' => 400 )
1266 );
1267 }
1268 if ( ! self::settings_module_reachable( $module ) ) {
1269 return new \WP_Error(
1270 'xspeed_mcp_unknown_module',
1271 sprintf(
1272 /* translators: %s: module slug. */
1273 __( 'Unknown module "%s".', 'xspeed' ),
1274 $module
1275 ),
1276 array( 'status' => 404 )
1277 );
1278 }
1279 // Writing credentials over MCP requires the explicit `configure` grant —
1280 // off by default even for a write-scoped connection — so an agent can't
1281 // silently repoint the Cloudflare/object-cache backend at an attacker
1282 // endpoint. Refuse with a message naming exactly which fields need it.
1283 // (Settings_Manager::update also strips these as a backstop covering the
1284 // run_command → CLI path.) (#116)
1285 if ( ! self::can_configure() ) {
1286 $secret_fields = Settings_Manager::secret_keys_in( $module, $values );
1287 if ( ! empty( $secret_fields ) ) {
1288 return new \WP_Error(
1289 'xspeed_mcp_configure_required',
1290 sprintf(
1291 /* translators: 1: comma-separated field names, 2: module slug. */
1292 __( 'Writing credential fields (%1$s) on "%2$s" needs the "configure" scope, which is off by default. Reconnect the MCP client granting the configure scope, or set these credentials from the xSpeed dashboard.', 'xspeed' ),
1293 implode( ', ', $secret_fields ),
1294 $module
1295 ),
1296 array(
1297 'status' => 403,
1298 'refused_fields' => $secret_fields,
1299 )
1300 );
1301 }
1302 }
1303 return array(
1304 'module' => $module,
1305 // Return value is already masked (Settings_Manager::update returns the
1306 // public view), so a written secret isn't echoed back either. (#115)
1307 'settings' => Settings_Manager::update( $module, $values ),
1308 );
1309 }
1310
1311 /**
1312 * List every command run_command can invoke (the full CLI surface).
1313 *
1314 * @param array $args Unused.
1315 * @return array
1316 */
1317 public static function list_commands( array $args ) {
1318 unset( $args );
1319 return array( 'commands' => Cli_Bridge::catalog() );
1320 }
1321
1322 /**
1323 * Run any registered xSpeed command via the CLI bridge.
1324 *
1325 * @param array $args { command:string, args?:array, options?:array }.
1326 * @return array|\WP_Error
1327 */
1328 public static function run_command( array $args ) {
1329 $command = isset( $args['command'] ) ? (string) $args['command'] : '';
1330 if ( '' === $command ) {
1331 return new \WP_Error(
1332 'xspeed_mcp_missing_command',
1333 __( 'The "command" parameter is required.', 'xspeed' ),
1334 array( 'status' => 400 )
1335 );
1336 }
1337 $positional = isset( $args['args'] ) && is_array( $args['args'] ) ? $args['args'] : array();
1338 $options = isset( $args['options'] ) && is_array( $args['options'] ) ? $args['options'] : array();
1339 return Cli_Bridge::run( $command, $positional, $options );
1340 }
1341
1342 /* --------------------------------------------------------------------- */
1343 /* Promoted action handlers — typed wrappers over Cli_Bridge. */
1344 /* Delegating to the bridge lets a Free tool drive a Pro action (psi, */
1345 /* ccss) with no cross-repo class reference, and keeps zero drift. */
1346 /* --------------------------------------------------------------------- */
1347
1348 /**
1349 * Purge the Cloudflare edge cache.
1350 *
1351 * @param array $args Unused.
1352 * @return array|\WP_Error
1353 */
1354 public static function purge_cloudflare( array $args ) {
1355 unset( $args );
1356 return Cli_Bridge::run( 'cf', array( 'purge' ) );
1357 }
1358
1359 /**
1360 * Scan the database for bloat (no deletion).
1361 *
1362 * @param array $args Unused.
1363 * @return array|\WP_Error
1364 */
1365 public static function scan_database( array $args ) {
1366 unset( $args );
1367 return Cli_Bridge::run( 'db', array( 'scan' ) );
1368 }
1369
1370 /**
1371 * Clean database bloat (destructive).
1372 *
1373 * @param array $args Unused.
1374 * @return array|\WP_Error
1375 */
1376 public static function clean_database( array $args ) {
1377 unset( $args );
1378 return Cli_Bridge::run( 'db', array( 'clean' ) );
1379 }
1380
1381 /**
1382 * Flush the persistent object cache.
1383 *
1384 * @param array $args Unused.
1385 * @return array|\WP_Error
1386 */
1387 public static function flush_object_cache( array $args ) {
1388 unset( $args );
1389 return Cli_Bridge::run( 'objcache', array( 'flush' ) );
1390 }
1391
1392 /**
1393 * Start the cache preloader.
1394 *
1395 * @param array $args Unused.
1396 * @return array|\WP_Error
1397 */
1398 public static function start_preloader( array $args ) {
1399 unset( $args );
1400 return Cli_Bridge::run( 'preloader', array( 'start' ) );
1401 }
1402
1403 /**
1404 * Full health diagnostics (checks + stats + buckets + activity).
1405 * Direct typed payload — same tier as get_cache_status — so the agent
1406 * gets structured tones/ids instead of parsing CLI log lines.
1407 *
1408 * @param array $args Unused.
1409 * @return array
1410 */
1411 public static function get_health( array $args ) {
1412 unset( $args );
1413 return array(
1414 'checks' => \XSpeed\Health::checks(),
1415 'stats' => Cache::get_stats(),
1416 'buckets' => \XSpeed\Hit_Counter::buckets(),
1417 'hit_daily' => \XSpeed\Hit_Counter::daily_series( 30 ),
1418 'activity' => \XSpeed\Activity_Log::entries(),
1419 );
1420 }
1421
1422 /**
1423 * Stored benchmark runs + settings-change events (trend data).
1424 *
1425 * @param array $args { limit?:int }.
1426 * @return array
1427 */
1428 public static function get_benchmark_history( array $args ) {
1429 $limit = isset( $args['limit'] ) ? max( 1, min( 100, (int) $args['limit'] ) ) : 100;
1430 $changes = array();
1431 foreach ( \XSpeed\Activity_Log::entries() as $entry ) {
1432 if ( 'settings_changed' === ( $entry['type'] ?? '' ) ) {
1433 $changes[] = array(
1434 'ts' => (int) $entry['ts'],
1435 'message' => (string) $entry['message'],
1436 );
1437 }
1438 }
1439 return array(
1440 'runs' => Cache_Benchmark::history( $limit ),
1441 'changes' => $changes,
1442 );
1443 }
1444
1445 /**
1446 * Purge a single URL's cache entries.
1447 *
1448 * @param array $args { url:string }.
1449 * @return array|\WP_Error
1450 */
1451 /**
1452 * Inspect what is in the page cache (pages + age, or size breakdown).
1453 *
1454 * @param array $args detail: pages|size, limit.
1455 * @return array|\WP_Error
1456 */
1457 public static function get_cache_inventory( array $args ) {
1458 $detail = isset( $args['detail'] ) ? (string) $args['detail'] : 'pages';
1459 $action = 'size' === $detail ? 'size' : 'inventory';
1460 $assoc = array();
1461 if ( isset( $args['limit'] ) && '' !== $args['limit'] ) {
1462 $assoc['limit'] = (string) $args['limit'];
1463 }
1464 return Cli_Bridge::run( 'cache', array( $action ), $assoc );
1465 }
1466
1467 /**
1468 * Recent cache purges and their causes.
1469 *
1470 * @param array $args limit.
1471 * @return array|\WP_Error
1472 */
1473 public static function get_purge_log( array $args ) {
1474 $assoc = array();
1475 if ( isset( $args['limit'] ) && '' !== $args['limit'] ) {
1476 $assoc['limit'] = (string) $args['limit'];
1477 }
1478 return Cli_Bridge::run( 'cache', array( 'purge-log' ), $assoc );
1479 }
1480
1481 /**
1482 * Re-verify (and repair) the server rewrite rules.
1483 *
1484 * @param array $args Unused.
1485 * @return array|\WP_Error
1486 */
1487 public static function recheck_rewrite_rules( array $args ) {
1488 unset( $args );
1489 return Cli_Bridge::run( 'cache', array( 'recheck-rewrite' ) );
1490 }
1491
1492 /**
1493 * Turn Cloudflare development mode on or off.
1494 *
1495 * A boolean rather than two tools: dev-on and dev-off are one decision,
1496 * and offering them separately doubles the surface for no gain.
1497 *
1498 * @param array $args enabled (bool, required).
1499 * @return array|\WP_Error
1500 */
1501 public static function set_cloudflare_dev_mode( array $args ) {
1502 if ( ! array_key_exists( 'enabled', $args ) ) {
1503 return new \WP_Error( 'xspeed_mcp_missing_enabled', __( 'The enabled argument is required.', 'xspeed' ), array( 'status' => 400 ) );
1504 }
1505 $on = filter_var( $args['enabled'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
1506 if ( null === $on ) {
1507 return new \WP_Error( 'xspeed_mcp_invalid_enabled', __( 'The enabled argument must be true or false.', 'xspeed' ), array( 'status' => 400 ) );
1508 }
1509 return Cli_Bridge::run( 'cf', array( $on ? 'dev-on' : 'dev-off' ) );
1510 }
1511
1512 /**
1513 * Optimize database tables (distinct from clean_database, which deletes).
1514 *
1515 * @param array $args Unused.
1516 * @return array|\WP_Error
1517 */
1518 public static function optimize_database( array $args ) {
1519 unset( $args );
1520 return Cli_Bridge::run( 'db', array( 'optimize' ) );
1521 }
1522
1523 /**
1524 * Object cache state, or the server snippet that enables it.
1525 *
1526 * @param array $args detail: status|snippet.
1527 * @return array|\WP_Error
1528 */
1529 public static function get_object_cache_status( array $args ) {
1530 $detail = isset( $args['detail'] ) ? (string) $args['detail'] : 'status';
1531 $action = 'snippet' === $detail ? 'snippet' : 'status';
1532 return Cli_Bridge::run( 'objcache', array( $action ) );
1533 }
1534
1535 /**
1536 * Install or remove the object-cache drop-in.
1537 *
1538 * @param array $args enabled (bool, required).
1539 * @return array|\WP_Error
1540 */
1541 public static function toggle_object_cache( array $args ) {
1542 if ( ! array_key_exists( 'enabled', $args ) ) {
1543 return new \WP_Error( 'xspeed_mcp_missing_enabled', __( 'The enabled argument is required.', 'xspeed' ), array( 'status' => 400 ) );
1544 }
1545 $on = filter_var( $args['enabled'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
1546 if ( null === $on ) {
1547 return new \WP_Error( 'xspeed_mcp_invalid_enabled', __( 'The enabled argument must be true or false.', 'xspeed' ), array( 'status' => 400 ) );
1548 }
1549 return Cli_Bridge::run( 'objcache', array( $on ? 'enable' : 'disable' ) );
1550 }
1551
1552 /**
1553 * List or clear stored Critical CSS.
1554 *
1555 * @param array $args action: list|clear.
1556 * @return array|\WP_Error
1557 */
1558 public static function manage_critical_css( array $args ) {
1559 $action = isset( $args['action'] ) ? (string) $args['action'] : '';
1560 if ( ! in_array( $action, array( 'list', 'clear' ), true ) ) {
1561 return new \WP_Error( 'xspeed_mcp_invalid_action', __( 'The action argument must be "list" or "clear".', 'xspeed' ), array( 'status' => 400 ) );
1562 }
1563 return Cli_Bridge::run( 'ccss', array( $action ) );
1564 }
1565
1566 /**
1567 * Preloader progress.
1568 *
1569 * @param array $args Unused.
1570 * @return array|\WP_Error
1571 */
1572 public static function get_preloader_status( array $args ) {
1573 unset( $args );
1574 return Cli_Bridge::run( 'preloader', array( 'status' ) );
1575 }
1576
1577 /**
1578 * Stop a running preload.
1579 *
1580 * @param array $args Unused.
1581 * @return array|\WP_Error
1582 */
1583 public static function stop_preloader( array $args ) {
1584 unset( $args );
1585 return Cli_Bridge::run( 'preloader', array( 'stop' ) );
1586 }
1587
1588 /**
1589 * Stored external audit runs (PSI / GTmetrix).
1590 *
1591 * Read-only by construction: it reads the option Score already wrote. No
1592 * outbound call is made, which is what lets the Hub poll this on a
1593 * schedule without spending the site owner's PSI or GTmetrix quota.
1594 *
1595 * @param array $args limit.
1596 * @return array|\WP_Error
1597 */
1598 public static function get_score_history( array $args ) {
1599 if ( ! class_exists( '\\XSpeed\\Score' ) ) {
1600 return new \WP_Error( 'xspeed_mcp_no_score', __( 'External scores are not available on this site.', 'xspeed' ), array( 'status' => 404 ) );
1601 }
1602
1603 $limit = isset( $args['limit'] ) ? (int) $args['limit'] : 100;
1604 $limit = max( 1, min( 500, $limit ) );
1605
1606 $runs = array();
1607 foreach ( array_slice( \XSpeed\Score::history(), 0, $limit ) as $run ) {
1608 if ( ! is_array( $run ) ) {
1609 continue;
1610 }
1611 $metrics = isset( $run['metrics'] ) && is_array( $run['metrics'] ) ? $run['metrics'] : array();
1612 $runs[] = array(
1613 'provider' => isset( $run['provider'] ) ? (string) $run['provider'] : 'unknown',
1614 'ts' => isset( $run['ts'] ) ? (int) $run['ts'] : 0,
1615 'url' => isset( $run['url'] ) ? (string) $run['url'] : '',
1616 'strategy' => isset( $run['strategy'] ) ? (string) $run['strategy'] : null,
1617 // Null, never 0: Score distinguishes "no score" from "scored
1618 // zero", and flattening that reports a failed audit as a
1619 // catastrophic result.
1620 'score' => isset( $run['score'] ) && is_numeric( $run['score'] ) ? (int) $run['score'] : null,
1621 'metrics' => array(
1622 'lcp' => self::metric_or_null( $metrics, 'lcp' ),
1623 'fcp' => self::metric_or_null( $metrics, 'fcp' ),
1624 'cls' => self::metric_or_null( $metrics, 'cls' ),
1625 'tbt' => self::metric_or_null( $metrics, 'tbt' ),
1626 'si' => self::metric_or_null( $metrics, 'si' ),
1627 'ttfb' => self::metric_or_null( $metrics, 'ttfb' ),
1628 ),
1629 'report_url' => self::report_url_for( $run ),
1630 );
1631 }
1632
1633 return array(
1634 'runs' => $runs,
1635 'total' => count( \XSpeed\Score::history() ),
1636 );
1637 }
1638
1639 /**
1640 * One metric as a float, or null when absent/non-numeric.
1641 *
1642 * @param array $metrics Metric bag.
1643 * @param string $key Metric id.
1644 */
1645 private static function metric_or_null( array $metrics, string $key ): ?float {
1646 return isset( $metrics[ $key ] ) && is_numeric( $metrics[ $key ] ) ? (float) $metrics[ $key ] : null;
1647 }
1648
1649 /**
1650 * Deep link to the provider's own report, when one exists.
1651 *
1652 * GTmetrix hosts a durable report per test, so its id is enough to build
1653 * the link. PSI does NOT — a Lighthouse result is returned to the caller
1654 * and never hosted, so there is genuinely nothing to link to and this
1655 * returns null rather than inventing a URL that 404s.
1656 *
1657 * @param array $run One stored run.
1658 */
1659 private static function report_url_for( array $run ): ?string {
1660 $provider = isset( $run['provider'] ) ? (string) $run['provider'] : '';
1661 if ( 'gtmetrix' !== $provider ) {
1662 return null;
1663 }
1664 $test_id = isset( $run['test_id'] ) ? trim( (string) $run['test_id'] ) : '';
1665 if ( '' === $test_id ) {
1666 return null;
1667 }
1668 return 'https://gtmetrix.com/reports/' . rawurlencode( $test_id );
1669 }
1670
1671 public static function purge_url( array $args ) {
1672 $url = isset( $args['url'] ) ? trim( (string) $args['url'] ) : '';
1673 if ( '' === $url ) {
1674 return new \WP_Error( 'xspeed_mcp_missing_url', __( 'The url argument is required.', 'xspeed' ), array( 'status' => 400 ) );
1675 }
1676 return Cli_Bridge::run( 'cache', array( 'purge-url', $url ), array( 'cause' => __( 'AI assistant', 'xspeed' ) ) );
1677 }
1678
1679 /**
1680 * Probe the configured object-cache backend (connect + read/write).
1681 *
1682 * @param array $args Unused.
1683 * @return array|\WP_Error
1684 */
1685 public static function test_object_cache( array $args ) {
1686 unset( $args );
1687 return Cli_Bridge::run( 'objcache', array( 'test' ) );
1688 }
1689
1690 /**
1691 * Verify the saved Cloudflare credentials.
1692 *
1693 * @param array $args Unused.
1694 * @return array|\WP_Error
1695 */
1696 public static function cloudflare_verify( array $args ) {
1697 unset( $args );
1698 return Cli_Bridge::run( 'cf', array( 'verify' ) );
1699 }
1700
1701 /**
1702 * Run an external audit on any install.
1703 *
1704 * Shares run_pagespeed's body: that handler ALREADY falls back to
1705 * `xspeed score run` when the Pro `xspeed psi` command is absent, so the
1706 * engine could always do this on Free — the tool was simply dropped from
1707 * the catalog before anyone could call it. The only thing missing was a
1708 * name that survives on a Free install. (#147)
1709 *
1710 * @param array $args target / strategy / provider.
1711 * @return array|\WP_Error
1712 */
1713 public static function run_score( array $args ) {
1714 // `target` is the CLI's name for it (--url is a reserved WP-CLI global,
1715 // so the score command deliberately uses --target). Accept both here
1716 // and normalise, so an assistant that guessed `url` still works.
1717 if ( ! empty( $args['target'] ) && empty( $args['url'] ) ) {
1718 $args['url'] = (string) $args['target'];
1719 }
1720 return self::run_pagespeed( $args );
1721 }
1722
1723 /**
1724 * Run an external performance audit. Prefers the Pro engine when present,
1725 * otherwise drives Free's own score command.
1726 *
1727 * @param array $args { url?:string, strategy?:string, provider?:string, force?:bool }.
1728 * @return array|\WP_Error
1729 */
1730 public static function run_pagespeed( array $args ) {
1731 $options = array();
1732 if ( ! empty( $args['url'] ) ) {
1733 $options['url'] = (string) $args['url'];
1734 }
1735 if ( ! empty( $args['strategy'] ) ) {
1736 $options['strategy'] = (string) $args['strategy'];
1737 }
1738 // Advertised in run_score's schema, and the Free score handler already
1739 // branches on it (ScoreModule::cli_handler reads $assoc['provider']),
1740 // so dropping it here meant a GTmetrix request ran a PSI audit and
1741 // reported ok:true — spending the wrong provider's quota with nothing
1742 // in the response to say so. (QA B1 on #162)
1743 if ( ! empty( $args['provider'] ) ) {
1744 $options['provider'] = (string) $args['provider'];
1745 }
1746 // Was reachable only via the generated xspeed_psi alias, which this
1747 // change removes — so it moves onto the typed tool rather than being
1748 // lost with it.
1749 if ( ! empty( $args['force'] ) && filter_var( $args['force'], FILTER_VALIDATE_BOOLEAN ) ) {
1750 $options['force'] = true;
1751 }
1752
1753 /*
1754 * Prefer the richer Pro engine when it's installed; otherwise drive
1755 * Free's own score command. Same tool name either way — an assistant
1756 * asking for a PageSpeed audit shouldn't have to know which tier the
1757 * site runs, and the two write to the same run history.
1758 *
1759 * EXCEPT when a provider was named that the Pro engine cannot serve.
1760 * `xspeed psi` is PageSpeed-only: it declares no --provider and
1761 * discards the option, so preferring it purely because it exists made
1762 * `provider: "gtmetrix"` run PSI and answer ok:true — the same silent
1763 * wrong-provider bug this tool just fixed on Free, reappearing only on
1764 * Pro. A site that configures GTmetrix would have stopped getting it
1765 * the moment Pro activated. Free's `score` command reads $assoc
1766 * ['provider'] and branches, so route there instead. (QA R1 on #162)
1767 */
1768 $wants_non_psi = isset( $options['provider'] ) && 'psi' !== strtolower( (string) $options['provider'] );
1769 if ( isset( Cli_Bridge::commands()['xspeed psi'] ) && ! $wants_non_psi ) {
1770 return Cli_Bridge::run( 'psi', array(), $options );
1771 }
1772
1773 // The Free `score` command reads --target, not --url: `url` is a
1774 // reserved WP-CLI global, so a value passed as `url` never reaches the
1775 // handler and the requested page is silently ignored in favour of the
1776 // default. Translate rather than passing it through. (#147)
1777 if ( isset( $options['url'] ) ) {
1778 $options['target'] = $options['url'];
1779 unset( $options['url'] );
1780 }
1781 return Cli_Bridge::run( 'score', array( 'run' ), $options );
1782 }
1783
1784 /**
1785 * Generate Critical CSS (Pro).
1786 *
1787 * @param array $args Unused.
1788 * @return array|\WP_Error
1789 */
1790 public static function generate_critical_css( array $args ) {
1791 unset( $args );
1792 return Cli_Bridge::run( 'ccss', array( 'generate' ) );
1793 }
1794
1795 /**
1796 * Build a JSON Schema object node.
1797 *
1798 * @param array $properties Property map.
1799 * @param string[] $required Required property names.
1800 */
1801 private static function object_schema( array $properties, array $required ): array {
1802 $schema = array(
1803 'type' => 'object',
1804 'properties' => (object) $properties,
1805 );
1806 if ( ! empty( $required ) ) {
1807 $schema['required'] = array_values( $required );
1808 }
1809 return $schema;
1810 }
1811 }
1812