Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

18.3 KB
Raw Blame
use crate::domain::{
    models::{MacroUserIdStr, McpServerRecord, OAuthClientMetadata},
    ports::{McpServerStore, OAuthClient},
    service::{AddServerError, AddedBy, ServerDirectory},
};
use axum::{
    Json, Router,
    extract::{FromRef, Query, State},
    http::StatusCode,
    response::IntoResponse,
    routing::{delete, get, post, put},
};
use macro_authorization::{
    MacroAuthorizationExtractor, MacroAuthorizationService, MacroAuthorizationState, UserOrInternal,
};
use model_error_response::ErrorResponse;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use utoipa::{IntoParams, ToSchema};

#[cfg(test)]
mod test;

/// Hook invoked after an OAuth flow completes and the credentials are saved.
/// Hosts use this to react to a connection the moment it exists (e.g. record
/// analytics); implementations must be quick or spawn.
pub type McpAuthCompletedHook = Arc<
    dyn Fn(McpServerRecord) -> std::pin::Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync,
>;

/// Hook invoked after a user removes a server they had added, with the record
/// as it was before removal. Implementations must be quick or spawn.
pub type McpServerRemovedHook = McpAuthCompletedHook;

/// Shared state for the MCP router.
pub struct McpRouterState<S, O, Auth> {
    store: Arc<S>,
    oauth: Arc<O>,
    authorization_state: MacroAuthorizationState<Auth>,
    client_metadata: OAuthClientMetadata,
    on_auth_completed: Option<McpAuthCompletedHook>,
    on_server_removed: Option<McpServerRemovedHook>,
}

impl<S, O, Auth> Clone for McpRouterState<S, O, Auth> {
    fn clone(&self) -> Self {
        Self {
            store: self.store.clone(),
            oauth: self.oauth.clone(),
            authorization_state: self.authorization_state.clone(),
            client_metadata: self.client_metadata.clone(),
            on_auth_completed: self.on_auth_completed.clone(),
            on_server_removed: self.on_server_removed.clone(),
        }
    }
}

impl<S, O, Auth> FromRef<McpRouterState<S, O, Auth>> for MacroAuthorizationState<Auth> {
    fn from_ref(state: &McpRouterState<S, O, Auth>) -> Self {
        state.authorization_state.clone()
    }
}

impl<S, O, Auth> McpRouterState<S, O, Auth>
where
    S: McpServerStore,
    O: OAuthClient,
    Auth: MacroAuthorizationService,
{
    /// Create a new router state from a server store, OAuth client, and
    /// authorization state.
    pub fn new(
        store: S,
        oauth: O,
        authorization_state: MacroAuthorizationState<Auth>,
        client_metadata: OAuthClientMetadata,
    ) -> Self {
        Self {
            store: Arc::new(store),
            oauth: Arc::new(oauth),
            authorization_state,
            client_metadata,
            on_auth_completed: None,
            on_server_removed: None,
        }
    }

    /// Invoke `hook` whenever an OAuth flow completes (see
    /// [`McpAuthCompletedHook`]).
    pub fn with_auth_completed_hook(mut self, hook: McpAuthCompletedHook) -> Self {
        self.on_auth_completed = Some(hook);
        self
    }

    /// Invoke `hook` whenever a user removes a server (see
    /// [`McpServerRemovedHook`]).
    pub fn with_server_removed_hook(mut self, hook: McpServerRemovedHook) -> Self {
        self.on_server_removed = Some(hook);
        self
    }

    /// Access the underlying server store.
    pub fn store(&self) -> Arc<S> {
        self.store.clone()
    }
}

