Skip to content

Commit 8d2e70c

Browse files
phernandezclaude
andauthored
refactor: async client context manager pattern for cloud consolidation (#344)
Signed-off-by: phernandez <paul@basicmachines.co> Co-authored-by: Claude <noreply@anthropic.com>
1 parent 53438d1 commit 8d2e70c

44 files changed

Lines changed: 9715 additions & 1295 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CLAUDE.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,41 @@ See the [README.md](README.md) file for a project overview.
6767
- Avoid creating mocks in tests in most circumstances.
6868
- Each test runs in a standalone environment with in memory SQLite and tmp_file directory
6969

70+
### Async Client Pattern (Important!)
71+
72+
**All MCP tools and CLI commands use the context manager pattern for HTTP clients:**
73+
74+
```python
75+
from basic_memory.mcp.async_client import get_client
76+
77+
async def my_mcp_tool():
78+
async with get_client() as client:
79+
# Use client for API calls
80+
response = await call_get(client, "/path")
81+
return response
82+
```
83+
84+
**Do NOT use:**
85+
-`from basic_memory.mcp.async_client import client` (deprecated module-level client)
86+
- ❌ Manual auth header management
87+
-`inject_auth_header()` (deleted)
88+
89+
**Key principles:**
90+
- Auth happens at client creation, not per-request
91+
- Proper resource management via context managers
92+
- Supports three modes: Local (ASGI), CLI cloud (HTTP + auth), Cloud app (factory injection)
93+
- Factory pattern enables dependency injection for cloud consolidation
94+
95+
**For cloud app integration:**
96+
```python
97+
from basic_memory.mcp import async_client
98+
99+
# Set custom factory before importing tools
100+
async_client.set_client_factory(your_custom_factory)
101+
```
102+
103+
See SPEC-16 for full context manager refactor details.
104+
70105
## BASIC MEMORY PRODUCT USAGE
71106

72107
### Knowledge Structure

0 commit comments

Comments
 (0)