Skip to content

fix(mcp): operator precedence in search_memory filter (#4470)#4474

Merged
whysosaket merged 1 commit intomem0ai:mainfrom
Himanshu-Sangshetti:fix/mcp-search-memory-4470
Mar 21, 2026
Merged

fix(mcp): operator precedence in search_memory filter (#4470)#4474
whysosaket merged 1 commit intomem0ai:mainfrom
Himanshu-Sangshetti:fix/mcp-search-memory-4470

Conversation

@Himanshu-Sangshetti
Copy link
Copy Markdown
Contributor

Description

search_memory filtered vector hits with:

if allowed and h.id is None or h.id not in allowed:

Because and binds tighter than or, Python evaluated (allowed and h.id is None) or (h.id not in allowed). When allowed is None (no accessible memory IDs after ACL filtering), the second part still ran and evaluated h.id not in None, causing:
TypeError: argument of type 'NoneType' is not iterable

This broke MCP memory search whenever the allow-list was empty / unset in that code path.

Change: add parentheses so we only evaluate membership when filtering applies:
if allowed and (h.id is None or h.id not in allowed):

Dependencies: None.

Fixes #4470

Type of change

Please delete options that are not relevant.

  • [x ] Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Reasoning / minimal repro: With allowed = None and a hit with a non-None id, the old condition raises the same TypeError as the server; the new condition does not evaluate h.id not in allowed when allowed is falsy.
  • Local check: Confirmed the updated line in openmemory/api/app/mcp_server.py matches the intended boolean (filter only when allowed is a non-empty set).

No new automated test was added in this PR (small one-line fix); happy to add a unit test in a follow-up if needed

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

Maintainer Checklist

@whysosaket whysosaket merged commit ec326f0 into mem0ai:main Mar 21, 2026
1 of 2 checks passed
jamebobob pushed a commit to jamebobob/mem0-vigil-recall that referenced this pull request Mar 29, 2026
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.

[Bug] Operator precedence error in mcp_server.py search_memory function causes TypeError when filtering memories

2 participants