octomind 0.18.0

Session-based AI development assistant with conversational codebase interaction, multimodal vision support, built-in MCP tools, and multi-provider AI integration
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Copyright 2025 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// AST-grep execution functionality for the Developer MCP provider

use super::super::{McpFunction, McpToolCall, McpToolResult};
use anyhow::{anyhow, Result};
use serde_json::{json, Value};

// Group ast-grep output by file for token efficiency while preserving line numbers
fn group_ast_grep_output(output: &str) -> String {
	let lines: Vec<&str> = output.lines().collect();
	let mut result = Vec::new();
	let mut current_file = String::new();
	let mut file_lines = Vec::new();

	for line in lines {
		// ast-grep output format: filename:line_number:column:content
		if let Some(colon_pos) = line.find(':') {
			let filename = &line[..colon_pos];
			let rest = &line[colon_pos + 1..];

			if filename != current_file {
				// New file - output previous file's lines
				if !file_lines.is_empty() {
					result.push(format!("{}:\n{}", current_file, file_lines.join("\n")));
					file_lines.clear();
				}
				current_file = filename.to_string();
			}

			// Add the line content (without filename)
			file_lines.push(rest.to_string());
		} else {
			// Non-matching lines (errors, etc.) - keep as-is
			if !file_lines.is_empty() {
				result.push(format!("{}:\n{}", current_file, file_lines.join("\n")));
				file_lines.clear();
				current_file.clear();
			}
			result.push(line.to_string());
		}
	}

	// Output the last file's lines
	if !file_lines.is_empty() {
		result.push(format!("{}:\n{}", current_file, file_lines.join("\n")));
	}

	if result.is_empty() {
		output.to_string()
	} else {
		result.join("\n\n")
	}
}

