| 1 |
<?php |
| 2 |
|
| 3 |
class Meow_MWCODE_MCP { |
| 4 |
private $core; |
| 5 |
private $api; |
| 6 |
|
| 7 |
public function __construct( $core ) { |
| 8 |
$this->core = $core; |
| 9 |
|
| 10 |
// Initialize everything on 'init' to ensure options are loaded |
| 11 |
add_action( 'init', array( $this, 'init' ), 20 ); |
| 12 |
} |
| 13 |
|
| 14 |
public function init() { |
| 15 |
global $mwcode, $mwai; |
| 16 |
$this->api = $mwcode; |
| 17 |
|
| 18 |
// Only register MCP if enabled AND AI Engine is available |
| 19 |
if ( $this->core->get_option( 'mcp_support', false ) && isset( $mwai ) ) { |
| 20 |
// Register MCP tools |
| 21 |
add_filter( 'mwai_mcp_tools', array( $this, 'register_tools' ) ); |
| 22 |
|
| 23 |
// Handle MCP tool execution |
| 24 |
add_filter( 'mwai_mcp_callback', array( $this, 'handle_tool_execution' ), 10, 4 ); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
public function register_tools( $tools ) { |
| 29 |
// Get Snippet |
| 30 |
$tools[] = [ |
| 31 |
'name' => 'mwcode_get_snippet', |
| 32 |
'description' => 'Get a Code Engine snippet by its ID', |
| 33 |
'category' => 'Code Engine', |
| 34 |
'inputSchema' => [ |
| 35 |
'type' => 'object', |
| 36 |
'properties' => [ |
| 37 |
'id' => [ |
| 38 |
'type' => 'integer', |
| 39 |
'description' => 'The snippet ID' |
| 40 |
], |
| 41 |
'options' => [ |
| 42 |
'type' => 'object', |
| 43 |
'description' => 'Optional filtering options', |
| 44 |
'properties' => [ |
| 45 |
'php_ready_args' => [ |
| 46 |
'type' => 'boolean', |
| 47 |
'description' => 'If false, arguments will not be formatted for PHP (no $ before names)' |
| 48 |
] |
| 49 |
] |
| 50 |
] |
| 51 |
], |
| 52 |
'required' => ['id'] |
| 53 |
] |
| 54 |
]; |
| 55 |
|
| 56 |
// Get Snippet by Name |
| 57 |
$tools[] = [ |
| 58 |
'name' => 'mwcode_get_snippet_by_name', |
| 59 |
'description' => 'Get a Code Engine snippet by its name', |
| 60 |
'category' => 'Code Engine', |
| 61 |
'inputSchema' => [ |
| 62 |
'type' => 'object', |
| 63 |
'properties' => [ |
| 64 |
'name' => [ |
| 65 |
'type' => 'string', |
| 66 |
'description' => 'The snippet name' |
| 67 |
], |
| 68 |
'options' => [ |
| 69 |
'type' => 'object', |
| 70 |
'description' => 'Optional filtering options', |
| 71 |
'properties' => [ |
| 72 |
'php_ready_args' => [ |
| 73 |
'type' => 'boolean', |
| 74 |
'description' => 'If false, arguments will not be formatted for PHP (no $ before names)' |
| 75 |
] |
| 76 |
] |
| 77 |
] |
| 78 |
], |
| 79 |
'required' => ['name'] |
| 80 |
] |
| 81 |
]; |
| 82 |
|
| 83 |
// Get Snippets |
| 84 |
$tools[] = [ |
| 85 |
'name' => 'mwcode_get_snippets', |
| 86 |
'description' => 'Get all Code Engine snippets, optionally filtered by scope', |
| 87 |
'category' => 'Code Engine', |
| 88 |
'inputSchema' => [ |
| 89 |
'type' => 'object', |
| 90 |
'properties' => [ |
| 91 |
'safe' => [ |
| 92 |
'type' => 'boolean', |
| 93 |
'description' => 'Whether to filter out snippets with invalid names', |
| 94 |
'default' => true |
| 95 |
], |
| 96 |
'scope' => [ |
| 97 |
'type' => 'string', |
| 98 |
'description' => 'Optional scope filter', |
| 99 |
'enum' => ['function', 'backend', 'frontend', 'scheduled', 'persistent'] |
| 100 |
] |
| 101 |
] |
| 102 |
] |
| 103 |
]; |
| 104 |
|
| 105 |
// Execute Snippet |
| 106 |
$tools[] = [ |
| 107 |
'name' => 'mwcode_execute_snippet', |
| 108 |
'description' => 'Execute a Code Engine snippet by its ID', |
| 109 |
'category' => 'Code Engine', |
| 110 |
'inputSchema' => [ |
| 111 |
'type' => 'object', |
| 112 |
'properties' => [ |
| 113 |
'id' => [ |
| 114 |
'type' => 'integer', |
| 115 |
'description' => 'The snippet ID' |
| 116 |
], |
| 117 |
'args' => [ |
| 118 |
'type' => 'object', |
| 119 |
'description' => 'Arguments to pass to the snippet (key-value pairs)', |
| 120 |
'additionalProperties' => true |
| 121 |
] |
| 122 |
], |
| 123 |
'required' => ['id'] |
| 124 |
] |
| 125 |
]; |
| 126 |
|
| 127 |
// Execute Snippet by Name |
| 128 |
$tools[] = [ |
| 129 |
'name' => 'mwcode_execute_snippet_by_name', |
| 130 |
'description' => 'Execute a Code Engine snippet by its name', |
| 131 |
'category' => 'Code Engine', |
| 132 |
'inputSchema' => [ |
| 133 |
'type' => 'object', |
| 134 |
'properties' => [ |
| 135 |
'name' => [ |
| 136 |
'type' => 'string', |
| 137 |
'description' => 'The snippet name' |
| 138 |
], |
| 139 |
'args' => [ |
| 140 |
'type' => 'object', |
| 141 |
'description' => 'Arguments to pass to the snippet (key-value pairs)', |
| 142 |
'additionalProperties' => true |
| 143 |
] |
| 144 |
], |
| 145 |
'required' => ['name'] |
| 146 |
] |
| 147 |
]; |
| 148 |
|
| 149 |
// Create Snippet |
| 150 |
$tools[] = [ |
| 151 |
'name' => 'mwcode_create_snippet', |
| 152 |
'description' => 'Create a new Code Engine snippet', |
| 153 |
'category' => 'Code Engine', |
| 154 |
'inputSchema' => [ |
| 155 |
'type' => 'object', |
| 156 |
'properties' => [ |
| 157 |
'name' => [ |
| 158 |
'type' => 'string', |
| 159 |
'description' => 'Name of the snippet' |
| 160 |
], |
| 161 |
'code' => [ |
| 162 |
'type' => 'string', |
| 163 |
'description' => 'Code of the snippet' |
| 164 |
], |
| 165 |
'scope' => [ |
| 166 |
'type' => 'string', |
| 167 |
'description' => 'Scope of the snippet', |
| 168 |
'enum' => ['function', 'backend', 'frontend', 'scheduled', 'persistent'], |
| 169 |
'default' => 'function' |
| 170 |
], |
| 171 |
'options' => [ |
| 172 |
'type' => 'object', |
| 173 |
'description' => 'Additional options for the snippet', |
| 174 |
'properties' => [ |
| 175 |
'target' => [ |
| 176 |
'type' => 'string', |
| 177 |
'enum' => ['php', 'js'], |
| 178 |
'description' => 'Target language (for function snippets)' |
| 179 |
], |
| 180 |
'description' => [ |
| 181 |
'type' => 'string', |
| 182 |
'description' => 'Description of the snippet' |
| 183 |
], |
| 184 |
'args' => [ |
| 185 |
'type' => 'array', |
| 186 |
'description' => 'Arguments for function snippets', |
| 187 |
'items' => [ 'type' => 'string' ] |
| 188 |
], |
| 189 |
'argsData' => [ |
| 190 |
'type' => 'object', |
| 191 |
'description' => 'Argument data for function snippets', |
| 192 |
'additionalProperties' => [ |
| 193 |
'type' => 'object', |
| 194 |
'properties' => [ |
| 195 |
'type' => [ 'type' => 'string' ], |
| 196 |
'description' => [ 'type' => 'string' ], |
| 197 |
'default' => [ 'type' => 'string' ] |
| 198 |
] |
| 199 |
] |
| 200 |
], |
| 201 |
'behavior' => [ |
| 202 |
'type' => 'string', |
| 203 |
'enum' => ['dynamic', 'static'], |
| 204 |
'description' => 'Behavior for function snippets' |
| 205 |
], |
| 206 |
'tags' => [ |
| 207 |
'type' => 'array', |
| 208 |
'description' => 'Array of tags', |
| 209 |
'items' => [ 'type' => 'string' ] |
| 210 |
], |
| 211 |
'priority' => [ |
| 212 |
'type' => 'integer', |
| 213 |
'description' => 'Execution priority' |
| 214 |
], |
| 215 |
'active' => [ |
| 216 |
'type' => 'boolean', |
| 217 |
'description' => 'Active status' |
| 218 |
], |
| 219 |
'endpoint' => [ |
| 220 |
'type' => 'string', |
| 221 |
'description' => 'REST endpoint' |
| 222 |
], |
| 223 |
'method' => [ |
| 224 |
'type' => 'string', |
| 225 |
'enum' => ['GET', 'POST'], |
| 226 |
'description' => 'HTTP method' |
| 227 |
], |
| 228 |
'intervalHours' => [ |
| 229 |
'type' => 'integer', |
| 230 |
'description' => 'Hours for scheduled snippets' |
| 231 |
], |
| 232 |
'intervalMinutes' => [ |
| 233 |
'type' => 'integer', |
| 234 |
'description' => 'Minutes for scheduled snippets' |
| 235 |
] |
| 236 |
] |
| 237 |
] |
| 238 |
], |
| 239 |
'required' => ['name', 'code'] |
| 240 |
] |
| 241 |
]; |
| 242 |
|
| 243 |
// Update Snippet |
| 244 |
$tools[] = [ |
| 245 |
'name' => 'mwcode_update_snippet', |
| 246 |
'description' => 'Update an existing Code Engine snippet', |
| 247 |
'category' => 'Code Engine', |
| 248 |
'inputSchema' => [ |
| 249 |
'type' => 'object', |
| 250 |
'properties' => [ |
| 251 |
'id' => [ |
| 252 |
'type' => 'integer', |
| 253 |
'description' => 'ID of the snippet to update' |
| 254 |
], |
| 255 |
'params' => [ |
| 256 |
'type' => 'object', |
| 257 |
'description' => 'Parameters to update', |
| 258 |
'properties' => [ |
| 259 |
'name' => [ 'type' => 'string' ], |
| 260 |
'code' => [ 'type' => 'string' ], |
| 261 |
'scope' => [ |
| 262 |
'type' => 'string', |
| 263 |
'enum' => ['function', 'backend', 'frontend', 'scheduled', 'persistent'] |
| 264 |
], |
| 265 |
'description' => [ 'type' => 'string' ], |
| 266 |
'active' => [ 'type' => 'boolean' ], |
| 267 |
'priority' => [ 'type' => 'integer' ], |
| 268 |
'tags' => [ |
| 269 |
'type' => 'array', |
| 270 |
'items' => [ 'type' => 'string' ] |
| 271 |
], |
| 272 |
'endpoint' => [ 'type' => 'string' ], |
| 273 |
'method' => [ |
| 274 |
'type' => 'string', |
| 275 |
'enum' => ['GET', 'POST'] |
| 276 |
], |
| 277 |
'target' => [ |
| 278 |
'type' => 'string', |
| 279 |
'enum' => ['php', 'js'] |
| 280 |
], |
| 281 |
'args' => [ |
| 282 |
'type' => 'array', |
| 283 |
'items' => [ 'type' => 'string' ] |
| 284 |
], |
| 285 |
'argsData' => [ |
| 286 |
'type' => 'object', |
| 287 |
'additionalProperties' => true |
| 288 |
], |
| 289 |
'behavior' => [ |
| 290 |
'type' => 'string', |
| 291 |
'enum' => ['dynamic', 'static'] |
| 292 |
], |
| 293 |
'intervalHours' => [ 'type' => 'integer' ], |
| 294 |
'intervalMinutes' => [ 'type' => 'integer' ] |
| 295 |
] |
| 296 |
] |
| 297 |
], |
| 298 |
'required' => ['id'] |
| 299 |
] |
| 300 |
]; |
| 301 |
|
| 302 |
// Delete Snippet |
| 303 |
$tools[] = [ |
| 304 |
'name' => 'mwcode_delete_snippet', |
| 305 |
'description' => 'Delete a Code Engine snippet by its ID', |
| 306 |
'category' => 'Code Engine', |
| 307 |
'inputSchema' => [ |
| 308 |
'type' => 'object', |
| 309 |
'properties' => [ |
| 310 |
'id' => [ |
| 311 |
'type' => 'integer', |
| 312 |
'description' => 'The snippet ID' |
| 313 |
] |
| 314 |
], |
| 315 |
'required' => ['id'] |
| 316 |
] |
| 317 |
]; |
| 318 |
|
| 319 |
// Delete Snippet by Name |
| 320 |
$tools[] = [ |
| 321 |
'name' => 'mwcode_delete_snippet_by_name', |
| 322 |
'description' => 'Delete a Code Engine snippet by its name', |
| 323 |
'category' => 'Code Engine', |
| 324 |
'inputSchema' => [ |
| 325 |
'type' => 'object', |
| 326 |
'properties' => [ |
| 327 |
'name' => [ |
| 328 |
'type' => 'string', |
| 329 |
'description' => 'The snippet name' |
| 330 |
] |
| 331 |
], |
| 332 |
'required' => ['name'] |
| 333 |
] |
| 334 |
]; |
| 335 |
|
| 336 |
// Activate Snippet |
| 337 |
$tools[] = [ |
| 338 |
'name' => 'mwcode_activate_snippet', |
| 339 |
'description' => 'Activate a Code Engine snippet', |
| 340 |
'category' => 'Code Engine', |
| 341 |
'inputSchema' => [ |
| 342 |
'type' => 'object', |
| 343 |
'properties' => [ |
| 344 |
'id' => [ |
| 345 |
'type' => 'integer', |
| 346 |
'description' => 'The snippet ID' |
| 347 |
] |
| 348 |
], |
| 349 |
'required' => ['id'] |
| 350 |
] |
| 351 |
]; |
| 352 |
|
| 353 |
// Deactivate Snippet |
| 354 |
$tools[] = [ |
| 355 |
'name' => 'mwcode_deactivate_snippet', |
| 356 |
'description' => 'Deactivate a Code Engine snippet', |
| 357 |
'category' => 'Code Engine', |
| 358 |
'inputSchema' => [ |
| 359 |
'type' => 'object', |
| 360 |
'properties' => [ |
| 361 |
'id' => [ |
| 362 |
'type' => 'integer', |
| 363 |
'description' => 'The snippet ID' |
| 364 |
] |
| 365 |
], |
| 366 |
'required' => ['id'] |
| 367 |
] |
| 368 |
]; |
| 369 |
|
| 370 |
// Get Snippets by Scope |
| 371 |
$tools[] = [ |
| 372 |
'name' => 'mwcode_get_snippets_by_scope', |
| 373 |
'description' => 'Get Code Engine snippets filtered by scope', |
| 374 |
'category' => 'Code Engine', |
| 375 |
'inputSchema' => [ |
| 376 |
'type' => 'object', |
| 377 |
'properties' => [ |
| 378 |
'scope' => [ |
| 379 |
'type' => 'string', |
| 380 |
'description' => 'The scope to filter by', |
| 381 |
'enum' => ['function', 'backend', 'frontend', 'scheduled', 'persistent'] |
| 382 |
], |
| 383 |
'filters' => [ |
| 384 |
'type' => 'object', |
| 385 |
'description' => 'Additional filters', |
| 386 |
'properties' => [ |
| 387 |
'active' => [ |
| 388 |
'type' => 'boolean', |
| 389 |
'description' => 'Filter by active status' |
| 390 |
], |
| 391 |
'tags' => [ |
| 392 |
'type' => 'array', |
| 393 |
'description' => 'Filter by tags', |
| 394 |
'items' => [ 'type' => 'string' ] |
| 395 |
] |
| 396 |
] |
| 397 |
] |
| 398 |
], |
| 399 |
'required' => ['scope'] |
| 400 |
] |
| 401 |
]; |
| 402 |
|
| 403 |
// Get Active Snippets |
| 404 |
$tools[] = [ |
| 405 |
'name' => 'mwcode_get_active_snippets', |
| 406 |
'description' => 'Get all active Code Engine snippets', |
| 407 |
'category' => 'Code Engine', |
| 408 |
'inputSchema' => [ |
| 409 |
'type' => 'object', |
| 410 |
'properties' => [ |
| 411 |
'scope' => [ |
| 412 |
'type' => 'string', |
| 413 |
'description' => 'Optional scope filter', |
| 414 |
'enum' => ['function', 'backend', 'frontend', 'scheduled', 'persistent'] |
| 415 |
] |
| 416 |
] |
| 417 |
] |
| 418 |
]; |
| 419 |
|
| 420 |
// Snippet Exists |
| 421 |
$tools[] = [ |
| 422 |
'name' => 'mwcode_snippet_exists', |
| 423 |
'description' => 'Check if a Code Engine snippet exists by ID', |
| 424 |
'category' => 'Code Engine', |
| 425 |
'inputSchema' => [ |
| 426 |
'type' => 'object', |
| 427 |
'properties' => [ |
| 428 |
'id' => [ |
| 429 |
'type' => 'integer', |
| 430 |
'description' => 'The snippet ID' |
| 431 |
] |
| 432 |
], |
| 433 |
'required' => ['id'] |
| 434 |
] |
| 435 |
]; |
| 436 |
|
| 437 |
// Snippet Exists by Name |
| 438 |
$tools[] = [ |
| 439 |
'name' => 'mwcode_snippet_exists_by_name', |
| 440 |
'description' => 'Check if a Code Engine snippet exists by name', |
| 441 |
'category' => 'Code Engine', |
| 442 |
'inputSchema' => [ |
| 443 |
'type' => 'object', |
| 444 |
'properties' => [ |
| 445 |
'name' => [ |
| 446 |
'type' => 'string', |
| 447 |
'description' => 'The snippet name' |
| 448 |
] |
| 449 |
], |
| 450 |
'required' => ['name'] |
| 451 |
] |
| 452 |
]; |
| 453 |
|
| 454 |
// Validate Snippet Code |
| 455 |
$tools[] = [ |
| 456 |
'name' => 'mwcode_validate_snippet_code', |
| 457 |
'description' => 'Validate snippet code syntax', |
| 458 |
'category' => 'Code Engine', |
| 459 |
'inputSchema' => [ |
| 460 |
'type' => 'object', |
| 461 |
'properties' => [ |
| 462 |
'code' => [ |
| 463 |
'type' => 'string', |
| 464 |
'description' => 'The snippet code to validate' |
| 465 |
], |
| 466 |
'target' => [ |
| 467 |
'type' => 'string', |
| 468 |
'description' => 'The target language', |
| 469 |
'enum' => ['php', 'js'], |
| 470 |
'default' => 'php' |
| 471 |
] |
| 472 |
], |
| 473 |
'required' => ['code'] |
| 474 |
] |
| 475 |
]; |
| 476 |
|
| 477 |
return $tools; |
| 478 |
} |
| 479 |
|
| 480 |
public function handle_tool_execution( $result, $tool, $args, $id ) { |
| 481 |
// Only handle our tools |
| 482 |
if ( strpos( $tool, 'mwcode_' ) !== 0 ) { |
| 483 |
return $result; |
| 484 |
} |
| 485 |
|
| 486 |
// Ensure API is initialized |
| 487 |
if ( !$this->api ) { |
| 488 |
return [ 'success' => false, 'error' => 'Code Engine API not initialized' ]; |
| 489 |
} |
| 490 |
|
| 491 |
try { |
| 492 |
switch ( $tool ) { |
| 493 |
case 'mwcode_get_snippet': |
| 494 |
$data = $this->api->getSnippet( $args['id'], $args['options'] ?? [] ); |
| 495 |
return [ 'success' => true, 'data' => $data ]; |
| 496 |
|
| 497 |
case 'mwcode_get_snippet_by_name': |
| 498 |
$data = $this->api->getSnippetByName( $args['name'], $args['options'] ?? [] ); |
| 499 |
return [ 'success' => true, 'data' => $data ]; |
| 500 |
|
| 501 |
case 'mwcode_get_snippets': |
| 502 |
$data = $this->api->getSnippets( $args['safe'] ?? true, $args['scope'] ?? null ); |
| 503 |
return [ 'success' => true, 'data' => $data ]; |
| 504 |
|
| 505 |
case 'mwcode_execute_snippet': |
| 506 |
$data = $this->api->executeSnippet( $args['id'], $args['args'] ?? [] ); |
| 507 |
return [ 'success' => true, 'data' => $data ]; |
| 508 |
|
| 509 |
case 'mwcode_execute_snippet_by_name': |
| 510 |
$data = $this->api->executeSnippetByName( $args['name'], $args['args'] ?? [] ); |
| 511 |
return [ 'success' => true, 'data' => $data ]; |
| 512 |
|
| 513 |
case 'mwcode_create_snippet': |
| 514 |
$data = $this->api->createSnippet( |
| 515 |
$args['name'], |
| 516 |
$args['code'], |
| 517 |
$args['scope'] ?? 'function', |
| 518 |
$args['options'] ?? [] |
| 519 |
); |
| 520 |
return [ 'success' => true, 'data' => $data ]; |
| 521 |
|
| 522 |
case 'mwcode_update_snippet': |
| 523 |
$data = $this->api->updateSnippet( $args['id'], $args['params'] ?? [] ); |
| 524 |
return [ 'success' => true, 'data' => $data ]; |
| 525 |
|
| 526 |
case 'mwcode_delete_snippet': |
| 527 |
$success = $this->api->deleteSnippet( $args['id'] ); |
| 528 |
return [ 'success' => true, 'data' => [ 'deleted' => $success ] ]; |
| 529 |
|
| 530 |
case 'mwcode_delete_snippet_by_name': |
| 531 |
$success = $this->api->deleteSnippetByName( $args['name'] ); |
| 532 |
return [ 'success' => true, 'data' => [ 'deleted' => $success ] ]; |
| 533 |
|
| 534 |
case 'mwcode_activate_snippet': |
| 535 |
$success = $this->api->activateSnippet( $args['id'] ); |
| 536 |
return [ 'success' => true, 'data' => [ 'activated' => $success ] ]; |
| 537 |
|
| 538 |
case 'mwcode_deactivate_snippet': |
| 539 |
$success = $this->api->deactivateSnippet( $args['id'] ); |
| 540 |
return [ 'success' => true, 'data' => [ 'deactivated' => $success ] ]; |
| 541 |
|
| 542 |
case 'mwcode_get_snippets_by_scope': |
| 543 |
$data = $this->api->getSnippetsByScope( $args['scope'], $args['filters'] ?? [] ); |
| 544 |
return [ 'success' => true, 'data' => $data ]; |
| 545 |
|
| 546 |
case 'mwcode_get_active_snippets': |
| 547 |
$data = $this->api->getActiveSnippets( $args['scope'] ?? null ); |
| 548 |
return [ 'success' => true, 'data' => $data ]; |
| 549 |
|
| 550 |
case 'mwcode_snippet_exists': |
| 551 |
$exists = $this->api->snippetExists( $args['id'] ); |
| 552 |
return [ 'success' => true, 'data' => [ 'exists' => $exists ] ]; |
| 553 |
|
| 554 |
case 'mwcode_snippet_exists_by_name': |
| 555 |
$exists = $this->api->snippetExistsByName( $args['name'] ); |
| 556 |
return [ 'success' => true, 'data' => [ 'exists' => $exists ] ]; |
| 557 |
|
| 558 |
case 'mwcode_validate_snippet_code': |
| 559 |
$validation = $this->api->validateSnippetCode( $args['code'], $args['target'] ?? 'php' ); |
| 560 |
return [ 'success' => true, 'data' => $validation ]; |
| 561 |
} |
| 562 |
} |
| 563 |
catch ( Exception $e ) { |
| 564 |
return [ 'success' => false, 'error' => $e->getMessage() ]; |
| 565 |
} |
| 566 |
|
| 567 |
return $result; |
| 568 |
} |
| 569 |
} |