PluginProbe
ZIP AI – AI Website Builder & AI Agent (Beta) / 0.0.8
ZIP AI – AI Website Builder & AI Agent (Beta) v0.0.8
0.0.10 0.0.9 trunk 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8
zip-ai / lib / mcp-adapter / includes / Servers / DefaultServerFactory.php

DefaultServerFactory.php in ZIP AI – AI Website Builder & AI Agent (Beta) 0.0.8, at lib/mcp-adapter/includes/Servers/DefaultServerFactory.php

167 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Factory for creating the default WordPress MCP server.
4 *
5 * @package McpAdapter
6 */
7
8 declare( strict_types=1 );
9
10 namespace WP\MCP\Servers;
11
12 use WP\MCP\Core\McpAdapter;
13 use WP\MCP\Infrastructure\ErrorHandling\ErrorLogMcpErrorHandler;
14 use WP\MCP\Infrastructure\Observability\NullMcpObservabilityHandler;
15 use WP\MCP\Transport\HttpTransport;
16
17 /**
18 * Factory for creating the default WordPress MCP server.
19 *
20 * This server automatically discovers and exposes abilities with mcp.public=true metadata:
21 * - discover-abilities: Lists all publicly available WordPress abilities
22 * - get-ability-info: Gets detailed information about specific abilities
23 * - execute-ability: Executes WordPress abilities with provided parameters
24 */
25 class DefaultServerFactory {
26
27 /**
28 * Create default server for WordPress MCP Adapter with WordPress filters support.
29 *
30 * This method creates a server using WordPress-specific defaults and applies
31 * WordPress filters for customization, making it perfect for use within
32 * the McpAdapter.
33 *
34 * @return void
35 */
36 public static function create(): void {
37
38 // Auto-discover resources and prompts from abilities
39 $auto_discovered_resources = self::discover_abilities_by_type( 'resource' );
40 $auto_discovered_prompts = self::discover_abilities_by_type( 'prompt' );
41
42 // WordPress-specific defaults
43 $wordpress_defaults = array(
44 'server_id' => 'mcp-adapter-default-server',
45 'server_route_namespace' => 'mcp',
46 'server_route' => 'mcp-adapter-default-server',
47 'server_name' => 'MCP Adapter Default Server',
48 'server_description' => 'Default MCP server for WordPress abilities discovery and execution',
49 'server_version' => 'v1.0.0',
50 'mcp_transports' => array( HttpTransport::class ),
51 'error_handler' => ErrorLogMcpErrorHandler::class,
52 'observability_handler' => NullMcpObservabilityHandler::class,
53 'tools' => array(
54 'mcp-adapter/discover-abilities',
55 'mcp-adapter/get-ability-info',
56 'mcp-adapter/execute-ability',
57 ),
58 'resources' => $auto_discovered_resources,
59 'prompts' => $auto_discovered_prompts,
60 );
61
62 /**
63 * Filters the default MCP server configuration.
64 *
65 * Allows customization of the default server's settings before creation.
66 * The filtered array is merged with defaults, so you only need to specify
67 * the values you want to override.
68 *
69 * @since 0.3.0
70 *
71 * @param array $config {
72 * Default server configuration.
73 *
74 * @type string $server_id Server identifier. Default 'mcp-adapter-default-server'.
75 * @type string $server_route_namespace REST API namespace. Default 'mcp-adapter/v1'.
76 * @type string $server_route REST API route. Default 'mcp'.
77 * @type string $server_name Human-readable name. Default 'WordPress MCP Server'.
78 * @type string $server_description Server description.
79 * @type string $server_version Server version. Default WORDPRESS_MCP_ADAPTER_VERSION.
80 * @type string[] $mcp_transports Transport class names. Default [HttpTransport::class].
81 * @type string $error_handler Error handler class. Default ErrorLogMcpErrorHandler::class.
82 * @type string $observability_handler Observability handler class. Default NullMcpObservabilityHandler::class.
83 * @type string[] $tools Tool ability names to expose.
84 * @type string[] $resources Resource ability names to expose.
85 * @type string[] $prompts Prompt ability names to expose.
86 * }
87 */
88 $config = apply_filters( 'mcp_adapter_default_server_config', $wordpress_defaults );
89
90 // Ensure config is an array and merge with defaults
91 if ( ! is_array( $config ) ) {
92 $config = $wordpress_defaults;
93 }
94 $config = wp_parse_args( $config, $wordpress_defaults );
95
96 // Use McpAdapter to create the server with full validation
97 $adapter = McpAdapter::instance();
98 $result = $adapter->create_server(
99 $config['server_id'],
100 $config['server_route_namespace'],
101 $config['server_route'],
102 $config['server_name'],
103 $config['server_description'],
104 $config['server_version'],
105 $config['mcp_transports'],
106 $config['error_handler'],
107 $config['observability_handler'],
108 $config['tools'],
109 $config['resources'],
110 $config['prompts']
111 );
112
113 // Log error if server creation failed, but don't halt execution.
114 // This allows other servers to be registered even if default server fails.
115 if ( ! is_wp_error( $result ) ) {
116 return;
117 }
118
119 _doing_it_wrong(
120 __METHOD__,
121 sprintf(
122 'MCP Adapter: Failed to create default server. Error: %s (Code: %s)',
123 esc_html( $result->get_error_message() ),
124 esc_html( (string) $result->get_error_code() )
125 ),
126 '0.5.0'
127 );
128 }
129
130 /**
131 * Discover abilities by MCP type.
132 *
133 * Scans all registered abilities and returns those with the specified type
134 * and public MCP exposure.
135 *
136 * @param string $type The MCP type to filter by ('tool', 'resource', or 'prompt').
137 *
138 * @return array Array of ability names matching the specified type.
139 */
140 private static function discover_abilities_by_type( string $type ): array {
141 $abilities = wp_get_abilities();
142 $filtered = array();
143
144 foreach ( $abilities as $ability ) {
145 $ability_name = $ability->get_name();
146 $meta = $ability->get_meta();
147
148 // Skip if not publicly exposed
149 if ( ! ( $meta['mcp']['public'] ?? false ) ) {
150 continue;
151 }
152
153 // Get the type (defaults to 'tool' if not specified)
154 $ability_type = $meta['mcp']['type'] ?? 'tool';
155
156 // Add to filtered list if type matches
157 if ( $ability_type !== $type ) {
158 continue;
159 }
160
161 $filtered[] = $ability_name;
162 }
163
164 return $filtered;
165 }
166 }
167