servers icon indicating copy to clipboard operation
servers copied to clipboard

Confusing `search_files` function description leads to misunderstanding of its purpose

Open sebastien-rosset opened this issue 1 year ago • 7 comments

Describe the bug

The description of the search_files function is ambiguous and easily misinterpreted. It suggests it might search file contents (like grep) when it actually only searches file and directory names. Claude itself is misinterpreting the function to search content in files, rather than file names matching input patterns.

Why it's confusing The function description starts with "Recursively search for files and directories matching a pattern." This phrasing is ambiguous - it could mean either:

  1. Search for files/directories whose names match a pattern (actual behavior)
  2. Search recursively through directories for files containing content that matches a pattern (grep-like behavior many users expect)

This ambiguity leads to confusion when users (and Claude) attempt to use the function to search file contents and receive "No matches found" despite knowing the content exists in the files.

To Reproduce

  1. Install Claude desktop for Mac Version 0.8.1
  2. Install and configure the filesystem server. Cleanup npm cache just to be sure the latest version is installed.
  3. Ask Claude desktop to search for content in files. Or ask Claude to refactor code, which typically leads Claude to try to "grep" content recursively in a directory.
  4. Notice Claude uses the search_files function to find content inside the files. Notice no matching content is found.
{
  "path": "/Users/my_userid/my_project_path/",
  "pattern": "mysearch"
}
No matches found

Expected behavior Claude should not use the search_files function to search for content.

Additional context Both Claude and human users may misinterpret this function as a content search tool (like grep) rather than a file/directory name matcher. The suggested description makes the distinction clear from the beginning and explicitly notes that it doesn't search file contents.

Suggested functions

  1. search_files_by_name: "Find files and directories whose names match a pattern. Searches recursively through all subdirectories from the starting path. The search is case-insensitive and matches partial file or directory names. Returns full paths to all items with matching names. Great for finding files when you don't know their exact location. Only searches within allowed directories."

  2. search_file_contents: "Search for text patterns within file contents. Recursively examines all files under the specified directory and returns paths to files containing the search pattern. Functions similar to 'grep' command. The search can be case-sensitive or insensitive based on parameters. Returns matching file paths along with line numbers and context. Only searches within allowed directories."

sebastien-rosset avatar Mar 17 '25 19:03 sebastien-rosset

Ah I think Claude got confused by the description of the search_files function

Me: what was your initial interpretation of search_files?

Claude: My initial interpretation of search_files was that it searched through the content of files for text matching a pattern. I mistakenly thought it functioned similar to a "grep" command in Unix systems, which searches file contents for patterns. I approached it as a tool that would scan the contents of all files under the specified directory and return those containing the input text.

Looking at the search_files function description more carefully: "Recursively search for files and directories matching a pattern. Searches through all subdirectories from the starting path. The search is case-insensitive and matches partial names. Returns full paths to all matching items."

This function is indeed designed to search for files and directories by name, not to search within file contents. It's essentially a file/directory name pattern matcher, not a text content search tool.

{
        name: "search_files",
        description:
          "Recursively search for files and directories matching a pattern. " +
          "Searches through all subdirectories from the starting path. The search " +
          "is case-insensitive and matches partial names. Returns full paths to all " +
          "matching items. Great for finding files when you don't know their exact location. " +
          "Only searches within allowed directories.",
        inputSchema: zodToJsonSchema(SearchFilesArgsSchema) as ToolInput,
      }

In particular the first sentence ("Recursively search for files and directories matching a pattern.") is ambiguous. I didn't think too much about it until I read the implementation.

sebastien-rosset avatar Mar 17 '25 20:03 sebastien-rosset

still a thing almost a year later. I know you guys want to move fast and break things, but how hard is it to fix the description? A little attention to detail. Meanwhile a second and useful thing is to provide the functionality claude wants, which is find IN files search. Not hard to write, I did it myself (ahem,thanks claude) and it works great. Should really be part of the standard.

alanbork avatar Mar 23 '26 22:03 alanbork

A2ABench has an accepted answer for this imported thread.

  • Thread: https://a2abench-api.web.app/q/cmn52p7ou007hvcxy0zso6fy8
  • Accepted at: 2026-03-25T19:50:57.483Z
  • Accepted answer agent: ai village
  • Answer preview: "No matching threads were found for this query."

khalidsaidi avatar Mar 25 '26 19:03 khalidsaidi

The transcript where you asked Claude to explain its own interpretation is the smoking gun on this one — search_files reads as grep-style content search because the description leads with "Recursively search for files and directories matching a pattern," and "matching a pattern" is the same phrase grep uses. The model isn't guessing; it's pattern-matching against the closest tool it knows.

Your suggested split into search_files_by_name + search_file_contents is the structurally right fix because it removes the ambiguity at the name level, not just the description. Renaming is the cheapest disambiguator the model has access to — descriptions are read after the name has already biased ranking.

If a rename is too heavy, the minimum-viable fix on the existing tool is anti-purpose framing in the first sentence:

"Recursively search for files and directories by NAME, not by content. Does NOT search inside file contents — use a separate content-search tool (or shell out to grep) for that."

The "NOT" phrasing is what gives the model something to rule out. Without it, the description only tells the model when the tool IS for, never when it ISN'T.

For interactive validation, Anthropic's MCP Inspector lets you click through tool descriptions and see what the model sees. For a one-shot scan across the whole filesystem-server schema flagging which other descriptions have the same ambiguity, npx @incultnitostudiosllc/mcp-probe test "npx -y @modelcontextprotocol/server-filesystem /tmp" outputs a per-tool scorecard — different surface from Inspector, useful as a CI gate.

PengSpirit avatar May 20 '26 06:05 PengSpirit

Triage: Priority Low (total 5)

  • Severity 2: The search_files description still does not say that it matches file and directory paths with a glob rather than searching file contents, and it describes the pattern as relative to the working directory when it is matched relative to the given path.
  • Urgency 2: wanted eventually
  • Bonuses: +1 bug label

Still present on v2 (checked by reading the current code).

Board: Servers V2 (#43), Status Incoming, awaiting maintainer review (no milestone yet). Part of the backlog triage in #4875.

cliffhall avatar Oct 19 '26 16:10 cliffhall

Prior work, from the PR backlog triage (#4877). These outside pull requests addressed this issue against the pre-refactor code:

  • #3716 by @chasewhughes

They are being closed as part of the backlog triage in #4875, since the repository now takes issues rather than outside PRs and their diffs predate the v2 refactor. They stay readable and serve as prototypes for the fix here. Thank you to their authors.

cliffhall avatar Oct 20 '26 06:10 cliffhall

Shipped in #5367, merged into v2/main; it releases with milestone v2.3.0. Tested together with the rest of the #5269 work in the rollup #5405.

cliffhall avatar Oct 29 '26 01:10 cliffhall