Skip to content

fix(memory): constrain search_nodes query length - #4662

Open
feiiiiii5 wants to merge 1 commit into
modelcontextprotocol:mainfrom
feiiiiii5:fix/memory-search-query-constraints
Open

fix(memory): constrain search_nodes query length#4662
feiiiiii5 wants to merge 1 commit into
modelcontextprotocol:mainfrom
feiiiiii5:fix/memory-search-query-constraints

Conversation

@feiiiiii5

Copy link
Copy Markdown

Summary

Add a length constraint to the memory server's search_nodes query parameter, per #3537 (official servers' string parameters lack maxLength/pattern constraints, creating DoS and malformed-input risk).

Root Cause

src/memory/index.ts registers search_nodes with query: 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 = 2048 named constant — generous for real retrieval queries, hard cap against abuse.
  • Exported SearchNodesQuerySchema (z.string().max(...)) used by the tool's inputSchema, so the constraint is testable directly.

Scope kept to query as 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):

  • normal query accepted; query at exactly the limit accepted; over-limit rejected; non-string input still rejected.

npm test in src/memory: 4 test files, 54 tests passed (including the 4 new cases).

Diff scope

src/memory/index.ts (+8/-1), new test file (+25).

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>
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.

1 participant