fix: register_setting for settings page + clean up Claude connector instructions

This commit is contained in:
2026-05-08 04:03:56 +00:00
parent 0df02bd8a1
commit fc7925b4d9
+43 -83
View File
@@ -9,6 +9,24 @@ class AdminMenu {
public function __construct() { public function __construct() {
add_action('admin_menu', [$this, 'register']); add_action('admin_menu', [$this, 'register']);
add_action('admin_enqueue_scripts', [$this, 'enqueueAssets']); add_action('admin_enqueue_scripts', [$this, 'enqueueAssets']);
add_action('admin_init', [$this, 'registerSettings']);
}
public function registerSettings(): void {
register_setting(
'asktam_mcp_settings_group',
'asktam_mcp_settings',
['sanitize_callback' => [$this, 'sanitizeSettings']]
);
}
public function sanitizeSettings(mixed $input): array {
$clean = [];
$clean['rate_limit'] = max(1, min(300, (int) ($input['rate_limit'] ?? 60)));
$clean['log_requests'] = !empty($input['log_requests']);
$clean['allow_raw_sql'] = !empty($input['allow_raw_sql']);
$clean['wc_enabled'] = !empty($input['wc_enabled']);
return $clean;
} }
public function register(): void { public function register(): void {
@@ -21,7 +39,6 @@ class AdminMenu {
if (!in_array($hook, ['toplevel_page_asktam-mcp', 'asktam-mcp_page_asktam-mcp-settings'], true)) { if (!in_array($hook, ['toplevel_page_asktam-mcp', 'asktam-mcp_page_asktam-mcp-settings'], true)) {
return; return;
} }
// Inline all styles — no external file dependency needed
add_action('admin_head', [$this, 'inlineStyles']); add_action('admin_head', [$this, 'inlineStyles']);
} }
@@ -34,7 +51,6 @@ class AdminMenu {
.mcp-cards { display:grid; grid-template-columns:1fr 1fr; gap:20px; margin-bottom:32px; } .mcp-cards { display:grid; grid-template-columns:1fr 1fr; gap:20px; margin-bottom:32px; }
.mcp-card { background:#fff; border:1px solid #ddd; border-radius:8px; padding:20px; } .mcp-card { background:#fff; border:1px solid #ddd; border-radius:8px; padding:20px; }
.mcp-card h3 { margin:0 0 6px; display:flex; align-items:center; gap:8px; font-size:15px; } .mcp-card h3 { margin:0 0 6px; display:flex; align-items:center; gap:8px; font-size:15px; }
.mcp-card h3 img { width:20px; height:20px; }
.mcp-card p { color:#555; font-size:13px; margin:0 0 14px; } .mcp-card p { color:#555; font-size:13px; margin:0 0 14px; }
.mcp-card pre { background:#1e1e2e; color:#cdd6f4; border-radius:6px; padding:12px 14px; font-size:12px; overflow-x:auto; line-height:1.6; position:relative; margin:0 0 12px; } .mcp-card pre { background:#1e1e2e; color:#cdd6f4; border-radius:6px; padding:12px 14px; font-size:12px; overflow-x:auto; line-height:1.6; position:relative; margin:0 0 12px; }
.mcp-card pre .key-placeholder { color:#f9e2af; } .mcp-card pre .key-placeholder { color:#f9e2af; }
@@ -56,9 +72,6 @@ class AdminMenu {
.mcp-form-row input[type=text] { border:1px solid #ddd; border-radius:4px; padding:7px 10px; font-size:13px; min-width:200px; } .mcp-form-row input[type=text] { border:1px solid #ddd; border-radius:4px; padding:7px 10px; font-size:13px; min-width:200px; }
.mcp-scope-checks { display:flex; gap:12px; flex-wrap:wrap; align-items:center; } .mcp-scope-checks { display:flex; gap:12px; flex-wrap:wrap; align-items:center; }
.mcp-scope-checks label { font-weight:normal; display:flex; align-items:center; gap:5px; font-size:13px; cursor:pointer; } .mcp-scope-checks label { font-weight:normal; display:flex; align-items:center; gap:5px; font-size:13px; cursor:pointer; }
.mcp-new-key-banner { background:#ecfdf5; border:1px solid #6ee7b7; border-radius:6px; padding:14px 18px; margin-bottom:20px; display:none; }
.mcp-new-key-banner strong { color:#065f46; }
.mcp-new-key-banner code { background:#fff; border:1px solid #a7f3d0; padding:4px 10px; border-radius:4px; font-family:monospace; font-size:13px; word-break:break-all; }
@media(max-width:700px){ .mcp-cards{grid-template-columns:1fr;} .mcp-form-row{flex-direction:column;} } @media(max-width:700px){ .mcp-cards{grid-template-columns:1fr;} .mcp-form-row{flex-direction:column;} }
</style> </style>
<?php <?php
@@ -78,7 +91,6 @@ class AdminMenu {
<span style="font-size:12px;font-weight:400;color:#999;background:#f0f0f0;padding:2px 8px;border-radius:10px;">v<?php echo esc_html(ASKTAM_MCP_VERSION); ?></span> <span style="font-size:12px;font-weight:400;color:#999;background:#f0f0f0;padding:2px 8px;border-radius:10px;">v<?php echo esc_html(ASKTAM_MCP_VERSION); ?></span>
</h1> </h1>
<!-- Endpoint bar -->
<div class="mcp-endpoint-bar"> <div class="mcp-endpoint-bar">
<strong>SSE Endpoint:</strong> <strong>SSE Endpoint:</strong>
<code id="mcp-sse-url"><?php echo esc_html($sseUrl); ?></code> <code id="mcp-sse-url"><?php echo esc_html($sseUrl); ?></code>
@@ -86,13 +98,6 @@ class AdminMenu {
<a href="<?php echo esc_url(rest_url('asktam-mcp/v1/info')); ?>" target="_blank" class="button button-small">Info →</a> <a href="<?php echo esc_url(rest_url('asktam-mcp/v1/info')); ?>" target="_blank" class="button button-small">Info →</a>
</div> </div>
<!-- NEW KEY BANNER (shown via JS after form submit) -->
<div class="mcp-new-key-banner" id="mcp-new-key-banner">
<strong>✓ New API key created:</strong><br><br>
<code id="mcp-new-key-value"></code><br><br>
<span style="color:#065f46;font-size:12px;">Copy it now — it won't be shown again in full.</span>
</div>
<!-- Generate key form --> <!-- Generate key form -->
<div class="mcp-generate-form"> <div class="mcp-generate-form">
<h3>Generate API Key</h3> <h3>Generate API Key</h3>
@@ -113,40 +118,35 @@ class AdminMenu {
<label><input type="checkbox" name="scopes[]" value="woocommerce"> WooCommerce</label> <label><input type="checkbox" name="scopes[]" value="woocommerce"> WooCommerce</label>
</div> </div>
</div> </div>
<div> <div><?php submit_button('Generate', 'primary', 'submit', false, ['style' => 'height:34px;margin:0;']); ?></div>
<?php submit_button('Generate', 'primary', 'submit', false, ['style' => 'height:34px;margin:0;']); ?>
</div> </div>
</div> <p style="margin:0;color:#888;font-size:12px;"><strong>Read</strong> = list/get &nbsp;·&nbsp; <strong>Write</strong> = create/update/delete content &nbsp;·&nbsp; <strong>Admin</strong> = plugins/themes/users/options (supersedes all) &nbsp;·&nbsp; <strong>WooCommerce</strong> = product/order mutations</p>
<p style="margin:0;color:#888;font-size:12px;">Scope guide: <strong>Read</strong> = list/get only &nbsp;·&nbsp; <strong>Write</strong> = create/update/delete content &nbsp;·&nbsp; <strong>Admin</strong> = plugins, themes, users, options (supersedes all) &nbsp;·&nbsp; <strong>WooCommerce</strong> = product/order mutations</p>
</form> </form>
</div> </div>
<!-- Integration cards --> <!-- Integration cards -->
<div class="mcp-cards"> <div class="mcp-cards">
<!-- Claude card --> <!-- Claude -->
<div class="mcp-card"> <div class="mcp-card">
<h3> <h3>
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><circle cx="12" cy="12" r="12" fill="#D97757"/><path d="M7.5 16.5L12 7.5L16.5 16.5" stroke="white" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/><path d="M9 13.5H15" stroke="white" stroke-width="1.8" stroke-linecap="round"/></svg> <svg width="20" height="20" viewBox="0 0 24 24" fill="none"><circle cx="12" cy="12" r="12" fill="#D97757"/><path d="M7.5 16.5L12 7.5L16.5 16.5" stroke="white" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/><path d="M9 13.5H15" stroke="white" stroke-width="1.8" stroke-linecap="round"/></svg>
Claude (claude.ai / Claude Desktop) Claude (claude.ai / Claude Desktop)
</h3> </h3>
<p>Add as a remote MCP server via SSE. Works with Claude.ai connectors and Claude Desktop config.</p> <p>Add as a remote MCP server. In the connector dialog, paste the SSE URL and your API key into the <strong>token field</strong> (leave OAuth Client ID blank).</p>
<strong style="font-size:12px;color:#555;display:block;margin-bottom:6px;">claude.ai → Settings → Connectors → Add MCP Server</strong> <strong style="font-size:12px;color:#555;display:block;margin-bottom:4px;">claude.ai → Settings → Connectors → Add custom connector</strong>
<pre id="claude-config">{ <pre style="font-size:12px;background:#f6f7f7;color:#333;border:1px solid #ddd;padding:10px 12px;border-radius:5px;margin-bottom:12px;">URL: <span style="color:#2271b1;"><?php echo esc_html($sseUrl); ?></span>
"url": "<span class="key-url"><?php echo esc_html($sseUrl); ?></span>", Token: <span style="color:#b45309;">YOUR_API_KEY ← paste here</span>
"headers": { OAuth Client ID: (leave blank)</pre>
"X-MCP-API-Key": "<span class="key-placeholder">YOUR_API_KEY</span>"
}
}</pre>
<strong style="font-size:12px;color:#555;display:block;margin-bottom:6px;">Claude Desktop → claude_desktop_config.json</strong> <strong style="font-size:12px;color:#555;display:block;margin-bottom:4px;">Claude Desktop → claude_desktop_config.json</strong>
<pre id="claude-desktop-config" style="position:relative;">{ <pre id="claude-desktop-config" style="position:relative;">{
"mcpServers": { "mcpServers": {
"wordpress": { "wordpress": {
"url": "<span class="key-url"><?php echo esc_html($sseUrl); ?></span>", "url": "<span class="key-url"><?php echo esc_html($sseUrl); ?></span>",
"headers": { "headers": {
"X-MCP-API-Key": "<span class="key-placeholder">YOUR_API_KEY</span>" "Authorization": "Bearer <span class="key-placeholder">YOUR_API_KEY</span>"
} }
} }
} }
@@ -154,33 +154,24 @@ class AdminMenu {
<button class="copy-btn" onclick="copyCode('claude-desktop-config',this)">Copy</button></pre> <button class="copy-btn" onclick="copyCode('claude-desktop-config',this)">Copy</button></pre>
</div> </div>
<!-- OpenAI card --> <!-- OpenAI -->
<div class="mcp-card"> <div class="mcp-card">
<h3> <h3>
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><circle cx="12" cy="12" r="12" fill="#10a37f"/><path d="M12 6.5C9 6.5 7 8.8 7 11.5C7 13.2 7.9 14.7 9.2 15.6L8.5 17.5H15.5L14.8 15.6C16.1 14.7 17 13.2 17 11.5C17 8.8 15 6.5 12 6.5Z" stroke="white" stroke-width="1.5" stroke-linejoin="round"/><line x1="10" y1="12" x2="14" y2="12" stroke="white" stroke-width="1.5" stroke-linecap="round"/></svg> <svg width="20" height="20" viewBox="0 0 24 24" fill="none"><circle cx="12" cy="12" r="12" fill="#10a37f"/><path d="M12 6.5C9 6.5 7 8.8 7 11.5C7 13.2 7.9 14.7 9.2 15.6L8.5 17.5H15.5L14.8 15.6C16.1 14.7 17 13.2 17 11.5C17 8.8 15 6.5 12 6.5Z" stroke="white" stroke-width="1.5" stroke-linejoin="round"/><line x1="10" y1="12" x2="14" y2="12" stroke="white" stroke-width="1.5" stroke-linecap="round"/></svg>
OpenAI (GPT Actions / Assistants) OpenAI (GPT Actions / Assistants)
</h3> </h3>
<p>Use as a Custom GPT Action or Assistants API tool via the OpenAPI schema endpoint below.</p> <p>Import the OpenAPI schema into a Custom GPT Action. Auth via API Key in the <code>X-MCP-API-Key</code> header.</p>
<strong style="font-size:12px;color:#555;display:block;margin-bottom:6px;">GPT Builder → Actions → Add Action → Import URL</strong> <strong style="font-size:12px;color:#555;display:block;margin-bottom:4px;">GPT Builder → Actions → Import from URL</strong>
<pre id="openai-schema-url" style="position:relative;"><span class="key-url"><?php echo esc_html(rest_url('asktam-mcp/v1/openapi.json')); ?></span> <pre id="openai-schema-url" style="position:relative;"><span class="key-url"><?php echo esc_html(rest_url('asktam-mcp/v1/openapi.json')); ?></span>
<button class="copy-btn" onclick="copyCode('openai-schema-url',this)">Copy</button></pre> <button class="copy-btn" onclick="copyCode('openai-schema-url',this)">Copy</button></pre>
<strong style="font-size:12px;color:#555;display:block;margin-bottom:6px;">Authentication → API Key → Header</strong> <strong style="font-size:12px;color:#555;display:block;margin-bottom:4px;">Authentication → API Key → Header name</strong>
<pre>{ <pre>{
"type": "apiKey", "type": "apiKey",
"in": "header", "in": "header",
"name": "X-MCP-API-Key", "name": "<span class="key-placeholder">X-MCP-API-Key</span>"
"value": "<span class="key-placeholder">YOUR_API_KEY</span>"
}</pre> }</pre>
<strong style="font-size:12px;color:#555;display:block;margin-bottom:6px;">Assistants API (function calling)</strong>
<pre style="position:relative;"><span style="color:#89b4fa;">POST</span> https://api.openai.com/v1/assistants
{
"tools": [{ "type": "function", ... }],
"instructions": "You control a WordPress site..."
}
<button class="copy-btn" onclick="navigator.clipboard.writeText('<?php echo esc_js($msgUrl); ?>');this.textContent='Copied!'">Copy msg URL</button></pre>
</div> </div>
</div> </div>
@@ -190,38 +181,20 @@ class AdminMenu {
<?php if ($keys): ?> <?php if ($keys): ?>
<table class="mcp-key-table"> <table class="mcp-key-table">
<thead> <thead>
<tr> <tr><th>Label</th><th>Key (masked)</th><th>Scopes</th><th>Created</th><th>Last Used</th><th>Status</th><th></th></tr>
<th>Label</th>
<th>Key (masked)</th>
<th>Scopes</th>
<th>Created</th>
<th>Last Used</th>
<th>Status</th>
<th></th>
</tr>
</thead> </thead>
<tbody> <tbody>
<?php foreach ($keys as $key): ?> <?php foreach ($keys as $key): ?>
<tr> <tr>
<td><strong><?php echo esc_html($key->name); ?></strong></td> <td><strong><?php echo esc_html($key->name); ?></strong></td>
<td> <td>
<code style="font-size:12px;background:#f6f7f7;padding:2px 8px;border-radius:3px;"> <code style="font-size:12px;background:#f6f7f7;padding:2px 8px;border-radius:3px;"><?php echo esc_html(substr($key->api_key, 0, 8) . '•••••••••••••' . substr($key->api_key, -4)); ?></code>
<?php echo esc_html(substr($key->api_key, 0, 8) . '•••••••••••••' . substr($key->api_key, -4)); ?>
</code>
<button onclick="navigator.clipboard.writeText('<?php echo esc_js($key->api_key); ?>');this.textContent='✓ Copied';setTimeout(()=>this.textContent='Copy',2000)" class="button button-small" style="margin-left:6px;">Copy</button> <button onclick="navigator.clipboard.writeText('<?php echo esc_js($key->api_key); ?>');this.textContent='✓ Copied';setTimeout(()=>this.textContent='Copy',2000)" class="button button-small" style="margin-left:6px;">Copy</button>
</td> </td>
<td> <td><?php foreach (array_map('trim', explode(',', $key->scopes)) as $s): ?><span class="mcp-scope"><?php echo esc_html($s); ?></span><?php endforeach; ?></td>
<?php foreach (array_map('trim', explode(',', $key->scopes)) as $scope): ?>
<span class="mcp-scope"><?php echo esc_html($scope); ?></span>
<?php endforeach; ?>
</td>
<td style="color:#666;font-size:12px;"><?php echo esc_html(date('Y-m-d', strtotime($key->created_at))); ?></td> <td style="color:#666;font-size:12px;"><?php echo esc_html(date('Y-m-d', strtotime($key->created_at))); ?></td>
<td style="color:#666;font-size:12px;"><?php echo $key->last_used ? esc_html(date('Y-m-d H:i', strtotime($key->last_used))) : '—'; ?></td> <td style="color:#666;font-size:12px;"><?php echo $key->last_used ? esc_html(date('Y-m-d H:i', strtotime($key->last_used))) : '—'; ?></td>
<td> <td><span class="mcp-badge <?php echo $key->is_active ? 'mcp-badge-active' : 'mcp-badge-inactive'; ?>"><?php echo $key->is_active ? 'Active' : 'Inactive'; ?></span></td>
<span class="mcp-badge <?php echo $key->is_active ? 'mcp-badge-active' : 'mcp-badge-inactive'; ?>">
<?php echo $key->is_active ? 'Active' : 'Inactive'; ?>
</span>
</td>
<td> <td>
<form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" style="display:inline;"> <form method="post" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" style="display:inline;">
<?php wp_nonce_field('asktam_mcp_revoke_' . $key->id, '_wpnonce_mcp'); ?> <?php wp_nonce_field('asktam_mcp_revoke_' . $key->id, '_wpnonce_mcp'); ?>
@@ -247,22 +220,6 @@ class AdminMenu {
btn.textContent = '✓ Copied'; btn.textContent = '✓ Copied';
setTimeout(() => btn.textContent = 'Copy', 2000); setTimeout(() => btn.textContent = 'Copy', 2000);
} }
// Show new key banner if redirected after creation
(function() {
const params = new URLSearchParams(window.location.search);
if (params.get('mcp_created') === '1') {
// Key was just created — pull from session storage if we stored it
const stored = sessionStorage.getItem('mcp_new_key');
if (stored) {
const banner = document.getElementById('mcp-new-key-banner');
const val = document.getElementById('mcp-new-key-value');
banner.style.display = 'block';
val.textContent = stored;
sessionStorage.removeItem('mcp_new_key');
}
}
})();
</script> </script>
<?php <?php
} }
@@ -273,7 +230,10 @@ class AdminMenu {
<div class="wrap mcp-wrap"> <div class="wrap mcp-wrap">
<h1>AskTam MCP — Settings</h1> <h1>AskTam MCP — Settings</h1>
<form method="post" action="options.php"> <form method="post" action="options.php">
<?php settings_fields('asktam_mcp_settings_group'); ?> <?php
settings_fields('asktam_mcp_settings_group');
do_settings_sections('asktam_mcp_settings_group');
?>
<table class="form-table" style="max-width:600px;"> <table class="form-table" style="max-width:600px;">
<tr> <tr>
<th>Rate Limit (req/min per key)</th> <th>Rate Limit (req/min per key)</th>