Skip to content

Commit 714f2ae

Browse files
committed
docs: add async client context manager pattern to CLAUDE.md
Document the new context manager pattern for MCP tools and CLI commands to guide future development work. This important architectural change affects all code that makes API calls. Key points documented: - Use get_client() context manager (not module-level client) - Auth happens at client creation, not per-request - Three transport modes: Local (ASGI), CLI cloud (HTTP), Cloud app (factory) - Factory pattern for dependency injection - Deprecated patterns to avoid This ensures future AI sessions and developers follow the correct pattern when working with the codebase. Related to SPEC-16 MCP Cloud Service Consolidation (Phase 0 complete). Signed-off-by: phernandez <paul@basicmachines.co>
1 parent 05b3928 commit 714f2ae

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

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)