Skip to content
Merged
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: 1 addition & 1 deletion crates/harnx-mcp-plans/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rmcp = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
serde_yaml = { workspace = true }
tokio = { workspace = true, features = ["fs"] }
tokio = { workspace = true, features = ["fs", "time"] }
schemars = { workspace = true }
similar = { workspace = true }

Expand Down
36 changes: 36 additions & 0 deletions crates/harnx-mcp-plans/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# harnx-mcp-plans

`harnx-mcp-plans` is an MCP server providing file-based management for plans, tasks, and notes. It stores data as YAML-frontmatter markdown files, making the storage human-readable and compatible with standard version control systems.

## Overview

The server organizes information into plans. Each plan is a directory containing:
- `plan.md`: The main plan description and metadata.
- `tasks/*.md`: Individual task files.
- `notes/*.md`: Individual note files.

## Usage

Run the server as a subprocess in your MCP host (e.g., Claude Desktop).

### CLI Options

| Option | Short | Environment Variable | Default | Description |
| :--- | :--- | :--- | :--- | :--- |
| `--dir <path>` | `-d` | `AGENT_PLANS_PATH` | `.agent/plans` | Path to the plans directory. |
| `--retention-days <N>` | `-r` | `AGENT_PLANS_RETENTION_DAYS` | `14` | Retention period in days. |

### Retention & Cleanup

The server automatically cleans up inactive plans to manage disk space.

- **Behavior**: On startup and every 24 hours thereafter, the server scans the plans directory. It deletes any plan directory where no file activity (`plan.md`, `tasks/*.md`, or `notes/*.md`) has occurred for longer than the retention period.
- **Disabling Cleanup**: Set `--retention-days 0` or `AGENT_PLANS_RETENTION_DAYS=0` to disable the automatic cleanup process.

## Tools

The server provides a comprehensive set of tools for managing the lifecycle of plans and their components:

- **Plans**: `list_plans`, `add_plan`, `get_plan`, `update_plan`, `delete_plan`
- **Tasks**: `list_tasks`, `add_task`, `get_task`, `update_task`, `append_task`, `delete_task`
- **Notes**: `list_notes`, `add_note`, `get_note`, `delete_note`
129 changes: 108 additions & 21 deletions crates/harnx-mcp-plans/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,71 @@ mod server;
use rmcp::ServiceExt;
use server::PlansServer;
use std::path::PathBuf;
use std::time::Duration;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let plans_dir = parse_args();
let (plans_dir, retention_days) = parse_args()?;

eprintln!(
"harnx-mcp-plans v{}: starting (dir: {})",
"harnx-mcp-plans v{}: starting (dir: {}, retention: {} days)",
env!("CARGO_PKG_VERSION"),
plans_dir.display()
plans_dir.display(),
retention_days
);

let server = PlansServer::new(plans_dir);
let server = PlansServer::new(plans_dir.clone());
let transport = rmcp::transport::stdio();
let service = server.serve(transport).await?;
service.waiting().await?;

