feat: settings page with connection tab, admin notices and site health test

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
This commit is contained in:
2026-09-26 03:21:56 +02:00
co-authored by Claude Opus 5.5
parent 2674bf2ed1
commit f084c47311
11 changed files with 731 additions and 0 deletions
+63
View File
@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Admin;
/**
* Admin notices for administrators (Etappe-0 follow-up d). Uses only cached check
* results so no HTTP request runs on ordinary admin page loads.
*/
final class AdminNotices
{
public function __construct(private ConnectionStatus $status)
{
}
public function register(): void
{
add_action('admin_notices', [$this, 'render']);
}
/** @return list<string> */
public function messages(bool $onOwnPage): array
{
$messages = [];
if (! $this->status->oauthEnabled()) {
$messages[] = __(
'LIT MCP: Der OAuth-Server ist deaktiviert (Filter wpmedia_mcp_oauth_server_enabled).',
'wp-lit-mcp'
) . ' ' . __('Claude kann sich nicht verbinden.', 'wp-lit-mcp');
}
$discovery = $this->status->discovery(false);
if (is_array($discovery) && ($discovery['status'] ?? 'good') === 'critical') {
$messages[] = __(
'LIT MCP: Die OAuth-Discovery-Dokumente unter /.well-known/ sind nicht erreichbar.',
'wp-lit-mcp'
) . ' ' . __('Details unter Werkzeuge → Website-Zustand.', 'wp-lit-mcp');
}
if ($onOwnPage && ! $this->status->https()) {
$messages[] = __(
'LIT MCP: Die Website läuft ohne HTTPS; claude.ai verbindet sich nur mit HTTPS-Websites.',
'wp-lit-mcp'
);
}
return $messages;
}
public function render(): void
{
if (! current_user_can('manage_options')) {
return;
}
$screen = function_exists('get_current_screen') ? get_current_screen() : null;
$onOwnPage = is_object($screen) && $screen->id === 'settings_page_' . SettingsPage::SLUG;
foreach ($this->messages($onOwnPage) as $message) {
printf('<div class="notice notice-warning"><p>%s</p></div>', esc_html($message));
}
}
}
+23
View File
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Admin;
/**
* One tab of the settings page "Einstellungen → LIT MCP".
*/
interface AdminTab
{
public function slug(): string;
public function label(): string;
/** Registers Settings API options; runs on admin_init. */
public function registerSettings(): void;
/** Registers admin-post.php handlers; runs when the page is registered. */
public function registerActions(): void;
public function render(): void;
}
+129
View File
@@ -0,0 +1,129 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Admin;
use LIT\MCP\Server\ServerRegistrar;
use WP\MCP\Core\McpAdapter;
use WPMedia\MCP\OAuth\Auth\Discovery\HealthCheck;
use WPMedia\MCP\OAuth\Context as OAuthContext;
/**
* Connection checks for the settings page, admin notices and Site Health.
* Not final so notices and Site Health can be tested with a mock.
*/
class ConnectionStatus
{
public const ENDPOINT_TRANSIENT = 'lit_mcp_endpoint_check';
private const CACHE_SECONDS = 300;
/** @var callable(): bool */
private $oauthEnabled;
public function __construct(?callable $oauthEnabled = null)
{
$this->oauthEnabled = $oauthEnabled ?? static fn (): bool => (new OAuthContext())->is_enabled();
}
public static function serverUrl(): string
{
return rest_url(ServerRegistrar::NAMESPACE . '/' . ServerRegistrar::ROUTE);
}
public function oauthEnabled(): bool
{
return ($this->oauthEnabled)();
}
public function adapterLoaded(): bool
{
return class_exists(McpAdapter::class);
}
public function https(): bool
{
return str_starts_with((string) home_url(), 'https://');
}
/** @return array<string, mixed>|null mcp-oauth's discovery self check (label, status, …). */
public function discovery(bool $allowFetch): ?array
{
if (! $allowFetch) {
$cached = get_transient(HealthCheck::TRANSIENT_KEY);
return is_array($cached) ? $cached : null;
}
return (new HealthCheck(new OAuthContext()))->run_self_check();
}
/** @return array{ok: bool, message: string}|null */
public function endpoint(bool $allowFetch): ?array
{
$cached = get_transient(self::ENDPOINT_TRANSIENT);
if (is_array($cached)) {
return $cached;
}
if (! $allowFetch) {
return null;
}
$response = wp_remote_post(self::serverUrl(), [
'timeout' => 5,
'headers' => ['Content-Type' => 'application/json'],
'body' => '{"jsonrpc":"2.0","id":1,"method":"ping"}',
]);
if (is_wp_error($response)) {
$result = ['ok' => false, 'message' => 'Endpunkt nicht erreichbar: ' . $response->get_error_message()];
} else {
$code = (int) wp_remote_retrieve_response_code($response);
$challenge = (string) wp_remote_retrieve_header($response, 'www-authenticate');
$result = $code === 401 && str_starts_with($challenge, 'Bearer')
? ['ok' => true, 'message' => 'Ohne Token: 401 mit WWW-Authenticate – korrekt.']
: ['ok' => false, 'message' => sprintf('Erwartet 401 mit WWW-Authenticate, erhalten HTTP %d.', $code)];
}
set_transient(self::ENDPOINT_TRANSIENT, $result, self::CACHE_SECONDS);
return $result;
}
/** @return list<array{label: string, ok: ?bool, message: string}> */
public function checks(bool $allowFetch): array
{
$adapter = $this->adapterLoaded();
$oauth = $this->oauthEnabled();
$https = $this->https();
$discovery = $this->discovery($allowFetch);
$endpoint = $this->endpoint($allowFetch);
return [
['label' => 'MCP-Adapter', 'ok' => $adapter, 'message' => $adapter ? 'geladen' : 'fehlt'],
[
'label' => 'OAuth-Server',
'ok' => $oauth,
'message' => $oauth ? 'aktiv' : 'deaktiviert (Filter wpmedia_mcp_oauth_server_enabled)',
],
[
'label' => 'HTTPS',
'ok' => $https,
'message' => $https ? 'aktiv' : 'Website läuft ohne HTTPS – claude.ai verbindet sich nur über HTTPS',
],
[
'label' => 'Discovery (/.well-known/)',
'ok' => $discovery === null ? null : ($discovery['status'] ?? '') === 'good',
'message' => $discovery === null
? 'noch nicht geprüft'
: wp_strip_all_tags((string) ($discovery['label'] ?? '')),
],
[
'label' => 'Endpunkt ohne Token',
'ok' => $endpoint['ok'] ?? null,
'message' => $endpoint['message'] ?? 'noch nicht geprüft',
],
];
}
}
+58
View File
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Admin;
final class ConnectionTab implements AdminTab
{
public function __construct(private ConnectionStatus $status)
{
}
public function slug(): string
{
return 'verbindung';
}
public function label(): string
{
return __('Verbindung', 'wp-lit-mcp');
}
public function registerSettings(): void
{
}
public function registerActions(): void
{
}
public function render(): void
{
echo '<h2>' . esc_html__('MCP-Server', 'wp-lit-mcp') . '</h2>';
printf(
'<p><label for="lit-mcp-url">%s</label><br>'
. '<input id="lit-mcp-url" type="text" class="large-text code" readonly value="%s"></p>',
esc_html__('Server-URL für den Connector', 'wp-lit-mcp'),
esc_attr(ConnectionStatus::serverUrl())
);
echo '<p>' . esc_html__(
'In claude.ai unter Einstellungen → Connectors → „Add custom connector“ eintragen;',
'wp-lit-mcp'
) . ' ' . esc_html__('Die Anmeldung läuft danach über diese Website.', 'wp-lit-mcp') . '</p>';
echo '<h2>' . esc_html__('Status', 'wp-lit-mcp') . '</h2><table class="widefat striped"><tbody>';
foreach ($this->status->checks(true) as $check) {
$state = $check['ok'] === null ? '–' : ($check['ok'] ? '✔' : '✖');
printf(
'<tr><th scope="row">%s</th><td>%s</td><td>%s</td></tr>',
esc_html($check['label']),
esc_html($state),
esc_html($check['message'])
);
}
echo '</tbody></table>';
echo '<p>' . esc_html__('Weitere Prüfungen unter Werkzeuge → Website-Zustand.', 'wp-lit-mcp') . '</p>';
}
}
+116
View File
@@ -0,0 +1,116 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Admin;
/**
* Settings page "Einstellungen → LIT MCP" with tabs (spec §8.4). Settings API, no JS framework.
*/
final class SettingsPage
{
public const SLUG = 'lit-mcp';
/** @var array<string, AdminTab> */
private array $tabs = [];
/** @param list<AdminTab> $tabs The first tab is the default. */
public function __construct(array $tabs)
{
foreach ($tabs as $tab) {
$this->tabs[$tab->slug()] = $tab;
}
}
public function register(): void
{
add_action('admin_menu', [$this, 'addMenu']);
add_action('admin_init', [$this, 'registerSettings']);
foreach ($this->tabs as $tab) {
$tab->registerActions();
}
}
public function addMenu(): void
{
add_options_page(
__('LIT MCP', 'wp-lit-mcp'),
__('LIT MCP', 'wp-lit-mcp'),
'manage_options',
self::SLUG,
[$this, 'render']
);
}
public function registerSettings(): void
{
foreach ($this->tabs as $tab) {
$tab->registerSettings();
}
}
/** @return list<string> */
public function tabSlugs(): array
{
return array_keys($this->tabs);
}
public function currentTab(): string
{
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only tab switch.
$requested = isset($_GET['tab']) ? sanitize_key(wp_unslash((string) $_GET['tab'])) : '';
return isset($this->tabs[$requested]) ? $requested : (string) array_key_first($this->tabs);
}
/** @param array<string, string> $args */
public static function url(string $tab = '', array $args = []): string
{
$query = array_merge(['page' => self::SLUG], $tab !== '' ? ['tab' => $tab] : [], $args);
return add_query_arg($query, admin_url('options-general.php'));
}
public function render(): void
{
if (! current_user_can('manage_options')) {
return;
}
$current = $this->currentTab();
echo '<div class="wrap"><h1>' . esc_html__('LIT MCP', 'wp-lit-mcp') . '</h1>';
$this->renderNotice();
echo '<nav class="nav-tab-wrapper">';
foreach ($this->tabs as $slug => $tab) {
printf(
'<a href="%s" class="nav-tab%s">%s</a>',
esc_url(self::url($slug)),
$slug === $current ? ' nav-tab-active' : '',
esc_html($tab->label())
);
}
echo '</nav>';
$this->tabs[$current]->render();
echo '</div>';
}
private function renderNotice(): void
{
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only.
$key = isset($_GET['lit_mcp_notice']) ? sanitize_key(wp_unslash((string) $_GET['lit_mcp_notice'])) : '';
$notice = match ($key) {
'revoked' => ['success', __('Verbindung widerrufen.', 'wp-lit-mcp')],
'revoke_failed' => ['error', __('Die Verbindung konnte nicht widerrufen werden.', 'wp-lit-mcp')],
'cleared' => ['success', __('Protokoll geleert.', 'wp-lit-mcp')],
default => null,
};
if ($notice !== null) {
printf(
'<div class="notice notice-%s is-dismissible"><p>%s</p></div>',
esc_attr($notice[0]),
esc_html($notice[1])
);
}
}
}
+63
View File
@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Admin;
/**
* Site Health test for our MCP endpoint (401 + WWW-Authenticate without a token).
* The discovery documents are covered by mcp-oauth's own Site Health test.
*/
final class SiteHealth
{
public const TEST = 'lit_mcp_endpoint';
public function __construct(private ConnectionStatus $status)
{
}
public function register(): void
{
add_filter('site_status_tests', [$this, 'addTest']);
}
public function addTest(mixed $tests): mixed
{
if (! is_array($tests)) {
return $tests;
}
$tests['direct'][self::TEST] = [
'label' => __('LIT MCP Endpunkt', 'wp-lit-mcp'),
'test' => [$this, 'run'],
];
return $tests;
}
/** @return array<string, mixed> */
public function run(): array
{
$oauth = $this->status->oauthEnabled();
$endpoint = $oauth ? $this->status->endpoint(true) : null;
$good = $oauth && ($endpoint['ok'] ?? false) === true;
$detail = $oauth
? (string) ($endpoint['message'] ?? '')
: __('Der OAuth-Server ist deaktiviert (Filter wpmedia_mcp_oauth_server_enabled).', 'wp-lit-mcp');
return [
'label' => $good
? __('Der LIT-MCP-Endpunkt ist erreichbar und verlangt OAuth', 'wp-lit-mcp')
: __('Der LIT-MCP-Endpunkt ist nicht nutzbar', 'wp-lit-mcp'),
'status' => $good ? 'good' : 'critical',
'badge' => ['label' => 'LIT MCP', 'color' => $good ? 'blue' : 'red'],
'description' => '<p>' . esc_html($detail) . '</p>',
'actions' => sprintf(
'<a href="%s">%s</a>',
esc_url(SettingsPage::url('verbindung')),
esc_html__('Einstellungen → LIT MCP', 'wp-lit-mcp')
),
'test' => self::TEST,
];
}
}
+21
View File
@@ -6,6 +6,11 @@ namespace LIT\MCP;
use LIT\MCP\Abilities\AbilityRegistrar;
use LIT\MCP\Abilities\ExposurePolicy;
use LIT\MCP\Admin\AdminNotices;
use LIT\MCP\Admin\ConnectionStatus;
use LIT\MCP\Admin\ConnectionTab;
use LIT\MCP\Admin\SettingsPage;
use LIT\MCP\Admin\SiteHealth;
use LIT\MCP\ChangeLog\ChangeLog;
use LIT\MCP\ChangeLog\ChangeLogCleanup;
use LIT\MCP\ChangeLog\ChangeLogInstaller;
@@ -30,6 +35,7 @@ final class Plugin
public const VERSION = '0.1.0';
private static string $pluginFile = '';
private static ?SettingsPage $settingsPage = null;
public static function boot(string $pluginFile): void
{
@@ -68,9 +74,24 @@ final class Plugin
$policy
))->register();
$status = new ConnectionStatus();
(new SiteHealth($status))->register();
self::$settingsPage = new SettingsPage([
new ConnectionTab($status),
]);
if (is_admin()) {
self::$settingsPage->register();
(new AdminNotices($status))->register();
}
\WPMedia\MCP\OAuth\Bootstrap::instance();
}
public static function settingsPage(): ?SettingsPage
{
return self::$settingsPage;
}
public static function pluginDir(): string
{
return dirname(self::$pluginFile);