Marco310 commited on
Commit
1518a95
·
1 Parent(s): 5bbc6a1

feat: 🚀 Evolve to Stateful MCP Architecture with Context Injection Middleware

Browse files

Major architectural overhaul to decouple Agent Logic (Service) from Tool Execution (Server) using the Model Context Protocol (MCP).

Key Highlights:
- **Micro-Architecture**: Migrated local toolkits to a standalone `FastMCP` server process (`mcp_server_lifeflow.py`).
- **Context Engineering**: Implemented an AOP-style interceptor using Python `ContextVars`. This automatically injects `session_id` and `api_key` into MCP tool calls, keeping Agent prompts clean and reducing token usage.
- **Multi-Tenant Safety**: Achieved strict session isolation. User-provided API keys dynamically override system environment variables via the injection layer.
- **Runtime Metaprogramming**: Solved Agno/Phidata internal naming conflicts (`_` prefix) by dynamically patching tool entrypoints at startup.
- **Exclusive Channels**: Configured dedicated MCP pipes for each Agent (`Scout`, `Navigator`, etc.) to prevent tool hallucination.

This commit satisfies all requirements for Hackathon Track 2: MCP in Action.

Files changed (1) hide show
  1. src/infra/client_context.py +5 -0
src/infra/client_context.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ # src/infra/client_context.py
2
+ from contextvars import ContextVar
3
+
4
+ # 建立一個 Async 任務隔離的變數,預設為 None
5
+ client_session_ctx = ContextVar("client_session_id", default=None)