mcp-methods 0.3.34

Reusable utility methods for MCP servers — pure-Rust library
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
//! ripgrep-powered file + line search.
//!
//! Pure Rust — Python bindings are in `mcp-methods-py`. Two entry
//! points:
//! - [`ripgrep_files`] walks a directory tree and searches every file
//!   matching the glob/type filter. Optional `transform` callback runs
//!   per-file before search (the Python wrapper bridges a callable into
//!   `&dyn Fn(&str) -> String`).
//! - [`ripgrep_lines`] greps through a `Vec<String>` of lines with
//!   context-window merging. Returns structured matches.

mod searcher;
mod types;
mod walker;

use std::path::PathBuf;

use types::{FileMatch, OutputMode};

/// Result of [`ripgrep_lines`] — one entry per merged context window.
#[derive(Debug, Clone)]
pub struct RipgrepLinesGroup {
    /// 1-indexed line numbers of the matching lines in this window.
    pub lines: Vec<usize>,
    /// 1-indexed start line of the window (inclusive).
    pub context_start: usize,
    /// 1-indexed end line of the window (inclusive).
    pub context_end: usize,
    /// Joined content of the window.
    pub content: String,
}

/// Optional knobs for [`ripgrep_files`].
#[derive(Default)]
pub struct RipgrepFilesOpts<'a> {
    /// File-name glob (default `"*"`).
    pub glob: Option<&'a str>,
    /// File-type filter (`"py"`, `"rust"`, …).
    pub type_filter: Option<&'a str>,
    /// `"content"` (default) | `"files_with_matches"` | `"count"`.
    pub output_mode: Option<&'a str>,
    pub case_insensitive: bool,
    pub multiline: bool,
    pub context_before: usize,
    pub context_after: usize,
    /// Symmetric context — overridden by `context_before` / `context_after` when set.
    pub context: usize,
    pub line_numbers: bool,
    pub max_results: Option<usize>,
    pub offset: usize,
    pub match_limit: Option<usize>,
    pub skip_dirs: Option<&'a [String]>,
    pub relative_to: Option<&'a str>,
    pub respect_gitignore: bool,
    /// Per-file transform applied to raw content before search. Used by
    /// the Python wrapper to bridge a Python callable; pure-Rust callers
    /// typically leave this `None`.
    pub transform: Option<&'a dyn Fn(&str) -> String>,
}

impl<'a> RipgrepFilesOpts<'a> {
    /// Builder-style helper for the most common defaults.
    pub fn new() -> Self {
        Self {
            line_numbers: true,
            respect_gitignore: true,
            ..Default::default()
        }
    }
}

/// Search for a regex pattern across files using ripgrep's engine.
///
/// Uses grep-searcher (mmap, SIMD, binary detection), grep-regex (literal
/// optimization), and ignore (parallel walk, .gitignore support).
pub fn ripgrep_files(source_dirs: &[String], pattern: &str, opts: &RipgrepFilesOpts) -> String {
    let glob = opts.glob.unwrap_or("*");
    let output_mode = opts.output_mode.unwrap_or("content");

    let mode = match OutputMode::from_str(output_mode) {
        Ok(m) => m,
        Err(e) => return e,
    };

    let matcher = match searcher::build_matcher(pattern, opts.case_insensitive, opts.multiline) {
        Ok(m) => m,
        Err(e) => return e,
    };

    let ctx_before = if opts.context_before > 0 {
        opts.context_before
    } else {
        opts.context
    };
    let ctx_after = if opts.context_after > 0 {
        opts.context_after
    } else {
        opts.context
    };

    let rel_base = opts.relative_to.map(PathBuf::from);

    let file_matches: Vec<FileMatch> = if let Some(transform) = opts.transform {
        // Sequential path: caller-supplied transform runs per-file before search.
        let paths = match walker::walk_sequential(
            source_dirs,
            glob,
            opts.type_filter,
            opts.skip_dirs,
            opts.respect_gitignore,
        ) {
            Ok(p) => p,
            Err(e) => return e,
        };
        let mut matches = Vec::new();
        let mut total = 0;
        let has_context = ctx_before > 0 || ctx_after > 0;
        let mut text_searcher =
            searcher::build_searcher(ctx_before, ctx_after, opts.multiline, false);
        let mut sink = searcher::CollectSink::new(has_context);

        for path in &paths {
            let raw = match std::fs::read_to_string(path) {
                Ok(t) => t,
                Err(_) => continue,
            };
            let text = transform(&raw);

            sink.clear();
            if let Some((line_matches, context_lines)) =
                searcher::search_text(&text, &matcher, &mut text_searcher, &mut sink)
            {
                total += line_matches.len();
                matches.push(FileMatch {
                    path: path.clone(),
                    match_count: line_matches.len(),
                    line_matches,
                    context_lines,
                });
                if let Some(cap) = opts.match_limit {
                    if total >= cap {
                        break;
                    }
                }
            }
        }
        matches
    } else {
        // Parallel path: walk + search in parallel walker threads.
        match walker::walk_and_search_parallel(
            source_dirs,
            glob,
            opts.type_filter,
            opts.skip_dirs,
            opts.respect_gitignore,
            &matcher,
            ctx_before,
            ctx_after,
            opts.multiline,
            opts.match_limit.unwrap_or(0),
        ) {
            Ok(m) => m,
            Err(e) => return e,
        }
    };

    let source_path = PathBuf::from(&source_dirs[0]);
    format_output(
        &file_matches,
        pattern,
        mode,
        opts.line_numbers,
        opts.max_results,
        opts.offset,
        opts.match_limit,
        rel_base.as_deref(),
        &source_path,
        glob,
    )
}

