use serde_json::{Value, json};
use crate::extract::format::{SUPPORTED_FORMATS, resolve_format};
use crate::extract::{self, Severity};
const DEFAULT_MAX_RESULTS: usize = 500;
const MAX_MAX_RESULTS: usize = 5000;
pub(crate) fn definition() -> Value {
json!({
"name": "extract_paths",
"description": "Extract every file and directory path from a document, with its kind and \
1-based line and column. Reads JSON, YAML, TOML, CSV, dotenv, \
JavaScript, TypeScript, HTML and CSS with a parser for that format, and \
anything else — Python, Go, Markdown, a Dockerfile — by scanning its \
text, which finds paths that carry a separator or a quoted filename. \
Each path is classified as file, relative, absolute or url. Paths are \
reported as written — nothing is resolved against a workspace or the \
filesystem.",
"inputSchema": {
"type": "object",
"properties": {
"content": { "type": "string", "description": "The document text to scan." },
"format": {
"type": "string",
"enum": SUPPORTED_FORMATS,
"description": "Document format. Common extensions and aliases are \
accepted. Omit it and `filename` to scan the text \
generically.",
},
"filename": {
"type": "string",
"description": "Filename used to infer the format when `format` is absent, \
e.g. \"tsconfig.json\".",
},
"dedupe": {
"type": "boolean",
"default": false,
"description": "Collapse repeated paths to their first occurrence.",
},
"maxResults": {
"type": "integer",
"minimum": 1,
"maximum": MAX_MAX_RESULTS,
"default": DEFAULT_MAX_RESULTS,
"description": format!(
"Cap on returned paths (default {DEFAULT_MAX_RESULTS}). meta.truncated \
reports whether any were dropped."
),
},
},
"required": ["content"],
"additionalProperties": false,
},
})
}
pub(crate) fn run(arguments: &Value) -> Result<Value, String> {
let content = arguments
.get("content")
.and_then(Value::as_str)
.ok_or_else(|| "content is required and must be a string".to_string())?;
let max_results = read_max_results(arguments)?;
let format = arguments.get("format").and_then(Value::as_str);
let filename = arguments.get("filename").and_then(Value::as_str);
let language_id = resolve_format(format, filename);
let result = extract::extract(content, language_id);
let mut values: Vec<Value> = result
.paths
.iter()
.map(|path| {
json!({
"value": path.value,
"type": path.kind,
"line": path.position.line,
"column": path.position.column,
})
})
.collect();
if arguments.get("dedupe").and_then(Value::as_bool) == Some(true) {
let mut seen: Vec<&str> = Vec::new();
let mut deduped = Vec::with_capacity(values.len());
for value in &values {
let text = value["value"].as_str().unwrap_or_default();
if seen.contains(&text) {
continue;
}
seen.push(text);
deduped.push(value.clone());
}
values = deduped;
}
let truncated = values.len() > max_results;
values.truncate(max_results);
let diagnostics: Vec<Value> = result
.errors
.iter()
.map(|error| {
json!({
"severity": if error.severity == Severity::Error { "error" } else { "warning" },
"code": format!("{:?}", error.category).to_lowercase(),
"message": error.message,
})
})
.collect();
let count = values.len();
Ok(super::envelope(
"extract_paths",
&json!({ "paths": values, "fileType": language_id }),
count,
&diagnostics,
truncated,
))
}
fn read_max_results(arguments: &Value) -> Result<usize, String> {
let Some(raw) = arguments.get("maxResults") else {
return Ok(DEFAULT_MAX_RESULTS);
};
let invalid = "maxResults must be a positive integer".to_string();
let value = raw.as_u64().ok_or(invalid.clone())?;
if value < 1 {
return Err(invalid);
}
Ok((value as usize).min(MAX_MAX_RESULTS))
}
#[cfg(test)]
mod tests {
use serde::Deserialize;
use super::*;
use crate::extract::corpus::document;
const CASES: &str = include_str!("../../fixtures/mcp-extract-paths.json");
#[derive(Debug, Deserialize)]
struct Case {
name: String,
file: Option<String>,
content: Option<String>,
arguments: Value,
expected: Option<Value>,
#[serde(rename = "expectedError")]
expected_error: Option<String>,
}
#[test]
fn every_shared_case_answers_identically() {
let cases: Vec<Case> = serde_json::from_str(CASES).expect("the corpus is valid JSON");
assert!(!cases.is_empty(), "the corpus is empty");
for case in cases {
let mut arguments = case.arguments.clone();
let content = case
.file
.as_deref()
.map(document)
.map(str::to_string)
.or(case.content);
if let Some(content) = content {
arguments["content"] = json!(content);
}
match (case.expected, case.expected_error) {
(_, Some(expected)) => {
let error = run(&arguments).expect_err(&case.name);
assert_eq!(error, expected, "{}", case.name);
}
(Some(expected), None) => {
let actual = run(&arguments).expect(&case.name);
assert_eq!(actual, expected, "{}", case.name);
}
(None, None) => panic!("{} pins neither a result nor an error", case.name),
}
}
}
#[test]
fn the_advertised_enum_matches_the_formats_that_resolve() {
let definition = definition();
let advertised = definition["inputSchema"]["properties"]["format"]["enum"]
.as_array()
.expect("an enum")
.iter()
.filter_map(|value| value.as_str().map(str::to_string))
.collect::<Vec<String>>();
assert_eq!(advertised, SUPPORTED_FORMATS);
}
#[test]
fn an_excessive_cap_is_clamped_rather_than_refused() {
let result = run(&json!({
"content": "{}",
"format": "json",
"maxResults": MAX_MAX_RESULTS + 1_000,
}));
assert!(result.is_ok(), "{result:?}");
}
#[test]
fn a_fractional_cap_is_refused() {
let error = run(&json!({ "content": "{}", "format": "json", "maxResults": 1.5 }))
.expect_err("a refusal");
assert_eq!(error, "maxResults must be a positive integer");
}
#[test]
fn the_tool_name_is_pinned() {
assert_eq!(definition()["name"], "extract_paths");
}
}