Skip to content

Commit 48cb4be

Browse files
committed
fix: instrument httpx client at module level for MCP context
The lifespan-based instrumentation only runs when FastAPI app starts. In MCP context, the app never starts but the httpx client is still used. Solution: Instrument the client immediately after creation at module level. This works in both contexts: - MCP: client is instrumented when module is imported - API: client is instrumented before lifespan runs (lifespan still safe) This enables distributed tracing from MCP -> Cloud -> API. Signed-off-by: phernandez <paul@basicmachines.co>
1 parent 3e876a7 commit 48cb4be

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

src/basic_memory/mcp/async_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,15 @@ def create_client() -> AsyncClient:
3838

3939
# Create shared async client
4040
client = create_client()
41+
42+
# Instrument client for distributed tracing when in cloud mode
43+
# This must happen AFTER client creation and works in both MCP and API contexts
44+
config = ConfigManager().config
45+
if config.cloud_mode_enabled:
46+
try:
47+
import logfire # pyright: ignore[reportMissingImports]
48+
49+
logger.info("Cloud mode: instrumenting httpx client for distributed tracing")
50+
logfire.instrument_httpx(client=client)
51+
except ImportError:
52+
logger.warning("logfire not available - skipping httpx instrumentation")

0 commit comments

Comments
 (0)