// Define the ast_grep function for the MCP protocol with enhanced description
pub fn get_ast_grep_function() -> McpFunction {
	McpFunction {
		name: "ast_grep".to_string(),
		description: "Search and refactor code using AST patterns with ast-grep (sg).

This tool uses ast-grep for efficient and semantic code search and transformation using AST patterns.
AST-grep understands code structure, making it superior to regex for code transformations.

Parameters:
- `pattern`: The AST pattern to search for (required)
- `paths`: Optional array of file paths or glob patterns to search within (default: current directory)
- `language`: Optional language of the code (e.g., 'rust', 'javascript', 'python', 'typescript', 'go', 'java', 'c', 'cpp', 'php')
- `rewrite`: Optional rewrite pattern to apply for refactoring transformations
- `json_output`: Optional boolean to get output in JSON format (default: false)
- `context`: Optional number of lines of context to show around matches (default: 0)
- `update_all`: Optional boolean to apply rewrites to all matches without confirmation (default: false)

Note: Response size is controlled by global mcp_response_tokens_threshold setting.
Use more specific patterns to reduce output size if responses are truncated.

Pattern Syntax:
- Use metavariables like $NAME, $ARGS, $BODY for flexible matching
- Use $$$ for matching any number of statements/expressions
- Use $_ for anonymous wildcards (non-capturing)
- Patterns match AST structure, not text

Meta Variables:
- `$VAR` - matches single AST node (like `$NAME`, `$VALUE`)
- `$$$` - matches zero or more nodes (like `$$$ARGS`, `$$$BODY`)
- `$_` - anonymous wildcard, doesn't capture content
- Same-named variables must match identical content

Advanced Patterns:
- Structural matching: `if ($COND) { $$$BODY }` finds all if statements
- Method chains: `$OBJ.$METHOD1().$METHOD2($$$)` finds chained calls
- Nested expressions: `console.log($$$)` matches even in `func(console.log(x))`

Common Examples by Language:

**JavaScript/TypeScript:**
- Function calls: `console.log($$$)` or `$OBJ.$METHOD($$$)`
- Function definitions: `function $NAME($ARGS) { $$$ }`
- Arrow functions: `($ARGS) => $BODY`
- Variable declarations: `const $VAR = $VALUE`
- Import statements: `import $NAME from '$PATH'`

**PHP:**
- Function calls: `$NAME($$$)`
- Method calls: `$OBJ->$METHOD($$$)`
- Class definitions: `class $NAME { $$$ }`
- Variable assignments: `$$VAR = $VALUE`

**Rust:**
- Function calls: `println!($$$)` or `$NAME($$$)`
- Function definitions: `fn $NAME($ARGS) { $$$ }`
- Struct definitions: `struct $NAME { $$$ }`
- Use statements: `use $PATH;`

**Python:**
- Function calls: `print($$$)` or `$OBJ.$METHOD($$$)`
- Function definitions: `def $NAME($ARGS): $$$`
- Class definitions: `class $NAME: $$$`
- Import statements: `import $NAME`

Rewrite Examples:
- Rename functions: pattern `old_func($ARGS)` → rewrite `new_func($ARGS)`
- Add visibility: pattern `fn $NAME($ARGS)` → rewrite `pub fn $NAME($ARGS)`
- Modernize JS: pattern `var $NAME = $VALUE` → rewrite `const $NAME = $VALUE`
- Update method calls: pattern `$OBJ.oldMethod($ARGS)` → rewrite `$OBJ.newMethod($ARGS)`

Usage Examples:
- Find console logs: `{\"pattern\": \"console.log($$$)\", \"language\": \"javascript\"}`
- Rename function: `{\"pattern\": \"oldFunc($ARGS)\", \"rewrite\": \"newFunc($ARGS)\", \"language\": \"javascript\"}`
- Find PHP classes: `{\"pattern\": \"class $NAME\", \"language\": \"php\", \"paths\": [\"src/**/*.php\"]}`
- Search with context: `{\"pattern\": \"TODO\", \"context\": 2}`
".to_string(),
		parameters: json!({
			"type": "object",
			"required": ["pattern"],
			"properties": {
				"pattern": {
					"type": "string",
					"description": "The AST pattern to search for. Use metavariables ($NAME, $$$) to match code structure, not text content"
				},
				"paths": {
					"type": "array",
					"items": {"type": "string"},
					"description": "Optional array of file paths or glob patterns to search within (default: current directory)"
				},
				"language": {
					"type": "string",
					"description": "Optional language of the code (e.g., 'rust', 'javascript', 'python', 'typescript', 'go', 'java', 'c', 'cpp', 'php')"
				},
				"rewrite": {
					"type": "string",
					"description": "Optional rewrite pattern to apply for refactoring transformations"
				},
				"json_output": {
					"type": "boolean",
					"default": false,
					"description": "Optional boolean to get output in JSON format (default: false)"
				},
				"context": {
					"type": "integer",
					"default": 0,
					"description": "Optional number of lines of context to show around matches (default: 0)"
				},
				"update_all": {
					"type": "boolean",
					"default": false,
					"description": "Optional boolean to apply rewrites to all matches without confirmation (default: false)"
				}
			}
		}),
	}
}

