Skip to main content

lean_ctx/tools/registered/
ctx_pack.rs

1use rmcp::ErrorData;
2use rmcp::model::Tool;
3use serde_json::{Map, Value, json};
4
5use crate::server::tool_trait::{
6    McpTool, ToolContext, ToolOutput, get_bool, get_int, get_str, get_str_array, get_usize,
7};
8use crate::tool_defs::tool_def;
9
10pub struct CtxPackTool;
11
12impl McpTool for CtxPackTool {
13    fn name(&self) -> &'static str {
14        "ctx_pack"
15    }
16
17    fn tool_def(&self) -> Tool {
18        tool_def(
19            "ctx_pack",
20            "WORKFLOW: create -> export -> import -> install for sharing context state.\n\
21            ANTIPATTERN: NOT for ephemeral session save (use ctx_session).\n\
22            Context Package Manager — create, install, manage portable context packages\n\
23            with knowledge, graph, session patterns, and gotchas.\n\
24            Actions: pr, create, list, info, remove, install, export, import, auto_load, summary.\n\
25            Saves tokens: pre-built context state (avoids re-building).",
26            json!({
27                "type": "object",
28                "properties": {
29                    "action": {
30                        "type": "string",
31                        "enum": ["pr", "create", "list", "info", "remove", "install", "export", "import", "auto_load", "summary"],
32                        "description": "Pack action to perform"
33                    },
34                    "project_root": {
35                        "type": "string",
36                        "description": "Project root directory"
37                    },
38                    "name": {
39                        "type": "string",
40                        "description": "Package name"
41                    },
42                    "version": {
43                        "type": "string",
44                        "description": "Package version (semver)"
45                    },
46                    "description": {
47                        "type": "string",
48                        "description": "Package description (for create)"
49                    },
50                    "author": {
51                        "type": "string",
52                        "description": "Package author (for create)"
53                    },
54                    "tags": {
55                        "type": "array",
56                        "items": { "type": "string" },
57                        "description": "Tags for categorization (for create)"
58                    },
59                    "layers": {
60                        "type": "array",
61                        "items": { "type": "string" },
62                        "description": "Layers to include: knowledge|graph|session|patterns|gotchas"
63                    },
64                    "level": {
65                        "type": "integer",
66                        "description": "Detail level 1-3 (higher = more detail)"
67                    },
68                    "scope": {
69                        "type": "string",
70                        "description": "Package scope (e.g. @org/name)"
71                    },
72                    "base": {
73                        "type": "string",
74                        "description": "Git base ref for PR diff"
75                    },
76                    "format": {
77                        "type": "string",
78                        "enum": ["markdown", "json"],
79                        "description": "Output format: markdown|json"
80                    },
81                    "depth": {
82                        "type": "integer",
83                        "description": "Impact depth for pr action (default: 3)"
84                    },
85                    "diff": {
86                        "type": "string",
87                        "description": "Git diff --name-status text input"
88                    },
89                    "file": {
90                        "type": "string",
91                        "description": "File path for import/export"
92                    },
93                    "apply": {
94                        "type": "boolean",
95                        "description": "Apply after import (default: false)"
96                    },
97                    "enable": {
98                        "type": "boolean",
99                        "description": "Enable auto-load (default: true)"
100                    }
101                },
102                "required": ["action"],
103                "allOf": [
104                    {
105                        "if": { "properties": { "action": { "const": "create" } }, "required": ["action"] },
106                        "then": { "required": ["action", "name"] }
107                    },
108                    {
109                        "if": { "properties": { "action": { "const": "info" } }, "required": ["action"] },
110                        "then": { "required": ["action", "name"] }
111                    },
112                    {
113                        "if": { "properties": { "action": { "const": "remove" } }, "required": ["action"] },
114                        "then": { "required": ["action", "name"] }
115                    },
116                    {
117                        "if": { "properties": { "action": { "const": "install" } }, "required": ["action"] },
118                        "then": { "required": ["action", "name"] }
119                    },
120                    {
121                        "if": { "properties": { "action": { "const": "export" } }, "required": ["action"] },
122                        "then": { "required": ["action", "name"] }
123                    },
124                    {
125                        "if": { "properties": { "action": { "const": "import" } }, "required": ["action"] },
126                        "then": { "required": ["action", "file"] }
127                    }
128                ]
129            }),
130        )
131    }
132
133    fn handle(
134        &self,
135        args: &Map<String, Value>,
136        ctx: &ToolContext,
137    ) -> Result<ToolOutput, ErrorData> {
138        let action = get_str(args, "action")
139            .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
140
141        let project_root = if let Some(p) = ctx
142            .resolved_path("project_root")
143            .or(ctx.resolved_path("root"))
144        {
145            p.to_string()
146        } else if let Some(err) = ctx.path_error("project_root").or(ctx.path_error("root")) {
147            return Err(ErrorData::invalid_params(
148                format!("project_root: {err}"),
149                None,
150            ));
151        } else {
152            ctx.project_root.clone()
153        };
154
155        let result = match action.as_str() {
156            "pr" => {
157                let base = get_str(args, "base");
158                let format = get_str(args, "format");
159                let depth = get_usize(args, "depth").map(|d| d.min(64));
160                let diff = get_str(args, "diff");
161                crate::tools::ctx_pack::handle(
162                    "pr",
163                    &project_root,
164                    base.as_deref(),
165                    format.as_deref(),
166                    depth,
167                    diff.as_deref(),
168                )
169            }
170            "create" => {
171                let name = get_str(args, "name")
172                    .ok_or_else(|| ErrorData::invalid_params("name is required for create", None))?;
173                let version = get_str(args, "version");
174                let description = get_str(args, "description");
175                let author = get_str(args, "author");
176                let tags = get_str_array(args, "tags");
177                let layers = get_str_array(args, "layers");
178                let level = get_int(args, "level").and_then(|l| u32::try_from(l).ok());
179                let scope = get_str(args, "scope");
180                crate::tools::ctx_pack::handle_create(
181                    &project_root,
182                    &name,
183                    version.as_deref(),
184                    description.as_deref(),
185                    author.as_deref(),
186                    tags.as_deref(),
187                    layers.as_deref(),
188                    level,
189                    scope.as_deref(),
190                )
191            }
192            "list" => crate::tools::ctx_pack::handle_list(),
193            "info" => {
194                let name = get_str(args, "name")
195                    .ok_or_else(|| ErrorData::invalid_params("name is required for info", None))?;
196                let version = get_str(args, "version");
197                crate::tools::ctx_pack::handle_info(&name, version.as_deref())
198            }
199            "remove" => {
200                let name = get_str(args, "name")
201                    .ok_or_else(|| ErrorData::invalid_params("name is required for remove", None))?;
202                let version = get_str(args, "version");
203                crate::tools::ctx_pack::handle_remove(&name, version.as_deref())
204            }
205            "install" => {
206                let name = get_str(args, "name").ok_or_else(|| {
207                    ErrorData::invalid_params("name is required for install", None)
208                })?;
209                let version = get_str(args, "version");
210                crate::tools::ctx_pack::handle_install(&name, version.as_deref(), &project_root)
211            }
212            "export" => {
213                let name = get_str(args, "name").ok_or_else(|| {
214                    ErrorData::invalid_params("name is required for export", None)
215                })?;
216                let version = get_str(args, "version");
217                let file = get_str(args, "file");
218                crate::tools::ctx_pack::handle_export(&name, version.as_deref(), file.as_deref())
219            }
220            "import" => {
221                let file = get_str(args, "file")
222                    .ok_or_else(|| ErrorData::invalid_params("file is required for import", None))?;
223                let apply = get_bool(args, "apply").unwrap_or(false);
224                crate::tools::ctx_pack::handle_import(&file, apply, &project_root)
225            }
226            "auto_load" => {
227                let name = get_str(args, "name");
228                let version = get_str(args, "version");
229                let enable = get_bool(args, "enable").unwrap_or(true);
230                crate::tools::ctx_pack::handle_auto_load(
231                    name.as_deref(),
232                    version.as_deref(),
233                    enable,
234                )
235            }
236            "summary" => crate::tools::ctx_pack::handle_summary(&project_root),
237            _ => "Unknown action. Use: pr, create, list, info, remove, install, export, import, auto_load, summary".to_string(),
238        };
239
240        Ok(ToolOutput::simple(result))
241    }
242}