/// Authenticated MCP routes (CRUD + start auth).
pub fn mcp_router<S, O, Auth, Global>(state: McpRouterState<S, O, Auth>) -> Router<Global>
where
    S: McpServerStore,
    O: OAuthClient,
    Auth: MacroAuthorizationService,
    anyhow::Error: From<S::Err>,
    Global: Send + Sync,
{
    Router::new()
        .route("/mcp/servers", get(list_servers::<S, O, Auth>))
        .route("/mcp/servers", post(add_server::<S, O, Auth>))
        .route("/mcp/servers", put(update_server::<S, O, Auth>))
        .route("/mcp/servers", delete(delete_server::<S, O, Auth>))
        .route("/mcp/servers/auth/start", post(start_auth::<S, O, Auth>))
        .with_state(state)
}

/// Unauthenticated OAuth callback and client metadata routes.
pub fn mcp_oauth_callback_router<S, O, Auth, Global>(
    state: McpRouterState<S, O, Auth>,
) -> Router<Global>
where
    S: McpServerStore,
    O: OAuthClient,
    Auth: MacroAuthorizationService,
    anyhow::Error: From<S::Err>,
    Global: Send + Sync,
{
    Router::new()
        .route(
            "/mcp/servers/auth/callback",
            get(auth_callback::<S, O, Auth>),
        )
        .route(
            "/mcp/servers/auth/client-metadata",
            get(client_metadata::<S, O, Auth>),
        )
        .with_state(state)
}

// -- request / response types ------------------------------------------------

/// Request body for adding a new MCP server.
#[derive(Debug, Deserialize, ToSchema)]
pub struct AddServerRequest {
    /// The MCP server's streamable HTTP URL.
    url: String,
    /// Human-readable name for the server.
    server_name: String,
}

/// Request body for updating an MCP server.
#[derive(Debug, Deserialize, ToSchema)]
pub struct UpdateServerRequest {
    /// The server URL to update.
    url: String,
    /// New name for the server.
    #[serde(default)]
    server_name: Option<String>,
    /// Enable or disable the server.
    #[serde(default)]
    enabled: Option<bool>,
}

/// Query parameters for deleting an MCP server.
#[derive(Debug, Deserialize, IntoParams)]
#[into_params(parameter_in = Query)]
pub struct DeleteServerParams {
    /// The server URL to delete.
    url: String,
}

/// Request body for starting an OAuth authorization flow.
#[derive(Debug, Deserialize, ToSchema)]
pub struct StartAuthRequest {
    /// The MCP server URL to authorize against.
    server_url: String,
    /// Human-readable name for the server.
    server_name: String,
}

/// Response from starting an OAuth authorization flow.
#[derive(Debug, Serialize, ToSchema)]
pub struct StartAuthResponse {
    /// The OAuth authorization URL to redirect the user to.
    authorization_url: String,
}

/// Query parameters received on the OAuth callback redirect.
///
/// Providers redirect here on both success (`code` + `state`) and failure
/// (`error` [+ `error_description`], per RFC 6749 §4.1.2.1). All fields are
/// optional so a rejected authorization can still be parsed and logged
/// instead of failing Axum's query extraction before the handler runs.
#[derive(Debug, Deserialize, IntoParams)]
#[into_params(parameter_in = Query)]
pub struct AuthCallbackParams {
    /// Authorization code from the OAuth provider. Present on success.
    code: Option<String>,
    /// CSRF state parameter.
    state: Option<String>,
    /// OAuth error code from the provider, e.g. `access_denied`. Present on failure.
    error: Option<String>,
    /// Human-readable error description from the provider.
    error_description: Option<String>,
}

/// An MCP server record as returned by the API.
#[derive(Debug, Serialize, ToSchema)]
pub struct ServerResponse {
    /// The MCP server URL.
    url: String,
    /// Human-readable server name.
    server_name: String,
    /// Whether the server is enabled for tool use.
    enabled: bool,
    /// Whether the server has valid stored credentials.
    authenticated: bool,
}

impl ServerResponse {
    fn from_record(record: &McpServerRecord) -> Self {
        Self {
            url: record.url.clone(),
            server_name: record.server_name.clone(),
            enabled: record.enabled,
            authenticated: record.credentials.is_some(),
        }
    }
}

