Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(mcp): persist refreshed OAuth credentials (#5107)
Eric Hayes12dfba54ba19
diff --git a/.sqlx/query-1fc6d4133d6252a01a56005faa3e7244ab1debf1731781813995f3be3075d9f1.json b/.sqlx/query-69895e1fdfef3998c26e8b62496504f96dc5d617be95ef79dd541acca264ddb4.jsonsimilarity index 65%rename from .sqlx/query-1fc6d4133d6252a01a56005faa3e7244ab1debf1731781813995f3be3075d9f1.jsonrename to .sqlx/query-69895e1fdfef3998c26e8b62496504f96dc5d617be95ef79dd541acca264ddb4.jsonindex 813b01d6d0..34a6493ba8 100644--- a/.sqlx/query-1fc6d4133d6252a01a56005faa3e7244ab1debf1731781813995f3be3075d9f1.json+++ b/.sqlx/query-69895e1fdfef3998c26e8b62496504f96dc5d617be95ef79dd541acca264ddb4.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n INSERT INTO mcp_servers (user_id, url, server_name, credentials, enabled)\n VALUES ($1, $2, $3, $4, $5)\n ON CONFLICT (user_id, url) DO UPDATE\n SET server_name = EXCLUDED.server_name,\n credentials = EXCLUDED.credentials,\n enabled = EXCLUDED.enabled,\n updated_at = NOW()\n ",+ "query": "\n INSERT INTO mcp_servers (user_id, url, server_name, credentials, enabled)\n VALUES ($1, $2, $3, $4, $5)\n ON CONFLICT (user_id, url) DO UPDATE\n SET server_name = EXCLUDED.server_name,\n credentials = COALESCE(EXCLUDED.credentials, mcp_servers.credentials),\n enabled = EXCLUDED.enabled,\n updated_at = NOW()\n ","describe": {"columns": [],"parameters": {@@ -14,5 +14,5 @@},"nullable": []},- "hash": "1fc6d4133d6252a01a56005faa3e7244ab1debf1731781813995f3be3075d9f1"+ "hash": "69895e1fdfef3998c26e8b62496504f96dc5d617be95ef79dd541acca264ddb4"}diff --git a/Cargo.lock b/Cargo.lockindex 6f6c407ebd..38e5eb3cc7 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -8713,6 +8713,7 @@ dependencies = ["ai_toolset","anyhow","async-openai",+ "async-trait","axum","base64 0.22.1","futures",@@ -8729,6 +8730,7 @@ dependencies = ["serde_json","sqlx","thiserror 2.0.18",+ "tokio","tracing","utoipa","workspace-hack",diff --git a/crates/import/src/domain/service.rs b/crates/import/src/domain/service.rsindex 873d38c306..b109072403 100644--- a/crates/import/src/domain/service.rs+++ b/crates/import/src/domain/service.rs@@ -461,7 +461,7 @@ where.collect();anyhow::ensure!(!records.is_empty(), "no {} connection", source.as_ref());- let mcp_tools = McpToolSet::new(&records).await;+ let mcp_tools = McpToolSet::new(&records, self.mcp_store.clone()).await;anyhow::ensure!(!mcp_tools.is_empty(),"could not load tools from {}",diff --git a/crates/mcp_client/Cargo.toml b/crates/mcp_client/Cargo.tomlindex 6f1918637a..37717ad689 100644--- a/crates/mcp_client/Cargo.toml+++ b/crates/mcp_client/Cargo.toml@@ -13,6 +13,7 @@ aes-gcm = "0.10"agent = { path = "../agent" }ai_toolset = { path = "../ai_toolset" }anyhow.workspace = true+async-trait.workspace = trueoauth2 = { version = "5.0", default-features = false }base64.workspace = trueasync-openai.workspace = true@@ -35,3 +36,6 @@ thiserror.workspace = truetracing.workspace = trueutoipa.workspace = trueworkspace-hack = { version = "0.1", path = "../workspace-hack" }++[dev-dependencies]+tokio.workspace = truediff --git a/crates/mcp_client/src/domain/models/server.rs b/crates/mcp_client/src/domain/models/server.rsindex faa94a5294..511998c7ba 100644--- a/crates/mcp_client/src/domain/models/server.rs+++ b/crates/mcp_client/src/domain/models/server.rs@@ -1,16 +1,15 @@use super::consts::MCP_CLIENT_NAME;-use crate::domain::ports::McpConnector;+use crate::domain::ports::{McpConnector, McpServerStore};+use crate::domain::service::PersistingCredentialStore;use macro_user_id::user_id::MacroUserIdStr;use rmcp::RoleClient;use rmcp::model::{ClientInfo, Implementation};use rmcp::service::{RunningService, ServiceExt};use rmcp::transport::StreamableHttpClientTransport;-use rmcp::transport::auth::{- AuthClient, AuthorizationManager, CredentialStore as _, InMemoryCredentialStore,- StoredCredentials,-};+use rmcp::transport::auth::{AuthClient, AuthorizationManager, StoredCredentials};use rmcp::transport::streamable_http_client::StreamableHttpClientTransportConfig;use serde::{Deserialize, Serialize};+use std::sync::Arc;/// A connected MCP server session.pub type McpServer = RunningService<RoleClient, ClientInfo>;@@ -52,12 +51,12 @@ pub struct McpServerRecord {impl McpConnector for McpServerRecord {#[tracing::instrument(skip_all, err)]- async fn connect(&self) -> anyhow::Result<McpServer> {+ async fn connect<S: McpServerStore>(&self, server_store: Arc<S>) -> anyhow::Result<McpServer> {match &self.credentials {Some(credentials) => {let mut auth_manager = AuthorizationManager::new(&self.url).await?;- let store = InMemoryCredentialStore::new();- store.save(credentials.clone()).await?;+ let store = PersistingCredentialStore::new(self.clone(), server_store);+ store.seed(credentials.clone()).await?;auth_manager.set_credential_store(store);auth_manager.initialize_from_store().await?;diff --git a/crates/mcp_client/src/domain/ports.rs b/crates/mcp_client/src/domain/ports.rsindex 7ef80fed87..ad4b83a426 100644--- a/crates/mcp_client/src/domain/ports.rs+++ b/crates/mcp_client/src/domain/ports.rs@@ -1,4 +1,5 @@use super::models::{MacroUserIdStr, McpServer, McpServerRecord};+use std::sync::Arc;/// Port for persisting MCP server records, keyed by user.pub trait McpServerStore: Send + Sync + 'static {@@ -32,7 +33,14 @@ pub trait McpServerStore: Send + Sync + 'static {/// Port for establishing a connection to an MCP server.pub trait McpConnector: Send + Sync {/// Connect to the MCP server described by this value.- fn connect(&self) -> impl Future<Output = anyhow::Result<McpServer>> + Send;+ ///+ /// Credential updates produced while connecting — and later, while the+ /// connection is in use (e.g. refreshed OAuth tokens) — are persisted+ /// through `server_store`.+ fn connect<S: McpServerStore>(+ &self,+ server_store: Arc<S>,+ ) -> impl Future<Output = anyhow::Result<McpServer>> + Send;}/// Everything needed to resume the OAuth flow on callback.diff --git a/crates/mcp_client/src/domain/service/mod.rs b/crates/mcp_client/src/domain/service/mod.rsindex 6dacdebbda..33ee6ae853 100644--- a/crates/mcp_client/src/domain/service/mod.rs+++ b/crates/mcp_client/src/domain/service/mod.rs@@ -1,6 +1,9 @@mod oauth;+/// Write-through OAuth credential store for MCP connections.+pub mod persisting_credential_store;/// MCP tool set and combined tool set for the AI loop.pub mod toolset;pub use oauth::OAuthService;+pub use persisting_credential_store::PersistingCredentialStore;pub use toolset::{CombinedToolSet, McpToolSet};diff --git a/crates/mcp_client/src/domain/service/persisting_credential_store.rs b/crates/mcp_client/src/domain/service/persisting_credential_store.rsnew file mode 100644index 0000000000..d5824f82cd--- /dev/null+++ b/crates/mcp_client/src/domain/service/persisting_credential_store.rs@@ -0,0 +1,87 @@+//! Write-through OAuth credential store for MCP connections.+//!+//! See [`PersistingCredentialStore`].++use crate::domain::{+ models::{McpServerRecord, StoredCredentials},+ ports::McpServerStore,+};+use rmcp::transport::auth::{AuthError, CredentialStore, InMemoryCredentialStore};+use std::sync::Arc;++#[cfg(test)]+mod test;++/// An rmcp [`CredentialStore`] that serves reads from memory and persists+/// every write back to the [`McpServerStore`].+///+/// Providers such as Linear rotate refresh tokens on every refresh: the+/// moment a new token pair is issued, the previous refresh token is+/// invalidated. rmcp refreshes lazily and writes the new credentials to its+/// credential store; when that store is purely in-memory, the rotated grant+/// is dropped at the end of the request and every subsequent connection+/// retries the invalidated grant still sitting in the database, forcing the+/// user to re-authorize roughly every access-token lifetime. Writing saves+/// through to the persistent store keeps the database in sync with the+/// authorization server.+pub struct PersistingCredentialStore<S> {+ inner: InMemoryCredentialStore,+ server_store: Arc<S>,+ record: McpServerRecord,+}++impl<S> PersistingCredentialStore<S> {+ /// Create a store for `record` that persists updates via `server_store`.+ ///+ /// The store starts empty; seed it with the record's current credentials+ /// via [`PersistingCredentialStore::seed`] before handing it to rmcp.+ pub fn new(record: McpServerRecord, server_store: Arc<S>) -> Self {+ Self {+ inner: InMemoryCredentialStore::new(),+ server_store,+ record,+ }+ }++ /// Seed the in-memory store without persisting.+ ///+ /// Used to load the already-persisted credentials into a fresh connection+ /// without triggering a redundant write back to the database.+ pub async fn seed(&self, credentials: StoredCredentials) -> Result<(), AuthError> {+ self.inner.save(credentials).await+ }+}++#[async_trait::async_trait]+impl<S> CredentialStore for PersistingCredentialStore<S>+where+ S: McpServerStore,+{+ async fn load(&self) -> Result<Option<StoredCredentials>, AuthError> {+ self.inner.load().await+ }++ async fn save(&self, credentials: StoredCredentials) -> Result<(), AuthError> {+ self.inner.save(credentials.clone()).await?;++ let mut record = self.record.clone();+ record.credentials = Some(credentials);+ if let Err(error) = self.server_store.save(&record).await {+ // The in-memory credentials stay usable for the remainder of+ // this connection. A failed persist only means the next+ // connection refreshes again (or, if the provider rotated+ // grants, requires re-authorization), so log and continue.+ tracing::error!(+ server = %record.server_name,+ error = ?error,+ "failed to persist refreshed MCP credentials"+ );+ }+ Ok(())+ }++ async fn clear(&self) -> Result<(), AuthError> {+ // Session-local only: never drop the persisted credentials.+ self.inner.clear().await+ }+}diff --git a/crates/mcp_client/src/domain/service/persisting_credential_store/test.rs b/crates/mcp_client/src/domain/service/persisting_credential_store/test.rsnew file mode 100644index 0000000000..c59e2e8908--- /dev/null+++ b/crates/mcp_client/src/domain/service/persisting_credential_store/test.rs@@ -0,0 +1,145 @@+use super::*;+use crate::domain::models::MacroUserIdStr;+use macro_user_id::cowlike::CowLike;+use std::sync::Mutex;++/// In-memory [`McpServerStore`] fake that records every save.+struct FakeServerStore {+ saved: Mutex<Vec<McpServerRecord>>,+ fail_saves: bool,+}++impl FakeServerStore {+ fn new() -> Self {+ Self {+ saved: Mutex::new(Vec::new()),+ fail_saves: false,+ }+ }++ fn failing() -> Self {+ Self {+ saved: Mutex::new(Vec::new()),+ fail_saves: true,+ }+ }++ fn saved(&self) -> Vec<McpServerRecord> {+ self.saved.lock().unwrap().clone()+ }+}++impl McpServerStore for FakeServerStore {+ type Err = anyhow::Error;++ async fn save(&self, record: &McpServerRecord) -> Result<(), Self::Err> {+ if self.fail_saves {+ anyhow::bail!("store unavailable");+ }+ self.saved.lock().unwrap().push(record.clone());+ Ok(())+ }++ async fn load(+ &self,+ _user_id: &MacroUserIdStr<'static>,+ _server_url: &str,+ ) -> Result<Option<McpServerRecord>, Self::Err> {+ unimplemented!()+ }++ async fn delete(+ &self,+ _user_id: &MacroUserIdStr<'static>,+ _server_url: &str,+ ) -> Result<(), Self::Err> {+ unimplemented!()+ }++ async fn list(+ &self,+ _user_id: &MacroUserIdStr<'static>,+ ) -> Result<Vec<McpServerRecord>, Self::Err> {+ unimplemented!()+ }+}++fn record() -> McpServerRecord {+ McpServerRecord {+ user_id: MacroUserIdStr::parse_from_str("macro|[email protected]")+ .unwrap()+ .into_owned(),+ url: "https://mcp.linear.app/mcp".to_string(),+ server_name: "Linear".to_string(),+ credentials: None,+ enabled: true,+ }+}++fn credentials(received_at: u64) -> StoredCredentials {+ StoredCredentials::new("client-id".to_string(), None, vec![], Some(received_at))+}++#[tokio::test]+async fn seed_loads_without_persisting() {+ let store = Arc::new(FakeServerStore::new());+ let creds_store = PersistingCredentialStore::new(record(), store.clone());++ creds_store.seed(credentials(1)).await.unwrap();++ let loaded = creds_store.load().await.unwrap().unwrap();+ assert_eq!(loaded.client_id, "client-id");+ assert_eq!(loaded.token_received_at, Some(1));+ assert!(store.saved().is_empty(), "seed must not persist");+}++#[tokio::test]+async fn save_writes_through_to_server_store() {+ let store = Arc::new(FakeServerStore::new());+ let creds_store = PersistingCredentialStore::new(record(), store.clone());++ creds_store.save(credentials(2)).await.unwrap();++ let saved = store.saved();+ assert_eq!(saved.len(), 1);+ let persisted = &saved[0];+ assert_eq!(persisted.url, "https://mcp.linear.app/mcp");+ assert_eq!(persisted.server_name, "Linear");+ assert!(persisted.enabled);+ let creds = persisted.credentials.as_ref().unwrap();+ assert_eq!(creds.client_id, "client-id");+ assert_eq!(creds.token_received_at, Some(2));++ // The in-memory view is updated as well.+ let loaded = creds_store.load().await.unwrap().unwrap();+ assert_eq!(loaded.token_received_at, Some(2));+}++#[tokio::test]+async fn save_survives_store_failure() {+ let store = Arc::new(FakeServerStore::failing());+ let creds_store = PersistingCredentialStore::new(record(), store.clone());++ // A persist failure must not fail the save: the refreshed credentials+ // stay usable for the rest of the connection.+ creds_store.save(credentials(3)).await.unwrap();++ let loaded = creds_store.load().await.unwrap().unwrap();+ assert_eq!(loaded.token_received_at, Some(3));+ assert!(store.saved().is_empty());+}++#[tokio::test]+async fn clear_is_session_local() {+ let store = Arc::new(FakeServerStore::new());+ let creds_store = PersistingCredentialStore::new(record(), store.clone());++ creds_store.seed(credentials(4)).await.unwrap();+ creds_store.clear().await.unwrap();++ assert!(creds_store.load().await.unwrap().is_none());+ assert!(+ store.saved().is_empty(),+ "clear must not touch the server store"+ );+}diff --git a/crates/mcp_client/src/domain/service/toolset.rs b/crates/mcp_client/src/domain/service/toolset.rsindex 2863d8956c..e2d80b57a6 100644--- a/crates/mcp_client/src/domain/service/toolset.rs+++ b/crates/mcp_client/src/domain/service/toolset.rs@@ -1,5 +1,5 @@use crate::domain::models::{Error, McpServer, McpServerRecord};-use crate::domain::ports::McpConnector;+use crate::domain::ports::{McpConnector, McpServerStore};use ai_toolset::{AsyncToolCollection, RequestContext, RequestSchema, SearchableTool, ToolCallError, ToolInfo,ToolResult, ToolSet, ToolSetError,@@ -59,16 +59,19 @@ pub struct McpToolSet {impl McpToolSet {/// Connect to every server in `records` concurrently, discover tools, and- /// register them.+ /// register them. Credential updates (e.g. refreshed OAuth tokens) are+ /// persisted through `server_store`.////// Servers that fail to connect or list tools are silently skipped.#[tracing::instrument(skip_all)]- pub async fn new(records: &[McpServerRecord]) -> Self {+ pub async fn new<S: McpServerStore>(records: &[McpServerRecord], server_store: Arc<S>) -> Self {let futs = records.iter().filter(|r| r.enabled)- .map(|record| async move {- let client = record.connect().await.inspect_err(|e| {+ .map(|record| {+ let server_store = server_store.clone();+ async move {+ let client = record.connect(server_store).await.inspect_err(|e| {tracing::warn!(server = %record.server_name, error = ?e, "failed to connect");}).ok()?;@@ -82,6 +85,7 @@ impl McpToolSet {};Some((record.server_name.clone(), client, server_tools))+ }});let results = futures::future::join_all(futs).await;@@ -267,11 +271,15 @@ pub struct CombinedToolSet<T> {impl<T> CombinedToolSet<T> {/// Build a combined toolset from the static tools and the user's MCP servers.- pub async fn new(+ ///+ /// Credential updates from the MCP connections (e.g. refreshed OAuth+ /// tokens) are persisted through `server_store`.+ pub async fn new<S: McpServerStore>(static_tools: Arc<AsyncToolCollection<T>>,records: &[McpServerRecord],+ server_store: Arc<S>,) -> Self {- let mcp_tools = McpToolSet::new(records).await;+ let mcp_tools = McpToolSet::new(records, server_store).await;Self {static_tools,mcp_tools,diff --git a/crates/mcp_client/src/outbound/pg_server_repo.rs b/crates/mcp_client/src/outbound/pg_server_repo.rsindex 6136f5268a..a5c8a1de3e 100644--- a/crates/mcp_client/src/outbound/pg_server_repo.rs+++ b/crates/mcp_client/src/outbound/pg_server_repo.rs@@ -72,13 +72,16 @@ impl McpServerStore for PgServerRepo {.map(|c| self.encrypt(c)).transpose()?;+ // Never clobber stored credentials with NULL on conflict: re-adding+ // an existing server (e.g. via the Add Server dialog) must not wipe+ // a valid OAuth grant.sqlx::query!(r#"INSERT INTO mcp_servers (user_id, url, server_name, credentials, enabled)VALUES ($1, $2, $3, $4, $5)ON CONFLICT (user_id, url) DO UPDATESET server_name = EXCLUDED.server_name,- credentials = EXCLUDED.credentials,+ credentials = COALESCE(EXCLUDED.credentials, mcp_servers.credentials),enabled = EXCLUDED.enabled,updated_at = NOW()"#,diff --git a/services/document_cognition_service/src/api/stream/chat_message/mod.rs b/services/document_cognition_service/src/api/stream/chat_message/mod.rsindex 288e74eadd..0115bf94d7 100644--- a/services/document_cognition_service/src/api/stream/chat_message/mod.rs+++ b/services/document_cognition_service/src/api/stream/chat_message/mod.rs@@ -504,7 +504,12 @@ fn stream_and_save_message(let mcp_records = mcp_store.list(&user_id).await.unwrap_or_default();let toolset: Arc<dyn ai_toolset::ToolSet<_> + Send + Sync> = Arc::new(- mcp_client::domain::service::CombinedToolSet::new(static_tools, &mcp_records).await,+ mcp_client::domain::service::CombinedToolSet::new(+ static_tools,+ &mcp_records,+ mcp_store.clone(),+ )+ .await,);let agent_loop =AgentLoop::new(tool_context.recorder.clone()).with_model(&model);diff --git a/services/document_cognition_service/src/api/structured_completion.rs b/services/document_cognition_service/src/api/structured_completion.rsindex 3725d32818..4eb7832249 100644--- a/services/document_cognition_service/src/api/structured_completion.rs+++ b/services/document_cognition_service/src/api/structured_completion.rs@@ -88,8 +88,12 @@ pub async fn structured_completion(let mcp_store = ctx.mcp_state.store();let mcp_records = mcp_store.list(&user_id).await.unwrap_or_default();let toolset: Arc<dyn ai_toolset::ToolSet<_> + Send + Sync> = Arc::new(- mcp_client::domain::service::CombinedToolSet::new(ctx.all_tools.clone(), &mcp_records)- .await,+ mcp_client::domain::service::CombinedToolSet::new(+ ctx.all_tools.clone(),+ &mcp_records,+ mcp_store.clone(),+ )+ .await,);let user_message = ChatMessage {