Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions mcp_fd_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@ def _suggest_fuzzy_terms(regex_pattern: str) -> str:
return fuzzy


def _handle_fzf_error(exc: subprocess.CalledProcessError, warnings: list[str]) -> dict[str, Any]:
"""Handle fzf CalledProcessError, returning appropriate result dict."""
# fzf returns exit code 1 when no matches found - this is not an error
if exc.returncode == 1:
return {"matches": []} # No matches found, return empty list

error_result: dict[str, Any] = {"error": str(exc)}
if warnings:
error_result["warnings"] = warnings
return error_result


# ---------------------------------------------------------------------------
# FastMCP server instance
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -355,10 +367,7 @@ def filter_files(
matches.append(chunk.decode("utf-8", errors="replace"))

except subprocess.CalledProcessError as exc:
error_result: dict[str, Any] = {"error": str(exc)}
if warnings:
error_result["warnings"] = warnings
return error_result
return _handle_fzf_error(exc, warnings)
else:
# Standard mode - file paths only
fd_cmd: list[str] = [fd_bin, *shlex.split(fd_flags), pattern, search_path]
Expand All @@ -374,10 +383,7 @@ def filter_files(
fd_proc.wait()
matches = [_normalize_path(p) for p in out.splitlines() if p]
except subprocess.CalledProcessError as exc:
error_result: dict[str, Any] = {"error": str(exc)}
if warnings:
error_result["warnings"] = warnings
return error_result
return _handle_fzf_error(exc, warnings)

if first and matches:
matches = matches[:1]
Expand Down
5 changes: 3 additions & 2 deletions tests/test_fd_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,9 @@ def test_fd_flag_handling(tmp_path: Path):

result = mcp_fd_server.filter_files("nomatch")

# Should handle empty results gracefully
assert "error" in result
# Should handle empty results gracefully (fzf exit code 1 = no matches, not error)
assert "matches" in result
assert result["matches"] == []


def test_multiline_support(tmp_path: Path):
Expand Down
Loading