Skip to content

Commit 9705425

Browse files
author
Andrew Brookins
committed
Fix MCP search_long_term_memory missing background_tasks parameter
The MCP server's search_long_term_memory tool was failing with: 'search_long_term_memory() missing 1 required positional argument: background_tasks' The core API function requires a HybridBackgroundTasks parameter, but the MCP function was not passing it. This fix creates a HybridBackgroundTasks instance and passes it to the core function, following the same pattern used by other MCP functions like memory_prompt. Also adds a regression test to ensure background_tasks is always passed. Fixes #85
1 parent a5ac7fb commit 9705425

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

agent_memory_server/mcp.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,12 @@ async def search_long_term_memory(
521521
limit=limit,
522522
offset=offset,
523523
)
524+
# Create a background tasks instance for the MCP call
525+
from agent_memory_server.dependencies import HybridBackgroundTasks
526+
527+
background_tasks = HybridBackgroundTasks()
524528
results = await core_search_long_term_memory(
525-
payload, optimize_query=optimize_query
529+
payload, background_tasks=background_tasks, optimize_query=optimize_query
526530
)
527531
return MemoryRecordResults(
528532
total=results.total,

tests/test_mcp.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ async def test_default_namespace_injection(self, monkeypatch):
180180
# Capture injected namespace
181181
injected = {}
182182

183-
async def fake_core_search(payload, optimize_query=False):
183+
async def fake_core_search(payload, background_tasks=None, optimize_query=False):
184184
injected["namespace"] = payload.namespace.eq if payload.namespace else None
185185
# Return a dummy result with total>0 to skip fake fallback
186186
return MemoryRecordResults(
@@ -590,3 +590,39 @@ async def test_memory_prompt_with_optimize_query_true_explicit(
590590
call_args = mock_prompt.call_args
591591
optimize_query = call_args[1]["optimize_query"]
592592
assert optimize_query is True
593+
594+
@pytest.mark.asyncio
595+
async def test_search_long_term_memory_passes_background_tasks(
596+
self, session, mcp_test_setup
597+
):
598+
"""Regression test: MCP search_long_term_memory must pass background_tasks to core API.
599+
600+
This test ensures that the MCP tool correctly passes a HybridBackgroundTasks
601+
instance to the core_search_long_term_memory function, which requires it.
602+
"""
603+
from agent_memory_server.dependencies import HybridBackgroundTasks
604+
605+
async with client_session(mcp_app._mcp_server) as client:
606+
with mock.patch(
607+
"agent_memory_server.mcp.core_search_long_term_memory"
608+
) as mock_search:
609+
mock_search.return_value = MemoryRecordResults(total=0, memories=[])
610+
611+
# Call search_long_term_memory via MCP
612+
await client.call_tool(
613+
"search_long_term_memory",
614+
{"text": "test query"},
615+
)
616+
617+
# Verify search was called with background_tasks parameter
618+
mock_search.assert_called_once()
619+
call_args = mock_search.call_args
620+
621+
# background_tasks should be passed as a keyword argument
622+
assert "background_tasks" in call_args[1], (
623+
"background_tasks parameter must be passed to core_search_long_term_memory"
624+
)
625+
background_tasks = call_args[1]["background_tasks"]
626+
assert isinstance(background_tasks, HybridBackgroundTasks), (
627+
f"background_tasks should be HybridBackgroundTasks, got {type(background_tasks)}"
628+
)

0 commit comments

Comments
 (0)