Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion V0/agent-memory-client/agent_memory_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,9 @@ async def search_long_term_memory(
distance_threshold: Optional distance threshold for search results
memory_type: Optional memory type filter
extraction_strategy: Optional extraction strategy filter
limit: Maximum number of results to return (default: 10)
limit: Maximum number of results to return (default: 10). The
server accepts 1-100; larger values raise MemoryServerError
(HTTP 422) rather than returning results.
offset: Offset for pagination (default: 0)
optimize_query: Whether to optimize the query for semantic (vector) search using a fast model; ignored for keyword and hybrid modes (default: False)

Expand Down
56 changes: 30 additions & 26 deletions V0/agent_memory_server/mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ async def search_long_term_memory(
extraction_strategy: Filter by extraction strategy
event_date: Filter by event date (for episodic memories)
distance_threshold: Distance threshold for semantic search
limit: Maximum number of results
limit: Maximum number of results (1-100)
offset: Offset for pagination
optimize_query: Whether to optimize the query for semantic (vector) search only; ignored for keyword and hybrid modes (default: False - LLMs typically provide already optimized queries)
recency_boost: Enable recency-aware re-ranking (defaults to enabled if None)
Expand All @@ -608,32 +608,36 @@ async def search_long_term_memory(
"distance_threshold is only supported for semantic search mode"
)

# Build the request outside the try block so invalid parameters (e.g. a
# limit above the maximum) surface as a tool error instead of being
# indistinguishable from an empty result set.
payload = SearchRequest(
text=text,
search_mode=search_mode,
session_id=session_id,
namespace=namespace,
topics=topics,
entities=entities,
created_at=created_at,
last_accessed=last_accessed,
user_id=user_id,
memory_type=memory_type,
extraction_strategy=extraction_strategy,
event_date=event_date,
distance_threshold=distance_threshold,
limit=limit,
offset=offset,
recency_boost=recency_boost,
recency_semantic_weight=recency_semantic_weight,
recency_recency_weight=recency_recency_weight,
recency_freshness_weight=recency_freshness_weight,
recency_novelty_weight=recency_novelty_weight,
recency_half_life_last_access_days=recency_half_life_last_access_days,
recency_half_life_created_days=recency_half_life_created_days,
server_side_recency=server_side_recency,
)

try:
payload = SearchRequest(
text=text,
search_mode=search_mode,
session_id=session_id,
namespace=namespace,
topics=topics,
entities=entities,
created_at=created_at,
last_accessed=last_accessed,
user_id=user_id,
memory_type=memory_type,
extraction_strategy=extraction_strategy,
event_date=event_date,
distance_threshold=distance_threshold,
limit=limit,
offset=offset,
recency_boost=recency_boost,
recency_semantic_weight=recency_semantic_weight,
recency_recency_weight=recency_recency_weight,
recency_freshness_weight=recency_freshness_weight,
recency_novelty_weight=recency_novelty_weight,
recency_half_life_last_access_days=recency_half_life_last_access_days,
recency_half_life_created_days=recency_half_life_created_days,
server_side_recency=server_side_recency,
)
# Create a background tasks instance for the MCP call
from agent_memory_server.dependencies import HybridBackgroundTasks

Expand Down
2 changes: 1 addition & 1 deletion V0/agent_memory_server/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ class SearchRequest(BaseModel):
default=10,
ge=1,
le=100,
description="Optional limit on the number of results",
description="Max number of results (1-100; larger values are rejected)",
)
offset: int = Field(
default=0,
Expand Down
2 changes: 1 addition & 1 deletion V0/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ Payload for long-term memory search
| `memory_type` | MemoryType \| null | No | Optional memory type to filter by |
| `event_date` | EventDate \| null | No | Optional event date to filter by (for episodic memories) |
| `extraction_strategy` | ExtractionStrategy \| null | No | Optional extraction strategy to filter by |
| `limit` | integer | No | Optional limit on the number of results |
| `limit` | integer | No | Max number of results (1-100; larger values are rejected) |
| `offset` | integer | No | Optional offset |
| `recency_boost` | boolean \| null | No | Enable recency-aware re-ranking (defaults to enabled if None |
| `recency_semantic_weight` | number \| null | No | Weight for semantic similarity |
Expand Down
12 changes: 12 additions & 0 deletions V0/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,18 @@ async def test_search_rejects_distance_threshold_for_keyword_mode(
)
mock_search.assert_not_called()

@patch("agent_memory_server.api.long_term_memory.search_long_term_memories")
@pytest.mark.asyncio
async def test_search_rejects_limit_above_maximum(self, mock_search, client):
"""A limit above 100 is a client error, not an empty result set."""
payload = {"text": "preference", "limit": 101}

response = await client.post("/v1/long-term-memory/search", json=payload)

assert response.status_code == 422
assert response.json()["detail"][0]["loc"] == ["body", "limit"]
mock_search.assert_not_called()

@patch("agent_memory_server.api.long_term_memory.search_long_term_memories")
@pytest.mark.parametrize("search_mode", ["keyword", "hybrid"])
@pytest.mark.asyncio
Expand Down
17 changes: 17 additions & 0 deletions V0/tests/test_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,23 @@ async def test_mcp_lenient_memory_record_defaults(self, session, mcp_test_setup)
)
assert extracted_memory.memory_type.value == "semantic"

@pytest.mark.asyncio
async def test_search_long_term_memory_rejects_limit_above_maximum(
self, session, mcp_test_setup
):
"""An over-limit search must surface as a tool error, not as no results."""
async with client_session(mcp_app._mcp_server) as client:
with mock.patch(
"agent_memory_server.mcp.core_search_long_term_memory"
) as mock_search:
result = await client.call_tool(
"search_long_term_memory", {"text": "anything", "limit": 101}
)

assert result.isError
assert "limit" in result.content[0].text
mock_search.assert_not_called()

@pytest.mark.asyncio
async def test_search_long_term_memory_with_optimize_query_false_default(
self, session, mcp_test_setup
Expand Down