cctakt 0.1.1

TUI orchestrator for multiple Claude Code agents using Git Worktree
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! MCP (Model Context Protocol) server for cctakt
//!
//! Provides tools for the orchestrator to manage tasks without directly
//! modifying plan.json, avoiding race conditions.

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::io::{self, BufRead, Write};

use crate::plan::{Plan, PlanManager, Task, TaskAction, TaskStatus};

/// JSON-RPC request
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct JsonRpcRequest {
    jsonrpc: String,
    id: Option<Value>,
    method: String,
    #[serde(default)]
    params: Value,
}

/// JSON-RPC response
#[derive(Debug, Serialize)]
struct JsonRpcResponse {
    jsonrpc: String,
    id: Value,
    #[serde(skip_serializing_if = "Option::is_none")]
    result: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    error: Option<JsonRpcError>,
}

#[derive(Debug, Serialize)]
struct JsonRpcError {
    code: i32,
    message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    data: Option<Value>,
}

/// MCP Tool definition
#[derive(Debug, Serialize)]
struct Tool {
    name: String,
    description: String,
    #[serde(rename = "inputSchema")]
    input_schema: Value,
}

/// MCP Server
pub struct McpServer {
    plan_manager: PlanManager,
}

impl McpServer {
    pub fn new() -> Result<Self> {
        let plan_manager = PlanManager::new(std::path::PathBuf::from("."));
        Ok(Self { plan_manager })
    }

    /// Run the MCP server (stdio mode)
    pub fn run(&mut self) -> Result<()> {
        let stdin = io::stdin();
        let mut stdout = io::stdout();

        for line in stdin.lock().lines() {
            let line = line.context("Failed to read line from stdin")?;
            if line.is_empty() {
                continue;
            }

            let response = self.handle_request(&line);
            let response_json = serde_json::to_string(&response)?;
            writeln!(stdout, "{}", response_json)?;
            stdout.flush()?;
        }

        Ok(())
    }

    fn handle_request(&mut self, line: &str) -> JsonRpcResponse {
        let request: JsonRpcRequest = match serde_json::from_str(line) {
            Ok(req) => req,
            Err(e) => {
                return JsonRpcResponse {
                    jsonrpc: "2.0".to_string(),
                    id: Value::Null,
                    result: None,
                    error: Some(JsonRpcError {
                        code: -32700,
                        message: format!("Parse error: {}", e),
                        data: None,
                    }),
                };
            }
        };

        let id = request.id.clone().unwrap_or(Value::Null);

        match request.method.as_str() {
            "initialize" => self.handle_initialize(id, request.params),
            "tools/list" => self.handle_tools_list(id),
            "tools/call" => self.handle_tools_call(id, request.params),
            "notifications/initialized" => {
                // Ignore notification
                JsonRpcResponse {
                    jsonrpc: "2.0".to_string(),
                    id,
                    result: Some(Value::Null),
                    error: None,
                }
            }
            _ => JsonRpcResponse {
                jsonrpc: "2.0".to_string(),
                id,
                result: None,
                error: Some(JsonRpcError {
                    code: -32601,
                    message: format!("Method not found: {}", request.method),
                    data: None,
                }),
            },
        }
    }

    fn handle_initialize(&self, id: Value, _params: Value) -> JsonRpcResponse {
        JsonRpcResponse {
            jsonrpc: "2.0".to_string(),
            id,
            result: Some(json!({
                "protocolVersion": "2024-11-05",
                "capabilities": {
                    "tools": {}
                },
                "serverInfo": {
                    "name": "cctakt",
                    "version": env!("CARGO_PKG_VERSION")
                }
            })),
            error: None,
        }
    }

