pathfinder-mcp 0.22.0

Pathfinder — The Headless IDE MCP Server for AI Coding Agents
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
//! `read` tool (batch mode) — batch read multiple files in a single call.

use crate::server::helpers::{
    invalid_params_error, io_error_data, language_from_path, serialize_metadata,
};
use crate::server::types::{FileResult, ReadFilesResponse, ReadParams};
use crate::server::PathfinderServer;
use rmcp::model::{CallToolResult, ErrorData};
use std::path::Path;
use tokio::task::JoinSet;

const READ_FILES_CONCURRENCY: usize = 5;

/// Source file extensions that get AST-based processing via `read_source_file_impl`.
///
/// Matches `SupportedLanguage::detect` in `crates/pathfinder-treesitter/src/language.rs`.
const SOURCE_FILE_EXTENSIONS: &[&str] = &[
    "rs", "ts", "tsx", "go", "py", "pyi", "vue", "js", "jsx", "mjs", "cjs", "java",
];

/// Check if a file path is a source file (eligible for AST-based processing).
fn is_source_file(path: &Path) -> bool {
    path.extension()
        .and_then(|e| e.to_str())
        .is_some_and(|ext| SOURCE_FILE_EXTENSIONS.contains(&ext))
}

/// Truncate content to `max_lines_per_file`.
fn truncate_content(content: &str, max_lines: u32) -> String {
    let lines: Vec<&str> = content.lines().collect();
    let end_idx = max_lines as usize;
    if lines.len() <= end_idx {
        content.to_string()
    } else {
        lines[..end_idx].join("\n")
    }
}

impl PathfinderServer {
    /// Consolidated `read` handler → delegates to `read_file_impl`,
    /// `read_source_file_impl`, or `read_files_impl`.
    ///
    /// Routing:
    /// - `paths` provided → batch mode → `read_files_impl`
    /// - `filepath` provided + source extension → `read_source_file_impl`
    /// - `filepath` provided + config extension → `read_file_impl`
    pub(crate) async fn read_impl(&self, params: ReadParams) -> Result<CallToolResult, ErrorData> {
        let detail_level = params.detail_level.as_str();
        if detail_level != "source_only"
            && detail_level != "compact"
            && detail_level != "symbols"
            && detail_level != "full"
        {
            return Err(invalid_params_error(
                "invalid `detail_level`: must be 'source_only', 'compact', 'symbols', or 'full'",
            ));
        }

        if params.start_line == 0 {
            return Err(invalid_params_error("`start_line` must be >= 1"));
        }

        if let Some(end) = params.end_line {
            if end == 0 {
                return Err(invalid_params_error("`end_line` must be >= 1"));
            }
        }

        if params.max_lines_per_file == 0 {
            return Err(invalid_params_error("`max_lines_per_file` must be >= 1"));
        }

        if params.paths.is_some() && (params.start_line != 1 || params.end_line.is_some()) {
            return Err(invalid_params_error(
                "`start_line` and `end_line` are not supported in batch mode (use single-file `filepath` mode instead)",
            ));
        }

        // Exactly one of filepath / paths must be set.
        match (&params.filepath, &params.paths) {
            (Some(_), Some(_)) => Err(invalid_params_error(
                "provide either `filepath` (single file) or `paths` (batch), not both",
            )),
            (None, None) => Err(invalid_params_error(
                "provide either `filepath` (single file) or `paths` (batch)",
            )),
            // Batch mode
            (None, Some(_)) => self.read_files_impl(params).await,
            // Single file
            (Some(filepath), None) => {
                if let Some(end) = params.end_line {
                    if end < params.start_line {
                        return Err(invalid_params_error("`end_line` must be >= `start_line`"));
                    }
                }
                if is_source_file(Path::new(filepath)) {
                    self.read_source_file_impl(params).await
                } else {
                    self.read_file_impl(params).await
                }
            }
        }
    }

