fix(mcp): treat empty optionalString args as absent so ctx_search file: "" searches unfiltered#80
Open
serhiizghama wants to merge 2 commits into
Conversation
optionalString only checked the type, so an empty or whitespace-only string passed through as a present value while its sibling requireString rejects the same input. In ctx_search this made `file: ""` behave as "filter to a file named ''", which links to no entity and silently returned zero results instead of an unfiltered search. The same gap affected `supersedes`. Return undefined for empty/whitespace-only strings so an optional argument set to "" is equivalent to omitting it, aligning with requireString's non-empty rule.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes #76.
The MCP arg helpers disagree on empty strings.
requireStringrejects an empty/whitespace-only string, butoptionalString(packages/agentctx/src/mcp/tools.ts) only checks the type, so""(or" ") passes through as a present value.This has a concrete effect in
ctx_search.filecomes fromoptionalString, and the handler branches onfile === undefinedto decide whether to apply the file filter:So
ctx_search(query, file: "")is treated as "filter to a file named''", matches no entity, and returns zero results instead of an unfiltered search. The same gap applies tosupersedes(also read viaoptionalString).Solution
Return
undefinedfromoptionalStringfor empty/whitespace-only strings, so an optional argument set to""is equivalent to omitting it. This matchesrequireString's non-empty rule and the codebase's ownnonEmptyStringconvention inextract/schema.ts. Returningundefined(rather than throwing) is appropriate becausefile/supersedesare genuinely optional.No change to the non-empty
file/supersedespaths or torequireString.Testing
Added a regression test in
test/mcp/tools.test.ts: with one file-linked record and one unrelated record,ctx_searchwithfile: ""andfile: " "now returns the same results as omittingfile(both records), not an empty set. Verified it fails on the unfixed helper and passes with the fix.