feat: register OAuth MCP server with direct tools

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-25 15:46:24 +02:00
co-authored by Claude Opus 5.5
parent fb4787c032
commit 3661d143fb
3 changed files with 195 additions and 0 deletions
+100
View File
@@ -0,0 +1,100 @@
<?php
declare(strict_types=1);
namespace LIT\MCP\Server;
use LIT\MCP\Guides\InstructionsBuilder;
use LIT\MCP\Integrations\IntegrationRegistry;
use WP\MCP\Core\McpAdapter;
use WP\MCP\Infrastructure\ErrorHandling\ErrorLogMcpErrorHandler;
use WPMedia\MCP\OAuth\Transport\OAuthHttpTransport;
/**
* Registers the OAuth-protected MCP server with our own tool list.
*
* wp-media/mcp-oauth hardcodes server id/route (token audience) and only three
* meta-tools. We register the same server id first (priority 5 < mcp-oauth's 10);
* mcp-oauth's later attempt fails with "duplicate server id" (logged via
* _doing_it_wrong in debug mode only) – see spec §4.2.
*/
final class ServerRegistrar
{
public const SERVER_ID = 'mcp-oauth-server';
public const NAMESPACE = 'mcp';
public const ROUTE = 'mcp-oauth-server';
public const META_TOOLS = [
'mcp-adapter/discover-abilities',
'mcp-adapter/get-ability-info',
'mcp-adapter/execute-ability',
];
/** @var callable(): bool */
private $oauthEnabled;
public function __construct(
private IntegrationRegistry $registry,
private InstructionsBuilder $instructions,
?callable $oauthEnabled = null
) {
$this->oauthEnabled = $oauthEnabled
?? static fn (): bool => (bool) apply_filters('wpmedia_mcp_oauth_server_enabled', true);
}
public function register(): void
{
add_action('mcp_adapter_init', [$this, 'createServer'], 5);
}
/** @return array<string, mixed>|null */
public function serverArguments(): ?array
{
if (! ($this->oauthEnabled)()) {
return null;
}
return [
'id' => self::SERVER_ID,
'namespace' => self::NAMESPACE,
'route' => self::ROUTE,
'name' => 'LIT MCP',
'description' => $this->instructions->build(),
'version' => '0.1.0',
'tools' => array_merge($this->registry->directAbilityNames(), self::META_TOOLS),
];
}
public function createServer(): void
{
if (
! doing_action('mcp_adapter_init')
|| ! class_exists(McpAdapter::class)
|| ! class_exists(OAuthHttpTransport::class)
) {
return;
}
$args = $this->serverArguments();
if ($args === null) {
return;
}
$result = McpAdapter::instance()->create_server(
$args['id'],
$args['namespace'],
$args['route'],
$args['name'],
$args['description'],
$args['version'],
[OAuthHttpTransport::class],
ErrorLogMcpErrorHandler::class,
null,
$args['tools']
);
if (is_wp_error($result)) {
$message = '[wp-lit-mcp] MCP server registration failed: ' . $result->get_error_message();
error_log($message); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
}
}
}