fix(memory): constrain search_nodes query length - #4662
Open
feiiiiii5 wants to merge 1 commit into
Open
Conversation
The search_nodes tool accepted an unbounded query string (per modelcontextprotocol#3537, official servers should constrain string parameters). An oversized query costs an O(graph) scan per call with no value. Cap it at 2048 chars via an exported SearchNodesQuerySchema, and add vitest coverage for the boundary (at-limit accepted, over-limit rejected, non-string still rejected). Signed-off-by: fei <204683769+feiiiiii5@users.noreply.github.com>
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.
Summary
Add a length constraint to the memory server's
search_nodesqueryparameter, per #3537 (official servers' string parameters lackmaxLength/patternconstraints, creating DoS and malformed-input risk).Root Cause
src/memory/index.tsregisterssearch_nodeswithquery: z.string()— completely unbounded. Each call with an oversized query still triggers a full O(graph) scan over every entity name, type, and observation, for no retrieval value.Fix
Follows the pattern in #4282 (time server string constraints):
SEARCH_QUERY_MAX_LENGTH = 2048named constant — generous for real retrieval queries, hard cap against abuse.SearchNodesQuerySchema(z.string().max(...)) used by the tool'sinputSchema, so the constraint is testable directly.Scope kept to
queryas named in #3537's memory-server finding; other parameters can follow in separate PRs.Test
New
src/memory/__tests__/search-nodes-schema.test.ts(vitest, per CONTRIBUTING):npm testinsrc/memory: 4 test files, 54 tests passed (including the 4 new cases).Diff scope
src/memory/index.ts(+8/-1), new test file (+25).