'not_found', 'message' => 'No MCP servers are published on this site.', ], 404 ); } AIREWP_Rewrite::send_json( self::build( $settings ) ); } /** * Whether there is a manifest to publish: the feature is on and at least * one server is listed. */ public static function is_active( array $settings ): bool { return ! empty( $settings['enable_mcp_json'] ) && [] !== self::get_servers( $settings ); } /** * Build the mcp.json data structure. * * Shared by the written file and the dynamic response so the two cannot * drift apart, filters included. */ public static function build( array $settings ): array { $servers = self::get_servers( $settings ); $mcp_json = [ 'schema_version' => '1.0.0', 'name' => sprintf( '%s MCP Servers', $settings['company_name'] ), 'description' => sprintf( 'MCP servers available on %s', home_url() ), 'homepage' => home_url(), 'servers' => $servers, 'metadata' => [ 'generatedBy' => 'AI-Ready WP ' . AIREWP_VERSION, 'generatedAt' => gmdate( 'c' ), 'siteUrl' => home_url(), ], ]; /** * Filter the complete mcp.json output. * * @since 4.2.0 * * @param array $mcp_json The mcp.json data. * @param array $settings Plugin settings. */ $mcp_json = apply_filters( 'airewp_mcp_json', $mcp_json, $settings ); return $mcp_json; } /** * Get configured MCP servers. * * @return array */ public static function get_servers( array $settings ): array { $servers = []; // Parse custom MCP servers from settings (JSON format) $custom_servers = $settings['mcp_servers'] ?? ''; if ( ! empty( $custom_servers ) ) { $parsed = json_decode( $custom_servers, true ); if ( is_array( $parsed ) ) { foreach ( $parsed as $server ) { if ( ! empty( $server['name'] ) && ! empty( $server['url'] ) ) { $servers[] = [ 'name' => sanitize_text_field( $server['name'] ), 'description' => sanitize_text_field( $server['description'] ?? '' ), 'transport' => sanitize_text_field( $server['transport'] ?? 'streamable-http' ), 'url' => esc_url_raw( $server['url'] ), ]; } } } } /** * Filter the MCP servers list. * * Allows themes and plugins to register their own MCP servers. * * @since 4.2.0 * * @param array $servers Current servers list. * @param array $settings Plugin settings. */ $servers = apply_filters( 'airewp_mcp_servers', $servers, $settings ); return is_array( $servers ) ? array_values( $servers ) : []; } }