Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ set as `PROOFLINE_TOKEN`. A token that works in one organization needs no

To register MCP without a prompt, run `proofline agent install CLIENT --yes`. Supported clients include `claude-code` (or `claude`), `cursor`, `codex`, `gemini`, and `kiro`. Without `--yes`, the command previews the entry and asks for confirmation, while `--print` previews without writing. Existing Proofline entries remain unchanged. New entries use this executable's absolute path and store no credentials. Claude Code uses the top-level `mcpServers` in `~/.claude.json`; a custom `CLAUDE_CONFIG_DIR` requires manual configuration with `--print`.

To unregister, run `proofline agent remove CLIENT`. It previews the `proofline` entry and its file, asks for confirmation, and removes only that server; `--yes` skips the prompt, and `--print` prints the entry without writing. Every other byte of a JSON file stays as it was, and a Codex file keeps its comments and layout. An emptied `mcpServers` object or `mcp_servers` table stays in place. With no entry, the command says so and exits 0. It refuses a file it cannot parse and leaves it unchanged, and it uses the same file checks and lock as `install`. A custom `CLAUDE_CONFIG_DIR` or `CODEX_HOME` requires removing the entry manually. When `install` finds an entry that starts a different executable, such as one from an earlier installation, it names that path and suggests `proofline agent remove CLIENT` followed by `proofline agent install CLIENT`.

Native knowledge, plan, fix and configuration commands send the selected organization as `organization_id` in each MCP tool call. With a `PROOFLINE_TOKEN` that works in more than one organization, select an authorized UUID using `--org ORGANIZATION_ID` or `PROOFLINE_ORGANIZATION_ID`; the flag takes precedence. A token that works in exactly one, such as a personal token, needs neither. A stored device sign-in uses its approved organization and refuses a different selection. For example, `proofline --org ORGANIZATION_ID wiki list --repo owner/name` and `proofline --org ORGANIZATION_ID config get` target that organization with a shared MCP OAuth credential. Selection grants no authority: the server checks each call, and a foreign target returns its refusal with a nonzero exit. MCP OAuth credentials remain limited to MCP and cannot use REST commands. The raw `proofline mcp` bridge preserves the caller's messages and per-call target unchanged.

## Reviews, history, and reruns
Expand Down
252 changes: 214 additions & 38 deletions src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub(crate) struct AgentArgs {
enum AgentCommand {
/// Add Proofline to a coding agent's user configuration after confirmation.
Install(InstallArgs),
/// Remove Proofline from a coding agent's user configuration after confirmation.
Remove(RemoveArgs),
}

#[derive(Debug, Args)]
Expand All @@ -40,6 +42,19 @@ struct InstallArgs {
yes: bool,
}

#[derive(Debug, Args)]
struct RemoveArgs {
/// Coding agent whose user configuration loses the MCP server.
#[arg(value_enum)]
client: Client,
/// Print the server entry that would be removed without writing any file.
#[arg(long)]
print: bool,
/// Remove the previewed entry without an interactive confirmation.
#[arg(long, conflicts_with = "print")]
yes: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
enum Client {
#[value(alias = "claude")]
Expand All @@ -60,6 +75,16 @@ impl Client {
Self::Kiro => ".kiro/settings/mcp.json",
}
}

const fn argument(self) -> &'static str {
match self {
Self::ClaudeCode => "claude-code",
Self::Codex => "codex",
Self::Cursor => "cursor",
Self::Gemini => "gemini",
Self::Kiro => "kiro",
}
}
}