/// Grep through `text_lines` with context-window merging.
/// Returns one [`RipgrepLinesGroup`] per merged window.
pub fn ripgrep_lines(
    text_lines: &[String],
    pattern: &str,
    context: usize,
) -> Result<Vec<RipgrepLinesGroup>, String> {
    let regex = regex::Regex::new(pattern).map_err(|e| format!("Invalid regex: {}", e))?;

    let mut raw: Vec<(usize, usize, usize)> = Vec::new();
    for (idx, line) in text_lines.iter().enumerate() {
        if regex.is_match(line) {
            let start = idx.saturating_sub(context);
            let end = (idx + context + 1).min(text_lines.len());
            raw.push((idx + 1, start, end));
        }
    }

    // Merge overlapping windows
    struct Group {
        lines: Vec<usize>,
        start: usize,
        end: usize,
    }
    let mut groups: Vec<Group> = Vec::new();
    for (hit_line, start, end) in raw {
        if let Some(last) = groups.last_mut() {
            if start <= last.end {
                last.lines.push(hit_line);
                last.end = last.end.max(end);
                continue;
            }
        }
        groups.push(Group {
            lines: vec![hit_line],
            start,
            end,
        });
    }

    Ok(groups
        .into_iter()
        .map(|g| {
            let content = text_lines[g.start..g.end].join("\n");
            RipgrepLinesGroup {
                lines: g.lines,
                context_start: g.start + 1,
                context_end: g.end,
                content,
            }
        })
        .collect())
}

// ---------------------------------------------------------------------------
// Output formatting
// ---------------------------------------------------------------------------

#[allow(clippy::too_many_arguments)]
fn format_output(
    file_matches: &[FileMatch],
    pattern: &str,
    mode: OutputMode,
    line_numbers: bool,
    max_results: Option<usize>,
    offset: usize,
    match_limit: Option<usize>,
    relative_to: Option<&std::path::Path>,
    source_path: &std::path::Path,
    glob: &str,
) -> String {
    match mode {
        OutputMode::Content => format_content(
            file_matches,
            pattern,
            line_numbers,
            max_results,
            offset,
            match_limit,
            relative_to,
            source_path,
            glob,
        ),
        OutputMode::FilesWithMatches => format_files(
            file_matches,
            max_results,
            offset,
            match_limit,
            relative_to,
            source_path,
        ),
        OutputMode::Count => format_count(
            file_matches,
            max_results,
            offset,
            match_limit,
            relative_to,
            source_path,
        ),
    }
}

