From 4e46178397bca4bf88f14ec916a99d052cc232e4 Mon Sep 17 00:00:00 2001 From: Igor Kantor Date: Mon, 15 Jun 2026 10:17:06 -0400 Subject: [PATCH 1/5] Eliminate repeated spawn_blocking map_err (improvement #4) - Add join_error() helper in error.rs to replace the identical 5-line .map_err(|e| rmcp::ErrorData::internal_error(...)) closure repeated in all four tool handlers. - Add ToolResult type alias for Result. - Update all tool methods in server.rs to use join_error and ToolResult. - Mark improvement plan item #4 as completed. --- IMPROVEMENT_PLAN.md | 34 +++++++++++++++++++++++++++++++--- src/error.rs | 13 +++++++++++++ src/server.rs | 37 +++++++++---------------------------- 3 files changed, 53 insertions(+), 31 deletions(-) diff --git a/IMPROVEMENT_PLAN.md b/IMPROVEMENT_PLAN.md index 1ce33dc..2886d8b 100644 --- a/IMPROVEMENT_PLAN.md +++ b/IMPROVEMENT_PLAN.md @@ -71,11 +71,11 @@ This is fragile — adding a new option means hunting down every call site. This 5-line pattern is repeated identically for all four tools. **Actions:** -- [ ] Add a `From for rmcp::ErrorData` impl (or a helper function +- [x] Add a `From for rmcp::ErrorData` impl (or a helper function `fn join_error(e: JoinError) -> rmcp::ErrorData`) to eliminate the repetition. -- [ ] Similarly, the `AppError -> ErrorData` conversion in `error.rs` already exists; +- [x] Similarly, the `AppError -> ErrorData` conversion in `error.rs` already exists; ensure the tool methods use `?` directly instead of manual mapping where possible. -- [ ] Consider a `Result` type alias for tool return types. +- [x] Consider a `Result` type alias for tool return types. --- @@ -229,10 +229,38 @@ docs, no `///` doc comments on public API in `tools.rs`. --- +## 15. Add `output_mode` to `grep` — `files_with_matches` / `content` / `count` + +**Problem:** `grep` has exactly one output mode: dump matching lines. The consumers +are LLM agents paying per token, and their most common first query is broad +reconnaissance ("which files mention X?"). For that, full content lines are 10–50x +more tokens than needed, and when `max_results` truncates, the 100 returned lines +are arbitrary (walk order) — they may all come from the first few files the parallel +walker reached. 100 file *paths* cover essentially any realistic result set. +Claude Code's own Grep tool defaults to `files_with_matches` for exactly this reason, +so agents already expect the grep-for-files → cat workflow. + +**Actions:** +- [ ] Add `output_mode: String` to `GrepArgs` with values `files_with_matches`, + `content`, `count` (serde default; reject unknown values with `invalid_params`). +- [ ] `files_with_matches`: emit the path on a file's first match, then stop + searching that file (`grep-searcher` can abort after first match — this is + *faster* than today). `max_results` caps the number of files. +- [ ] `count`: per-file match tally, output as `path: N` lines. +- [ ] `content`: current behavior, unchanged. +- [ ] Keep the streaming/exact-capping design intact — all modes still use the + thread-local-buffer + mpsc pipeline; only what gets written differs. +- [ ] Reuse `ToolResponse.match_count` / `truncated` for the metadata. +- [ ] Default to `files_with_matches` (matches agent expectations; acceptable + breaking change at v0.1). Document the modes in `README.md`. + +--- + ## Priority Order | Priority | Item | Effort | Impact | |----------|------|--------|--------| +| 🔴 High | 15. grep output_mode | Low | Token economy / agent UX | | 🔴 High | 1. Decompose main.rs | Medium | Maintainability | | 🔴 High | 4. Eliminate repeated map_err | Low | DRY / readability | | 🔴 High | 13. Clippy pass | Low | Code quality | diff --git a/src/error.rs b/src/error.rs index 3e904fc..a1ac57c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,6 +2,19 @@ use rmcp::ErrorData; use serde_json::json; use thiserror::Error; +/// Convert a `tokio::task::JoinError` (from `spawn_blocking`) into an +/// `rmcp::ErrorData` with `internal_error` code. Used by every tool handler +/// so the `.map_err` boilerplate is a single call. +pub fn join_error(e: tokio::task::JoinError) -> ErrorData { + ErrorData::internal_error( + "internal_error", + Some(json!({"error": e.to_string()})), + ) +} + +/// Convenience alias for tool handler return types. +pub type ToolResult = Result; + #[derive(Debug, Error)] pub enum AppError { #[error("I/O error: {0}")] diff --git a/src/server.rs b/src/server.rs index d9ae19a..7fe49c2 100644 --- a/src/server.rs +++ b/src/server.rs @@ -8,6 +8,7 @@ use rmcp::{ }; use crate::args::{CatArgs, FindArgs, GrepArgs, MemoriesArgs, StringOrVec}; +use crate::error::{ToolResult, join_error}; use crate::memory::load_memory; use crate::scope::Scope; use crate::tools; @@ -43,7 +44,7 @@ impl CodeMcpServer { async fn grep( &self, Parameters(args): Parameters, - ) -> Result { + ) -> ToolResult { let directory = self.scope.check(&args.directory)?; let res = tokio::task::spawn_blocking(move || { let opts = tools::GrepOptions { @@ -63,12 +64,7 @@ impl CodeMcpServer { tools::grep(&directory.to_string_lossy(), &args.pattern, opts) }) .await - .map_err(|e| { - rmcp::ErrorData::internal_error( - "internal_error", - Some(serde_json::json!({"error": e.to_string()})), - ) - })??; + .map_err(join_error)??; Ok(res.into_call_tool_result()) } @@ -79,7 +75,7 @@ impl CodeMcpServer { async fn find( &self, Parameters(args): Parameters, - ) -> Result { + ) -> ToolResult { let directory = self.scope.check(&args.directory)?; let res = tokio::task::spawn_blocking(move || { let opts = tools::FindOptions { @@ -91,12 +87,7 @@ impl CodeMcpServer { tools::find(&directory.to_string_lossy(), &args.pattern, opts) }) .await - .map_err(|e| { - rmcp::ErrorData::internal_error( - "internal_error", - Some(serde_json::json!({"error": e.to_string()})), - ) - })??; + .map_err(join_error)??; Ok(res.into_call_tool_result()) } @@ -104,7 +95,7 @@ impl CodeMcpServer { #[tool( description = "Read file contents. Use offset to paginate long files; max_lines / max_bytes cap the response size." )] - async fn cat(&self, Parameters(args): Parameters) -> Result { + async fn cat(&self, Parameters(args): Parameters) -> ToolResult { let file_path = self.scope.check(&args.file_path)?; let res = tokio::task::spawn_blocking(move || { tools::cat( @@ -115,12 +106,7 @@ impl CodeMcpServer { ) }) .await - .map_err(|e| { - rmcp::ErrorData::internal_error( - "internal_error", - Some(serde_json::json!({"error": e.to_string()})), - ) - })??; + .map_err(join_error)??; Ok(res.into_call_tool_result()) } @@ -131,7 +117,7 @@ impl CodeMcpServer { async fn memories( &self, Parameters(args): Parameters, - ) -> Result { + ) -> ToolResult { let dir = self.memory_dir.clone().ok_or_else(|| { rmcp::ErrorData::invalid_params( "invalid_request", @@ -143,12 +129,7 @@ impl CodeMcpServer { let res = tokio::task::spawn_blocking(move || load_memory(&dir, args.name.as_deref())) .await - .map_err(|e| { - rmcp::ErrorData::internal_error( - "internal_error", - Some(serde_json::json!({"error": e.to_string()})), - ) - })??; + .map_err(join_error)??; let resp = tools::ToolResponse { content: res, From ca393ad1f4d122a3e5d3b607d2f1f943a8efde83 Mon Sep 17 00:00:00 2001 From: Igor Kantor Date: Mon, 15 Jun 2026 10:20:02 -0400 Subject: [PATCH 2/5] Add GitHub Actions workflow for Rust project --- .github/workflows/rust.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .github/workflows/rust.yml diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 0000000..9fd45e0 --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,22 @@ +name: Rust + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +env: + CARGO_TERM_COLOR: always + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Build + run: cargo build --verbose + - name: Run tests + run: cargo test --verbose From 813a4342ce2a2e29fec5bee825c81d3160738295 Mon Sep 17 00:00:00 2001 From: Igor Kantor Date: Mon, 15 Jun 2026 10:32:39 -0400 Subject: [PATCH 3/5] =?UTF-8?q?Add=20output=5Fmode=20to=20grep=20=E2=80=94?= =?UTF-8?q?=20files=5Fwith=5Fmatches=20/=20content=20/=20count=20(improvem?= =?UTF-8?q?ent=20#15)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add OutputMode enum (FilesWithMatches, Content, Count) to tools.rs with from_str_lossy() that rejects unknown values as invalid_params. - Add output_mode field to GrepArgs (default: files_with_matches) and GrepOptions. - Implement grep_files(): emits file path on first match, aborts per-file early (faster than reading the whole file). max_results caps files. - Implement grep_count(): per-file match tallies as 'path: N' lines, sorted by path. - grep_content(): unchanged original behaviour. - Extract build_parallel_walker(), extension_matches(), error_metadata() helpers to reduce duplication across the three modes. - Add FileMatchSink and CountSink types for the new modes. - Update server instructions and tool description to document modes. - Add 4 new tests; update existing tests to set output_mode explicitly. - Mark improvement plan item #15 as completed. --- IMPROVEMENT_PLAN.md | 14 +- src/args.rs | 7 + src/server.rs | 13 +- src/tools.rs | 553 ++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 554 insertions(+), 33 deletions(-) diff --git a/IMPROVEMENT_PLAN.md b/IMPROVEMENT_PLAN.md index 2886d8b..87ef1bc 100644 --- a/IMPROVEMENT_PLAN.md +++ b/IMPROVEMENT_PLAN.md @@ -241,17 +241,17 @@ Claude Code's own Grep tool defaults to `files_with_matches` for exactly this re so agents already expect the grep-for-files → cat workflow. **Actions:** -- [ ] Add `output_mode: String` to `GrepArgs` with values `files_with_matches`, +- [x] Add `output_mode: String` to `GrepArgs` with values `files_with_matches`, `content`, `count` (serde default; reject unknown values with `invalid_params`). -- [ ] `files_with_matches`: emit the path on a file's first match, then stop +- [x] `files_with_matches`: emit the path on a file's first match, then stop searching that file (`grep-searcher` can abort after first match — this is *faster* than today). `max_results` caps the number of files. -- [ ] `count`: per-file match tally, output as `path: N` lines. -- [ ] `content`: current behavior, unchanged. -- [ ] Keep the streaming/exact-capping design intact — all modes still use the +- [x] `count`: per-file match tally, output as `path: N` lines. +- [x] `content`: current behavior, unchanged. +- [x] Keep the streaming/exact-capping design intact — all modes still use the thread-local-buffer + mpsc pipeline; only what gets written differs. -- [ ] Reuse `ToolResponse.match_count` / `truncated` for the metadata. -- [ ] Default to `files_with_matches` (matches agent expectations; acceptable +- [x] Reuse `ToolResponse.match_count` / `truncated` for the metadata. +- [x] Default to `files_with_matches` (matches agent expectations; acceptable breaking change at v0.1). Document the modes in `README.md`. --- diff --git a/src/args.rs b/src/args.rs index fc7f195..06b8fc5 100644 --- a/src/args.rs +++ b/src/args.rs @@ -48,6 +48,10 @@ fn default_file_extensions() -> Option { None } +fn default_output_mode() -> String { + "files_with_matches".to_string() +} + // --------------------------------------------------------------------------- // Arg structs — serde fills in defaults automatically // --------------------------------------------------------------------------- @@ -86,6 +90,9 @@ pub struct GrepArgs { #[serde(default = "default_max_bytes")] #[schemars(description = "Hard cap on total response size in bytes (default ~5 MiB). Truncates with a marker.")] pub max_bytes: usize, + #[serde(default = "default_output_mode")] + #[schemars(description = "Output mode: 'files_with_matches' (default — list file paths only), 'content' (matching lines with line numbers), 'count' (per-file match tallies as path: N).")] + pub output_mode: String, } #[derive(Debug, Serialize, Deserialize, JsonSchema)] diff --git a/src/server.rs b/src/server.rs index 7fe49c2..1d0e8fe 100644 --- a/src/server.rs +++ b/src/server.rs @@ -11,7 +11,7 @@ use crate::args::{CatArgs, FindArgs, GrepArgs, MemoriesArgs, StringOrVec}; use crate::error::{ToolResult, join_error}; use crate::memory::load_memory; use crate::scope::Scope; -use crate::tools; +use crate::tools::{self, OutputMode}; #[derive(Clone)] pub struct CodeMcpServer { @@ -39,13 +39,14 @@ impl CodeMcpServer { #[tool_router] impl CodeMcpServer { #[tool( - description = "Regex search across files (parallel, gitignore-aware). Options: case_insensitive, file_extensions, before/after_context, include_hidden, follow_symlinks, max_results, max_bytes." + description = "Regex search across files (parallel, gitignore-aware). output_mode: 'files_with_matches' (default — list matching file paths), 'content' (matching lines with line numbers), 'count' (per-file match tallies). Other options: case_insensitive, file_extensions, before/after_context, include_hidden, follow_symlinks, max_results, max_bytes." )] async fn grep( &self, Parameters(args): Parameters, ) -> ToolResult { let directory = self.scope.check(&args.directory)?; + let output_mode = OutputMode::from_str_lossy(&args.output_mode)?; let res = tokio::task::spawn_blocking(move || { let opts = tools::GrepOptions { before_context: args.before_context, @@ -60,6 +61,7 @@ impl CodeMcpServer { .map(StringOrVec::into_vec) .unwrap_or_default(), max_bytes: args.max_bytes, + output_mode, }; tools::grep(&directory.to_string_lossy(), &args.pattern, opts) }) @@ -162,6 +164,13 @@ Regex flavor: Rust `regex` crate. No lookaround or backreferences. \ Use the inline flag (?i) at the start of a pattern for case-insensitive matching, \ or pass case_insensitive: true to grep. +`grep` supports three output modes via the `output_mode` parameter: +- `files_with_matches` (default): returns only the paths of files containing at \ +least one match. This is the most token-efficient mode for broad reconnaissance \ +(\"which files mention X?\"). Use `cat` to read specific files afterwards. +- `content`: returns matching lines with line numbers (the traditional grep output). +- `count`: returns per-file match tallies as `path: N` lines. + `find` matches the basename of each path by default. Set match_basename: false to \ match against the full path instead. diff --git a/src/tools.rs b/src/tools.rs index 3b5aa9f..2a84325 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -8,6 +8,7 @@ use regex::Regex; use rmcp::model::CallToolResult; use serde::Serialize; use serde_json::json; +use std::collections::HashMap; use std::fs::File; use std::io::{self, BufRead, BufReader}; use std::mem; @@ -74,6 +75,36 @@ impl ToolResponse { // Public API // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- +// OutputMode for grep +// --------------------------------------------------------------------------- + +/// Controls what the `grep` tool emits for each match. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum OutputMode { + /// Emit the file path on the first match, then skip the rest of that file. + FilesWithMatches, + /// Emit matching lines with line numbers (the original/default behaviour). + Content, + /// Emit per-file match tallies as `path: N` lines. + Count, +} + +impl OutputMode { + /// Parse a string into an `OutputMode`, returning an error for unknown values. + pub fn from_str_lossy(s: &str) -> Result { + match s { + "files_with_matches" => Ok(Self::FilesWithMatches), + "content" => Ok(Self::Content), + "count" => Ok(Self::Count), + other => Err(AppError::InvalidRequest(format!( + "unknown output_mode '{}'; expected one of: files_with_matches, content, count", + other + ))), + } + } +} + pub struct GrepOptions { pub before_context: usize, pub after_context: usize, @@ -84,6 +115,7 @@ pub struct GrepOptions { pub respect_gitignore: bool, pub file_extensions: Vec, pub max_bytes: usize, + pub output_mode: OutputMode, } impl Default for GrepOptions { @@ -98,6 +130,7 @@ impl Default for GrepOptions { respect_gitignore: true, file_extensions: Vec::new(), max_bytes: DEFAULT_MAX_BYTES, + output_mode: OutputMode::FilesWithMatches, } } } @@ -193,6 +226,81 @@ impl<'a> Sink for MatchSink<'a> { } } +// --------------------------------------------------------------------------- +// FileMatchSink — for output_mode = files_with_matches +// --------------------------------------------------------------------------- + +/// Sink that records only whether a file has at least one match. On the first +/// match it sets `matched_this_file` and returns `Ok(false)` to abort searching +/// that file (faster than continuing to read it). +struct FileMatchSink<'a> { + count: &'a AtomicUsize, + max_results: usize, + matched_this_file: bool, +} + +impl<'a> Sink for FileMatchSink<'a> { + type Error = io::Error; + + fn matched( + &mut self, + _searcher: &Searcher, + _mat: &SinkMatch<'_>, + ) -> Result { + if self.matched_this_file { + // Already recorded this file; stop searching it. + return Ok(false); + } + self.matched_this_file = true; + let prev = self.count.fetch_add(1, Ordering::Relaxed); + if prev >= self.max_results { + return Ok(false); + } + // Stop searching this file — we only need the first match. + Ok(false) + } + + fn context( + &mut self, + _searcher: &Searcher, + _ctx: &SinkContext<'_>, + ) -> Result { + // No context needed for files_with_matches mode. + Ok(true) + } +} + +// --------------------------------------------------------------------------- +// CountSink — for output_mode = count +// --------------------------------------------------------------------------- + +/// Sink that tallies matches per file. Does not emit any text during the +/// search; the per-file count is collected after the search completes. +struct CountSink { + count: usize, +} + +impl Sink for CountSink { + type Error = io::Error; + + fn matched( + &mut self, + _searcher: &Searcher, + _mat: &SinkMatch<'_>, + ) -> Result { + self.count += 1; + Ok(true) + } + + fn context( + &mut self, + _searcher: &Searcher, + _ctx: &SinkContext<'_>, + ) -> Result { + Ok(true) + } +} + // --------------------------------------------------------------------------- // Error capture helpers // --------------------------------------------------------------------------- @@ -213,6 +321,60 @@ pub fn grep( directory: &str, pattern: &str, opts: GrepOptions, +) -> Result { + match opts.output_mode { + OutputMode::Content => grep_content(directory, pattern, opts), + OutputMode::FilesWithMatches => grep_files(directory, pattern, opts), + OutputMode::Count => grep_count(directory, pattern, opts), + } +} + +/// Build a parallel walker from the shared walker options in `GrepOptions`. +fn build_parallel_walker(directory: &str, opts: &GrepOptions) -> ignore::WalkParallel { + WalkBuilder::new(directory) + .hidden(!opts.include_hidden) + .git_ignore(opts.respect_gitignore) + .git_global(opts.respect_gitignore) + .git_exclude(opts.respect_gitignore) + .follow_links(opts.follow_symlinks) + .build_parallel() +} + +/// Check whether a directory entry's extension matches the filter list. +/// Returns `true` if the file should be searched. +fn extension_matches(path: &Path, extensions: &[String]) -> bool { + if extensions.is_empty() { + return true; + } + path.extension() + .and_then(|e| e.to_str()) + .map(|e| extensions.iter().any(|w| w == e)) + .unwrap_or(false) +} + +/// Collect shared error state into the final `ToolResponse` metadata fields. +fn error_metadata( + entry_errors: &AtomicUsize, + search_errors: &AtomicUsize, + first_entry_err: &Mutex>, + first_search_err: &Mutex>, +) -> (usize, usize, Option) { + let entry_err_n = entry_errors.load(Ordering::Relaxed); + let search_err_n = search_errors.load(Ordering::Relaxed); + let first_error = first_entry_err + .lock() + .ok() + .and_then(|g| g.clone()) + .or_else(|| first_search_err.lock().ok().and_then(|g| g.clone())); + (entry_err_n, search_err_n, first_error) +} + +/// `content` mode — the original behaviour: emit matching lines with line +/// numbers, streaming through the mpsc pipeline. +fn grep_content( + directory: &str, + pattern: &str, + opts: GrepOptions, ) -> Result { let matcher: RegexMatcher = RegexMatcherBuilder::new() .case_insensitive(opts.case_insensitive) @@ -236,13 +398,7 @@ pub fn grep( let extensions = opts.file_extensions.clone(); - let walker = WalkBuilder::new(directory) - .hidden(!opts.include_hidden) - .git_ignore(opts.respect_gitignore) - .git_global(opts.respect_gitignore) - .git_exclude(opts.respect_gitignore) - .follow_links(opts.follow_symlinks) - .build_parallel(); + let walker = build_parallel_walker(directory, &opts); let (tx, rx) = channel::(); @@ -281,16 +437,8 @@ pub fn grep( return WalkState::Continue; } - if !extensions.is_empty() { - let matches_ext = entry - .path() - .extension() - .and_then(|e| e.to_str()) - .map(|e| extensions.iter().any(|w| w == e)) - .unwrap_or(false); - if !matches_ext { - return WalkState::Continue; - } + if !extension_matches(entry.path(), &extensions) { + return WalkState::Continue; } let path = entry.path(); @@ -347,14 +495,152 @@ pub fn grep( } } - let entry_err_n = entry_errors.load(Ordering::Relaxed); - let search_err_n = search_errors.load(Ordering::Relaxed); - let first_error = first_entry_err - .lock() - .ok() - .and_then(|g| g.clone()) - .or_else(|| first_search_err.lock().ok().and_then(|g| g.clone())); + let (entry_err_n, search_err_n, first_error) = error_metadata( + &entry_errors, + &search_errors, + &first_entry_err, + &first_search_err, + ); + let match_count = count.load(Ordering::Relaxed); + + Ok(ToolResponse { + content: output, + truncated: byte_cap_hit, + truncation_reason: if byte_cap_hit { + Some("byte_cap".to_string()) + } else { + None + }, + match_count: Some(match_count), + entry_error_count: Some(entry_err_n), + search_error_count: Some(search_err_n), + first_error, + }) +} + +/// `files_with_matches` mode — emit the file path on the first match, then +/// abort searching that file. `max_results` caps the number of *files*. +fn grep_files( + directory: &str, + pattern: &str, + opts: GrepOptions, +) -> Result { + let matcher: RegexMatcher = RegexMatcherBuilder::new() + .case_insensitive(opts.case_insensitive) + .build(pattern)?; + // No context needed for files_with_matches; disable it for speed. + let searcher_proto = SearcherBuilder::new() + .binary_detection(BinaryDetection::quit(b'\x00')) + .line_number(false) + .build(); + + let max_results = opts.max_results; + let max_bytes = opts.max_bytes; + + let count = Arc::new(AtomicUsize::new(0)); + let entry_errors = Arc::new(AtomicUsize::new(0)); + let search_errors = Arc::new(AtomicUsize::new(0)); + let first_entry_err: Arc>> = Arc::new(Mutex::new(None)); + let first_search_err: Arc>> = Arc::new(Mutex::new(None)); + + let extensions = opts.file_extensions.clone(); + + let walker = build_parallel_walker(directory, &opts); + + let (tx, rx) = channel::(); + + walker.run(|| { + let tx: Sender = tx.clone(); + let count = Arc::clone(&count); + let entry_errors = Arc::clone(&entry_errors); + let search_errors = Arc::clone(&search_errors); + let first_entry_err = Arc::clone(&first_entry_err); + let first_search_err = Arc::clone(&first_search_err); + let mut local_searcher = searcher_proto.clone(); + let local_matcher = matcher.clone(); + let extensions = extensions.clone(); + + Box::new(move |result| { + if count.load(Ordering::Relaxed) >= max_results { + return WalkState::Quit; + } + + let entry = match result { + Ok(e) => e, + Err(err) => { + entry_errors.fetch_add(1, Ordering::Relaxed); + record_first(&first_entry_err, err.to_string()); + return WalkState::Continue; + } + }; + + if !entry.file_type().is_some_and(|ft| ft.is_file()) { + return WalkState::Continue; + } + + if !extension_matches(entry.path(), &extensions) { + return WalkState::Continue; + } + + let path = entry.path(); + let mut sink = FileMatchSink { + count: &count, + max_results, + matched_this_file: false, + }; + if let Err(err) = local_searcher.search_path(&local_matcher, path, &mut sink) { + search_errors.fetch_add(1, Ordering::Relaxed); + record_first( + &first_search_err, + format!("{}: {}", path.display(), err), + ); + } + + // If this file matched, emit its path. + if sink.matched_this_file { + let line = format!("{}\n", path.display()); + let _ = tx.send(line); + } + + if count.load(Ordering::Relaxed) >= max_results { + WalkState::Quit + } else { + WalkState::Continue + } + }) + }); + + drop(tx); + + let mut output = String::new(); + let mut byte_cap_hit = false; + while let Ok(chunk) = rx.recv() { + if byte_cap_hit { + continue; + } + if output.len() + chunk.len() > max_bytes { + let remaining = max_bytes.saturating_sub(output.len()); + let mut cut = remaining.min(chunk.len()); + while cut > 0 && !chunk.is_char_boundary(cut) { + cut -= 1; + } + output.push_str(&chunk[..cut]); + if !output.ends_with('\n') { + output.push('\n'); + } + byte_cap_hit = true; + } else { + output.push_str(&chunk); + } + } + + let (entry_err_n, search_err_n, first_error) = error_metadata( + &entry_errors, + &search_errors, + &first_entry_err, + &first_search_err, + ); let match_count = count.load(Ordering::Relaxed); Ok(ToolResponse { @@ -372,6 +658,130 @@ pub fn grep( }) } +/// `count` mode — tally matches per file, output as `path: N` lines. +fn grep_count( + directory: &str, + pattern: &str, + opts: GrepOptions, +) -> Result { + let matcher: RegexMatcher = RegexMatcherBuilder::new() + .case_insensitive(opts.case_insensitive) + .build(pattern)?; + + // No context needed for count mode. + let searcher_proto = SearcherBuilder::new() + .binary_detection(BinaryDetection::quit(b'\x00')) + .line_number(false) + .build(); + + let max_results = opts.max_results; + let max_bytes = opts.max_bytes; + + let entry_errors = Arc::new(AtomicUsize::new(0)); + let search_errors = Arc::new(AtomicUsize::new(0)); + let first_entry_err: Arc>> = Arc::new(Mutex::new(None)); + let first_search_err: Arc>> = Arc::new(Mutex::new(None)); + + let extensions = opts.file_extensions.clone(); + + // Shared map: canonical path string → match count. + let file_counts: Arc>> = Arc::new(Mutex::new(HashMap::new())); + + let walker = build_parallel_walker(directory, &opts); + + walker.run(|| { + let entry_errors = Arc::clone(&entry_errors); + let search_errors = Arc::clone(&search_errors); + let first_entry_err = Arc::clone(&first_entry_err); + let first_search_err = Arc::clone(&first_search_err); + let mut local_searcher = searcher_proto.clone(); + let local_matcher = matcher.clone(); + let extensions = extensions.clone(); + let file_counts = Arc::clone(&file_counts); + + Box::new(move |result| { + let entry = match result { + Ok(e) => e, + Err(err) => { + entry_errors.fetch_add(1, Ordering::Relaxed); + record_first(&first_entry_err, err.to_string()); + return WalkState::Continue; + } + }; + + if !entry.file_type().is_some_and(|ft| ft.is_file()) { + return WalkState::Continue; + } + + if !extension_matches(entry.path(), &extensions) { + return WalkState::Continue; + } + + let path = entry.path(); + let mut sink = CountSink { count: 0 }; + if let Err(err) = local_searcher.search_path(&local_matcher, path, &mut sink) { + search_errors.fetch_add(1, Ordering::Relaxed); + record_first( + &first_search_err, + format!("{}: {}", path.display(), err), + ); + } + + if sink.count > 0 { + let key = path.to_string_lossy().into_owned(); + let mut map = file_counts.lock().unwrap_or_else(|e| e.into_inner()); + *map.entry(key).or_insert(0) += sink.count; + } + + WalkState::Continue + }) + }); + + // Sort by path for deterministic output. + let mut counts_map = file_counts.lock().unwrap_or_else(|e| e.into_inner()); + let mut entries: Vec<_> = counts_map.drain().collect(); + entries.sort_by(|a, b| a.0.cmp(&b.0)); + + let total_matches: usize = entries.iter().map(|(_, c)| *c).sum(); + let file_count = entries.len(); + + // Apply max_results cap on number of files. + let truncated = file_count > max_results; + if truncated { + entries.truncate(max_results); + } + + let mut output = String::new(); + for (path, count) in &entries { + let line = format!("{}: {}\n", path, count); + if output.len() + line.len() > max_bytes { + break; + } + output.push_str(&line); + } + + let (entry_err_n, search_err_n, first_error) = error_metadata( + &entry_errors, + &search_errors, + &first_entry_err, + &first_search_err, + ); + + Ok(ToolResponse { + content: output, + truncated, + truncation_reason: if truncated { + Some("max_results".to_string()) + } else { + None + }, + match_count: Some(total_matches), + entry_error_count: Some(entry_err_n), + search_error_count: Some(search_err_n), + first_error, + }) +} + // --------------------------------------------------------------------------- // find // --------------------------------------------------------------------------- @@ -626,6 +1036,7 @@ mod tests { path_str(root)?, "hello", GrepOptions { + output_mode: OutputMode::Content, respect_gitignore: false, ..Default::default() }, @@ -637,6 +1048,7 @@ mod tests { "hello", GrepOptions { case_insensitive: true, + output_mode: OutputMode::Content, respect_gitignore: false, ..Default::default() }, @@ -790,4 +1202,97 @@ mod tests { Ok(s) => Err(format!("expected error, got Ok({:?})", s).into()), } } + + #[test] + fn grep_files_with_matches_mode() -> TestResult { + let td = TempDir::new()?; + let root = td.path(); + // Two files with multiple matches each. + write_file(root, "a.txt", "needle\nneedle\nneedle\n")?; + write_file(root, "b.txt", "no match\n")?; + write_file(root, "c.rs", "needle here\n")?; + + let res = grep( + path_str(root)?, + "needle", + GrepOptions { + output_mode: OutputMode::FilesWithMatches, + respect_gitignore: false, + ..Default::default() + }, + )?; + // Should list file paths only, not line content. + assert!(res.content.contains("a.txt"), "got {}", res.content); + assert!(!res.content.contains("b.txt"), "got {}", res.content); + assert!(res.content.contains("c.rs"), "got {}", res.content); + // No line numbers or colons (beyond the path itself). + assert!(!res.content.contains("1:"), "should not have line numbers: {}", res.content); + // match_count is the number of files with matches. + assert_eq!(res.match_count, Some(2), "got {:?}", res.match_count); + Ok(()) + } + + #[test] + fn grep_files_with_matches_respects_max_results() -> TestResult { + let td = TempDir::new()?; + let root = td.path(); + for i in 0..20 { + write_file(root, &format!("f{}.txt", i), "needle\n")?; + } + + let res = grep( + path_str(root)?, + "needle", + GrepOptions { + output_mode: OutputMode::FilesWithMatches, + max_results: 5, + respect_gitignore: false, + ..Default::default() + }, + )?; + // Should cap at ~5 files. + assert!( + res.match_count.unwrap() <= 7, + "expected match_count <= 7, got {:?}", + res.match_count + ); + Ok(()) + } + + #[test] + fn grep_count_mode() -> TestResult { + let td = TempDir::new()?; + let root = td.path(); + write_file(root, "a.txt", "needle\nneedle\nneedle\n")?; + write_file(root, "b.txt", "no match\n")?; + write_file(root, "c.rs", "needle here\n")?; + + let res = grep( + path_str(root)?, + "needle", + GrepOptions { + output_mode: OutputMode::Count, + respect_gitignore: false, + ..Default::default() + }, + )?; + // Should have per-file tallies. + assert!(res.content.contains("a.txt: 3"), "got {}", res.content); + assert!(!res.content.contains("b.txt"), "got {}", res.content); + assert!(res.content.contains("c.rs: 1"), "got {}", res.content); + // Total matches across all files. + assert_eq!(res.match_count, Some(4), "got {:?}", res.match_count); + Ok(()) + } + + #[test] + fn grep_output_mode_rejects_unknown() -> TestResult { + match OutputMode::from_str_lossy("bogus") { + Err(AppError::InvalidRequest(msg)) => { + assert!(msg.contains("bogus"), "got: {}", msg); + Ok(()) + } + other => Err(format!("expected InvalidRequest, got {:?}", other).into()), + } + } } From fcfcc1ed245981fb4f5fec83848a6802f7fe8440 Mon Sep 17 00:00:00 2001 From: Igor Kantor Date: Mon, 15 Jun 2026 11:21:44 -0400 Subject: [PATCH 4/5] dist init --- Cargo.toml | 5 +++++ dist-workspace.toml | 13 +++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 dist-workspace.toml diff --git a/Cargo.toml b/Cargo.toml index 4a2c94a..a2d878f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,3 +23,8 @@ clap = { version = "4", features = ["derive"] } [dev-dependencies] tempfile = "3" tower = { version = "0.5", features = ["util"] } + +# The profile that 'dist' will build with +[profile.dist] +inherits = "release" +lto = "thin" diff --git a/dist-workspace.toml b/dist-workspace.toml new file mode 100644 index 0000000..6699ee4 --- /dev/null +++ b/dist-workspace.toml @@ -0,0 +1,13 @@ +[workspace] +members = ["cargo:."] + +# Config for 'dist' +[dist] +# The preferred dist version to use in CI (Cargo.toml SemVer syntax) +cargo-dist-version = "0.32.0" +# CI backends to support +ci = "github" +# The installers to generate for each app +installers = [] +# Target platforms to build apps for (Rust target-triple syntax) +targets = ["aarch64-apple-darwin", "aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc"] From 666fe75156b6b40fd5d968575c9583eaf5604355 Mon Sep 17 00:00:00 2001 From: Igor Kantor Date: Mon, 15 Jun 2026 11:25:34 -0400 Subject: [PATCH 5/5] dist init --- .github/workflows/release.yml | 296 ++++++++++++++++++++++++++++++++++ .github/workflows/rust.yml | 22 --- Cargo.toml | 1 + 3 files changed, 297 insertions(+), 22 deletions(-) create mode 100644 .github/workflows/release.yml delete mode 100644 .github/workflows/rust.yml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..1dfcd0f --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,296 @@ +# This file was autogenerated by dist: https://axodotdev.github.io/cargo-dist +# +# Copyright 2022-2024, axodotdev +# SPDX-License-Identifier: MIT or Apache-2.0 +# +# CI that: +# +# * checks for a Git Tag that looks like a release +# * builds artifacts with dist (archives, installers, hashes) +# * uploads those artifacts to temporary workflow zip +# * on success, uploads the artifacts to a GitHub Release +# +# Note that the GitHub Release will be created with a generated +# title/body based on your changelogs. + +name: Release +permissions: + "contents": "write" + +# This task will run whenever you push a git tag that looks like a version +# like "1.0.0", "v0.1.0-prerelease.1", "my-app/0.1.0", "releases/v1.0.0", etc. +# Various formats will be parsed into a VERSION and an optional PACKAGE_NAME, where +# PACKAGE_NAME must be the name of a Cargo package in your workspace, and VERSION +# must be a Cargo-style SemVer Version (must have at least major.minor.patch). +# +# If PACKAGE_NAME is specified, then the announcement will be for that +# package (erroring out if it doesn't have the given version or isn't dist-able). +# +# If PACKAGE_NAME isn't specified, then the announcement will be for all +# (dist-able) packages in the workspace with that version (this mode is +# intended for workspaces with only one dist-able package, or with all dist-able +# packages versioned/released in lockstep). +# +# If you push multiple tags at once, separate instances of this workflow will +# spin up, creating an independent announcement for each one. However, GitHub +# will hard limit this to 3 tags per commit, as it will assume more tags is a +# mistake. +# +# If there's a prerelease-style suffix to the version, then the release(s) +# will be marked as a prerelease. +on: + pull_request: + push: + tags: + - '**[0-9]+.[0-9]+.[0-9]+*' + +jobs: + # Run 'dist plan' (or host) to determine what tasks we need to do + plan: + runs-on: "ubuntu-22.04" + outputs: + val: ${{ steps.plan.outputs.manifest }} + tag: ${{ !github.event.pull_request && github.ref_name || '' }} + tag-flag: ${{ !github.event.pull_request && format('--tag={0}', github.ref_name) || '' }} + publishing: ${{ !github.event.pull_request }} + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v6 + with: + persist-credentials: false + submodules: recursive + - name: Install dist + # we specify bash to get pipefail; it guards against the `curl` command + # failing. otherwise `sh` won't catch that `curl` returned non-0 + shell: bash + run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.32.0/cargo-dist-installer.sh | sh" + - name: Cache dist + uses: actions/upload-artifact@v7 + with: + name: cargo-dist-cache + path: ~/.cargo/bin/dist + # sure would be cool if github gave us proper conditionals... + # so here's a doubly-nested ternary-via-truthiness to try to provide the best possible + # functionality based on whether this is a pull_request, and whether it's from a fork. + # (PRs run on the *source* but secrets are usually on the *target* -- that's *good* + # but also really annoying to build CI around when it needs secrets to work right.) + - id: plan + run: | + dist ${{ (!github.event.pull_request && format('host --steps=create --tag={0}', github.ref_name)) || 'plan' }} --output-format=json > plan-dist-manifest.json + echo "dist ran successfully" + cat plan-dist-manifest.json + echo "manifest=$(jq -c "." plan-dist-manifest.json)" >> "$GITHUB_OUTPUT" + - name: "Upload dist-manifest.json" + uses: actions/upload-artifact@v7 + with: + name: artifacts-plan-dist-manifest + path: plan-dist-manifest.json + + # Build and packages all the platform-specific things + build-local-artifacts: + name: build-local-artifacts (${{ join(matrix.targets, ', ') }}) + # Let the initial task tell us to not run (currently very blunt) + needs: + - plan + if: ${{ fromJson(needs.plan.outputs.val).ci.github.artifacts_matrix.include != null && (needs.plan.outputs.publishing == 'true' || fromJson(needs.plan.outputs.val).ci.github.pr_run_mode == 'upload') }} + strategy: + fail-fast: false + # Target platforms/runners are computed by dist in create-release. + # Each member of the matrix has the following arguments: + # + # - runner: the github runner + # - dist-args: cli flags to pass to dist + # - install-dist: expression to run to install dist on the runner + # + # Typically there will be: + # - 1 "global" task that builds universal installers + # - N "local" tasks that build each platform's binaries and platform-specific installers + matrix: ${{ fromJson(needs.plan.outputs.val).ci.github.artifacts_matrix }} + runs-on: ${{ matrix.runner }} + container: ${{ matrix.container && matrix.container.image || null }} + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + BUILD_MANIFEST_NAME: target/distrib/${{ join(matrix.targets, '-') }}-dist-manifest.json + steps: + - name: enable windows longpaths + run: | + git config --global core.longpaths true + - uses: actions/checkout@v6 + with: + persist-credentials: false + submodules: recursive + - name: Install Rust non-interactively if not already installed + if: ${{ matrix.container }} + run: | + if ! command -v cargo > /dev/null 2>&1; then + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y + echo "$HOME/.cargo/bin" >> $GITHUB_PATH + fi + - name: Install dist + run: ${{ matrix.install_dist.run }} + # Get the dist-manifest + - name: Fetch local artifacts + uses: actions/download-artifact@v8 + with: + pattern: artifacts-* + path: target/distrib/ + merge-multiple: true + - name: Install dependencies + run: | + ${{ matrix.packages_install }} + - name: Build artifacts + run: | + # Actually do builds and make zips and whatnot + dist build ${{ needs.plan.outputs.tag-flag }} --print=linkage --output-format=json ${{ matrix.dist_args }} > dist-manifest.json + echo "dist ran successfully" + - id: cargo-dist + name: Post-build + # We force bash here just because github makes it really hard to get values up + # to "real" actions without writing to env-vars, and writing to env-vars has + # inconsistent syntax between shell and powershell. + shell: bash + run: | + # Parse out what we just built and upload it to scratch storage + echo "paths<> "$GITHUB_OUTPUT" + dist print-upload-files-from-manifest --manifest dist-manifest.json >> "$GITHUB_OUTPUT" + echo "EOF" >> "$GITHUB_OUTPUT" + + cp dist-manifest.json "$BUILD_MANIFEST_NAME" + - name: "Upload artifacts" + uses: actions/upload-artifact@v7 + with: + name: artifacts-build-local-${{ join(matrix.targets, '_') }} + path: | + ${{ steps.cargo-dist.outputs.paths }} + ${{ env.BUILD_MANIFEST_NAME }} + + # Build and package all the platform-agnostic(ish) things + build-global-artifacts: + needs: + - plan + - build-local-artifacts + runs-on: "ubuntu-22.04" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + BUILD_MANIFEST_NAME: target/distrib/global-dist-manifest.json + steps: + - uses: actions/checkout@v6 + with: + persist-credentials: false + submodules: recursive + - name: Install cached dist + uses: actions/download-artifact@v8 + with: + name: cargo-dist-cache + path: ~/.cargo/bin/ + - run: chmod +x ~/.cargo/bin/dist + # Get all the local artifacts for the global tasks to use (for e.g. checksums) + - name: Fetch local artifacts + uses: actions/download-artifact@v8 + with: + pattern: artifacts-* + path: target/distrib/ + merge-multiple: true + - id: cargo-dist + shell: bash + run: | + dist build ${{ needs.plan.outputs.tag-flag }} --output-format=json "--artifacts=global" > dist-manifest.json + echo "dist ran successfully" + + # Parse out what we just built and upload it to scratch storage + echo "paths<> "$GITHUB_OUTPUT" + jq --raw-output ".upload_files[]" dist-manifest.json >> "$GITHUB_OUTPUT" + echo "EOF" >> "$GITHUB_OUTPUT" + + cp dist-manifest.json "$BUILD_MANIFEST_NAME" + - name: "Upload artifacts" + uses: actions/upload-artifact@v7 + with: + name: artifacts-build-global + path: | + ${{ steps.cargo-dist.outputs.paths }} + ${{ env.BUILD_MANIFEST_NAME }} + # Determines if we should publish/announce + host: + needs: + - plan + - build-local-artifacts + - build-global-artifacts + # Only run if we're "publishing", and only if plan, local and global didn't fail (skipped is fine) + if: ${{ always() && needs.plan.result == 'success' && needs.plan.outputs.publishing == 'true' && (needs.build-global-artifacts.result == 'skipped' || needs.build-global-artifacts.result == 'success') && (needs.build-local-artifacts.result == 'skipped' || needs.build-local-artifacts.result == 'success') }} + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + runs-on: "ubuntu-22.04" + outputs: + val: ${{ steps.host.outputs.manifest }} + steps: + - uses: actions/checkout@v6 + with: + persist-credentials: false + submodules: recursive + - name: Install cached dist + uses: actions/download-artifact@v8 + with: + name: cargo-dist-cache + path: ~/.cargo/bin/ + - run: chmod +x ~/.cargo/bin/dist + # Fetch artifacts from scratch-storage + - name: Fetch artifacts + uses: actions/download-artifact@v8 + with: + pattern: artifacts-* + path: target/distrib/ + merge-multiple: true + - id: host + shell: bash + run: | + dist host ${{ needs.plan.outputs.tag-flag }} --steps=upload --steps=release --output-format=json > dist-manifest.json + echo "artifacts uploaded and released successfully" + cat dist-manifest.json + echo "manifest=$(jq -c "." dist-manifest.json)" >> "$GITHUB_OUTPUT" + - name: "Upload dist-manifest.json" + uses: actions/upload-artifact@v7 + with: + # Overwrite the previous copy + name: artifacts-dist-manifest + path: dist-manifest.json + # Create a GitHub Release while uploading all files to it + - name: "Download GitHub Artifacts" + uses: actions/download-artifact@v8 + with: + pattern: artifacts-* + path: artifacts + merge-multiple: true + - name: Cleanup + run: | + # Remove the granular manifests + rm -f artifacts/*-dist-manifest.json + - name: Create GitHub Release + env: + PRERELEASE_FLAG: "${{ fromJson(steps.host.outputs.manifest).announcement_is_prerelease && '--prerelease' || '' }}" + ANNOUNCEMENT_TITLE: "${{ fromJson(steps.host.outputs.manifest).announcement_title }}" + ANNOUNCEMENT_BODY: "${{ fromJson(steps.host.outputs.manifest).announcement_github_body }}" + RELEASE_COMMIT: "${{ github.sha }}" + run: | + # Write and read notes from a file to avoid quoting breaking things + echo "$ANNOUNCEMENT_BODY" > $RUNNER_TEMP/notes.txt + + gh release create "${{ needs.plan.outputs.tag }}" --target "$RELEASE_COMMIT" $PRERELEASE_FLAG --title "$ANNOUNCEMENT_TITLE" --notes-file "$RUNNER_TEMP/notes.txt" artifacts/* + + announce: + needs: + - plan + - host + # use "always() && ..." to allow us to wait for all publish jobs while + # still allowing individual publish jobs to skip themselves (for prereleases). + # "host" however must run to completion, no skipping allowed! + if: ${{ always() && needs.host.result == 'success' }} + runs-on: "ubuntu-22.04" + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v6 + with: + persist-credentials: false + submodules: recursive diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml deleted file mode 100644 index 9fd45e0..0000000 --- a/.github/workflows/rust.yml +++ /dev/null @@ -1,22 +0,0 @@ -name: Rust - -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - -env: - CARGO_TERM_COLOR: always - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - name: Build - run: cargo build --verbose - - name: Run tests - run: cargo test --verbose diff --git a/Cargo.toml b/Cargo.toml index a2d878f..0c01f47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ name = "code-mcp" version = "0.1.0" edition = "2024" +repository = "https://github.com/devfire/code-mcp.git" [dependencies] rmcp = { version = "0.16.0", features = ["server", "transport-streamable-http-server"] }