The metadata. * @since 1.4.0 */ public static function authorization_server(): array { $base_url = self::base_url(); return [ // Required fields. The issuer is a path, not the bare domain // (see issuer_url()); the endpoints are their own absolute URLs. 'issuer' => self::issuer_url(), 'authorization_endpoint' => $base_url . '/oauth/authorize', 'token_endpoint' => self::rest_url( Plugin::rest_namespace() . '/oauth/token' ), 'registration_endpoint' => self::rest_url( Plugin::rest_namespace() . '/oauth/register' ), // Recommended fields. 'response_types_supported' => [ 'code' ], 'grant_types_supported' => [ 'authorization_code', 'refresh_token' ], /* * `none` is not padding for completeness. Every loopback/native * client — RFC 8252, which is every local MCP bridge — is registered * as a public client with exactly this auth method; see * {@see \Albert\OAuth\Endpoints\ClientRegistration::register()}. * Leaving it out states that the server will not accept what the * server itself just issued, and a conforming client believes the * metadata over its own credentials. */ 'token_endpoint_auth_methods_supported' => [ 'client_secret_post', 'client_secret_basic', 'none' ], 'code_challenge_methods_supported' => [ 'S256' ], // Optional but useful fields. 'scopes_supported' => [ 'default' ], ]; } /** * The RFC 9728 Protected Resource Metadata document. * * One builder for both callers (the `.well-known` route and the REST route), so * they cannot drift. `authorization_servers` must be an issuer identifier * (RFC 9728 §7.6), matching the metadata's own `issuer`, or a client's RFC 8414 * lookup goes to the wrong URL and fails validation. * * @return array The metadata. * @since 1.4.1 */ public static function protected_resource(): array { return [ 'resource' => self::rest_url( Plugin::rest_namespace() . '/mcp' ), 'authorization_servers' => [ self::issuer_url() ], 'scopes_supported' => [ 'default' ], ]; } /** * The issuer identifier: a path, not the bare domain. * * A path issuer makes a client's RFC 8414 §3.1 discovery fall through to * `/.well-known/openid-configuration`, where `.well-known` sits mid-path. * Hosts that intercept only a *root* `/.well-known/` (SiteGround, Servebolt) leave * that alone, so it reaches WordPress and needs no files. Endpoints are their own * URLs, not derived from this. * * @return string The issuer identifier. * @since 1.4.1 */ public static function issuer_url(): string { return self::rest_url( Plugin::rest_namespace() . '/oauth' ); } /** * The base URL for OAuth endpoints. * * Uses the external URL setting when one is configured and valid, otherwise * `home_url()`. * * @return string The base URL. * @since 1.4.0 */ public static function base_url(): string { $external_url = (string) apply_filters( 'albert/mcp/external_url', '' ); $external_url = rtrim( $external_url, '/' ); if ( $external_url !== '' ) { $validated = wp_http_validate_url( $external_url ); if ( $validated !== false ) { return $validated; } } return home_url(); } /** * A REST URL built on the current base URL. * * Deliberately not `rest_url()`: that resolves against `home_url()` and * would ignore the external URL an MCP client was told to use. * * @param string $path The REST route path. * * @return string The full REST URL. * @since 1.4.0 */ public static function rest_url( string $path ): string { return self::base_url() . '/wp-json/' . ltrim( $path, '/' ); } }