    fn handle_tools_list(&self, id: Value) -> JsonRpcResponse {
        let tools = vec![
            Tool {
                name: "add_task".to_string(),
                description: "Add a new worker task to the current plan. Creates a new plan if none exists.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "id": {
                            "type": "string",
                            "description": "Unique task ID (e.g., 'feat-login', 'fix-bug-123')"
                        },
                        "branch": {
                            "type": "string",
                            "description": "Git branch name for the worker (e.g., 'feat/login', 'fix/bug-123')"
                        },
                        "description": {
                            "type": "string",
                            "description": "Detailed task description for the worker. Include requirements, files to modify, and completion criteria."
                        },
                        "plan_description": {
                            "type": "string",
                            "description": "Optional: Description for the plan (only used when creating a new plan)"
                        }
                    },
                    "required": ["id", "branch", "description"]
                }),
            },
            Tool {
                name: "list_tasks".to_string(),
                description: "List all tasks in the current plan with their status.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {},
                    "required": []
                }),
            },
            Tool {
                name: "get_task".to_string(),
                description: "Get details of a specific task by ID.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {
                        "id": {
                            "type": "string",
                            "description": "Task ID to look up"
                        }
                    },
                    "required": ["id"]
                }),
            },
            Tool {
                name: "get_plan_status".to_string(),
                description: "Get overall plan status including task counts by status.".to_string(),
                input_schema: json!({
                    "type": "object",
                    "properties": {},
                    "required": []
                }),
            },
        ];

        JsonRpcResponse {
            jsonrpc: "2.0".to_string(),
            id,
            result: Some(json!({ "tools": tools })),
            error: None,
        }
    }

    fn handle_tools_call(&mut self, id: Value, params: Value) -> JsonRpcResponse {
        let name = params.get("name").and_then(|v| v.as_str()).unwrap_or("");
        let arguments = params.get("arguments").cloned().unwrap_or(json!({}));

        let result = match name {
            "add_task" => self.tool_add_task(arguments),
            "list_tasks" => self.tool_list_tasks(),
            "get_task" => self.tool_get_task(arguments),
            "get_plan_status" => self.tool_get_plan_status(),
            _ => Err(anyhow::anyhow!("Unknown tool: {}", name)),
        };

        match result {
            Ok(content) => JsonRpcResponse {
                jsonrpc: "2.0".to_string(),
                id,
                result: Some(json!({
                    "content": [{
                        "type": "text",
                        "text": content
                    }]
                })),
                error: None,
            },
            Err(e) => JsonRpcResponse {
                jsonrpc: "2.0".to_string(),
                id,
                result: Some(json!({
                    "content": [{
                        "type": "text",
                        "text": format!("Error: {}", e)
                    }],
                    "isError": true
                })),
                error: None,
            },
        }
    }

    fn tool_add_task(&mut self, args: Value) -> Result<String> {
        let task_id = args
            .get("id")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Missing required parameter: id"))?;
        let branch = args
            .get("branch")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Missing required parameter: branch"))?;
        let description = args
            .get("description")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Missing required parameter: description"))?;
        let plan_description = args.get("plan_description").and_then(|v| v.as_str());

        // Load or create plan
        let mut plan = self.plan_manager.load()?.unwrap_or_else(|| {
            match plan_description {
                Some(desc) => Plan::with_description(desc),
                None => Plan::new(),
            }
        });

        // Check if task already exists
        if plan.get_task(task_id).is_some() {
            return Err(anyhow::anyhow!("Task with ID '{}' already exists", task_id));
        }

        // Add task
        let task = Task::new(
            task_id.to_string(),
            TaskAction::CreateWorker {
                branch: branch.to_string(),
                task_description: description.to_string(),
                base_branch: None,
            },
        );
        plan.add_task(task);

        // Save plan
        self.plan_manager.save(&plan)?;

        Ok(format!(
            "Task '{}' added successfully.\n\nBranch: {}\nStatus: pending\n\nThe task will be picked up by cctakt automatically.",
            task_id, branch
        ))
    }

    fn tool_list_tasks(&mut self) -> Result<String> {
        let plan = self.plan_manager.load()?;

        match plan {
            Some(plan) => {
                if plan.tasks.is_empty() {
                    return Ok("No tasks in current plan.".to_string());
                }

                let mut output = String::new();
                if let Some(ref desc) = plan.description {
                    output.push_str(&format!("Plan: {}\n\n", desc));
                }
                output.push_str("Tasks:\n");

                for task in &plan.tasks {
                    let status_emoji = match task.status {
                        TaskStatus::Pending => "",
                        TaskStatus::Running => "🔄",
                        TaskStatus::Completed => "",
                        TaskStatus::Failed => "",
                        TaskStatus::Skipped => "⏭️",
                    };
                    output.push_str(&format!(
                        "  {} {} - {:?}\n",
                        status_emoji, task.id, task.status
                    ));

                    if let TaskAction::CreateWorker { branch, .. } = &task.action {
                        output.push_str(&format!("      Branch: {}\n", branch));
                    }
                }

                Ok(output)
            }
            None => Ok("No active plan. Use add_task to create one.".to_string()),
        }
    }

    fn tool_get_task(&mut self, args: Value) -> Result<String> {
        let task_id = args
            .get("id")
            .and_then(|v| v.as_str())
            .ok_or_else(|| anyhow::anyhow!("Missing required parameter: id"))?;

        let plan = self.plan_manager.load()?;

        match plan {
            Some(plan) => {
                if let Some(task) = plan.get_task(task_id) {
                    let mut output = format!("Task: {}\n", task.id);
                    output.push_str(&format!("Status: {:?}\n", task.status));

                    if let TaskAction::CreateWorker {
                        branch,
                        task_description,
                        ..
                    } = &task.action
                    {
                        output.push_str(&format!("Branch: {}\n", branch));
                        output.push_str(&format!("\nDescription:\n{}\n", task_description));
                    }

                    if let Some(ref result) = task.result {
                        output.push_str(&format!("\nResult:\n"));
                        if !result.commits.is_empty() {
                            output.push_str("  Commits:\n");
                            for commit in &result.commits {
                                output.push_str(&format!("    - {}\n", commit));
                            }
                        }
                        if let Some(ref url) = result.pr_url {
                            output.push_str(&format!("  PR: {}\n", url));
                        }
                    }

                    if let Some(ref error) = task.error {
                        output.push_str(&format!("\nError: {}\n", error));
                    }

                    Ok(output)
                } else {
                    Err(anyhow::anyhow!("Task '{}' not found", task_id))
                }
            }
            None => Err(anyhow::anyhow!("No active plan")),
        }
    }

    fn tool_get_plan_status(&mut self) -> Result<String> {
        let plan = self.plan_manager.load()?;

        match plan {
            Some(plan) => {
                let (pending, running, completed, failed) = plan.count_by_status();
                let total = plan.tasks.len();

                let mut output = String::new();
                if let Some(ref desc) = plan.description {
                    output.push_str(&format!("Plan: {}\n\n", desc));
                }

                output.push_str(&format!("Total tasks: {}\n", total));
                output.push_str(&format!("  ⏳ Pending:   {}\n", pending));
                output.push_str(&format!("  🔄 Running:   {}\n", running));
                output.push_str(&format!("  ✅ Completed: {}\n", completed));
                output.push_str(&format!("  ❌ Failed:    {}\n", failed));

                if plan.is_complete() {
                    output.push_str("\n✨ All tasks completed!");
                }

                Ok(output)
            }
            None => Ok("No active plan.".to_string()),
        }
    }
}