DefaultServerFactory.php
142 lines
| 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 | // Apply WordPress filter for customization |
| 63 | $config = apply_filters( 'mcp_adapter_default_server_config', $wordpress_defaults ); |
| 64 | |
| 65 | // Ensure config is an array and merge with defaults |
| 66 | if ( ! is_array( $config ) ) { |
| 67 | $config = $wordpress_defaults; |
| 68 | } |
| 69 | $config = wp_parse_args( $config, $wordpress_defaults ); |
| 70 | |
| 71 | // Use McpAdapter to create the server with full validation |
| 72 | $adapter = McpAdapter::instance(); |
| 73 | $result = $adapter->create_server( |
| 74 | $config['server_id'], |
| 75 | $config['server_route_namespace'], |
| 76 | $config['server_route'], |
| 77 | $config['server_name'], |
| 78 | $config['server_description'], |
| 79 | $config['server_version'], |
| 80 | $config['mcp_transports'], |
| 81 | $config['error_handler'], |
| 82 | $config['observability_handler'], |
| 83 | $config['tools'], |
| 84 | $config['resources'], |
| 85 | $config['prompts'] |
| 86 | ); |
| 87 | |
| 88 | // Log error if server creation failed, but don't halt execution. |
| 89 | // This allows other servers to be registered even if default server fails. |
| 90 | if ( ! is_wp_error( $result ) ) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | _doing_it_wrong( |
| 95 | __METHOD__, |
| 96 | sprintf( |
| 97 | 'MCP Adapter: Failed to create default server. Error: %s (Code: %s)', |
| 98 | esc_html( $result->get_error_message() ), |
| 99 | esc_html( (string) $result->get_error_code() ) |
| 100 | ), |
| 101 | 'n.e.x.t' |
| 102 | ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Discover abilities by MCP type. |
| 107 | * |
| 108 | * Scans all registered abilities and returns those with the specified type |
| 109 | * and public MCP exposure. |
| 110 | * |
| 111 | * @param string $type The MCP type to filter by ('tool', 'resource', or 'prompt'). |
| 112 | * |
| 113 | * @return array Array of ability names matching the specified type. |
| 114 | */ |
| 115 | private static function discover_abilities_by_type( string $type ): array { |
| 116 | $abilities = wp_get_abilities(); |
| 117 | $filtered = array(); |
| 118 | |
| 119 | foreach ( $abilities as $ability ) { |
| 120 | $ability_name = $ability->get_name(); |
| 121 | $meta = $ability->get_meta(); |
| 122 | |
| 123 | // Skip if not publicly exposed |
| 124 | if ( ! ( $meta['mcp']['public'] ?? false ) ) { |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | // Get the type (defaults to 'tool' if not specified) |
| 129 | $ability_type = $meta['mcp']['type'] ?? 'tool'; |
| 130 | |
| 131 | // Add to filtered list if type matches |
| 132 | if ( $ability_type !== $type ) { |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | $filtered[] = $ability_name; |
| 137 | } |
| 138 | |
| 139 | return $filtered; |
| 140 | } |
| 141 | } |
| 142 |