    /// Core logic for the `read_files` batch tool.
    ///
    /// Reads multiple files in a single call with per-file error resilience.
    /// Files are processed sequentially to avoid file descriptor exhaustion.
    #[allow(clippy::too_many_lines)]
    #[tracing::instrument(skip(self, params), fields(count = %params.paths.as_ref().map_or(0, std::vec::Vec::len)))]
    pub(crate) async fn read_files_impl(
        &self,
        params: ReadParams,
    ) -> Result<CallToolResult, ErrorData> {
        let start = std::time::Instant::now();

        tracing::info!(tool = "read_files", "read_files: start");

        let paths = params
            .paths
            .as_ref()
            .ok_or_else(|| io_error_data("paths must be provided"))?;

        if paths.is_empty() {
            tracing::warn!(tool = "read_files", "empty paths");
            return Err(ErrorData::invalid_params(
                "Must provide at least 1 file path",
                Some(serde_json::json!({
                    "provided": 0,
                    "min": 1,
                    "max": 10
                })),
            ));
        }

        if paths.len() > 10 {
            tracing::warn!(tool = "read_files", count = paths.len(), "too many paths");
            return Err(ErrorData::invalid_params(
                "Maximum 10 file paths allowed per call",
                Some(serde_json::json!({
                    "provided": paths.len(),
                    "max": 10
                })),
            ));
        }

        let mut indexed_results: Vec<(usize, FileResult)> = Vec::with_capacity(paths.len());
        let mut set = JoinSet::new();
        let mut spawned = 0;

        for (idx, file_path) in paths.iter().enumerate() {
            let server = self.clone();
            let file_path = file_path.clone();
            let params = params.clone();

            while spawned >= READ_FILES_CONCURRENCY {
                if let Some(res) = set.join_next().await {
                    spawned -= 1;
                    match res {
                        Ok((i, result)) => indexed_results.push((i, result)),
                        Err(e) => {
                            tracing::error!(tool = "read_files", error = %e, "spawned task panicked");
                        }
                    }
                } else {
                    // Safety valve: JoinSet drained but counter disagrees — break to
                    // prevent an infinite spin.  This should never happen in practice.
                    tracing::warn!(
                        tool = "read_files",
                        spawned,
                        "join_next returned None while spawned > 0 — resetting"
                    );
                    spawned = 0;
                    break;
                }
            }

            set.spawn(async move { (idx, server.read_single_file(&file_path, &params).await) });
            spawned += 1;
        }

        while let Some(res) = set.join_next().await {
            match res {
                Ok((idx, result)) => indexed_results.push((idx, result)),
                Err(e) => tracing::error!(tool = "read_files", error = %e, "spawned task panicked"),
            }
        }

        indexed_results.sort_by_key(|(idx, _)| *idx);
        let file_results: Vec<FileResult> = indexed_results.into_iter().map(|(_, r)| r).collect();

        let (mut succeeded, mut failed) = (0, 0);
        for result in &file_results {
            if result.error.is_some() {
                failed += 1;
            } else {
                succeeded += 1;
            }
        }

        let duration_ms = start.elapsed().as_millis();
        tracing::info!(
            tool = "read_files",
            succeeded,
            failed,
            duration_ms,
            "read_files: complete"
        );

        let response = ReadFilesResponse {
            files: file_results,
            succeeded,
            failed,
            duration_ms: Some(u64::try_from(duration_ms).unwrap_or(u64::MAX)),
        };

        let mut text_parts: Vec<String> = Vec::new();
        for file in &response.files {
            if let Some(ref content) = file.content {
                text_parts.push(format!("--- {} ---", file.path));
                let numbered_content = content
                    .lines()
                    .enumerate()
                    .map(|(i, line)| format!("{}: {}", i + 1, line))
                    .collect::<Vec<_>>()
                    .join("\n");
                text_parts.push(numbered_content);
            } else if let Some(ref error) = file.error {
                text_parts.push(format!("--- {} (error: {}) ---", file.path, error));
            }
        }
        text_parts.push(format!(
            "[completed in {}ms, {}/{} files read]",
            u64::try_from(duration_ms).unwrap_or(u64::MAX),
            response.succeeded,
            response.succeeded + response.failed
        ));

        let mut result =
            CallToolResult::success(vec![rmcp::model::Content::text(text_parts.join("\n"))]);
        result.structured_content = serialize_metadata(&response);
        Ok(result)
    }