#[allow(clippy::too_many_arguments)]
fn format_content(
    file_matches: &[FileMatch],
    pattern: &str,
    line_numbers: bool,
    max_results: Option<usize>,
    offset: usize,
    match_limit: Option<usize>,
    relative_to: Option<&std::path::Path>,
    source_path: &std::path::Path,
    glob: &str,
) -> String {
    let estimated: usize = file_matches
        .iter()
        .map(|fm| fm.line_matches.len() + fm.context_lines.len())
        .sum();
    let mut lines: Vec<String> = Vec::with_capacity(estimated);

    for fm in file_matches {
        let rel = walker::relativize(&fm.path, relative_to, source_path);

        if fm.context_lines.is_empty() {
            for lm in &fm.line_matches {
                if line_numbers {
                    lines.push(format!(
                        "  {}:{}:{} {}",
                        rel, lm.line_number, ':', lm.content
                    ));
                } else {
                    lines.push(format!("  {}  {}", rel, lm.content));
                }
            }
        } else {
            let matches = &fm.line_matches;
            let contexts = &fm.context_lines;
            let mut mi = 0;
            let mut ci = 0;
            let mut prev_ln: Option<u64> = None;

            while mi < matches.len() || ci < contexts.len() {
                let (ln, content, is_match) = match (matches.get(mi), contexts.get(ci)) {
                    (Some(m), Some((cln, _))) if m.line_number <= *cln => {
                        if *cln == m.line_number {
                            ci += 1;
                        }
                        mi += 1;
                        (m.line_number, m.content.as_str(), true)
                    }
                    (Some(_), Some((cln, cc))) => {
                        ci += 1;
                        (*cln, cc.as_str(), false)
                    }
                    (Some(m), None) => {
                        mi += 1;
                        (m.line_number, m.content.as_str(), true)
                    }
                    (None, Some((cln, cc))) => {
                        ci += 1;
                        (*cln, cc.as_str(), false)
                    }
                    (None, None) => unreachable!(),
                };

                if let Some(prev) = prev_ln {
                    if ln > prev + 1 {
                        lines.push("--".to_string());
                    }
                }
                prev_ln = Some(ln);

                if line_numbers {
                    let sep = if is_match { ':' } else { '-' };
                    lines.push(format!("  {}:{}{} {}", rel, ln, sep, content));
                } else {
                    lines.push(format!("  {}  {}", rel, content));
                }
            }
        }
    }

    if offset > 0 && offset < lines.len() {
        lines.drain(..offset);
    } else if offset >= lines.len() && !lines.is_empty() {
        lines.clear();
    }
    if let Some(limit) = max_results {
        if lines.len() > limit {
            lines.truncate(limit);
        }
    }

    if lines.is_empty() {
        return format!("No matches for '{}' in {} files.", pattern, glob);
    }

    let total_matches: usize = file_matches.iter().map(|fm| fm.match_count).sum();
    let mut header = format!("Found {} match(es) for '{}'", total_matches, pattern);
    if let Some(cap) = match_limit {
        if total_matches >= cap {
            header.push_str(&format!(" (capped at {})", cap));
        }
    }
    header.push(':');

    format!("{}\n{}", header, lines.join("\n"))
}

fn format_files(
    file_matches: &[FileMatch],
    max_results: Option<usize>,
    offset: usize,
    match_limit: Option<usize>,
    relative_to: Option<&std::path::Path>,
    source_path: &std::path::Path,
) -> String {
    let mut paths: Vec<String> = file_matches
        .iter()
        .map(|fm| walker::relativize(&fm.path, relative_to, source_path))
        .collect();

    if offset > 0 && offset < paths.len() {
        paths.drain(..offset);
    } else if offset >= paths.len() && !paths.is_empty() {
        paths.clear();
    }
    if let Some(limit) = max_results {
        if paths.len() > limit {
            paths.truncate(limit);
        }
    }

    if paths.is_empty() {
        return "No matching files.".to_string();
    }

    let mut result = paths.join("\n");
    if let Some(cap) = match_limit {
        let total_matches: usize = file_matches.iter().map(|fm| fm.match_count).sum();
        if total_matches >= cap {
            result.push_str(&format!(
                "\n\n(results may be incomplete — hit {} match limit across {} files)",
                cap,
                file_matches.len()
            ));
        }
    }
    result
}

fn format_count(
    file_matches: &[FileMatch],
    max_results: Option<usize>,
    offset: usize,
    match_limit: Option<usize>,
    relative_to: Option<&std::path::Path>,
    source_path: &std::path::Path,
) -> String {
    let mut entries: Vec<String> = file_matches
        .iter()
        .map(|fm| {
            let rel = walker::relativize(&fm.path, relative_to, source_path);
            format!("{}:{}", rel, fm.match_count)
        })
        .collect();

    if offset > 0 && offset < entries.len() {
        entries.drain(..offset);
    } else if offset >= entries.len() && !entries.is_empty() {
        entries.clear();
    }
    if let Some(limit) = max_results {
        if entries.len() > limit {
            entries.truncate(limit);
        }
    }

    if entries.is_empty() {
        return "No matching files.".to_string();
    }

    let mut result = entries.join("\n");
    if let Some(cap) = match_limit {
        let total_matches: usize = file_matches.iter().map(|fm| fm.match_count).sum();
        if total_matches >= cap {
            result.push_str(&format!(
                "\n\n(results may be incomplete — hit {} match limit across {} files)",
                cap,
                file_matches.len()
            ));
        }
    }
    result
}