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
use crate::extractor;
use crate::git;
use crate::redact;
use crate::search;
use crate::workspace::WorkspaceBoundary;
use serde_json::{json, Value};
use std::io::{self, BufRead, Read, Write};
fn error_response(id: Option<Value>, code: i32, message: &str) -> Value {
json!({
"jsonrpc": "2.0",
"id": id.unwrap_or(Value::Null),
"error": {
"code": code,
"message": message
}
})
}
fn success_response(id: Value, result: Value) -> Value {
json!({
"jsonrpc": "2.0",
"id": id,
"result": result
})
}
pub fn verify_patch(path: &std::path::Path) -> Result<(), String> {
if let Some(ext) = path.extension().and_then(|e| e.to_str()) {
match ext {
"rs" => {
if let Ok(output) = std::process::Command::new("cargo")
.args(["check", "--quiet", "--message-format=short"])
.output()
{
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = String::from_utf8_lossy(&output.stdout);
let err_msg = if !stderr.trim().is_empty() {
stderr.trim()
} else {
stdout.trim()
};
return Err(format!("Cargo check failed: {}", err_msg));
}
}
}
"py" => {
if let Ok(output) = std::process::Command::new("python3")
.args(["-m", "py_compile", path.to_str().unwrap_or("")])
.output()
{
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(format!("Python syntax check failed: {}", stderr.trim()));
}
}
}
"js" | "mjs" | "cjs" => {
if let Ok(output) = std::process::Command::new("node")
.args(["--check", path.to_str().unwrap_or("")])
.output()
{
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(format!("Node syntax check failed: {}", stderr.trim()));
}
}
}
_ => {}
}
}
Ok(())
}
pub async fn run_server() -> anyhow::Result<()> {
let boundary = WorkspaceBoundary::current().unwrap_or_else(|_| {
WorkspaceBoundary::new(std::env::current_dir().unwrap_or_default())
.expect("workspace boundary initialization")
});
let analyzer_state = crate::analyzer::AppState::new();
let stdin = io::stdin();
let mut stdout = io::stdout();
let mut handle = stdin.lock();
loop {
let mut line = String::new();
// VULN-07: Limit read to 50MB to prevent MCP DoS
let n = handle.by_ref().take(50 * 1024 * 1024).read_line(&mut line)?;
if n == 0 {
break; // EOF
}
if line.trim().is_empty() {
continue;
}
let req: Value = match serde_json::from_str(&line) {
Ok(v) => v,
Err(_) => {
let err = error_response(None, -32700, "Parse error or line too long");
writeln!(stdout, "{}", serde_json::to_string(&err)?)?;
stdout.flush()?;
continue;
}
};
let id = req.get("id").cloned();
let method = match req.get("method").and_then(|v| v.as_str()) {
Some(m) => m,
None => {
let err = error_response(id, -32600, "Invalid Request: missing method");
writeln!(stdout, "{}", serde_json::to_string(&err)?)?;
stdout.flush()?;
continue;
}
};
let response = match method {
"initialize" => Some(success_response(
id.unwrap_or(Value::Null),
json!({
"protocolVersion": "2024-11-05",
"capabilities": {
"tools": {}
},
"serverInfo": {
"name": "tokenectomy",
"version": env!("CARGO_PKG_VERSION")
}
}),
)),
"notifications/initialized" => None,
"ping" => Some(success_response(id.unwrap_or(Value::Null), json!({}))),
"tools/list" => Some(success_response(
id.unwrap_or(Value::Null),
json!({
"tools": [
{
"name": "get_error_context",
"description": "Extracts detailed source code context and git diff from an error log, removing framework noise and redacting secrets.",
"inputSchema": {
"type": "object",
"properties": {
"log": {
"type": "string",
"description": "The raw error stack trace or compiler panic message string to sanitize and extract context from."
},
"context_lines": {
"type": "integer",
"description": "Number of source code lines to extract above and below the error location (default: 10)."
}
},
"required": ["log"]
}
},
{
"name": "search_stack_overflow",
"description": "Queries Stack Overflow API for solutions matching a sanitized error signature.",
"inputSchema": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Targeted, sanitized exception message or error signature (free of secrets and local file paths)."
}
},
"required": ["query"]
}
},
{
"name": "apply_code_patch",
"description": "Applies an atomic code patch to a specific file by replacing original_code with new_code, with automatic syntax validation and rollback.",
"inputSchema": {
"type": "object",
"properties": {
"file_path": {
"type": "string",
"description": "Absolute path to the file within the workspace boundary."
},
"original_code": {
"type": "string",
"description": "The exact code block to be replaced (must match uniquely)."
},
"new_code": {
"type": "string",
"description": "The replacement code block."
}
},
"required": ["file_path", "original_code", "new_code"]
}
},
{
"name": "analyze_code",
"description": "Performs static AST code analysis to detect resource leaks, security vulnerabilities, and code defects with bounded execution limits and precise LSP UTF-16 coordinates.",
"inputSchema": {
"type": "object",
"properties": {
"language": {
"type": "string",
"description": "Programming language of the code to analyze (e.g. 'python', 'py')."
},
"code": {
"type": "string",
"description": "Raw source code to analyze."
}
},
"required": ["language", "code"]
}
}
]
}),
)),
"tools/call" => {
let params = req.get("params");
let name = params.and_then(|p| p.get("name")).and_then(|n| n.as_str());
let args = params.and_then(|p| p.get("arguments"));
if name.is_none() || args.is_none() {
Some(error_response(id, -32602, "Invalid params"))
} else {
let name = name.unwrap();
let args = args.unwrap();
match name {
"get_error_context" => {
if let Some(log) = args.get("log").and_then(|l| l.as_str()) {
let context_lines = args
.get("context_lines")
.and_then(|c| c.as_u64())
.unwrap_or(10) as usize;
// P1: Redact secrets BEFORE extracting context
let safe_log = redact::redact_secrets(log);
let (context, _) = extractor::extract_context_with_boundary(
&safe_log,
context_lines,
Some(&boundary),
);
let mut combined =
format!("Log:\n{}\nContext:\n{}", safe_log, context);
if let Some(git_diff) = git::get_recent_changes() {
let safe_diff = redact::redact_secrets(&git_diff);
combined.push_str(&format!(
"\n\nRecent Git Changes:\n{}",
safe_diff
));
}
Some(success_response(
id.unwrap_or(Value::Null),
json!({
"content": [{ "type": "text", "text": combined }]
}),
))
} else {
Some(success_response(
id.unwrap_or(Value::Null),
json!({
"content": [{ "type": "text", "text": "Error: Missing required 'log' argument." }],
"isError": true
}),
))
}
}
"search_stack_overflow" => {
if let Some(query) = args.get("query").and_then(|q| q.as_str()) {
let results = search::search_stackoverflow(query).await;
let text = results
.unwrap_or_else(|| "No results found".to_string());
Some(success_response(
id.unwrap_or(Value::Null),
json!({
"content": [{ "type": "text", "text": text }]
}),
))
} else {
Some(success_response(
id.unwrap_or(Value::Null),
json!({
"content": [{ "type": "text", "text": "Error: Missing required 'query' argument." }],
"isError": true
}),
))
}
}
"apply_code_patch" => {
let file_path = args.get("file_path").and_then(|f| f.as_str());
let original = args.get("original_code").and_then(|o| o.as_str());
let new_code = args.get("new_code").and_then(|n| n.as_str());
if let (Some(fp), Some(orig), Some(new_c)) =
(file_path, original, new_code)
{
match boundary.resolve(fp) {
Err(e) => Some(success_response(
id.unwrap_or(Value::Null),
json!({
"content": [{ "type": "text", "text": format!("Security Error: {}", e) }],
"isError": true
}),
)),
Ok(safe_path) => {
match boundary.read(&safe_path) {
Ok(content) => {
let count = content.matches(orig).count();
if count == 0 {
Some(success_response(
id.unwrap_or(Value::Null),
json!({
"content": [{ "type": "text", "text": "Error: original_code not found in the file. Make sure it matches exactly." }],
"isError": true
}),
))
} else if count > 1 {
Some(success_response(
id.unwrap_or(Value::Null),
json!({
"content": [{ "type": "text", "text": format!("Error: original_code found {} times. Please provide a more specific code block.", count) }],
"isError": true
}),
))
} else {
let backup = content.clone();
let updated = content.replacen(orig, new_c, 1);
match boundary.write(&safe_path, updated.as_bytes()) {
Ok(_) => match verify_patch(&safe_path) {
Ok(_) => Some(success_response(
id.unwrap_or(Value::Null),
json!({
"content": [{ "type": "text", "text": "Patch applied and verified successfully." }]
}),
)),
Err(verify_err) => {
let rollback_status = if let Err(rb_err) =
boundary.write(&safe_path, backup.as_bytes())
{
format!(
"CRITICAL: Patch verification failed ({}), and rollback ALSO failed: {}",
verify_err, rb_err
)
} else {
format!(
"Patch rejected and automatically rolled back: {}",
verify_err
)
};
Some(success_response(
id.unwrap_or(Value::Null),
json!({
"content": [{ "type": "text", "text": rollback_status }],
"isError": true
}),
))
}
},
Err(e) => Some(success_response(
id.unwrap_or(Value::Null),
json!({
"content": [{ "type": "text", "text": format!("Failed to write file: {}", e) }],
"isError": true
}),
)),
}
}
}
Err(e) => Some(success_response(
id.unwrap_or(Value::Null),
json!({
"content": [{ "type": "text", "text": format!("Failed to read file: {}", e) }],
"isError": true
}),
)),
}
}
}
} else {
Some(success_response(
id.unwrap_or(Value::Null),
json!({
"content": [{ "type": "text", "text": "Missing required arguments for apply_code_patch." }],
"isError": true
}),
))
}
}
"analyze_code" => {
let language = args.get("language").and_then(|l| l.as_str());
let code = args.get("code").and_then(|c| c.as_str());
if let (Some(lang), Some(source)) = (language, code) {
match crate::analyzer::analyze_source(&analyzer_state, lang, source) {
Ok(resp) => {
let json_output = serde_json::to_string_pretty(&resp).unwrap_or_default();
Some(success_response(
id.unwrap_or(Value::Null),
json!({
"content": [{ "type": "text", "text": json_output }]
}),
))
}
Err(e) => {
Some(success_response(
id.unwrap_or(Value::Null),
json!({
"content": [{ "type": "text", "text": format!("Analysis error: {}", e) }],
"isError": true
}),
))
}
}
} else {
Some(success_response(
id.unwrap_or(Value::Null),
json!({
"content": [{ "type": "text", "text": "Error: Missing required 'language' or 'code' argument." }],
"isError": true
}),
))
}
}
_ => Some(error_response(
id,
-32601,
&format!("Tool '{}' not found", name),
)),
}
}
}
_ => {
if id.is_some() {
Some(error_response(id, -32601, "Method not found"))
} else {
None
}
}
};
if let Some(res) = response {
writeln!(stdout, "{}", serde_json::to_string(&res)?)?;
stdout.flush()?;
}
}
Ok(())
}