// -- error --------------------------------------------------------------------

/// Error type for MCP HTTP handlers.
#[derive(Debug, thiserror::Error)]
pub enum McpHandlerErr {
    /// The requested server was not found.
    #[error("server not found")]
    NotFound,
    /// The request was malformed.
    #[error("{0}")]
    InvalidRequest(String),
    /// The OAuth provider rejected the authorization request.
    #[error("authorization rejected by provider: {0}")]
    OAuthRejected(String),
    /// The callback was missing both a code and an error parameter.
    #[error("malformed OAuth callback: missing code and error parameters")]
    MalformedCallback,
    /// An internal error occurred.
    #[error("{0}")]
    Internal(#[from] anyhow::Error),
}

impl IntoResponse for McpHandlerErr {
    fn into_response(self) -> axum::response::Response {
        let status = match &self {
            McpHandlerErr::NotFound => StatusCode::NOT_FOUND,
            McpHandlerErr::InvalidRequest(_)
            | McpHandlerErr::OAuthRejected(_)
            | McpHandlerErr::MalformedCallback => StatusCode::BAD_REQUEST,
            McpHandlerErr::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
        };
        (
            status,
            Json(ErrorResponse {
                message: self.to_string().into(),
            }),
        )
            .into_response()
    }
}

// -- handlers -----------------------------------------------------------------

#[utoipa::path(
    get,
    path = "/mcp/servers",
    tag = "mcp",
    operation_id = "list_mcp_servers",
    responses(
        (status = 200, body = Vec<ServerResponse>),
        (status = 401, body = String),
        (status = 500, body = ErrorResponse),
    )
)]
/// List all MCP servers configured for the authenticated user.
#[tracing::instrument(skip_all, err)]
pub async fn list_servers<S, O, Auth>(
    State(state): State<McpRouterState<S, O, Auth>>,
    authorization: MacroAuthorizationExtractor<Auth, UserOrInternal>,
) -> Result<Json<Vec<ServerResponse>>, McpHandlerErr>
where
    S: McpServerStore,
    O: OAuthClient,
    Auth: MacroAuthorizationService,
    anyhow::Error: From<S::Err>,
{
    let user = &authorization.authorization.user;
    let records = state
        .store
        .list(&user.macro_user_id)
        .await
        .map_err(anyhow::Error::from)?;

    Ok(Json(
        records.iter().map(ServerResponse::from_record).collect(),
    ))
}

#[utoipa::path(
    post,
    path = "/mcp/servers",
    tag = "mcp",
    operation_id = "add_mcp_server",
    request_body = AddServerRequest,
    responses(
        (status = 201, body = ServerResponse),
        (status = 400, body = ErrorResponse),
        (status = 401, body = String),
        (status = 500, body = ErrorResponse),
    )
)]
/// Add a new MCP server for the authenticated user. A URL the user already
/// has is returned as stored, keeping its credentials.
#[tracing::instrument(skip_all, err)]
pub async fn add_server<S, O, Auth>(
    State(state): State<McpRouterState<S, O, Auth>>,
    authorization: MacroAuthorizationExtractor<Auth, UserOrInternal>,
    Json(body): Json<AddServerRequest>,
) -> Result<(StatusCode, Json<ServerResponse>), McpHandlerErr>
where
    S: McpServerStore,
    O: OAuthClient,
    Auth: MacroAuthorizationService,
    anyhow::Error: From<S::Err>,
{
    let user = &authorization.authorization.user;
    let added = ServerDirectory::new(state.store.clone())
        .add(
            user.macro_user_id.clone(),
            &body.url,
            &body.server_name,
            AddedBy::User,
        )
        .await
        .map_err(|error| match error {
            AddServerError::InvalidUrl(_)
            | AddServerError::NonPublicDestination(_)
            | AddServerError::EmptyName => McpHandlerErr::InvalidRequest(error.to_string()),
            AddServerError::Store(error) => McpHandlerErr::Internal(error),
        })?;

    Ok((
        StatusCode::CREATED,
        Json(ServerResponse::from_record(added.record())),
    ))
}

