|null */ private static $context = null; /** * Why authentication failed, or FAILURE_NONE. * * @var string */ private static $failure = self::FAILURE_NONE; /** * Whether the request carried an MCP credential at all. * * @var bool */ private static $credentialPresented = false; /** * Record the authenticated connection for this request. * * Refuses (and records an internal failure) when any required field is * missing or invalid, so a half-built context can never authenticate. * * @param array $context connection_id, user_id, auth_type, permission_level, * token_id, token_type, and optionally oauth_client_id and label. * @return bool True when the context was accepted. */ public static function set( array $context ): bool { $normalized = self::normalize( $context ); if ( null === $normalized ) { self::markFailure( self::FAILURE_INTERNAL ); return false; } self::$context = $normalized; self::$failure = self::FAILURE_NONE; self::$credentialPresented = true; return true; } /** * The authenticated context, or null when the request is not authenticated. * * @return array|null * @phpstan-return McpConnectionContext|null */ public static function get(): ?array { return self::$context; } /** * Whether an MCP connection authenticated this request. * * @return bool */ public static function has(): bool { return null !== self::$context; } /** * Authenticated connection id, or 0. * * @return int */ public static function connectionId(): int { return null === self::$context ? 0 : (int) self::$context['connection_id']; } /** * Owner user id of the authenticated connection, or 0. * * @return int */ public static function userId(): int { return null === self::$context ? 0 : (int) self::$context['user_id']; } /** * Permission level of the authenticated connection, or ''. * * @return string */ public static function permissionLevel(): string { return null === self::$context ? '' : (string) self::$context['permission_level']; } /** * Authentication type ('oauth' or 'api_key') of the connection, or ''. * * @return string */ public static function authType(): string { return null === self::$context ? '' : (string) self::$context['auth_type']; } /** * Note that the request carried an MCP credential, valid or not. * * @return void */ public static function markCredentialPresented(): void { self::$credentialPresented = true; } /** * Whether the request carried an MCP credential, valid or not. * * @return bool */ public static function credentialPresented(): bool { return self::$credentialPresented; } /** * Record why authentication failed. Clears any context already set. * * @param string $reason One of the FAILURE_* constants. * @return void */ public static function markFailure( string $reason ): void { self::$context = null; self::$failure = '' === $reason ? self::FAILURE_INTERNAL : $reason; } /** * Why authentication failed, or FAILURE_NONE. * * @return string */ public static function failure(): string { return self::$failure; } /** * Forget everything. Called at the start of each resolution and by tests. * * @return void */ public static function reset(): void { self::$context = null; self::$failure = self::FAILURE_NONE; self::$credentialPresented = false; } /** * Validate and normalize a context array. * * @param array $context Raw context. * @return array|null */ private static function normalize( array $context ): ?array { $connection_id = isset( $context['connection_id'] ) ? (int) $context['connection_id'] : 0; $user_id = isset( $context['user_id'] ) ? (int) $context['user_id'] : 0; $token_id = isset( $context['token_id'] ) ? (int) $context['token_id'] : 0; $auth_type = isset( $context['auth_type'] ) ? (string) $context['auth_type'] : ''; $token_type = isset( $context['token_type'] ) ? (string) $context['token_type'] : ''; $level = isset( $context['permission_level'] ) ? (string) $context['permission_level'] : ''; if ( $connection_id <= 0 || $user_id <= 0 || $token_id <= 0 ) { return null; } if ( ! in_array( $auth_type, array( 'oauth', 'api_key' ), true ) ) { return null; } if ( ! in_array( $token_type, array( 'api_key', 'oauth_access' ), true ) ) { return null; } if ( ! McpPermissionLevel::isValid( $level ) ) { return null; } return array( 'connection_id' => $connection_id, 'user_id' => $user_id, 'auth_type' => $auth_type, 'permission_level' => $level, 'token_id' => $token_id, 'token_type' => $token_type, 'oauth_client_id' => isset( $context['oauth_client_id'] ) ? (int) $context['oauth_client_id'] : 0, 'label' => isset( $context['label'] ) ? (string) $context['label'] : '', ); } }