PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta3
Elementor Website Builder – more than just a page builder v4.3.0-beta3
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / vendor / wordpress / mcp-adapter / includes / Servers / DefaultServerFactory.php

DefaultServerFactory.php in Elementor Website Builder – more than just a page builder 4.3.0-beta3, at vendor/wordpress/mcp-adapter/includes/Servers/DefaultServerFactory.php

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