    /// Read a single file and return its result.
    ///
    /// For source files (`.rs`, `.ts`, `.tsx`, `.go`, `.py`, `.pyi`, `.vue`, `.js`, `.jsx`, `.mjs`, `.cjs`, `.java`),
    /// this delegates to `read_source_file_impl` to get full content without symbols (for token efficiency).
    /// For config files and other files, reads raw content directly.
    #[allow(clippy::too_many_lines)]
    async fn read_single_file(&self, file_path: &str, params: &ReadParams) -> FileResult {
        let path = Path::new(file_path);

        if let Err(e) = self.sandbox.check(path) {
            return FileResult {
                path: file_path.to_string(),
                content: None,
                language: None,
                total_lines: None,
                version_hash: None,
                error: Some(format!("sandbox denied: {}", e.error_code())),
            };
        }

        let absolute_path = self.workspace_root.resolve(path);

        if is_source_file(path) {
            let rs_params = crate::server::types::ReadParams {
                filepath: Some(file_path.to_string()),
                paths: None,
                detail_level: params.detail_level.clone(),
                start_line: 1,
                end_line: None,
                max_lines_per_file: params.max_lines_per_file,
            };

            match self.read_source_file_impl(rs_params).await {
                Ok(result) => {
                    let structured_content_cloned = result.structured_content.clone();
                    let metadata = structured_content_cloned.and_then(|v| {
                        serde_json::from_value::<crate::server::types::ReadSourceFileMetadata>(v)
                            .ok()
                    });

                    // Prefer clean content from metadata (without timing line appended).
                    // Fall back to text output for backward compat.
                    let content = metadata
                        .as_ref()
                        .and_then(|m| m.content.clone())
                        .or_else(|| {
                            result.content.first().and_then(|c| match &c.raw {
                                rmcp::model::RawContent::Text(t) => Some(t.text.clone()),
                                _ => None,
                            })
                        });

                    let language = metadata.as_ref().map(|m| m.language.clone()).or_else(|| {
                        let lang = language_from_path(Path::new(file_path));
                        if lang == "text" {
                            None
                        } else {
                            Some(lang.to_owned())
                        }
                    });

                    let (content, total_lines, version_hash) = if let Some(content) = content {
                        // short() = 7-char hex; consistent with the explore tool's version_hashes format.
                        let version_hash =
                            pathfinder_common::types::VersionHash::compute(content.as_bytes())
                                .short()
                                .to_owned();
                        let total = u32::try_from(content.lines().count()).unwrap_or(u32::MAX);
                        let truncated = truncate_content(&content, params.max_lines_per_file);
                        (Some(truncated), Some(total), Some(version_hash))
                    } else {
                        (None, None, None)
                    };

                    FileResult {
                        path: file_path.to_string(),
                        content,
                        language,
                        total_lines,
                        version_hash,
                        error: None,
                    }
                }
                Err(e) => {
                    let error = e
                        .data
                        .as_ref()
                        .and_then(|d| d.get("error"))
                        .and_then(|v| v.as_str())
                        .unwrap_or("failed to read file")
                        .to_string();

                    FileResult {
                        path: file_path.to_string(),
                        content: None,
                        language: None,
                        total_lines: None,
                        version_hash: None,
                        error: Some(error),
                    }
                }
            }
        } else {
            let content = match tokio::fs::read_to_string(&absolute_path).await {
                Ok(c) => c,
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                    return FileResult {
                        path: file_path.to_string(),
                        content: None,
                        language: None,
                        total_lines: None,
                        version_hash: None,
                        error: Some("file not found".to_string()),
                    }
                }
                Err(e) if e.kind() == std::io::ErrorKind::InvalidData => {
                    return FileResult {
                        path: file_path.to_string(),
                        content: None,
                        language: None,
                        total_lines: None,
                        version_hash: None,
                        error: Some("file appears to be binary (not valid UTF-8)".to_string()),
                    }
                }
                Err(e) => {
                    return FileResult {
                        path: file_path.to_string(),
                        content: None,
                        language: None,
                        total_lines: None,
                        version_hash: None,
                        error: Some(format!("failed to read file: {e}")),
                    }
                }
            };

            // short() = 7-char hex; consistent with the explore tool's version_hashes format.
            let version_hash = pathfinder_common::types::VersionHash::compute(content.as_bytes())
                .short()
                .to_owned();
            let total_lines = u32::try_from(content.lines().count()).unwrap_or(u32::MAX);
            let content = truncate_content(&content, params.max_lines_per_file);
            let language = language_from_path(Path::new(file_path));
            let language = if language == "text" {
                String::new()
            } else {
                language.to_owned()
            };

            FileResult {
                path: file_path.to_string(),
                content: Some(content),
                language: Some(language),
                total_lines: Some(total_lines),
                version_hash: Some(version_hash),
                error: None,
            }
        }
    }
}

#[cfg(test)]
#[path = "read_files_test.rs"]
mod tests;