Spaces:
Running
Running
Objective
Use the existing MCP server as the single source of truth for tools/resources/prompts, and have the LangGraph agent call MCP rather than local Python tool implementations.
Rationale
- Single source of truth for schemas/behavior (no drift).
- Multi-client support out of the box (Claude Desktop via supergateway, others).
- Clean security boundary via the auth proxy (no local secrets in clients).
Architecture
- MCP server:
python -m foodwise.mcp_server
(HTTP transport, token-gated viaauth_proxy
at/mcp/
). - Agent: uses Adapter Tools that delegate to MCP over HTTP instead of importing
foodwise.agent.tools
. - Feature flag:
USE_MCP_TOOLS=1
toggles MCP-backed adapters vs. current local tools during migration.
Transport & Auth
- Prefer Authorization header:
Authorization: Bearer $MCP_AUTH_TOKEN
. - Desktop bridge compatibility:
?key=$MCP_AUTH_TOKEN
supported (bridge only). - Health check:
GET /health
(no auth) before first tool call. - Agent should never embed tokens in URLs; use header-based auth.
Implementation Plan
Verify server parity
fastmcp_tools.py
exposes inventory + shopping tools with typed params and clear descriptions.fastmcp_resources.py
andfastmcp_prompts.py
registered inmcp_server/main.py
(already done).
Add MCP client wrapper (Python)
- File:
src/foodwise/mcp_client/client.py
- Responsibilities:
- Manage base URL, auth header/query param
- Discover tool schemas and call tools over HTTP transport
- Provide minimal typed helpers using Pydantic models (derive from
database/db_models.py
or shared models viamodel_dump
) [[Prefer Pydantic model_dump]].
- File:
Agent tool adapters
- File:
src/foodwise/agent/mcp_tools.py
- Define LangChain/LangGraph
Tool
objects that call the MCP client - Maintain current tool names/semantics for drop-in replacement
- Switch
agent/agent.py
to useget_mcp_tools()
whenUSE_MCP_TOOLS=1
- File:
Prompts and resources usage
- Keep agent system prompt in
agent/prompts.py
unchanged - MCP prompts remain for MCP clients; agent doesn’t need them directly
- Optionally fetch MCP resources at the start of an interaction to seed context (later optimization)
- Keep agent system prompt in
Config/env (do not edit pyproject directly)
.env
/env.example
additions:- Production endpoint (HF Spaces):
MCP_ENDPOINT=https://<your-space>.hf.space/mcp/
- Local dev alternative:
MCP_ENDPOINT=http://127.0.0.1:7860/mcp/
- Auth:
MCP_AUTH_TOKEN=...
(orMCP_AUTH_TOKENS
) - Toggle:
USE_MCP_TOOLS=1
- Production endpoint (HF Spaces):
- Do NOT include tokens in the URL; clients should send
Authorization: Bearer
headers.
Tests
- Unit tests for MCP client: auth header, timeouts, error propagation, happy-path tool call
- Agent smoke: run
scripts/agent_smoke.py
withUSE_MCP_TOOLS=1
and proxy running; verify same outputs for read paths
Rollout
- Phase 1: Behind flag in dev; local tools remain fallback
- Phase 2: Make MCP the default; keep fallback briefly
- Phase 3: Remove local tool implementations once parity confirmed
Risks & Mitigations
- Schema drift → Source request/response from shared Pydantic models via
model_dump()
- Network/auth errors → Early
/health
check, clear error messages, retries where sensible
Done Criteria
- With
USE_MCP_TOOLS=1
, agent uses MCP-backed tools for inventory/shopping and passes existing smoke checks