| 1 |
<?php |
| 2 |
/** |
| 3 |
* OTTO MCP Integration |
| 4 |
* |
| 5 |
* Provides direct access to MCP tools for OTTO functionality. |
| 6 |
* Replaces the AI Agent intermediary layer with direct MCP tool calls. |
| 7 |
* |
| 8 |
* @package MetaSync |
| 9 |
* @subpackage OTTO |
| 10 |
*/ |
| 11 |
|
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
class Metasync_Otto_MCP_Integration { |
| 17 |
|
| 18 |
/** |
| 19 |
* Singleton instance |
| 20 |
* |
| 21 |
* @var Metasync_Otto_MCP_Integration |
| 22 |
*/ |
| 23 |
private static $instance = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* Get singleton instance |
| 27 |
* |
| 28 |
* @return Metasync_Otto_MCP_Integration |
| 29 |
*/ |
| 30 |
public static function get_instance() { |
| 31 |
if (self::$instance === null) { |
| 32 |
self::$instance = new self(); |
| 33 |
} |
| 34 |
return self::$instance; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor |
| 39 |
*/ |
| 40 |
private function __construct() { |
| 41 |
// Private constructor for singleton |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Execute MCP tool directly |
| 46 |
* |
| 47 |
* @param string $tool_name MCP tool name |
| 48 |
* @param array $params Tool parameters |
| 49 |
* @return array Result |
| 50 |
* @throws Exception If MCP server not initialized or tool execution fails |
| 51 |
*/ |
| 52 |
public function execute_mcp_tool($tool_name, $params = []) { |
| 53 |
global $metasync_mcp_server; |
| 54 |
|
| 55 |
if (!$metasync_mcp_server) { |
| 56 |
throw new Exception('MCP Server not initialized'); |
| 57 |
} |
| 58 |
|
| 59 |
$tool_registry = $metasync_mcp_server->get_tool_registry(); |
| 60 |
return $tool_registry->execute_tool($tool_name, $params); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Get available MCP tools list |
| 65 |
* |
| 66 |
* @return array List of available tools |
| 67 |
*/ |
| 68 |
public function get_available_tools() { |
| 69 |
global $metasync_mcp_server; |
| 70 |
|
| 71 |
if (!$metasync_mcp_server) { |
| 72 |
return []; |
| 73 |
} |
| 74 |
|
| 75 |
return $metasync_mcp_server->get_tool_registry()->get_tools_list(); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Map OTTO action to MCP tool call |
| 80 |
* |
| 81 |
* Maps legacy OTTO action names to MCP tool names and parameters. |
| 82 |
* |
| 83 |
* @param string $action_name OTTO action name |
| 84 |
* @param array $params Action parameters |
| 85 |
* @return array MCP tool result |
| 86 |
* @throws Exception If action not found or execution fails |
| 87 |
*/ |
| 88 |
public function map_otto_action($action_name, $params = []) { |
| 89 |
// Action to MCP tool mapping |
| 90 |
$tool_mapping = [ |
| 91 |
// Meta Operations |
| 92 |
'update_meta_title' => ['wordpress_update_post_meta', ['meta_key' => '_metasync_meta_title']], |
| 93 |
'update_meta_description' => ['wordpress_update_post_meta', ['meta_key' => '_metasync_meta_description']], |
| 94 |
'set_focus_keyword' => ['wordpress_update_post_meta', ['meta_key' => '_metasync_focus_keyword']], |
| 95 |
'toggle_indexing' => ['wordpress_update_post_meta', ['meta_key' => '_metasync_robots']], |
| 96 |
'get_seo_data' => ['wordpress_get_seo_meta', []], |
| 97 |
|
| 98 |
// Post Operations |
| 99 |
'list_posts' => ['wordpress_list_posts', []], |
| 100 |
'get_post' => ['wordpress_get_post', []], |
| 101 |
'update_post' => ['wordpress_update_post', []], |
| 102 |
'analyze_seo' => ['wordpress_analyze_seo', []], |
| 103 |
'search_posts' => ['wordpress_search_posts', []], |
| 104 |
|
| 105 |
// Redirect Management |
| 106 |
'create_redirect' => ['wordpress_create_redirect', []], |
| 107 |
'list_redirects' => ['wordpress_list_redirects', []], |
| 108 |
'delete_redirect' => ['wordpress_delete_redirect', []], |
| 109 |
'update_redirect' => ['wordpress_update_redirect', []], |
| 110 |
|
| 111 |
// 404 Monitor |
| 112 |
'list_404_errors' => ['wordpress_list_404_errors', []], |
| 113 |
'get_404_stats' => ['wordpress_get_404_stats', []], |
| 114 |
'delete_404_error' => ['wordpress_delete_404_error', []], |
| 115 |
'clear_404_errors' => ['wordpress_clear_404_errors', []], |
| 116 |
'create_redirect_from_404' => ['wordpress_create_redirect_from_404', []], |
| 117 |
|
| 118 |
// Robots & Sitemap |
| 119 |
'get_robots_txt' => ['wordpress_get_robots_txt', []], |
| 120 |
'update_robots_txt' => ['wordpress_update_robots_txt', []], |
| 121 |
'regenerate_sitemap' => ['wordpress_regenerate_sitemap', []], |
| 122 |
'get_sitemap_status' => ['wordpress_get_sitemap_status', []], |
| 123 |
'exclude_from_sitemap' => ['wordpress_exclude_from_sitemap', []], |
| 124 |
|
| 125 |
// Plugin Settings |
| 126 |
'get_plugin_settings' => ['wordpress_get_plugin_settings', []], |
| 127 |
'update_plugin_settings' => ['wordpress_update_plugin_settings', []], |
| 128 |
|
| 129 |
// Schema Markup |
| 130 |
'get_schema_markup' => ['wordpress_get_schema_markup', []], |
| 131 |
'update_schema_markup' => ['wordpress_update_schema_markup', []], |
| 132 |
'add_schema_type' => ['wordpress_add_schema_type', []], |
| 133 |
'remove_schema_type' => ['wordpress_remove_schema_type', []], |
| 134 |
'validate_schema' => ['wordpress_validate_schema', []], |
| 135 |
|
| 136 |
// Instant Index |
| 137 |
'instant_index_update' => ['wordpress_instant_index_update', []], |
| 138 |
'instant_index_delete' => ['wordpress_instant_index_delete', []], |
| 139 |
'instant_index_status' => ['wordpress_instant_index_status', []], |
| 140 |
'instant_index_bulk_update' => ['wordpress_instant_index_bulk_update', []], |
| 141 |
'get_instant_index_settings' => ['wordpress_get_instant_index_settings', []], |
| 142 |
'update_instant_index_settings' => ['wordpress_update_instant_index_settings', []], |
| 143 |
|
| 144 |
// Custom Pages |
| 145 |
'create_custom_page' => ['wordpress_create_custom_page', []], |
| 146 |
'get_custom_page' => ['wordpress_get_custom_page', []], |
| 147 |
'list_custom_pages' => ['wordpress_list_custom_pages', []], |
| 148 |
'update_custom_page' => ['wordpress_update_custom_page', []], |
| 149 |
'delete_custom_page' => ['wordpress_delete_custom_page', []], |
| 150 |
|
| 151 |
// Code Snippets |
| 152 |
'get_header_snippet' => ['wordpress_get_header_snippet', []], |
| 153 |
'update_header_snippet' => ['wordpress_update_header_snippet', []], |
| 154 |
'get_footer_snippet' => ['wordpress_get_footer_snippet', []], |
| 155 |
'update_footer_snippet' => ['wordpress_update_footer_snippet', []], |
| 156 |
'get_post_snippets' => ['wordpress_get_post_snippets', []], |
| 157 |
'update_post_snippets' => ['wordpress_update_post_snippets', []], |
| 158 |
|
| 159 |
// Categories & Taxonomies |
| 160 |
'list_categories' => ['wordpress_list_categories', []], |
| 161 |
'get_category' => ['wordpress_get_category', []], |
| 162 |
'create_category' => ['wordpress_create_category', []], |
| 163 |
'update_category' => ['wordpress_update_category', []], |
| 164 |
'delete_category' => ['wordpress_delete_category', []], |
| 165 |
'get_post_categories' => ['wordpress_get_post_categories', []], |
| 166 |
'set_post_categories' => ['wordpress_set_post_categories', []], |
| 167 |
|
| 168 |
// Media & Featured Images |
| 169 |
'get_featured_image' => ['wordpress_get_featured_image', []], |
| 170 |
'set_featured_image' => ['wordpress_set_featured_image', []], |
| 171 |
'upload_featured_image' => ['wordpress_upload_featured_image', []], |
| 172 |
'remove_featured_image' => ['wordpress_remove_featured_image', []], |
| 173 |
'list_media' => ['wordpress_list_media', []], |
| 174 |
'get_media_details' => ['wordpress_get_media_details', []], |
| 175 |
|
| 176 |
// Post CRUD |
| 177 |
'create_post' => ['wordpress_create_post', []], |
| 178 |
'delete_post' => ['wordpress_delete_post', []], |
| 179 |
'restore_post' => ['wordpress_restore_post', []], |
| 180 |
|
| 181 |
// Enhanced Robots |
| 182 |
'add_robots_rule' => ['wordpress_add_robots_rule', []], |
| 183 |
'remove_robots_rule' => ['wordpress_remove_robots_rule', []], |
| 184 |
'parse_robots_txt' => ['wordpress_parse_robots_txt', []], |
| 185 |
'validate_robots_txt' => ['wordpress_validate_robots_txt', []], |
| 186 |
|
| 187 |
// Bulk Operations |
| 188 |
'bulk_update_meta' => ['wordpress_bulk_update_meta', []], |
| 189 |
'bulk_set_categories' => ['wordpress_bulk_set_categories', []], |
| 190 |
'bulk_change_status' => ['wordpress_bulk_change_status', []], |
| 191 |
'bulk_delete_posts' => ['wordpress_bulk_delete_posts', []], |
| 192 |
|
| 193 |
// WordPress Core SEO Settings |
| 194 |
'get_site_info' => ['wordpress_get_site_info', []], |
| 195 |
'update_site_info' => ['wordpress_update_site_info', []], |
| 196 |
'get_permalink_structure' => ['wordpress_get_permalink_structure', []], |
| 197 |
'update_permalink_structure' => ['wordpress_update_permalink_structure', []], |
| 198 |
'get_reading_settings' => ['wordpress_get_reading_settings', []], |
| 199 |
'update_reading_settings' => ['wordpress_update_reading_settings', []], |
| 200 |
'get_search_visibility' => ['wordpress_get_search_visibility', []], |
| 201 |
'update_search_visibility' => ['wordpress_update_search_visibility', []], |
| 202 |
'get_date_format' => ['wordpress_get_date_format', []], |
| 203 |
'get_discussion_settings' => ['wordpress_get_discussion_settings', []], |
| 204 |
]; |
| 205 |
|
| 206 |
if (!isset($tool_mapping[$action_name])) { |
| 207 |
throw new Exception("Unknown OTTO action: {$action_name}"); |
| 208 |
} |
| 209 |
|
| 210 |
[$tool_name, $default_params] = $tool_mapping[$action_name]; |
| 211 |
$merged_params = array_merge($default_params, $params); |
| 212 |
|
| 213 |
return $this->execute_mcp_tool($tool_name, $merged_params); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Check if MCP tool exists |
| 218 |
* |
| 219 |
* @param string $tool_name Tool name to check |
| 220 |
* @return bool True if tool exists |
| 221 |
*/ |
| 222 |
public function has_tool($tool_name) { |
| 223 |
global $metasync_mcp_server; |
| 224 |
|
| 225 |
if (!$metasync_mcp_server) { |
| 226 |
return false; |
| 227 |
} |
| 228 |
|
| 229 |
return $metasync_mcp_server->get_tool_registry()->has_tool($tool_name); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Get tool count |
| 234 |
* |
| 235 |
* @return int Number of registered tools |
| 236 |
*/ |
| 237 |
public function get_tool_count() { |
| 238 |
global $metasync_mcp_server; |
| 239 |
|
| 240 |
if (!$metasync_mcp_server) { |
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
return $metasync_mcp_server->get_tool_registry()->get_tool_count(); |
| 245 |
} |
| 246 |
} |
| 247 |
|