Skip to content

More accessible auth error and avoid multiple types #795

Description

@thomasa88

Is your feature request related to a problem? Please describe.
When the refresh token no longer works for the client, I want to send the user back to the authorization flow.

My code currently looks something like this:

        let client_service = ClientInfo::default();
        let mut mcp_client_res = client_service.serve(transport).await;

        if let Err(rmcp::service::ClientInitializeError::TransportError {
            error: dyn_transport_err,
            context: _,
        }) = &mcp_client_res
        {
            debug!("Transport error: {dyn_transport_err:#?}");
            // Try again if it was an authorization error
            if Self::is_auth_required_error(dyn_transport_err) {
                // new auth
             }
        }


    fn is_auth_required_error(dyn_transport_err: &rmcp::transport::DynamicTransportError) -> bool {
        let http_error = dyn_transport_err
            .error
            .downcast_ref::<rmcp::transport::streamable_http_client::StreamableHttpError<
            reqwest::Error,
        >>();
        matches!(
            http_error,
            Some(
                rmcp::transport::streamable_http_client::StreamableHttpError::Auth(
                    rmcp::transport::AuthError::AuthorizationRequired,
                ) | rmcp::transport::streamable_http_client::StreamableHttpError::AuthRequired(
                    rmcp::transport::streamable_http_client::AuthRequiredError { .. }
                )
            )
        )
    }

I have to first downcast the error and then match on two different authorization errors.

Describe the solution you'd like

Avoid the downcast if possible. Return one error for authorization required instead of two.

Describe alternatives you've considered

Additional context

Metadata

Metadata

Assignees

Labels

P2Medium: important but non-blocking improvementenhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions