Skip to content

fix(mcp): search_code converts multi-word patterns to regex#304

Open
jjoos wants to merge 1 commit intoDeusData:mainfrom
jjoos:fix/search-code-multi-word
Open

fix(mcp): search_code converts multi-word patterns to regex#304
jjoos wants to merge 1 commit intoDeusData:mainfrom
jjoos:fix/search-code-multi-word

Conversation

@jjoos
Copy link
Copy Markdown

@jjoos jjoos commented Apr 30, 2026

Problem

search_code returns 0 results when the pattern contains spaces (e.g. "slack webhook handler"). Single-word queries like "webhook" or "handler" work fine.

Root cause

handle_search_code writes the pattern to a temp file and passes it to grep -F -f (fixed-string mode). A multi-word pattern like "slack webhook handler" is treated as a literal substring — grep looks for that exact 22-character sequence on one line. This almost never matches real code.

Fix

Before invoking grep, if the pattern contains whitespace and use_regex is false:

  1. Split on whitespace runs
  2. Escape regex metacharacters in each word
  3. Join with .*slack.*webhook.*handler
  4. Flip to regex mode (grep -E)

This matches lines containing all words in order. Single-word patterns are unaffected (no spaces → no conversion).

Changes

  • src/mcp/mcp.c: 36 lines added in a new "Phase 0.5" block before grep invocation.
  • tests/test_mcp.c: New search_code_multi_word test verifying a two-word query finds a matching line.

Testing

  • New unit test: search_code_multi_word — searches for "HandleRequest error" in a fixture file containing func HandleRequest() error {. Verifies non-zero results.
  • Manual testing against indexed Ruby monolith confirmed multi-word queries now return relevant results.

When pattern contains spaces and is not already a regex, convert to a
regex with '.*' between words: "foo bar baz" → "foo.*bar.*baz".
This matches all terms in order on a line instead of requiring the
exact multi-word substring.

Metacharacters in user input are escaped so the conversion is safe
for arbitrary text.

Includes test: search_code_multi_word verifies that a two-word query
matches a line containing both words non-contiguously.
@DeusData DeusData added the bug Something isn't working label May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants