'GET',
'callback' => [ $this, 'render_done_page' ],
// Says only "you can go back to Claude Desktop": nothing to protect.
'permission_callback' => '__return_true',
]
);
}
/**
* Where the bridge sends the browser once the sign-in has finished.
*
* Without it the bridge shows its own page, written for its command line
* ("return to the CLI"), which reads as an error to somebody who never
* opened one. The bridge accepts only an address without a query string,
* so a site on plain permalinks keeps the bridge's page.
*
* @return string The address, or '' when the bridge would refuse it.
* @since 1.5.0
*/
public static function done_url(): string {
$url = rest_url( Plugin::rest_namespace() . '/connect/done' );
return str_contains( $url, '?' ) ? '' : $url;
}
/**
* Print the page and stop: it is HTML, not a REST response.
*
* @return void
* @since 1.5.0
*/
public function render_done_page(): void {
nocache_headers();
header( 'Content-Type: text/html; charset=utf-8' );
$site_name = get_bloginfo( 'name' );
?>
>
-
403 ] );
}
if ( ! self::is_available() ) {
wp_die( esc_html__( 'This server cannot build the Claude Desktop file: PHP’s Zip extension is missing. Use the steps under “Set it up by hand” instead.', 'albert-ai-butler' ), '', [ 'response' => 500 ] );
}
$file = wp_tempnam( self::filename() );
if ( ! self::build( $file ) ) {
wp_delete_file( $file );
wp_die( esc_html__( 'The Claude Desktop file could not be built. Please try again.', 'albert-ai-butler' ), '', [ 'response' => 500 ] );
}
nocache_headers();
header( 'Content-Type: application/zip' );
header( 'Content-Disposition: attachment; filename="' . self::filename() . '"' );
header( 'Content-Length: ' . (string) filesize( $file ) );
header( 'X-Content-Type-Options: nosniff' );
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_readfile -- Streaming a temp file to the browser.
readfile( $file );
wp_delete_file( $file );
exit;
}
/**
* Write the install file to a path.
*
* @param string $path Where to write the zip.
*
* @return bool
* @since 1.5.0
*/
public static function build( string $path ): bool {
$zip = new \ZipArchive();
if ( $zip->open( $path, \ZipArchive::CREATE | \ZipArchive::OVERWRITE ) !== true ) {
return false;
}
$manifest = wp_json_encode( self::manifest(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES );
$ok = is_string( $manifest )
&& $zip->addFromString( 'manifest.json', $manifest )
&& $zip->addFile( self::bridge_path(), 'server/bridge.mjs' );
$licenses = self::bridge_path() . '.LEGAL.txt';
if ( $ok && is_readable( $licenses ) ) {
$ok = $zip->addFile( $licenses, 'server/LICENSES.txt' );
}
return $zip->close() && $ok;
}
/**
* The MCPB manifest: what Claude Desktop shows and how it starts the bridge.
*
* @return array
* @since 1.5.0
*/
public static function manifest(): array {
$endpoint = McpServer::get_endpoint_url();
$title = wp_specialchars_decode( (string) get_bloginfo( 'name' ), ENT_QUOTES );
$host = (string) wp_parse_url( $endpoint, PHP_URL_HOST );
$env = [
'ALBERT_MCP_URL' => $endpoint,
'ALBERT_USER_AGENT' => 'Albert-ClaudeDesktop/' . ALBERT_VERSION . ' (mcp-remote ' . self::BRIDGE_VERSION . ')',
// Node ignores the system's trust store by default, and a local
// tool's certificate is usually only in there.
'NODE_USE_SYSTEM_CA' => '1',
];
if ( self::done_url() !== '' ) {
$env['ALBERT_AUTH_SUCCESS_URL'] = self::done_url();
}
// Only a local site's certificate needs it, and a public site's file
// should never carry a path from the server's own disk.
if ( SiteLocation::current() === SiteLocation::LOCAL && wp_parse_url( $endpoint, PHP_URL_SCHEME ) === 'https' && LocalCertificate::path() !== null ) {
$env['NODE_EXTRA_CA_CERTS'] = (string) LocalCertificate::path();
}
return [
'manifest_version' => '0.3',
'name' => 'albert-' . self::slug(),
/* translators: %s: the site title. */
'display_name' => sprintf( __( 'Albert: %s', 'albert-ai-butler' ), $title !== '' ? $title : $host ),
'version' => ALBERT_VERSION,
/* translators: %s: the site's host name. */
'description' => sprintf( __( 'Connects Claude Desktop to the WordPress site %s through Albert. You approve it once in your browser.', 'albert-ai-butler' ), $host ),
'author' => [
'name' => 'Albert',
'url' => 'https://albertwp.com',
],
'server' => [
'type' => 'node',
'entry_point' => 'server/bridge.mjs',
'mcp_config' => [
'command' => 'node',
'args' => [ '${__dirname}/server/bridge.mjs' ],
'env' => $env,
],
],
'license' => 'GPL-2.0-or-later',
];
}
/**
* The site's host as a file-name-safe slug.
*
* @return string
* @since 1.5.0
*/
private static function slug(): string {
$host = (string) wp_parse_url( McpServer::get_endpoint_url(), PHP_URL_HOST );
$slug = sanitize_title( str_replace( '.', '-', $host ) );
return $slug !== '' ? $slug : 'site';
}
/**
* Where the built bridge lives in the plugin.
*
* @return string
* @since 1.5.0
*/
private static function bridge_path(): string {
return ALBERT_PLUGIN_DIR . 'assets/claude-desktop/bridge.mjs';
}
}