Skip to main content

lean_ctx_server/tools/registered/
ctx_doc.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use rmcp::ErrorData;
4use rmcp::model::Tool;
5use serde_json::{Map, Value, json};
6
7use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_str, get_str_array};
8use crate::tools::ctx_doc::{Args, handle};
9use lean_ctx::tool_defs::tool_def;
10
11pub struct CtxDocTool;
12
13impl McpTool for CtxDocTool {
14    fn name(&self) -> &'static str {
15        "ctx_doc"
16    }
17
18    fn tool_def(&self) -> Tool {
19        tool_def(
20            "ctx_doc",
21            "Project documentation: pages of sections, bound to code. In the linked Hive vault, else local to this Instance.\n\
22             WORKFLOW: action=list or search first, then show slug (outline=true, section=<heading-slug>).\n\
23             Reads are offline. action=write slug content='# Title\\n…' creates or revises a page;\n\
24             action=edit slug find replace changes one unique stretch; action=note adds an addendum.\n\
25             anchors=[file:…] binds a page to code; action=status lists stale pages, confirm re-pins after a check.\n\
26             Vault writes queue in the background; local writes save at once as revisions\n\
27             (local only: action=history slug, show slug rev=N, move slug to=new, delete slug). Link pages as [text](/slug#heading).",
28            json!({
29                "type": "object",
30                "properties": {
31                    "action": { "type": "string", "description": "list|show|search|status|write|edit|note|confirm|history|move|delete" },
32                    "to": { "type": "string", "description": "move: the new slug" },
33                    "rev": { "type": "integer", "description": "show/list (local docs): as of this revision" },
34                    "anchors": { "type": "array", "items": { "type": "string" }, "description": "write: code the page derives from (file:src/x.rs, dir:src/core, glob:src/**/*.rs, shape:docs); a change makes the page stale" },
35                    "slug": { "type": "string", "description": "Page slug, e.g. guide/retry (list: a prefix)" },
36                    "section": { "type": "string", "description": "show: one section by its heading slug" },
37                    "outline": { "type": "boolean", "description": "show: headings only" },
38                    "query": { "type": "string", "description": "search words" },
39                    "content": { "type": "string", "description": "write: the whole page markdown; note: the note" },
40                    "find": { "type": "string", "description": "edit: exact text that occurs once" },
41                    "replace": { "type": "string", "description": "edit: its replacement" },
42                    "type": { "type": "string", "description": "write (new page): reference|guide|design|…" },
43                    "status": { "type": "string", "description": "write (new page): draft|canonical" }
44                },
45                "required": ["action"]
46            }),
47        )
48    }
49
50    fn handle(
51        &self,
52        args: &Map<String, Value>,
53        ctx: &ToolContext,
54    ) -> Result<ToolOutput, ErrorData> {
55        let owned = |k: &str| get_str(args, k);
56        let action = owned("action").unwrap_or_else(|| "list".to_owned());
57        let (slug, section, query, content, find, replace, kind, status) = (
58            owned("slug"),
59            owned("section"),
60            owned("query"),
61            owned("content"),
62            owned("find"),
63            owned("replace"),
64            owned("type"),
65            owned("status"),
66        );
67        let anchors = get_str_array(args, "anchors").unwrap_or_default();
68        let to = owned("to");
69        let agent = ctx.agent_id.as_ref().and_then(|agent| {
70            crate::server::bounded_lock::read(agent, "ctx_doc agent").and_then(|a| a.clone())
71        });
72        let call = Args {
73            anchors: &anchors,
74            action: &action,
75            slug: slug.as_deref(),
76            section: section.as_deref(),
77            query: query.as_deref(),
78            content: content.as_deref(),
79            find: find.as_deref(),
80            replace: replace.as_deref(),
81            kind: kind.as_deref(),
82            status: status.as_deref(),
83            to: to.as_deref(),
84            rev: args.get("rev").and_then(Value::as_u64),
85            author: agent.map(lean_ctx_doc::local::Author::agent),
86            outline: args
87                .get("outline")
88                .and_then(Value::as_bool)
89                .unwrap_or(false),
90        };
91        Ok(ToolOutput::simple(handle(&ctx.project_root, &call)))
92    }
93}