99
1010namespace WP \MCP \Core ;
1111
12- use Exception ;
1312use WP \MCP \Infrastructure \ErrorHandling \Contracts \McpErrorHandlerInterface ;
1413use WP \MCP \Infrastructure \ErrorHandling \NullMcpErrorHandler ;
1514use WP \MCP \Infrastructure \Observability \Contracts \McpObservabilityHandlerInterface ;
1615use WP \MCP \Infrastructure \Observability \NullMcpObservabilityHandler ;
17- use WP \MCP \Core \McpServer ;
1816
1917/**
2018 * WordPress MCP Registry - Main class for managing multiple MCP servers.
@@ -23,9 +21,9 @@ class McpAdapter {
2321 /**
2422 * Registry instance
2523 *
26- * @var McpAdapter|null
24+ * @var \WP\MCP\Core\ McpAdapter|null
2725 */
28- private static ?McpAdapter $ instance = null ;
26+ private static ?self $ instance = null ;
2927
3028 /**
3129 * The initialized flag.
@@ -51,7 +49,7 @@ class McpAdapter {
5149 /**
5250 * Registered servers
5351 *
54- * @var McpServer[]
52+ * @var \WP\MCP\Core\ McpServer[]
5553 */
5654 private array $ servers = array ();
5755
@@ -66,14 +64,16 @@ class McpAdapter {
6664 * Constructor
6765 */
6866 private function __construct () {
69- if ( ! self ::$ initialized && ! self ::$ initialization_failed ) {
70- if ( ! $ this -> check_dependencies () ) {
71- self :: $ initialization_failed = true ;
72- return ;
73- }
74- add_action ( ' rest_api_init ' , array ( $ this , ' mcp_adapter_init ' ), 20000 ) ;
75- self :: $ initialized = true ;
67+ if ( self ::$ initialized || self ::$ initialization_failed ) {
68+ return ;
69+ }
70+
71+ if ( ! $ this -> check_dependencies () ) {
72+ self :: $ initialization_failed = true ;
73+ return ;
7674 }
75+ add_action ( 'rest_api_init ' , array ( $ this , 'mcp_adapter_init ' ), 20000 );
76+ self ::$ initialized = true ;
7777 }
7878
7979 /**
@@ -97,12 +97,9 @@ private function check_dependencies(): bool {
9797 // Store errors for later retrieval.
9898 self ::$ initialization_errors = $ errors ;
9999
100- // Log errors if any.
101- if ( ! empty ( $ errors ) ) {
102- return false ;
103- }
100+ // @todo Log errors if any.
104101
105- return true ;
102+ return empty ( $ errors ) ;
106103 }
107104
108105 /**
@@ -114,18 +111,20 @@ public function mcp_adapter_init(): void {
114111 return ;
115112 }
116113
117- if ( ! $ this ->has_triggered_init ) {
118- do_action ( 'mcp_adapter_init ' , $ this );
119- $ this ->has_triggered_init = true ;
114+ if ( $ this ->has_triggered_init ) {
115+ return ;
120116 }
117+
118+ do_action ( 'mcp_adapter_init ' , $ this );
119+ $ this ->has_triggered_init = true ;
121120 }
122121
123122 /**
124123 * Get the registry instance
125124 *
126- * @return McpAdapter|null Returns null if initialization failed due to missing dependencies.
125+ * @return ?\WP\MCP\Core\ McpAdapter Returns null if initialization failed due to missing dependencies.
127126 */
128- public static function instance (): ?McpAdapter {
127+ public static function instance (): ?self {
129128 if ( null === self ::$ instance && ! self ::$ initialization_failed ) {
130129 self ::$ instance = new self ();
131130 }
@@ -188,8 +187,8 @@ public static function get_dependency_status(): array {
188187 * @param array $prompts Prompts to register.
189188 * @param callable|null $transport_permission_callback Optional custom permission callback for transport-level authentication. If null, defaults to is_user_logged_in().
190189 *
191- * @return McpAdapter
192- * @throws Exception If the server already exists or if called outside of the mcp_adapter_init action.
190+ * @return \WP\MCP\Core\ McpAdapter
191+ * @throws \ Exception If the server already exists or if called outside of the mcp_adapter_init action.
193192 */
194193 public function create_server ( string $ server_id , string $ server_route_namespace , string $ server_route , string $ server_name , string $ server_description , string $ server_version , array $ mcp_transports , ?string $ error_handler , ?string $ observability_handler = null , array $ tools = array (), array $ resources = array (), array $ prompts = array (), ?callable $ transport_permission_callback = null ): self {
195194
@@ -200,7 +199,7 @@ public function create_server( string $server_id, string $server_route_namespace
200199
201200 // Validate error handler class implements McpErrorHandlerInterface.
202201 if ( ! in_array ( McpErrorHandlerInterface::class, class_implements ( $ error_handler ) ?: array (), true ) ) {
203- throw new Exception (
202+ throw new \ Exception (
204203 esc_html__ ( 'Error handler class must implement the McpErrorHandlerInterface. ' , 'mcp-adapter ' )
205204 );
206205 }
@@ -212,18 +211,18 @@ public function create_server( string $server_id, string $server_route_namespace
212211
213212 // Validate observability handler class implements McpObservabilityHandlerInterface.
214213 if ( ! in_array ( McpObservabilityHandlerInterface::class, class_implements ( $ observability_handler ) ?: array (), true ) ) {
215- throw new Exception (
214+ throw new \ Exception (
216215 esc_html__ ( 'Observability handler class must implement the McpObservabilityHandlerInterface interface. ' , 'mcp-adapter ' )
217216 );
218217 }
219218
220219 if ( ! doing_action ( 'mcp_adapter_init ' ) ) {
221- throw new Exception (
220+ throw new \ Exception (
222221 esc_html__ ( 'MCP Server creation must be done during mcp_adapter_init action. ' , 'mcp-adapter ' )
223222 );
224223 }
225224 if ( isset ( $ this ->servers [ $ server_id ] ) ) {
226- throw new Exception (
225+ throw new \ Exception (
227226 // translators: %s: server ID.
228227 sprintf ( esc_html__ ( 'Server with ID "%s" already exists. ' , 'mcp-adapter ' ), esc_html ( $ server_id ) )
229228 );
@@ -269,7 +268,7 @@ public function create_server( string $server_id, string $server_route_namespace
269268 *
270269 * @param string $server_id Server ID.
271270 *
272- * @return McpServer|null
271+ * @return \WP\MCP\Core\ McpServer|null
273272 */
274273 public function get_server ( string $ server_id ): ?McpServer {
275274 return $ this ->servers [ $ server_id ] ?? null ;
@@ -278,7 +277,7 @@ public function get_server( string $server_id ): ?McpServer {
278277 /**
279278 * Get all registered servers
280279 *
281- * @return McpServer[]
280+ * @return \WP\MCP\Core\ McpServer[]
282281 */
283282 public function get_servers (): array {
284283 return $ this ->servers ;
0 commit comments