1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// SPDX-License-Identifier: Apache-2.0
//! Command line.
use clap::{Parser, Subcommand};
use crate::serve::Access;
/// iCloud Mail, Calendar, Reminders and Contacts for MCP clients.
#[derive(Debug, Parser)]
#[command(name = "ibridge", version, about)]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Debug, Subcommand)]
enum Command {
/// Checks an app-specific password, stores it in the keychain and prints the setup line for Claude Code.
Login,
/// Removes the stored password and account state.
Logout,
/// Checks the keychain, the network and the sign-in for each service.
Doctor,
/// Installs, removes or checks the background service that serves MCP on loopback.
#[cfg(feature = "http")]
Daemon {
#[command(subcommand)]
action: DaemonAction,
},
/// Operator commands for the hosted service, run on its host (for example with `railway ssh`).
#[cfg(feature = "hosted")]
Admin {
/// Where the hosted database lives.
#[arg(long, value_name = "DIR", default_value = "/data", global = true)]
data_dir: std::path::PathBuf,
#[command(subcommand)]
action: AdminCommand,
},
/// Runs the MCP server.
Serve {
/// Speak MCP over stdin and stdout. Logs go to stderr.
#[arg(long)]
stdio: bool,
/// Speak MCP over HTTP on a loopback address, for example 127.0.0.1:8765.
#[cfg(feature = "http")]
#[arg(long, value_name = "ADDR", conflicts_with = "stdio")]
http: Option<std::net::SocketAddr>,
/// Hide and reject every tool that changes mail.
#[arg(long)]
read_only: bool,
/// Run the hosted service: OAuth sign-in for many accounts, with `--http` as the listen address.
#[cfg(feature = "hosted")]
#[arg(
long,
conflicts_with_all = ["stdio", "read_only"],
requires_all = ["http", "public_url", "data_dir"]
)]
hosted: bool,
/// The https origin clients reach, for example `https://mcp.ibridge.fyi`.
#[cfg(feature = "hosted")]
#[arg(long, value_name = "URL", requires = "hosted")]
public_url: Option<String>,
/// Where the hosted database and its backups live.
#[cfg(feature = "hosted")]
#[arg(long, value_name = "DIR", requires = "hosted")]
data_dir: Option<std::path::PathBuf>,
/// Let any iCloud account sign in (the public service).
#[cfg(feature = "hosted")]
#[arg(long, requires = "hosted")]
multi_tenant: bool,
/// Trust `X-Forwarded-For` from the platform's proxy (Railway, Fly). Also `IBRIDGE_BEHIND_PROXY=true`.
#[cfg(feature = "hosted")]
#[arg(long, requires = "hosted")]
behind_proxy: bool,
},
}
#[cfg(feature = "hosted")]
#[derive(Debug, Subcommand)]
pub(crate) enum AdminCommand {
/// Blocks an account: its tokens, API keys and sign-in stop working. Nothing is deleted.
Suspend {
/// The account's iCloud address, in any letter case.
apple_id: String,
},
/// Deletes an account and every sealed copy of its password.
Delete {
/// The account's iCloud address, in any letter case.
apple_id: String,
},
/// Gives a user the plan for free, so the owner and test users are never charged.
Comp {
/// The user's Apple email (a private relay address is fine) or Firebase uid.
user: String,
},
/// Takes a user's free access away. A subscription they pay for still counts.
Uncomp {
/// The user's Apple email or Firebase uid.
user: String,
},
}
#[cfg(feature = "http")]
#[derive(Debug, Subcommand)]
enum DaemonAction {
/// Installs and starts the service, then prints the setup line for Claude Code.
Install {
/// The loopback port the service listens on.
#[arg(long, default_value_t = crate::daemon::DEFAULT_PORT)]
port: u16,
/// Hide and reject every tool that changes mail.
#[arg(long)]
read_only: bool,
},
/// Stops the service and removes it.
Uninstall,
/// Says whether the service is installed and running.
Status,
}
/// Parses the command line and runs the command.
pub(crate) async fn run() -> anyhow::Result<()> {
match Cli::parse().command {
Command::Login => crate::login::run().await,
Command::Doctor => crate::doctor::run().await,
Command::Logout => {
crate::platform::install_keychain()?;
crate::login::logout()
}
#[cfg(feature = "http")]
Command::Daemon { action } => match action {
DaemonAction::Install { port, read_only } => crate::daemon::run_install(
port,
if read_only {
Access::ReadOnly
} else {
Access::ReadWrite
},
),
DaemonAction::Uninstall => crate::daemon::run_uninstall(),
DaemonAction::Status => crate::daemon::run_status(),
},
#[cfg(feature = "hosted")]
Command::Admin { data_dir, action } => crate::hosted::admin(&data_dir, action).await,
Command::Serve {
stdio,
#[cfg(feature = "http")]
http,
read_only,
#[cfg(feature = "hosted")]
hosted,
#[cfg(feature = "hosted")]
public_url,
#[cfg(feature = "hosted")]
data_dir,
#[cfg(feature = "hosted")]
multi_tenant,
#[cfg(feature = "hosted")]
behind_proxy,
} => {
#[cfg(feature = "hosted")]
if hosted {
// clap's `requires_all` guarantees these three are set.
let (Some(addr), Some(public_url), Some(data_dir)) = (http, public_url, data_dir)
else {
anyhow::bail!("--hosted needs --http, --public-url and --data-dir");
};
return crate::hosted::run(ibridge_hosted::HostedOptions {
addr,
public_url,
data_dir,
multi_tenant,
behind_proxy,
})
.await;
}
let access = if read_only {
Access::ReadOnly
} else {
Access::ReadWrite
};
#[cfg(feature = "http")]
if let Some(addr) = http {
return crate::serve::http(addr, access).await;
}
if !stdio {
anyhow::bail!("choose a transport: --stdio or --http <ADDR>");
}
crate::serve::stdio(access).await
}
}
}
#[cfg(all(test, feature = "hosted"))]
mod tests {
use clap::Parser;
use super::{AdminCommand, Cli, Command};
#[test]
fn admin_takes_an_action_an_address_and_a_data_dir() {
let cli =
Cli::try_parse_from(["ibridge", "admin", "suspend", "ada@icloud.com"]).expect("parses");
let Command::Admin { data_dir, action } = cli.command else {
panic!("not the admin command");
};
assert_eq!(
data_dir,
std::path::Path::new("/data"),
"the container's volume by default"
);
assert!(
matches!(action, AdminCommand::Suspend { apple_id } if apple_id == "ada@icloud.com")
);
let cli = Cli::try_parse_from([
"ibridge",
"admin",
"delete",
"ada@icloud.com",
"--data-dir",
"/tmp/x",
])
.expect("the flag comes after the action");
let Command::Admin { data_dir, action } = cli.command else {
panic!("not the admin command");
};
assert_eq!(data_dir, std::path::Path::new("/tmp/x"));
assert!(matches!(action, AdminCommand::Delete { .. }));
assert!(
Cli::try_parse_from(["ibridge", "admin", "suspend"]).is_err(),
"an address is required"
);
}
#[test]
fn admin_comp_takes_email_or_uid() {
for target in ["ada@privaterelay.appleid.com", "firebase-uid-123"] {
for (word, comp) in [("comp", true), ("uncomp", false)] {
let cli =
Cli::try_parse_from(["ibridge", "admin", word, target, "--data-dir", "/tmp/x"])
.expect("parses");
let Command::Admin { data_dir, action } = cli.command else {
panic!("not the admin command");
};
assert_eq!(data_dir, std::path::Path::new("/tmp/x"));
match action {
AdminCommand::Comp { user } if comp => assert_eq!(user, target),
AdminCommand::Uncomp { user } if !comp => assert_eq!(user, target),
other => panic!("{word} {target} parsed as {other:?}"),
}
}
}
assert!(
Cli::try_parse_from(["ibridge", "admin", "comp"]).is_err(),
"a user is required"
);
}
}