Skip to content

Commit 1b20e6c

Browse files
phernandezclaude
andcommitted
refactor(mcp): convert search.py prompt to context manager pattern
Convert search_prompt MCP prompt from module-level client import to the new get_client() context manager pattern, enabling proper dependency injection. Changes: - Import get_client() instead of client from async_client module - Wrap function body in 'async with get_client() as client:' block - Indent all code inside context manager (lines 44-57) - Update SPEC-16 checklist (16 of 16 main tools converted - prompts done!) The search_prompt helps users search and explore their knowledge base by: - Searching across all content in basic-memory - Providing helpful context about search results - Supporting optional timeframe filtering (e.g., '1d', '1 week') - Formatting results with rich context for better understanding This conversion maintains the prompt's search functionality while: - Ensuring proper client lifecycle management - Enabling cloud app to inject custom transport via factory - Allowing tests to mock client behavior - Following httpx best practices for async context managers - Moving auth to client creation instead of per-request Testing: - Typecheck passed (0 errors, 0 warnings) - Prompt function properly scoped (client inside async with block) Part of Phase 0.3: Converting 16 MCP tools/prompts to context manager pattern. Related to SPEC-16 MCP Cloud Service Consolidation. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent c951aed commit 1b20e6c

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

specs/SPEC-16 MCP Cloud Service Consolidation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ Convert from `from async_client import client` to `async with get_client() as cl
463463
- [x] `tools/build_context.py` (6/6 tests passing)
464464
- [x] `tools/sync_status.py` (typecheck passed)
465465
- [x] `prompts/continue_conversation.py` (typecheck passed)
466-
- [ ] `prompts/search.py`
466+
- [x] `prompts/search.py` (typecheck passed)
467467
- [ ] `resources/project_info.py`
468468

469469
#### 0.4 Update CLI Commands (~3 files)

src/basic_memory/mcp/prompts/search.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pydantic import Field
1010

1111
from basic_memory.config import get_project_config
12-
from basic_memory.mcp.async_client import client
12+
from basic_memory.mcp.async_client import get_client
1313
from basic_memory.mcp.server import mcp
1414
from basic_memory.mcp.tools.utils import call_post
1515
from basic_memory.schemas.base import TimeFrame
@@ -41,16 +41,17 @@ async def search_prompt(
4141
"""
4242
logger.info(f"Searching knowledge base, query: {query}, timeframe: {timeframe}")
4343

44-
# Create request model
45-
request = SearchPromptRequest(query=query, timeframe=timeframe)
44+
async with get_client() as client:
45+
# Create request model
46+
request = SearchPromptRequest(query=query, timeframe=timeframe)
4647

47-
project_url = get_project_config().project_url
48+
project_url = get_project_config().project_url
4849

49-
# Call the prompt API endpoint
50-
response = await call_post(
51-
client, f"{project_url}/prompt/search", json=request.model_dump(exclude_none=True)
52-
)
50+
# Call the prompt API endpoint
51+
response = await call_post(
52+
client, f"{project_url}/prompt/search", json=request.model_dump(exclude_none=True)
53+
)
5354

54-
# Extract the rendered prompt from the response
55-
result = response.json()
56-
return result["prompt"]
55+
# Extract the rendered prompt from the response
56+
result = response.json()
57+
return result["prompt"]

0 commit comments

Comments
 (0)