#[utoipa::path(
    put,
    path = "/mcp/servers",
    tag = "mcp",
    operation_id = "update_mcp_server",
    request_body = UpdateServerRequest,
    responses(
        (status = 200, body = ServerResponse),
        (status = 401, body = String),
        (status = 404, body = ErrorResponse),
        (status = 500, body = ErrorResponse),
    )
)]
/// Update an existing MCP server's name or enabled status.
#[tracing::instrument(skip_all, err)]
pub async fn update_server<S, O, Auth>(
    State(state): State<McpRouterState<S, O, Auth>>,
    authorization: MacroAuthorizationExtractor<Auth, UserOrInternal>,
    Json(body): Json<UpdateServerRequest>,
) -> Result<Json<ServerResponse>, McpHandlerErr>
where
    S: McpServerStore,
    O: OAuthClient,
    Auth: MacroAuthorizationService,
    anyhow::Error: From<S::Err>,
{
    let user = &authorization.authorization.user;
    let mut record = state
        .store
        .load(&user.macro_user_id, &body.url)
        .await
        .map_err(anyhow::Error::from)?
        .ok_or(McpHandlerErr::NotFound)?;

    if let Some(name) = body.server_name {
        record.server_name = name;
    }
    if let Some(enabled) = body.enabled {
        record.enabled = enabled;
    }

    state
        .store
        .save(&record)
        .await
        .map_err(anyhow::Error::from)?;

    Ok(Json(ServerResponse::from_record(&record)))
}

#[utoipa::path(
    delete,
    path = "/mcp/servers",
    tag = "mcp",
    operation_id = "delete_mcp_server",
    params(DeleteServerParams),
    responses(
        (status = 204),
        (status = 401, body = String),
        (status = 500, body = ErrorResponse),
    )
)]
/// Delete an MCP server by URL.
#[tracing::instrument(skip_all, err)]
pub async fn delete_server<S, O, Auth>(
    State(state): State<McpRouterState<S, O, Auth>>,
    authorization: MacroAuthorizationExtractor<Auth, UserOrInternal>,
    Query(params): Query<DeleteServerParams>,
) -> Result<StatusCode, McpHandlerErr>
where
    S: McpServerStore,
    O: OAuthClient,
    Auth: MacroAuthorizationService,
    anyhow::Error: From<S::Err>,
{
    let user = &authorization.authorization.user;
    remove_server(
        state.store.as_ref(),
        state.on_server_removed.as_ref(),
        &user.macro_user_id,
        &params.url,
    )
    .await?;

    Ok(StatusCode::NO_CONTENT)
}

/// Deletes a user's server, then passes the removed record to `on_removed`.
///
/// The record is read only when there is a hook, so a plain delete does no
/// extra read or decryption. A record that can't be read (e.g. credentials
/// that no longer decrypt) never blocks the delete: the hook is skipped
/// instead. Deleting an unknown URL stays a quiet no-op.
async fn remove_server<S>(
    store: &S,
    on_removed: Option<&McpServerRemovedHook>,
    user_id: &MacroUserIdStr<'static>,
    server_url: &str,
) -> anyhow::Result<()>
where
    S: McpServerStore,
    anyhow::Error: From<S::Err>,
{
    let removed = match on_removed {
        Some(_) => store.load(user_id, server_url).await.unwrap_or_else(|error| {
            tracing::warn!(error = ?error, "could not read MCP server before removal; skipping the removal hook");
            None
        }),
        None => None,
    };
    store
        .delete(user_id, server_url)
        .await
        .map_err(anyhow::Error::from)?;

    if let (Some(record), Some(hook)) = (removed, on_removed) {
        hook(record).await;
    }
    Ok(())
}

