lean_ctx/tools/registered/
ctx_share.rs1use rmcp::ErrorData;
2use rmcp::model::Tool;
3use serde_json::{Map, Value, json};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_str};
6use crate::tool_defs::tool_def;
7
8pub struct CtxShareTool;
9
10impl McpTool for CtxShareTool {
11 fn name(&self) -> &'static str {
12 "ctx_share"
13 }
14
15 fn tool_def(&self) -> Tool {
16 tool_def(
17 "ctx_share",
18 "WORKFLOW: push from agent A → pull from agent B shares cached file contexts.\n\
19 Actions: push|pull|list|clear. Omit to_agent for broadcast.\n\
20 ANTIPATTERN: NOT file transfer — shares lean-ctx cache entries only.",
21 json!({
22 "type": "object",
23 "properties": {
24 "action": {
25 "type": "string",
26 "enum": ["push", "pull", "list", "clear"],
27 "description": "push|pull|list|clear"
28 },
29 "paths": {
30 "type": "string",
31 "description": "Comma-separated paths (for push)"
32 },
33 "to_agent": {
34 "type": "string",
35 "description": "Target agent ID (omit for broadcast)"
36 },
37 "message": {
38 "type": "string",
39 "description": "Context message about what was shared"
40 }
41 },
42 "required": ["action"]
43 }),
44 )
45 }
46
47 fn handle(
48 &self,
49 args: &Map<String, Value>,
50 ctx: &ToolContext,
51 ) -> Result<ToolOutput, ErrorData> {
52 let action = get_str(args, "action")
53 .ok_or_else(|| ErrorData::invalid_params("action is required", None))?;
54 let to_agent = get_str(args, "to_agent");
55 let paths = get_str(args, "paths");
56 let message = get_str(args, "message");
57
58 let from_agent = ctx
59 .agent_id
60 .as_ref()
61 .map(|a| a.blocking_read().clone())
62 .unwrap_or_default();
63
64 let cache_handle = ctx
65 .cache
66 .as_ref()
67 .ok_or_else(|| ErrorData::internal_error("cache not available", None))?;
68 let cache = cache_handle.blocking_read();
69 let result = crate::tools::ctx_share::handle(
70 &action,
71 from_agent.as_deref(),
72 to_agent.as_deref(),
73 paths.as_deref(),
74 message.as_deref(),
75 &cache,
76 &ctx.project_root,
77 );
78 drop(cache);
79
80 Ok(ToolOutput {
81 text: result,
82 original_tokens: 0,
83 saved_tokens: 0,
84 mode: Some(action),
85 path: None,
86 changed: false,
87 shell_outcome: None,
88 })
89 }
90}