Skip to main content

agentmux_srv_core/backend/
claudeai_connectors.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The claude.ai account's connectors (Gmail, Calendar, Drive, Docs) are off in Claude agents by default.
5//!
6//! Claude Code loads the connectors of the signed-in claude.ai account into every session. AgentMux has its
7//! own Google tools (`Gmail`, `Calendar`, `Drive`, once the person grants them), so the claude.ai ones only
8//! add a second, unauthorized copy of each, and agents keep telling the person to authorize them in claude.ai.
9//!
10//! The switch is an environment variable the CLI reads at launch (`ENABLE_CLAUDEAI_MCP_SERVERS`, also the
11//! CLI's `disableClaudeAiConnectors` setting). Its name is the CLI's, not ours: when the pinned CLI version
12//! moves, the canary test below and this note are rechecked against it.
13//!
14//! Setting `agent:claudeaiconnectors`: `false` (the default) launches Claude agents with the variable set to
15//! `false`; `true` leaves it unset, so the CLI loads the connectors as it does outside AgentMux. It takes
16//! effect at an agent's next launch.
17
18use std::collections::HashMap;
19use std::sync::{Arc, OnceLock};
20
21use super::wconfig::ConfigState;
22
23/// The setting. Off by default.
24pub const SETTING_CLAUDEAI_CONNECTORS: &str = "agent:claudeaiconnectors";
25
26/// The CLI's own switch: `false` keeps the claude.ai connectors out of the session.
27pub const ENV_SWITCH: &str = "ENABLE_CLAUDEAI_MCP_SERVERS";
28
29static CONFIG: OnceLock<Arc<ConfigState>> = OnceLock::new();
30
31/// Attach the live settings, so [`SETTING_CLAUDEAI_CONNECTORS`] is read at each launch (bootstrap, through
32/// `cli_messaging::install_config`).
33pub fn install_config(config: Arc<ConfigState>) {
34    let _ = CONFIG.set(config);
35}
36
37/// Whether the connectors stay on: only a boolean `true` turns them on.
38pub fn enabled(extra: &HashMap<String, serde_json::Value>) -> bool {
39    extra.get(SETTING_CLAUDEAI_CONNECTORS).and_then(|v| v.as_bool()).unwrap_or(false)
40}
41
42/// Keep the connectors out of a launch environment: a Claude agent's, when the setting is off. It
43/// overwrites what the environment already carries (a persisted `cmd:env`), as the setting is the only way
44/// to turn them back on. Any other provider's launch is left alone.
45pub fn apply(env_vars: &mut HashMap<String, String>, provider: &str, connectors_on: bool) {
46    if provider == "claude" && !connectors_on {
47        env_vars.insert(ENV_SWITCH.to_string(), "false".to_string());
48    }
49}
50
51/// [`apply`] for a launch of `provider` (already resolved from a legacy alias), reading the setting now.
52/// Called from `cli_messaging::apply_for_spawn`, which resolves the block's provider for both switches.
53pub fn apply_for_spawn(env_vars: &mut HashMap<String, String>, provider: &str) {
54    let on = CONFIG.get().is_some_and(|c| enabled(&c.get_settings().extra));
55    apply(env_vars, provider, on);
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    fn extra(value: Option<serde_json::Value>) -> HashMap<String, serde_json::Value> {
63        value.into_iter().map(|v| (SETTING_CLAUDEAI_CONNECTORS.to_string(), v)).collect()
64    }
65
66    #[test]
67    fn the_connectors_are_off_unless_the_setting_is_true() {
68        assert!(!enabled(&extra(None)));
69        assert!(!enabled(&extra(Some(serde_json::json!("yes")))));
70        assert!(!enabled(&extra(Some(serde_json::json!(false)))));
71        assert!(enabled(&extra(Some(serde_json::json!(true)))));
72    }
73
74    /// The canary: the switch is present, as `false`, for a Claude agent while the setting is off, and absent
75    /// once it is on. If the CLI renames its switch, this is where the new name goes.
76    #[test]
77    fn a_claude_launch_carries_the_switch_with_the_setting_off_and_not_with_it_on() {
78        let mut off = HashMap::new();
79        apply(&mut off, "claude", false);
80        assert_eq!(off.get(ENV_SWITCH).map(String::as_str), Some("false"));
81
82        let mut on = HashMap::new();
83        apply(&mut on, "claude", true);
84        assert!(!on.contains_key(ENV_SWITCH));
85    }
86
87    #[test]
88    fn the_switch_overwrites_a_persisted_value_and_other_providers_are_left_alone() {
89        let mut persisted = HashMap::from([(ENV_SWITCH.to_string(), "true".to_string())]);
90        apply(&mut persisted, "claude", false);
91        assert_eq!(persisted.get(ENV_SWITCH).map(String::as_str), Some("false"));
92
93        let mut codex = HashMap::new();
94        apply(&mut codex, "codex", false);
95        assert!(codex.is_empty());
96    }
97}