use std::{collections::BTreeMap, ffi::OsString};
use serde_json::{Value, json};
use crate::platform;
pub const PROGRAM: &str = "claude";
pub const SERVER: &str = crate::stdio_mcp::NAME;
#[derive(Debug, Default)]
pub struct Launch<'a> {
pub model: Option<&'a str>,
pub effort: Option<&'a str>,
pub system: Option<&'a str>,
pub tools: Vec<&'a str>,
pub schema: Option<&'a Value>,
}
impl Launch<'_> {
pub fn args(&self) -> Vec<OsString> {
let mut args: Vec<String> = [
"-p",
"--input-format",
"stream-json",
"--output-format",
"stream-json",
"--verbose",
"--include-partial-messages",
"--tools",
"",
"--strict-mcp-config",
"--permission-mode",
"dontAsk",
"--setting-sources",
"",
"--settings",
r#"{"disableAllHooks":true}"#,
"--no-session-persistence",
"--disable-slash-commands",
"--no-chrome",
]
.map(str::to_owned)
.into();
if self.model.is_some() {
args.extend([
"--mcp-config".into(),
json!({"mcpServers": {SERVER: {"type": "sdk", "name": SERVER}}}).to_string(),
]);
}
if !self.tools.is_empty() {
let tools: Vec<String> = self.tools.iter().map(|name| tool_name(name)).collect();
args.extend(["--allowedTools".into(), tools.join(",")]);
}
if let Some(model) = self.model {
args.extend([
"--model".into(),
model.into(),
"--thinking".into(),
"adaptive".into(),
"--thinking-display".into(),
"summarized".into(),
]);
}
if let Some(effort) = self.effort {
args.extend(["--effort".into(), effort.into()]);
}
if let Some(system) = self.system {
args.extend(["--append-system-prompt".into(), system.into()]);
}
if let Some(schema) = self.schema {
args.extend(["--json-schema".into(), schema.to_string()]);
}
args.into_iter().map(OsString::from).collect()
}
}
pub fn tool_name(name: &str) -> String {
format!("mcp__{SERVER}__{name}")
}
const CLEARED: [&str; 5] = [
"CLAUDE_CODE_SIMPLE",
"CLAUDE_CODE_EFFORT_LEVEL",
"MCP_TOOL_TIMEOUT",
"CLAUDECODE",
"CLAUDE_CODE_ENTRYPOINT",
];
const SET: [(&str, &str); 9] = [
("CLAUDE_CODE_DISABLE_CLAUDE_MDS", "1"),
("CLAUDE_CODE_DISABLE_AUTO_MEMORY", "1"),
("DISABLE_AUTO_COMPACT", "1"),
("CLAUDE_CODE_ENABLE_AWAY_SUMMARY", "0"),
("ENABLE_TOOL_SEARCH", "false"),
("CLAUDE_CODE_DISABLE_ADVISOR_TOOL", "1"),
("MAX_MCP_OUTPUT_TOKENS", "200000"),
("CLAUDE_CODE_DISABLE_NONSTREAMING_FALLBACK", "1"),
("CLAUDE_CODE_DISABLE_REFUSAL_FALLBACK", "1"),
];
pub fn environment(mut inherited: BTreeMap<OsString, OsString>) -> BTreeMap<OsString, OsString> {
for name in CLEARED {
platform::remove(&mut inherited, name.as_ref());
}
for (name, value) in SET {
platform::set(&mut inherited, name, value);
}
inherited
}
#[cfg(test)]
mod tests;