Skip to content

fix(filesystem): improve edit_file error with nearest-match diagnostics#3738

Open
Christian-Sidak wants to merge 1 commit intomodelcontextprotocol:mainfrom
Christian-Sidak:fix-edit-file-diagnostic-error-message
Open

fix(filesystem): improve edit_file error with nearest-match diagnostics#3738
Christian-Sidak wants to merge 1 commit intomodelcontextprotocol:mainfrom
Christian-Sidak:fix-edit-file-diagnostic-error-message

Conversation

@Christian-Sidak
Copy link
Copy Markdown

Summary

When edit_file fails to find a match (neither exact nor whitespace-flexible), the error message currently just dumps the oldText with no context about what the file actually contains. This makes it hard for both agents and humans to diagnose why the match failed.

This PR improves the error to include:

  • File path in the error message
  • Closest near-match location with 1-indexed line numbers
  • Match percentage (how many lines matched after trimming)
  • Line-by-line comparison showing which lines matched and which diverged, with both the actual and expected content

Example error after this change:

Could not find match for edit in /path/to/file.ts
Closest match (75% of lines) at lines 42-45:
   line 42: "  const foo = bar;"
 ! line 43: "  baz();"
     expected: "  baz(x);"
   line 44: "  return result;"
   line 45: "}"

Edit oldText:
  const foo = bar;
  baz(x);
  return result;
}

No changes to the actual matching/replacement logic -- this is purely diagnostic improvement.

Fixes #2034

Test plan

  • All 146 existing tests pass
  • Updated test name and assertion to match new error format
  • Verified error includes file path and diagnostic context on mismatch

…iagnostics

When edit_file fails to find a match, the error now shows the closest
near-match location with line numbers, matching percentage, and a
line-by-line diff highlighting which lines diverge. This helps agents
and users quickly identify whitespace or content mismatches.

Fixes modelcontextprotocol#2034
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

edit_file tool fails when exact text match not found due to whitespace differences

2 participants