impl fmt::Display for Client {
Expand Down Expand Up @@ -90,6 +115,22 @@ enum InstallError {
"CODEX_HOME selects a different directory; use --print and add the entry to that user configuration manually"
)]
CustomCodexHome,
#[error(
"CLAUDE_CONFIG_DIR selects custom configuration; remove the proofline server from that Claude Code configuration manually"
)]
RemoveCustomClaudeHome,
#[error(
"CODEX_HOME selects a different directory; remove the proofline server from that user configuration manually"
)]
RemoveCustomCodexHome,
#[error(
"could not parse the {0} configuration; it stays unchanged, so remove the proofline server manually"
)]
RemoveInvalidConfig(Client),
#[error(
"the configuration exceeds the 1 MiB installer limit; it stays unchanged, so remove the proofline server manually"
)]
RemoveTooLarge,
#[error(
"could not parse the {0} configuration; leave it unchanged and use --print for the server entry"
)]
Expand Down Expand Up @@ -156,6 +197,15 @@ impl InstallError {
const fn console(operation: &'static str, source: io::Error) -> Self {
Self::Console { operation, source }
}

/// Replace install advice that does not apply to a removal.
fn for_removal(self) -> Self {
match self {
Self::InvalidConfig(client) => Self::RemoveInvalidConfig(client),
Self::TooLarge => Self::RemoveTooLarge,
other => other,
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand All @@ -164,31 +214,49 @@ enum Outcome {
Cancelled,
Existing,
Installed,
Absent,
Removed,
}

pub(crate) async fn run(args: AgentArgs, origin: Origin) -> anyhow::Result<()> {
tokio::task::spawn_blocking(move || {
let AgentCommand::Install(args) = args.command;
let home = std::env::home_dir().ok_or(InstallError::Home)?;
let codex_home = std::env::var_os("CODEX_HOME");
if args.client == Client::ClaudeCode
&& !args.print
&& std::env::var_os("CLAUDE_CONFIG_DIR").is_some()
{
return Err(InstallError::CustomClaudeHome);
}
let custom_claude = std::env::var_os("CLAUDE_CONFIG_DIR").is_some();
let stdin = io::stdin();
let stdout = io::stdout();
let stderr = io::stderr();
install(
&home,
codex_home.as_deref(),
&args,
&origin,
&mut stdin.lock(),
&mut stdout.lock(),
&mut stderr.lock(),
)
match args.command {
AgentCommand::Install(args) => {
if args.client == Client::ClaudeCode && !args.print && custom_claude {
return Err(InstallError::CustomClaudeHome);
}
install(
&home,
codex_home.as_deref(),
&args,
&origin,
&mut stdin.lock(),
&mut stdout.lock(),
&mut stderr.lock(),
)
}
AgentCommand::Remove(args) => {
// Even the preview reads ~/.claude.json, which Claude Code
// ignores while CLAUDE_CONFIG_DIR is set.
if args.client == Client::ClaudeCode && custom_claude {
return Err(InstallError::RemoveCustomClaudeHome);
}
remove(
&home,
codex_home.as_deref(),
&args,
&mut stdin.lock(),
&mut stdout.lock(),
&mut stderr.lock(),
)
}
}
})
.await
.map_err(InstallError::Task)??;
Expand Down Expand Up @@ -223,7 +291,7 @@ fn install(
let before = layout.read()?;
let config::Update::Change(bytes) = config::prepare(args.client, before.bytes(), origin)?
else {
existing(&layout, diagnostics)?;
existing(&layout, args.client, before.bytes(), diagnostics)?;
return Ok(Outcome::Existing);
};
layout.writable(&before)?;
Expand All @@ -234,28 +302,18 @@ fn install(
layout.target()
)
.map_err(|error| InstallError::console("show the proposed server entry", error))?;
if !args.yes {
write!(diagnostics, "Write this entry? [y/N] ")
.and_then(|()| diagnostics.flush())
.map_err(|error| InstallError::console("ask for confirmation", error))?;
let mut answer = String::new();
input
.take(65)
.read_line(&mut answer)
.map_err(|error| InstallError::console("read confirmation", error))?;
if answer.len() > 64
|| !answer.ends_with('\n')
|| !matches!(answer.trim().to_ascii_lowercase().as_str(), "y" | "yes")
{
writeln!(diagnostics, "No configuration was written.").map_err(|error| {
InstallError::console("report the unchanged configuration", error)
})?;
return Ok(Outcome::Cancelled);
}
if !args.yes && !confirm("Write this entry?", input, diagnostics)? {
return Ok(Outcome::Cancelled);
}
match layout.save(&before, &bytes, origin)? {
store::Saved::Existing => {
existing(&layout, diagnostics)?;
let current = layout.read().ok();
existing(
&layout,
args.client,
current.as_ref().and_then(store::Snapshot::bytes),
diagnostics,
)?;
Ok(Outcome::Existing)
}
store::Saved::Installed => {
Expand All @@ -266,11 +324,129 @@ fn install(
}
}

fn existing(layout: &store::Layout, diagnostics: &mut impl Write) -> Result<(), InstallError> {
fn remove(
home: &Path,
codex_home: Option<&OsStr>,
args: &RemoveArgs,
input: &mut impl BufRead,
output: &mut impl Write,
diagnostics: &mut impl Write,
) -> Result<Outcome, InstallError> {
if !home.is_absolute() {
return Err(InstallError::Home);
}
if args.client == Client::Codex
&& codex_home.is_some_and(|selected| Path::new(selected) != home.join(".codex"))
{
return Err(InstallError::RemoveCustomCodexHome);
}
let layout = store::Layout::new(home, args.client)?;
let before = layout.read().map_err(InstallError::for_removal)?;
let Some(removal) =
config::remove(args.client, before.bytes()).map_err(InstallError::for_removal)?
else {
writeln!(
diagnostics,
"No proofline entry in {:?}; nothing to remove.",
layout.target()
)
.map_err(|error| InstallError::console("report the missing entry", error))?;
return Ok(Outcome::Absent);
};
if args.print {
output
.write_all(removal.entry.as_bytes())
.map_err(|error| InstallError::console("print the server entry", error))?;
writeln!(
diagnostics,
"This entry would be removed from {:?}. No configuration was written.",
layout.target()
)
.map_err(|error| InstallError::console("name the configuration", error))?;
return Ok(Outcome::Printed);
}
layout.writable(&before)?;
writeln!(
diagnostics,
"Remove this {0} server from {1:?}:\n{2}",
args.client,
layout.target(),
removal.entry
)
.map_err(|error| InstallError::console("show the server entry", error))?;
if !args.yes && !confirm("Remove this entry?", input, diagnostics)? {
return Ok(Outcome::Cancelled);
}
layout
.replace(&before, &removal.bytes)
.map_err(InstallError::for_removal)?;
writeln!(
diagnostics,
"Removed the proofline server from {:?}. Sessions that already started may keep it until they restart.",
layout.target()
)
.map_err(|error| InstallError::console("report the saved configuration", error))?;
Ok(Outcome::Removed)
}

/// Ask a yes/no question; anything but a complete `y` or `yes` line declines.
fn confirm(
question: &str,
input: &mut impl BufRead,
diagnostics: &mut impl Write,
) -> Result<bool, InstallError> {
write!(diagnostics, "{question} [y/N] ")
.and_then(|()| diagnostics.flush())
.map_err(|error| InstallError::console("ask for confirmation", error))?;
let mut answer = String::new();
input
.take(65)
.read_line(&mut answer)
.map_err(|error| InstallError::console("read confirmation", error))?;
if answer.len() > 64
|| !answer.ends_with('\n')
|| !matches!(answer.trim().to_ascii_lowercase().as_str(), "y" | "yes")
{
writeln!(diagnostics, "No configuration was written.")
.map_err(|error| InstallError::console("report the unchanged configuration", error))?;
return Ok(false);
}
Ok(true)
}

fn existing(
layout: &store::Layout,
client: Client,
bytes: Option<&[u8]>,
diagnostics: &mut impl Write,
) -> Result<(), InstallError> {
writeln!(
diagnostics,
"A proofline entry already exists in {:?}; it stays unchanged.",
layout.target()
)
.map_err(|error| InstallError::console("report the existing entry", error))
.map_err(|error| InstallError::console("report the existing entry", error))?;
let Some(recorded) = bytes.and_then(|bytes| config::recorded_command(client, bytes)) else {
return Ok(());
};
if !other_executable(&recorded) {
return Ok(());
}
writeln!(
diagnostics,
"It starts {recorded:?}, not this executable. To use this one, run `proofline agent remove {0}` and then `proofline agent install {0}`.",
client.argument()
)
.map_err(|error| InstallError::console("suggest replacing the existing entry", error))
}

/// Whether a recorded command names a different file than the running one.
/// Paths are compared after resolving symlinks, so a symlinked launch path
/// such as macOS `/var` and `/private/var` still counts as the same file.
fn other_executable(recorded: &str) -> bool {
let Ok(current) = std::env::current_exe() else {
return false;
};
let recorded = Path::new(recorded);
recorded != current && recorded.canonicalize().ok() != current.canonicalize().ok()
}
Loading