if retention_days == 0 {
eprintln!("[cleanup] retention disabled");
service.waiting().await?;
} else {
let cleanup_dir = plans_dir.clone();
let mut cleanup_handle = tokio::spawn(server::cleanup_loop(plans_dir, retention_days));
let service_handle = tokio::spawn(async move { service.waiting().await });
tokio::pin!(service_handle);

// Exponential backoff state for cleanup task restarts.
// Reset to base on a clean exit; doubles on each panic up to MAX_BACKOFF.
const BASE_BACKOFF: Duration = Duration::from_secs(1);
const MAX_BACKOFF: Duration = Duration::from_secs(300); // 5 min cap
let mut backoff = BASE_BACKOFF;

loop {
tokio::select! {
result = &mut *service_handle => {
// MCP service exited — normal shutdown
result??;
break;
}
result = &mut cleanup_handle => {
match result {
Err(e) => {
// Task panicked — log, back off, restart
eprintln!("[cleanup] task failed: {e}");
tokio::time::sleep(backoff).await;
backoff = (backoff * 2).min(MAX_BACKOFF);
}
Ok(()) => {
// Clean exit (shouldn't happen in normal operation) — restart immediately
backoff = BASE_BACKOFF;
}
}
cleanup_handle = tokio::spawn(server::cleanup_loop(cleanup_dir.clone(), retention_days));
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
}
}

Ok(())
}

fn parse_args() -> PathBuf {
fn parse_args() -> anyhow::Result<(PathBuf, u64)> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ New issue: Complex Method
parse_args has a cyclomatic complexity of 12, threshold = 9

Suppress

let args: Vec<String> = std::env::args().collect();
let mut plans_dir: Option<PathBuf> = None;
let mut retention_days: Option<u64> = None;
let mut i = 1;

while i < args.len() {
Expand All @@ -45,8 +88,25 @@ fn parse_args() -> PathBuf {
plans_dir = Some(PathBuf::from(&args[i + 1]));
i += 2;
} else {
eprintln!("harnx-mcp-plans: --dir requires a path argument");
std::process::exit(1);
anyhow::bail!("harnx-mcp-plans: --dir requires a path argument");
}
}
"--retention-days" | "-r" => {
if i + 1 < args.len() {
match args[i + 1].parse::<u64>() {
Ok(days) => {
retention_days = Some(days);
i += 2;
}
Err(_) => {
anyhow::bail!(
"harnx-mcp-plans: --retention-days requires a non-negative integer (got: {})",
args[i + 1]
);
}
}
} else {
anyhow::bail!("harnx-mcp-plans: --retention-days requires a number argument");
}
}
"--help" | "-h" => {
Expand All @@ -55,29 +115,56 @@ fn parse_args() -> PathBuf {
eprintln!("Usage: harnx-mcp-plans [OPTIONS]");
eprintln!();
eprintln!("Options:");
eprintln!(" --dir, -d <path> Set the plans directory (default: .agent/plans)");
eprintln!(" --help, -h Show this help message");
eprintln!(
" --dir, -d <path> Set the plans directory (default: .agent/plans)"
);
eprintln!(
" --retention-days, -r <N> Set retention period in days (default: 14)"
);
eprintln!(" --help, -h Show this help message");
eprintln!();
eprintln!("Env: AGENT_PLANS_PATH overrides the default directory.");
eprintln!("Env:");
eprintln!(" AGENT_PLANS_PATH Overrides the default directory.");
eprintln!(" AGENT_PLANS_RETENTION_DAYS Overrides the default retention days.");
std::process::exit(0);
}
other => {
eprintln!("harnx-mcp-plans: unknown argument: {}", other);
eprintln!("Try: harnx-mcp-plans --help");
std::process::exit(1);
anyhow::bail!(
"harnx-mcp-plans: unknown argument: {other}\nTry: harnx-mcp-plans --help"
);
}
}
}

if let Some(dir) = plans_dir {
return dir;
}
// Resolve retention_days: CLI flag > env var > default (14)
let retention_days = if let Some(days) = retention_days {
days
} else if let Ok(env_days) = std::env::var("AGENT_PLANS_RETENTION_DAYS") {
match env_days.trim().parse::<u64>() {
Ok(days) => days,
Err(_) => {
anyhow::bail!(
"harnx-mcp-plans: AGENT_PLANS_RETENTION_DAYS must be a non-negative integer (got: {})",
env_days.trim()
);
}
}
} else {
14
};

if let Ok(env_path) = std::env::var("AGENT_PLANS_PATH") {
// Resolve plans_dir: CLI flag > env var > default
let plans_dir = if let Some(dir) = plans_dir {
dir
} else if let Ok(env_path) = std::env::var("AGENT_PLANS_PATH") {
if !env_path.trim().is_empty() {
return PathBuf::from(env_path.trim());
PathBuf::from(env_path.trim())
} else {
PathBuf::from(".agent/plans")
}
}
} else {
PathBuf::from(".agent/plans")
};

PathBuf::from(".agent/plans")
Ok((plans_dir, retention_days))
}
Loading
Loading