| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentBoards\App\Modules\MCP\MCPInit; |
| 6 |
use FluentBoards\App\Services\MCPAccessService; |
| 7 |
use FluentBoards\Framework\Http\Request\Request; |
| 8 |
|
| 9 |
/** |
| 10 |
* Settings endpoints for connecting Fluent Boards MCP tools to AI agents. |
| 11 |
*/ |
| 12 |
class MCPSettingsController extends Controller |
| 13 |
{ |
| 14 |
const ADAPTER_PLUGIN_FILE = 'mcp-adapter/mcp-adapter.php'; |
| 15 |
const TOOLKIT_PLUGIN_FILE = 'fluent-toolkit/fluent-toolkit.php'; |
| 16 |
|
| 17 |
public function status() |
| 18 |
{ |
| 19 |
if (!function_exists('is_plugin_active')) { |
| 20 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 21 |
} |
| 22 |
|
| 23 |
$adapterInstalled = $this->isAdapterPresent(); |
| 24 |
$toolkitInstalled = $this->isToolkitPresent(); |
| 25 |
$adapterRuntimeAvailable = $this->isAdapterRuntimeAvailable(); |
| 26 |
$standaloneActive = is_plugin_active(self::ADAPTER_PLUGIN_FILE) && $adapterRuntimeAvailable; |
| 27 |
$toolkitActive = $this->isToolkitLoaded(); |
| 28 |
$toolkitAdapterActive = $toolkitActive && $this->isToolkitAdapterAvailable(); |
| 29 |
$adapterActive = $standaloneActive || $toolkitAdapterActive; |
| 30 |
$adapterProvider = $standaloneActive ? 'plugin' : ($toolkitAdapterActive ? 'toolkit' : ''); |
| 31 |
$abilitiesAvailable = function_exists('wp_register_ability'); |
| 32 |
$canAutoInstall = $this->canAutoInstallFluentKit(); |
| 33 |
$currentUser = wp_get_current_user(); |
| 34 |
|
| 35 |
return $this->sendSuccess([ |
| 36 |
'adapter_installed' => $adapterInstalled || $toolkitInstalled, |
| 37 |
'adapter_active' => $adapterActive, |
| 38 |
'adapter_provider' => $adapterProvider, |
| 39 |
'standalone_adapter_installed' => $adapterInstalled, |
| 40 |
'toolkit_installed' => $toolkitInstalled, |
| 41 |
'toolkit_active' => $toolkitActive, |
| 42 |
'toolkit_adapter_available' => $toolkitAdapterActive, |
| 43 |
'adapter_runtime_available' => $adapterRuntimeAvailable, |
| 44 |
'adapter_version' => $this->detectAdapterVersion(), |
| 45 |
'toolkit_version' => $this->detectToolkitVersion(), |
| 46 |
'abilities_api_loaded' => $abilitiesAvailable, |
| 47 |
'endpoint_url' => MCPInit::getEndpointUrl(), |
| 48 |
'tools_count' => $abilitiesAvailable ? MCPInit::getToolsCount() : 0, |
| 49 |
'mcp_enabled' => fluent_boards_get_option('mcp_enabled', 'yes') === 'yes', |
| 50 |
'mcp_member_access' => MCPAccessService::isMemberAccessEnabled(), |
| 51 |
'plugins_url' => admin_url('plugins.php'), |
| 52 |
'can_auto_install_adapter' => $canAutoInstall, |
| 53 |
'toolkit_download_url' => 'https://github.com/WPManageNinja/fluent-toolkit', |
| 54 |
'current_user_login' => $currentUser ? $currentUser->user_login : '', |
| 55 |
'is_local_dev' => MCPAccessService::isLocalDev(), |
| 56 |
]); |
| 57 |
} |
| 58 |
|
| 59 |
public function toggle(Request $request) |
| 60 |
{ |
| 61 |
$value = $request->get('mcp_enabled'); |
| 62 |
$enabled = is_string($value) ? ($value === 'yes' || $value === 'true' || $value === '1') : (bool) $value; |
| 63 |
|
| 64 |
fluent_boards_update_option('mcp_enabled', $enabled ? 'yes' : 'no'); |
| 65 |
|
| 66 |
return $this->sendSuccess([ |
| 67 |
'ok' => true, |
| 68 |
'mcp_enabled' => $enabled, |
| 69 |
'message' => $enabled |
| 70 |
? __('MCP tools enabled. New requests will see the Fluent Boards abilities.', 'fluent-boards') |
| 71 |
: __('MCP tools disabled. The adapter will no longer report Fluent Boards abilities.', 'fluent-boards'), |
| 72 |
]); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* All-or-none switch: when on, every board member can use MCP and create keys from Account. |
| 77 |
* Turning it off blocks members immediately; their keys are kept but stop working. |
| 78 |
*/ |
| 79 |
public function toggleMemberAccess(Request $request) |
| 80 |
{ |
| 81 |
$value = $request->get('mcp_member_access'); |
| 82 |
$enabled = is_string($value) ? in_array($value, ['yes', 'true', '1'], true) : (bool) $value; |
| 83 |
|
| 84 |
fluent_boards_update_option('mcp_member_access', $enabled ? 'yes' : 'no'); |
| 85 |
|
| 86 |
return $this->sendSuccess([ |
| 87 |
'mcp_member_access' => $enabled, |
| 88 |
'message' => $enabled |
| 89 |
? __('Board members can now create their own MCP connections.', 'fluent-boards') |
| 90 |
: __('MCP is now limited to administrators. Member connections are blocked.', 'fluent-boards'), |
| 91 |
]); |
| 92 |
} |
| 93 |
|
| 94 |
public function installAdapter() |
| 95 |
{ |
| 96 |
if (!current_user_can('install_plugins')) { |
| 97 |
return $this->sendError([ |
| 98 |
'message' => __('Sorry! you do not have permission to install plugins', 'fluent-boards'), |
| 99 |
]); |
| 100 |
} |
| 101 |
|
| 102 |
$canAutoInstall = $this->canAutoInstallFluentKit(); |
| 103 |
if (!$canAutoInstall) { |
| 104 |
return $this->sendError([ |
| 105 |
'message' => __('Please install FluentHub from GitHub, then reload this page to connect Fluent Boards with AI agents.', 'fluent-boards'), |
| 106 |
'toolkit_download_url' => 'https://github.com/WPManageNinja/fluent-toolkit', |
| 107 |
]); |
| 108 |
} |
| 109 |
|
| 110 |
$this->autoInstallFluentKit(); |
| 111 |
|
| 112 |
wp_clean_plugins_cache(); |
| 113 |
|
| 114 |
if (!function_exists('is_plugin_active')) { |
| 115 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 116 |
} |
| 117 |
|
| 118 |
$toolkitInstalled = $this->isToolkitPresent(); |
| 119 |
$toolkitActive = $this->isToolkitLoaded(); |
| 120 |
$adapterRuntimeAvailable = $this->isAdapterRuntimeAvailable(); |
| 121 |
$toolkitAdapterAvailable = $toolkitActive && $this->isToolkitAdapterAvailable(); |
| 122 |
$isInstalled = $this->isAdapterPresent() || $toolkitInstalled; |
| 123 |
$isActive = (is_plugin_active(self::ADAPTER_PLUGIN_FILE) && $adapterRuntimeAvailable) || $toolkitAdapterAvailable; |
| 124 |
|
| 125 |
if ($isInstalled && $isActive) { |
| 126 |
$message = __('FluentHub installed and activated. Reload the page to register Fluent Boards MCP tools.', 'fluent-boards'); |
| 127 |
} elseif ($toolkitInstalled && $toolkitActive) { |
| 128 |
$message = __('FluentHub is installed and active, but this version does not include the bundled MCP adapter yet. Please update FluentHub when the MCP-ready build is available, then reload this page.', 'fluent-boards'); |
| 129 |
} elseif ($toolkitInstalled) { |
| 130 |
$message = __('FluentHub is installed but could not be activated automatically. Please activate FluentHub from the Plugins page, then reload this page.', 'fluent-boards'); |
| 131 |
} else { |
| 132 |
$message = __('Could not install FluentHub automatically. Please install FluentHub manually, then reload this page.', 'fluent-boards'); |
| 133 |
} |
| 134 |
|
| 135 |
return $this->sendSuccess([ |
| 136 |
'is_installed' => $isInstalled, |
| 137 |
'adapter_active' => $isActive, |
| 138 |
'toolkit_active' => $toolkitActive, |
| 139 |
'toolkit_adapter_available' => $toolkitAdapterAvailable, |
| 140 |
'message' => $message, |
| 141 |
]); |
| 142 |
} |
| 143 |
|
| 144 |
private function isAdapterPresent() |
| 145 |
{ |
| 146 |
return $this->isPluginPresent(self::ADAPTER_PLUGIN_FILE); |
| 147 |
} |
| 148 |
|
| 149 |
private function isToolkitPresent() |
| 150 |
{ |
| 151 |
return $this->isToolkitLoaded() || $this->isPluginPresent(self::TOOLKIT_PLUGIN_FILE); |
| 152 |
} |
| 153 |
|
| 154 |
private function detectAdapterVersion() |
| 155 |
{ |
| 156 |
return $this->detectPluginVersion(self::ADAPTER_PLUGIN_FILE); |
| 157 |
} |
| 158 |
|
| 159 |
private function detectToolkitVersion() |
| 160 |
{ |
| 161 |
if ($this->isToolkitLoaded()) { |
| 162 |
return (string) FLUENT_TOOLKIT_VERSION; |
| 163 |
} |
| 164 |
|
| 165 |
return $this->detectPluginVersion(self::TOOLKIT_PLUGIN_FILE); |
| 166 |
} |
| 167 |
|
| 168 |
private function isToolkitLoaded() |
| 169 |
{ |
| 170 |
return defined('FLUENT_TOOLKIT_VERSION'); |
| 171 |
} |
| 172 |
|
| 173 |
private function canAutoInstallFluentKit() |
| 174 |
{ |
| 175 |
$canAutoInstall = (bool) apply_filters('fluent_kit/can_auto_install', false); |
| 176 |
|
| 177 |
if (!$canAutoInstall) { |
| 178 |
$canAutoInstall = (bool) apply_filters('fluent_toolkit/can_auto_install', false); |
| 179 |
} |
| 180 |
|
| 181 |
return $canAutoInstall; |
| 182 |
} |
| 183 |
|
| 184 |
private function autoInstallFluentKit() |
| 185 |
{ |
| 186 |
if ((bool) apply_filters('fluent_kit/can_auto_install', false)) { |
| 187 |
do_action('fluent_kit/do_auto_install'); |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
do_action('fluent_toolkit/do_auto_install'); |
| 192 |
} |
| 193 |
|
| 194 |
private function isPluginPresent($pluginFile) |
| 195 |
{ |
| 196 |
if (!function_exists('get_plugins')) { |
| 197 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 198 |
} |
| 199 |
|
| 200 |
$plugins = get_plugins(); |
| 201 |
return isset($plugins[$pluginFile]); |
| 202 |
} |
| 203 |
|
| 204 |
private function detectPluginVersion($pluginFile) |
| 205 |
{ |
| 206 |
if (!function_exists('get_plugins')) { |
| 207 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 208 |
} |
| 209 |
|
| 210 |
$plugins = get_plugins(); |
| 211 |
if (!isset($plugins[$pluginFile])) { |
| 212 |
return null; |
| 213 |
} |
| 214 |
|
| 215 |
return $plugins[$pluginFile]['Version'] ?? null; |
| 216 |
} |
| 217 |
|
| 218 |
private function isToolkitAdapterAvailable() |
| 219 |
{ |
| 220 |
if (!$this->isToolkitLoaded()) { |
| 221 |
return false; |
| 222 |
} |
| 223 |
|
| 224 |
if (class_exists('\FluentToolkit\Mcp\AdapterBootstrap') && method_exists('\FluentToolkit\Mcp\AdapterBootstrap', 'available')) { |
| 225 |
return (bool) \FluentToolkit\Mcp\AdapterBootstrap::available(); |
| 226 |
} |
| 227 |
|
| 228 |
return $this->isAdapterRuntimeAvailable(); |
| 229 |
} |
| 230 |
|
| 231 |
private function isAdapterRuntimeAvailable() |
| 232 |
{ |
| 233 |
return defined('WP_MCP_VERSION') |
| 234 |
&& class_exists('\WP\MCP\Core\McpAdapter') |
| 235 |
&& function_exists('wp_register_ability'); |
| 236 |
} |
| 237 |
|
| 238 |
} |
| 239 |
|