diff --git a/V0/agent-memory-client/agent_memory_client/client.py b/V0/agent-memory-client/agent_memory_client/client.py index 6c4b093a..c385730e 100644 --- a/V0/agent-memory-client/agent_memory_client/client.py +++ b/V0/agent-memory-client/agent_memory_client/client.py @@ -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) diff --git a/V0/agent_memory_server/mcp.py b/V0/agent_memory_server/mcp.py index 10e7c084..62d4cf01 100644 --- a/V0/agent_memory_server/mcp.py +++ b/V0/agent_memory_server/mcp.py @@ -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) @@ -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 diff --git a/V0/agent_memory_server/models.py b/V0/agent_memory_server/models.py index e7c2a505..00ee7a82 100644 --- a/V0/agent_memory_server/models.py +++ b/V0/agent_memory_server/models.py @@ -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, diff --git a/V0/docs/api.md b/V0/docs/api.md index 32181f16..41b06260 100644 --- a/V0/docs/api.md +++ b/V0/docs/api.md @@ -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 | diff --git a/V0/tests/test_api.py b/V0/tests/test_api.py index 4deb3850..4fab550b 100644 --- a/V0/tests/test_api.py +++ b/V0/tests/test_api.py @@ -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 diff --git a/V0/tests/test_mcp.py b/V0/tests/test_mcp.py index 115c307d..bed382a9 100644 --- a/V0/tests/test_mcp.py +++ b/V0/tests/test_mcp.py @@ -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