// Execute an ast-grep command
pub async fn execute_ast_grep_command(call: &McpToolCall) -> Result<McpToolResult> {
	use tokio::process::Command as TokioCommand;

	// Extract pattern parameter (required)
	let pattern = match call.parameters.get("pattern") {
		Some(Value::String(p)) => {
			if p.trim().is_empty() {
				return Ok(McpToolResult::error(
					call.tool_name.clone(),
					call.tool_id.clone(),
					"Pattern parameter cannot be empty".to_string(),
				));
			}
			p.clone()
		}
		Some(_) => {
			return Ok(McpToolResult::error(
				call.tool_name.clone(),
				call.tool_id.clone(),
				"Pattern parameter must be a string".to_string(),
			));
		}
		None => {
			return Ok(McpToolResult::error(
				call.tool_name.clone(),
				call.tool_id.clone(),
				"Missing required 'pattern' parameter".to_string(),
			));
		}
	};

	// Extract optional parameters
	let paths = call
		.parameters
		.get("paths")
		.and_then(|v| v.as_array())
		.map(|arr| {
			arr.iter()
				.filter_map(|item| item.as_str().map(|s| s.to_string()))
				.collect::<Vec<String>>()
		});

	let language = call
		.parameters
		.get("language")
		.and_then(|v| v.as_str())
		.map(|s| s.to_string());

	let rewrite = call
		.parameters
		.get("rewrite")
		.and_then(|v| v.as_str())
		.map(|s| s.to_string());

	let json_output = call
		.parameters
		.get("json_output")
		.and_then(|v| v.as_bool())
		.unwrap_or(false);

	let context = call
		.parameters
		.get("context")
		.and_then(|v| v.as_i64())
		.unwrap_or(0);

	let update_all = call
		.parameters
		.get("update_all")
		.and_then(|v| v.as_bool())
		.unwrap_or(false);

	// Build the ast-grep command using proper argument passing
	let mut cmd = TokioCommand::new("sg");

	// Add pattern
	cmd.arg("-p");
	cmd.arg(&pattern);

	// Add language if specified
	if let Some(lang) = &language {
		cmd.arg("-l");
		cmd.arg(lang);
	}

	// Add rewrite if specified
	if let Some(rewrite_pattern) = &rewrite {
		cmd.arg("--rewrite");
		cmd.arg(rewrite_pattern);

		// Add update-all flag if specified for rewrite operations
		if update_all {
			cmd.arg("--update-all");
		}
	}

	// Add JSON output if requested
	if json_output {
		cmd.arg("--json");
	}

	// Add context if specified
	if context > 0 {
		cmd.arg("-A");
		cmd.arg(context.to_string());
		cmd.arg("-B");
		cmd.arg(context.to_string());
	}

	// Expand glob patterns to actual file paths first
	// CRITICAL: Handle both glob patterns AND plain directory paths
	let expanded_paths_result = if let Some(file_paths) = &paths {
		// Check if paths contain glob patterns or are plain directories/files
		let has_glob_patterns = file_paths
			.iter()
			.any(|p| p.contains('*') || p.contains('?'));

		if has_glob_patterns {
			// Use glob expansion for patterns
			crate::utils::glob::expand_glob_patterns_filtered(file_paths, None)
		} else {
			// Plain paths - verify they exist and pass through
			let mut verified_paths = Vec::new();
			for path in file_paths {
				let path_obj = std::path::Path::new(path);
				if path_obj.exists() {
					verified_paths.push(path.clone());
				} else {
					return Ok(McpToolResult::error(
						call.tool_name.clone(),
						call.tool_id.clone(),
						format!("Path does not exist: {}", path),
					));
				}
			}
			Ok(verified_paths)
		}
	} else {
		Ok(vec![])
	};

	// Add paths if specified, otherwise default to current directory
	let actual_file_paths = match expanded_paths_result {
		Ok(expanded_paths) => {
			match (expanded_paths.is_empty(), &paths) {
				(true, Some(provided_paths)) => {
					// If paths were explicitly provided but no files found, return error
					let paths_str = provided_paths.join(", ");
					return Ok(McpToolResult::error(
						call.tool_name.clone(),
						call.tool_id.clone(),
						format!("No files found matching the specified paths: [{}]. Please verify the file paths exist and are not in ignored directories.", paths_str),
					));
				}
				(true, None) => {
					// No paths specified at all, use current directory
					cmd.arg(".");
					vec![".".to_string()]
				}
				(false, _) => {
					// Add all expanded paths
					for path in &expanded_paths {
						cmd.arg(path);
					}
					expanded_paths
				}
			}
		}
		Err(e) => {
			return Ok(McpToolResult::error(
				call.tool_name.clone(),
				call.tool_id.clone(),
				format!("Failed to expand glob patterns: {e}"),
			));
		}
	};

	// Configure the command
	cmd.stdout(std::process::Stdio::piped())
		.stderr(std::process::Stdio::piped())
		.stdin(std::process::Stdio::null())
		.kill_on_drop(true); // CRITICAL: Kill process when dropped

	// Debug: Log the complete command being executed with all arguments
	let mut debug_args = vec!["-p".to_string(), pattern.clone()];
	if let Some(lang) = &language {
		debug_args.push("-l".to_string());
		debug_args.push(lang.clone());
	}
	if let Some(rewrite_pattern) = &rewrite {
		debug_args.push("--rewrite".to_string());
		debug_args.push(rewrite_pattern.clone());
		if update_all {
			debug_args.push("--update-all".to_string());
		}
	}
	if json_output {
		debug_args.push("--json".to_string());
	}
	if context > 0 {
		debug_args.push("-A".to_string());
		debug_args.push(context.to_string());
		debug_args.push("-B".to_string());
		debug_args.push(context.to_string());
	}

	// Add file arguments to debug info (show first few and count if many)
	let file_count = actual_file_paths.len();
	if file_count <= 5 {
		for path in &actual_file_paths {
			debug_args.push(path.clone());
		}
	} else {
		for path in actual_file_paths.iter().take(3) {
			debug_args.push(path.clone());
		}
		debug_args.push(format!("... and {} more files", file_count - 3));
	}

	crate::log_debug!(
		"Executing ast-grep command: sg {:?} (targeting {} files)",
		debug_args,
		file_count
	);

	// Spawn the process
	let child = cmd
		.spawn()
		.map_err(|e| anyhow!("Failed to spawn ast-grep command: {}", e))?;

	// Execute the command and wait for completion
	let result = child.wait_with_output().await;
	let output = match result.map_err(|e| anyhow!("AST-grep command execution failed: {}", e)) {
		Ok(output) => {
			let stdout = String::from_utf8_lossy(&output.stdout).to_string();
			let stderr = String::from_utf8_lossy(&output.stderr).to_string();

			// Group FIRST to preserve file-based organization
			let grouped_output = group_ast_grep_output(&stdout);

			// Global truncation will be applied by MCP response handler
			let final_output = grouped_output;

			// Format the final output
			let combined = if stderr.is_empty() {
				final_output
			} else if final_output.is_empty() {
				stderr
			} else {
				format!("{}\n\nError: {}", final_output, stderr)
			};

			// Add detailed execution results including status code
			let status_code = output.status.code().unwrap_or(-1);
			let success = output.status.success();

			// For rewrite operations, provide additional context
			let operation_type = if rewrite.is_some() {
				"rewrite"
			} else {
				"search"
			};

			let result = json!({
				"success": success,
				"output": combined,
				"code": status_code,
				"operation": operation_type,
				"parameters": {
					"pattern": pattern,
					"paths": paths,
					"language": language,
					"rewrite": rewrite,
					"json_output": json_output,
					"context": context,
					"update_all": update_all
				},
				"message": if success {
					format!("AST-grep {operation_type} executed successfully with exit code {status_code}")
				} else {
					format!("AST-grep {operation_type} failed with exit code {status_code}")
				}
			});

			result
		}
		Err(e) => json!({
			"success": false,
			"output": format!("Failed to execute ast-grep command: {e}"),
			"code": -1,
			"operation": if rewrite.is_some() { "rewrite" } else { "search" },
			"parameters": {
				"pattern": pattern,
				"paths": paths,
				"language": language,
				"rewrite": rewrite,
				"json_output": json_output,
				"context": context,
				"update_all": update_all
			},
			"message": format!("Failed to execute ast-grep command: {}", e)
		}),
	};

	Ok(McpToolResult {
		tool_name: "ast_grep".to_string(),
		tool_id: call.tool_id.clone(),
		result: output,
	})
}