#[utoipa::path(
    post,
    path = "/mcp/servers/auth/start",
    tag = "mcp",
    operation_id = "start_mcp_auth",
    request_body = StartAuthRequest,
    responses(
        (status = 200, body = StartAuthResponse),
        (status = 401, body = String),
        (status = 500, body = ErrorResponse),
    )
)]
/// Start the OAuth authorization flow for an MCP server.
#[tracing::instrument(skip_all, err)]
pub async fn start_auth<S, O, Auth>(
    State(state): State<McpRouterState<S, O, Auth>>,
    authorization: MacroAuthorizationExtractor<Auth, UserOrInternal>,
    Json(body): Json<StartAuthRequest>,
) -> Result<Json<StartAuthResponse>, McpHandlerErr>
where
    S: McpServerStore,
    O: OAuthClient,
    Auth: MacroAuthorizationService,
{
    let user = &authorization.authorization.user;
    let authorization_url = state
        .oauth
        .start_authorization(&user.macro_user_id, &body.server_url, &body.server_name)
        .await?;

    Ok(Json(StartAuthResponse { authorization_url }))
}

#[utoipa::path(
    get,
    path = "/mcp/servers/auth/client-metadata",
    tag = "mcp",
    operation_id = "mcp_oauth_client_metadata",
    responses(
        (status = 200, description = "Macro OAuth client metadata document"),
    )
)]
/// Return Macro's public OAuth Client ID Metadata Document.
pub async fn client_metadata<S, O, Auth>(
    State(state): State<McpRouterState<S, O, Auth>>,
) -> Json<OAuthClientMetadata>
where
    S: McpServerStore,
    O: OAuthClient,
    Auth: MacroAuthorizationService,
{
    Json(state.client_metadata.clone())
}

/// Classify a callback as a successful `(code, state)` pair or a handler
/// error, logging provider rejections and malformed callbacks along the way.
fn parse_callback_params(params: AuthCallbackParams) -> Result<(String, String), McpHandlerErr> {
    if let Some(error) = params.error {
        let reason = match params.error_description {
            Some(description) => format!("{error}: {description}"),
            None => error,
        };
        tracing::warn!(reason, "MCP OAuth provider rejected authorization");
        return Err(McpHandlerErr::OAuthRejected(reason));
    }

    match (params.code, params.state) {
        (Some(code), Some(state)) => Ok((code, state)),
        _ => {
            tracing::warn!("MCP OAuth callback missing both code and error parameters");
            Err(McpHandlerErr::MalformedCallback)
        }
    }
}

#[utoipa::path(
    get,
    path = "/mcp/servers/auth/callback",
    tag = "mcp",
    operation_id = "mcp_auth_callback",
    params(AuthCallbackParams),
    responses(
        (status = 200, description = "OAuth flow completed successfully"),
        (status = 400, body = ErrorResponse, description = "Provider rejected authorization, or the callback was malformed"),
        (status = 500, body = ErrorResponse),
    )
)]
/// OAuth callback endpoint — receives code and state, or an error, from the
/// authorization server.
#[tracing::instrument(skip_all, err, fields(state = ?params.state))]
pub async fn auth_callback<S, O, Auth>(
    State(state): State<McpRouterState<S, O, Auth>>,
    Query(params): Query<AuthCallbackParams>,
) -> Result<String, McpHandlerErr>
where
    S: McpServerStore,
    O: OAuthClient,
    Auth: MacroAuthorizationService,
{
    let (code, csrf_state) = parse_callback_params(params)?;

    let record = state
        .oauth
        .exchange_authorization_code(&code, &csrf_state)
        .await?;

    if let Some(hook) = &state.on_auth_completed {
        hook(record).await;
    }

    Ok("Authorization successful. You can close this tab.".to_string())
}