| 1 |
<?php |
| 2 |
declare( strict_types=1 ); |
| 3 |
|
| 4 |
namespace WPMedia\MCP\OAuth\Transport; |
| 5 |
|
| 6 |
use WP\MCP\Abilities\DiscoverAbilitiesAbility; |
| 7 |
use WP\MCP\Abilities\ExecuteAbilityAbility; |
| 8 |
use WP\MCP\Abilities\GetAbilityInfoAbility; |
| 9 |
use WPMedia\MCP\OAuth\Context; |
| 10 |
|
| 11 |
class ServerRegistrar { |
| 12 |
/** |
| 13 |
* MCP server instance. |
| 14 |
* |
| 15 |
* @var Server |
| 16 |
*/ |
| 17 |
private $server; |
| 18 |
|
| 19 |
/** |
| 20 |
* OAuth server context. |
| 21 |
* |
| 22 |
* @var Context |
| 23 |
*/ |
| 24 |
private $context; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor. |
| 28 |
* |
| 29 |
* @param Server $server MCP server instance. |
| 30 |
* @param Context $context OAuth server context. |
| 31 |
*/ |
| 32 |
public function __construct( Server $server, Context $context ) { |
| 33 |
$this->server = $server; |
| 34 |
$this->context = $context; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Register the MCP server with the adapter. |
| 39 |
* |
| 40 |
* Creates an isolated server at /wp-json/mcp/mcp-oauth-server using the custom |
| 41 |
* OAuthHttpTransport for JWT Bearer authentication. |
| 42 |
* |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public function register_server(): void { |
| 46 |
if ( ! $this->context->is_enabled() ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
|
| 50 |
$this->server->register_server(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Whether nobody else will register the shared mcp-adapter abilities. |
| 55 |
* |
| 56 |
* True only when the mcp-adapter package's *default* server is disabled |
| 57 |
* via the `mcp_adapter_create_default_server` filter — the only thing |
| 58 |
* that normally registers the 'mcp-adapter' category and its three |
| 59 |
* shared abilities (McpAdapter::maybe_create_default_server()). |
| 60 |
* |
| 61 |
* @return bool |
| 62 |
*/ |
| 63 |
private function must_register_shared_abilities_ourselves(): bool { |
| 64 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- reading the mcp-adapter package's own filter, not defining a new hook. |
| 65 |
return ! wpm_apply_filters_typed( 'boolean', 'mcp_adapter_create_default_server', true ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Register the 'mcp-adapter' ability category ourselves when the |
| 70 |
* default server (which normally owns this) is disabled. |
| 71 |
* |
| 72 |
* Must run on the `wp_abilities_api_categories_init` action specifically |
| 73 |
* — the Abilities API rejects category registration anywhere else. |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function ensure_default_category(): void { |
| 78 |
if ( ! $this->must_register_shared_abilities_ourselves() ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
wp_register_ability_category( |
| 83 |
'mcp-adapter', |
| 84 |
[ |
| 85 |
'label' => 'MCP Adapter', |
| 86 |
'description' => 'Abilities for the MCP Adapter', |
| 87 |
] |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Register the mcp-adapter package's shared discover-abilities/ |
| 93 |
* get-ability-info/execute-ability abilities ourselves when the default |
| 94 |
* server (which normally owns them) is disabled. |
| 95 |
* |
| 96 |
* Our server's tool list reuses these three abilities rather than |
| 97 |
* duplicating them, but they are normally only registered by the |
| 98 |
* mcp-adapter package's *default* server — gated behind the |
| 99 |
* `mcp_adapter_create_default_server` filter, disabled in our own test |
| 100 |
* bootstrap and something any site could legitimately disable. |
| 101 |
* Registering our own server must not depend on that unrelated toggle. |
| 102 |
* |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function ensure_shared_abilities_registered(): void { |
| 106 |
if ( ! $this->must_register_shared_abilities_ourselves() ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
DiscoverAbilitiesAbility::register(); |
| 111 |
GetAbilityInfoAbility::register(); |
| 112 |
ExecuteAbilityAbility::register(); |
| 113 |
} |
| 114 |
} |
| 115 |
|