use std::fmt::Write;
use std::path::PathBuf;
use std::process::Command;
use std::sync::Arc;
use anyhow::Result;
use rmcp::handler::server::router::tool::ToolRouter;
use rmcp::handler::server::wrapper::Parameters;
use rmcp::model::{CallToolResult, ContentBlock, Implementation, ServerCapabilities, ServerConfig};
use rmcp::transport::streamable_http_server::session::local::LocalSessionManager;
use rmcp::transport::streamable_http_server::{StreamableHttpServerConfig, StreamableHttpService};
use rmcp::{ErrorData, Peer, RoleServer, ServerHandler, ServiceExt, tool, tool_handler, tool_router};
use schemars::JsonSchema;
use serde::Deserialize;
use tokio_util::sync::CancellationToken;
use crate::home::Home;
use crate::store::{self, Error, Project, Store, Task, TaskPatch, short_time};
const DONE_LISTED: usize = 20;
const INSTRUCTIONS: &str = "The user's kanban board, for all their projects. Tasks look like api#12. \
Without `project`, Kanzo uses the project linked to this folder's GitHub repo. A good loop: next_task, \
add_comment with what you did, then update_task to review. Leave tasks marked human to the user.";
const HTTP_INSTRUCTIONS: &str = "The user's kanban board, for all their projects. Tasks look like api#12. \
Over HTTP Kanzo can't see your folder, so pass `project` (see list_projects). A good loop: next_task, \
add_comment with what you did, then update_task to review. Leave tasks marked human to the user.";
pub async fn serve(home: Home) -> Result<()> {
let cwd = std::env::current_dir().unwrap_or_default();
let repos = git_repos(&cwd);
let server = Kanzo { ctx: Arc::new(Ctx { home, cwd: Some(cwd), repos }), tool_router: Kanzo::tools() };
let service = server.serve(rmcp::transport::stdio()).await?;
service.waiting().await?;
Ok(())
}
pub fn http_service(home: Home, shutdown: CancellationToken) -> StreamableHttpService<Kanzo, LocalSessionManager> {
let ctx = Arc::new(Ctx { home, cwd: None, repos: Vec::new() });
let config = StreamableHttpServerConfig::default().disable_allowed_hosts().with_cancellation_token(shutdown);
let tool_router = Kanzo::tools();
StreamableHttpService::new(
move || Ok(Kanzo { ctx: ctx.clone(), tool_router: tool_router.clone() }),
Default::default(),
config,
)
}
struct Ctx {
home: Home,
cwd: Option<PathBuf>,
repos: Vec<String>,
}
impl Ctx {
fn project(&self, store: &Store, name: Option<&str>) -> store::Result<Project> {
if let Some(name) = name.map(str::trim).filter(|n| !n.is_empty()) {
return store.find_project(name);
}
for repo in &self.repos {
if let Some(p) = store.project_by_repo(repo)? {
return Ok(p);
}
}
let known: Vec<String> = store.list_projects()?.into_iter().map(|p| p.slug).collect();
let known = known.join(", ");
let Some(cwd) = &self.cwd else {
let msg = if known.is_empty() {
"There are no projects yet. Create one with create_project.".to_string()
} else {
format!("Pass `project` (one of: {known}); over HTTP Kanzo cannot see which folder you work in.")
};
return Err(Error::NotFound(msg));
};
let cwd = cwd.display();
let msg = match (self.repos.first(), known.is_empty()) {
(Some(repo), false) => format!(
"No project is linked to {repo} (the git remote of {cwd}). Pass `project` (one of: {known}), \
link this repo to one with link_repo, or create a project for it with create_project and repo \"{repo}\"."
),
(Some(repo), true) => format!(
"No project is linked to {repo} (the git remote of {cwd}), and there are no projects yet. \
Create one with create_project and repo \"{repo}\"."
),
(None, false) => {
format!("Cannot tell the project: {cwd} has no git remote. Pass `project` (one of: {known}).")
}
(None, true) => "There are no projects yet. Create one with create_project.".to_string(),
};
Err(Error::NotFound(msg))
}
}
fn git_repos(dir: &std::path::Path) -> Vec<String> {
let Ok(out) = Command::new("git").arg("-C").arg(dir).args(["remote", "-v"]).output() else {
return Vec::new();
};
let text = String::from_utf8_lossy(&out.stdout);
let mut lines: Vec<&str> = text.lines().collect();
lines.sort_by_key(|l| !l.starts_with("origin\t"));
let mut repos = Vec::new();
for line in lines {
if let Some(repo) = line.split_whitespace().nth(1).and_then(store::repo_from_url)
&& !repos.contains(&repo)
{
repos.push(repo);
}
}
repos
}
#[derive(Clone)]
pub struct Kanzo {
ctx: Arc<Ctx>,
tool_router: ToolRouter<Self>,
}
#[derive(Deserialize)]
#[serde(untagged)]
enum TaskKey {
Number(i64),
Text(String),
}
impl TaskKey {
fn find(&self, store: &Store) -> store::Result<Task> {
match self {
TaskKey::Number(n) => store.get_task(*n),
TaskKey::Text(s) => store.find_task(s),
}
}
}
#[derive(Deserialize, JsonSchema)]
struct CreateProjectArgs {
slug: String,
name: Option<String>,
repo: Option<String>,
}
#[derive(Deserialize, JsonSchema)]
struct ListTasksArgs {
project: Option<String>,
status: Option<String>,
}
#[derive(Deserialize, JsonSchema)]
struct TaskArgs {
#[schemars(with = "String")]
id: TaskKey,
}
#[derive(Deserialize, JsonSchema)]
struct CreateTaskArgs {
title: String,
description: Option<String>,
project: Option<String>,
tags: Option<String>,
}
#[derive(Deserialize, JsonSchema)]
struct UpdateTaskArgs {
#[schemars(with = "String")]
id: TaskKey,
title: Option<String>,
description: Option<String>,
status: Option<String>,
tags: Option<String>,
}
#[derive(Deserialize, JsonSchema)]
struct AddCommentArgs {
#[schemars(with = "String")]
id: TaskKey,
body: String,
author: Option<String>,
}
#[derive(Deserialize, JsonSchema)]
struct LinkRepoArgs {
project: String,
repo: String,
}
#[derive(Deserialize, JsonSchema)]
struct NextTaskArgs {
project: Option<String>,
}
impl Kanzo {
fn tools() -> ToolRouter<Self> {
let mut router = Self::tool_router();
for route in router.map.values_mut() {
let mut schema = (*route.attr.input_schema).clone();
schema.remove("$schema");
if let Some(serde_json::Value::Object(props)) = schema.get_mut("properties") {
for prop in props.values_mut() {
if let Some(kind) = prop.get_mut("type")
&& let Some(first) = kind.as_array().and_then(|k| k.first()).cloned()
{
*kind = first;
}
}
}
route.attr.input_schema = Arc::new(schema);
}
router
}
}
#[tool_router]
impl Kanzo {
async fn run(&self, f: impl FnOnce(&Ctx, &Store) -> store::Result<String> + Send + 'static) -> CallToolResult {
let ctx = self.ctx.clone();
let outcome = tokio::task::spawn_blocking(move || {
let store = ctx.home.store().map_err(|e| e.to_string())?;
f(&ctx, &store).map_err(|e| e.to_string())
})
.await
.unwrap_or_else(|e| Err(format!("internal error: {e}")));
match outcome {
Ok(text) => CallToolResult::success(vec![ContentBlock::text(text)]),
Err(text) => CallToolResult::error(vec![ContentBlock::text(text)]),
}
}
#[tool(description = "List projects with their repos and open task counts.")]
async fn list_projects(&self) -> Result<CallToolResult, ErrorData> {
Ok(self
.run(|ctx, store| {
let projects = store.list_projects()?;
if projects.is_empty() {
return Ok("No projects yet. Create one with create_project.".into());
}
let here = projects.iter().find(|p| p.repos.iter().any(|r| ctx.repos.contains(r)));
let mut out = String::new();
for p in &projects {
out.push_str(&p.slug);
if p.name != p.slug {
let _ = write!(out, " \"{}\"", p.name);
}
if !p.repos.is_empty() {
let _ = write!(out, " ({})", p.repos.join(", "));
}
let _ = write!(out, ": {} open of {}", p.open, p.total);
if here.is_some_and(|h| h.id == p.id) {
out.push_str(" <- current repo");
}
out.push('\n');
}
out.pop();
Ok(out)
})
.await)
}
#[tool(description = "Create a project.")]
async fn create_project(
&self,
Parameters(args): Parameters<CreateProjectArgs>,
) -> Result<CallToolResult, ErrorData> {
Ok(self
.run(move |_, store| {
let p = store.create_project(&args.slug, args.name.as_deref().unwrap_or(""), args.repo.as_deref())?;
let repo = p.repos.first().map(|r| format!(", linked to {r}")).unwrap_or_default();
Ok(format!("Created project {} ({}){repo}.", p.slug, p.name))
})
.await)
}
#[tool(description = "Link another GitHub repo to a project.")]
async fn link_repo(&self, Parameters(args): Parameters<LinkRepoArgs>) -> Result<CallToolResult, ErrorData> {
Ok(self
.run(move |_, store| {
let p = store.link_repo(&args.project, &args.repo)?;
Ok(format!("Project {} is linked to {}.", p.slug, p.repos.join(", ")))
})
.await)
}
#[tool(description = "List tasks in board order.")]
async fn list_tasks(&self, Parameters(args): Parameters<ListTasksArgs>) -> Result<CallToolResult, ErrorData> {
Ok(self
.run(move |ctx, store| {
let all = args.project.as_deref().is_some_and(|p| p.trim().eq_ignore_ascii_case("all"));
let project = if all { None } else { Some(ctx.project(store, args.project.as_deref())?) };
let status = args.status.as_deref().map(str::trim).filter(|s| !s.is_empty());
let only = match status {
Some("all") | None => None,
Some(s) if store.statuses().iter().any(|x| x == s) => Some(s),
Some(s) => {
return Err(Error::Invalid(format!(
"unknown status {s:?}; use one of: {}, or all",
store.statuses().join(", ")
)));
}
};
let keep = if status.is_none() { 0 } else { DONE_LISTED };
let (tasks, hidden) =
store.list_tasks_keeping_done(project.as_ref().map(|p| p.id), only, Some(keep))?;
let done = store.statuses().last().map_or("", String::as_str);
let mut out = String::new();
let mut people = 0;
for t in &tasks {
if t.human {
people += 1;
} else {
task_line(&mut out, t);
out.push('\n');
}
}
if out.is_empty() {
let scope = project.as_ref().map_or("any project", |p| p.slug.as_str());
let _ = writeln!(out, "No tasks in {scope}.");
}
if people > 0 {
let tasks = if people == 1 { "task" } else { "tasks" };
let _ = writeln!(out, "({people} human {tasks} not shown: for the user, not for agents)");
}
if hidden > 0 && status.is_none() {
let _ = writeln!(out, "({hidden} {done} not shown; pass status \"{done}\" or \"all\")");
} else if hidden > 0 {
let _ = writeln!(out, "({hidden} older {done} not shown)");
}
out.pop();
Ok(out)
})
.await)
}
#[tool(description = "Show a task with its comments and image paths.")]
async fn get_task(&self, Parameters(args): Parameters<TaskArgs>) -> Result<CallToolResult, ErrorData> {
Ok(self
.run(move |ctx, store| {
let task = args.id.find(store)?;
task_detail(ctx, store, &task)
})
.await)
}
#[tool(description = "Add a task to todo.")]
async fn create_task(&self, Parameters(args): Parameters<CreateTaskArgs>) -> Result<CallToolResult, ErrorData> {
Ok(self
.run(move |ctx, store| {
let project = ctx.project(store, args.project.as_deref())?;
let mut t = store.create_task(project.id, &args.title, args.description.as_deref().unwrap_or(""))?;
if args.tags.is_some() {
t = store.update_task(t.id, TaskPatch { tags: args.tags, ..Default::default() })?;
}
Ok(format!("Created {} in {}: {}", t.key(), t.status, t.title))
})
.await)
}
#[tool(description = "Change the fields you pass.")]
async fn update_task(&self, Parameters(args): Parameters<UpdateTaskArgs>) -> Result<CallToolResult, ErrorData> {
Ok(self
.run(move |_, store| {
let before = args.id.find(store)?;
let status = args.status.map(|s| s.trim().to_ascii_lowercase());
let after = store.update_task(
before.id,
TaskPatch {
title: args.title,
description: args.description,
status,
tags: args.tags,
project_id: None,
human: None,
},
)?;
let moved = if before.status != after.status {
format!(", {} -> {}", before.status, after.status)
} else {
String::new()
};
Ok(format!("Updated {}{moved}: {}", after.key(), after.title))
})
.await)
}
#[tool(description = "Comment on a task, like to report progress.")]
async fn add_comment(
&self,
Parameters(args): Parameters<AddCommentArgs>,
peer: Peer<RoleServer>,
) -> Result<CallToolResult, ErrorData> {
let client = peer.peer_info().map(|i| i.client_info.name.clone()).filter(|n| !n.is_empty());
let author = args.author.filter(|a| !a.trim().is_empty()).or(client).unwrap_or_else(|| "agent".into());
Ok(self
.run(move |_, store| {
let task = args.id.find(store)?;
store.add_comment(task.id, &author, &args.body, &[])?;
Ok(format!("Comment added to {} as {author}.", task.key()))
})
.await)
}
#[tool(description = "Take the top todo task, move it to doing and show it.")]
async fn next_task(&self, Parameters(args): Parameters<NextTaskArgs>) -> Result<CallToolResult, ErrorData> {
Ok(self
.run(move |ctx, store| {
let all = args.project.as_deref().is_some_and(|p| p.trim().eq_ignore_ascii_case("all"));
let project = if all { None } else { Some(ctx.project(store, args.project.as_deref())?) };
match store.next_task(project.as_ref().map(|p| p.id))? {
Some(task) => Ok(format!("Moved to {}:\n{}", task.status, task_detail(ctx, store, &task)?)),
None => {
let scope = project.map_or("any project".into(), |p| p.slug);
Ok(format!("Nothing in {} for {scope}.", store.statuses()[0]))
}
}
})
.await)
}
#[tool(description = "Delete a task with its comments and images. Can't be undone.")]
async fn delete_task(&self, Parameters(args): Parameters<TaskArgs>) -> Result<CallToolResult, ErrorData> {
Ok(self
.run(move |ctx, store| {
let task = args.id.find(store)?;
ctx.home.deleting_images(store, store::ImagesOf::Task(task.id), || store.delete_task(task.id))?;
let comments = match task.comments {
0 => String::new(),
1 => " and its comment".into(),
n => format!(" and its {n} comments"),
};
Ok(format!("Deleted {}{comments}: {}", task.key(), task.title))
})
.await)
}
}
#[tool_handler(router = self.tool_router)]
impl ServerHandler for Kanzo {
fn get_info(&self) -> ServerConfig {
ServerConfig::new(ServerCapabilities::builder().enable_tools().build())
.with_server_info(Implementation::new("kanzo", env!("CARGO_PKG_VERSION")))
.with_instructions(if self.ctx.cwd.is_some() { INSTRUCTIONS } else { HTTP_INSTRUCTIONS })
}
}
fn task_line(out: &mut String, t: &Task) {
let _ = write!(out, "{}#{} [{}] {}", t.project, t.id, t.status, t.title);
write_tags(out, t);
match t.comments {
0 => {}
1 => out.push_str(" (1 comment)"),
n => {
let _ = write!(out, " ({n} comments)");
}
}
}
fn write_tags(out: &mut String, t: &Task) {
if !t.tags.is_empty() {
let _ = write!(out, " {{{}}}", t.tags.join(", "));
}
}
fn task_detail(ctx: &Ctx, store: &Store, t: &Task) -> store::Result<String> {
let comments = store.list_comments(t.id)?;
let images = store.list_images(t.id)?;
let dir = ctx.home.dir.join("images");
let paths = |comment: Option<i64>| -> Vec<String> {
images.iter().filter(|i| i.comment_id == comment).map(|i| dir.join(&i.file).display().to_string()).collect()
};
let (created, updated) = (short_time(&t.created_at), short_time(&t.updated_at));
let mut out = format!("{}#{} {}", t.project, t.id, t.title);
write_tags(&mut out, t);
let _ = write!(out, "\nstatus: {}", t.status);
if t.human {
out.push_str(", human: for the user, not for agents");
}
let _ = write!(out, ", created {created}");
if updated != created {
let _ = write!(out, ", updated {updated}");
}
out.push('\n');
if !t.description.is_empty() {
let _ = writeln!(out, "\n{}", t.description);
}
let own = paths(None);
if !own.is_empty() {
let _ = writeln!(out, "\nImages: {}", own.join(", "));
}
if !comments.is_empty() {
let _ = writeln!(out, "\nComments ({}):", comments.len());
for c in &comments {
let _ = write!(out, "- {}, {}: {}", c.author, short_time(&c.created_at), c.body);
let attached = paths(Some(c.id));
if !attached.is_empty() {
let sep = if c.body.is_empty() { "" } else { " " };
let _ = write!(out, "{sep}[images: {}]", attached.join(", "));
}
out.push('\n');
}
}
out.truncate(out.trim_end().len());
Ok(out)
}