AI agents: the switch, who may call and the limit; how to connect an agent; and a * "Try it" panel that calls an ability as the administrator. */ final class AgentsScreen { private const TRY_TRANSIENT = 'aipcf_agents_try_'; public const MCP_ADAPTER = 'https://github.com/WordPress/mcp-adapter'; public static function boot(): void { add_action('admin_post_aipcf_agents_try', [self::class, 'handleTry']); } public static function render(): string { $body = Ui::row(__('AI agents', 'aipcf-ai-provider-for-cloudflare'), Ui::toggle('agents_enabled', !empty(Settings::get('agents_enabled')), __('Let AI agents search and quote your site', 'aipcf-ai-provider-for-cloudflare'), 'aipcf-agents-enabled'), [ 'for' => 'aipcf-agents-enabled', 'help' => __('Read-only: agents cannot change anything.', 'aipcf-ai-provider-for-cloudflare'), ]); $body .= Ui::row(__('Who can connect', 'aipcf-ai-provider-for-cloudflare'), Ui::select('agents_capability', SearchScreen::who(), (string) Settings::get('agents_capability'), 'aipcf-agents-capability'), [ 'for' => 'aipcf-agents-capability', 'help' => __('An agent signs in as a user, with an application password.', 'aipcf-ai-provider-for-cloudflare'), ]); $body .= Ui::row(__('Daily limit', 'aipcf-ai-provider-for-cloudflare'), Ui::number('agents_daily_neurons', (int) Settings::get('agents_daily_neurons'), 'aipcf-agents-daily-neurons', ['min' => 1, 'max' => 10000000, 'unit' => __('neurons a day', 'aipcf-ai-provider-for-cloudflare')]), [ 'for' => 'aipcf-agents-daily-neurons', 'help' => __('About 2 neurons a search, and 10 to 15 an answer.', 'aipcf-ai-provider-for-cloudflare'), ]); $html = Ui::card(__('AI agents', 'aipcf-ai-provider-for-cloudflare'), $body, [ 'text' => __('Each call is checked as the user, limited to 30 a minute, and listed in Activity.', 'aipcf-ai-provider-for-cloudflare'), ]); return $html . self::connect(); } /** * How to connect: the MCP adapter, an application password, and the client settings. */ private static function connect(): string { $user = wp_get_current_user(); $config = [ 'mcpServers' => [ sanitize_title((string) get_bloginfo('name')) ?: 'wordpress' => [ 'command' => 'npx', 'args' => ['-y', '@automattic/mcp-wordpress-remote@latest'], 'env' => [ 'WP_API_URL' => Agents::mcpUrl(), 'WP_API_USERNAME' => $user->user_login, 'WP_API_PASSWORD' => 'your application password', ], ], ], ]; $json = (string) wp_json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); $steps = Ui::checklist([ ['done' => Agents::mcpAdapter(), 'text' => __('Install and activate the MCP adapter plugin.', 'aipcf-ai-provider-for-cloudflare'), 'action' => Ui::external(__('MCP adapter', 'aipcf-ai-provider-for-cloudflare'), self::MCP_ADAPTER)], ['done' => false, 'text' => __('Make an application password for the user the agent signs in as.', 'aipcf-ai-provider-for-cloudflare'), 'action' => Ui::link(__('Application passwords', 'aipcf-ai-provider-for-cloudflare'), admin_url('profile.php#application-passwords-section'))], ['done' => false, 'text' => __('Add these settings to the agent, for example Claude Desktop.', 'aipcf-ai-provider-for-cloudflare')], ]); $copy = '
'; $pre = '
' . esc_html($json) . '
'; $facts = []; foreach (Agents::names() as $name => $label) { $facts[] = [$label, $name]; } return Ui::card(__('Connect an agent', 'aipcf-ai-provider-for-cloudflare'), $steps . $pre . $copy . Ui::facts($facts, __('What agents can do', 'aipcf-ai-provider-for-cloudflare')), [ 'text' => __('Agents reach the site through the MCP adapter.', 'aipcf-ai-provider-for-cloudflare'), ]); } /** * "Try it", after the settings form: call an ability as you, and see what an agent gets. */ public static function tryIt(): string { $user = get_current_user_id(); $last = get_transient(self::TRY_TRANSIENT . $user); $name = is_array($last) ? (string) $last['ability'] : Agents::SEARCH; $q = is_array($last) ? (string) $last['question'] : ''; $form = '
' . '' . wp_nonce_field('aipcf_agents_try', '_wpnonce', true, false) . Ui::row(__('Ability', 'aipcf-ai-provider-for-cloudflare'), Ui::select('ability', Agents::names(), $name, 'aipcf-agents-ability'), ['for' => 'aipcf-agents-ability']) . Ui::row(__('Question, or post ID', 'aipcf-ai-provider-for-cloudflare'), Ui::text('question', $q, 'aipcf-agents-question'), ['for' => 'aipcf-agents-question']) . '
' . '
'; if (is_array($last)) { $form .= '
' . esc_html((string) $last['result']) . '
'; } $note = Agents::enabled() ? '' : Ui::notice(__('Switch AI agents on and save first.', 'aipcf-ai-provider-for-cloudflare'), 'info'); return Ui::card(__('Try it', 'aipcf-ai-provider-for-cloudflare'), $note . $form, [ 'id' => 'aipcf-agents-try', 'text' => __('Calls an ability as you and shows what an agent gets. It counts like a real call.', 'aipcf-ai-provider-for-cloudflare'), ]); } public static function handleTry(): never { if (!current_user_can('manage_options')) { wp_die(esc_html__('Sorry, you are not allowed to do that.', 'aipcf-ai-provider-for-cloudflare'), 403); } check_admin_referer('aipcf_agents_try'); $in = (array) (wp_unslash($_POST)[AIPCF_SETTINGS_OPTION] ?? []); // the fields are named like the settings $name = sanitize_text_field((string) ($in['ability'] ?? '')); $question = sanitize_text_field((string) ($in['question'] ?? '')); $name = isset(Agents::names()[$name]) ? $name : Agents::SEARCH; $input = $name === Agents::RELATED ? ['post' => ctype_digit($question) ? (int) $question : $question] : ['question' => $question]; $ability = function_exists('wp_has_ability') && wp_has_ability($name) ? wp_get_ability($name) : null; if ($ability === null) { $result = __('Not available: switch AI agents on and save.', 'aipcf-ai-provider-for-cloudflare'); } else { $out = $ability->execute($input); $result = is_wp_error($out) ? $out->get_error_code() . ': ' . $out->get_error_message() : (string) wp_json_encode($out, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); } set_transient(self::TRY_TRANSIENT . get_current_user_id(), ['ability' => $name, 'question' => $question, 'result' => $result], 10 * MINUTE_IN_SECONDS); wp_safe_redirect(FeaturesPage::url('agents') . '#aipcf-agents-try'); exit; } }