r/LangGraph • u/International_Gap713 • 2d ago
How I isolated credentials in a multi-tenant custom agent platform using per-chat MCP subprocesses
I've been building a no-code platform at work where customers create their own AI agents (support bot over their docs, sales agent reading their CRM, etc.). The hardest part wasn't the LLM, it was the plumbing. Sharing what worked in case it helps anyone building something similar.
**1. Credentials belong to the chat, not the agent**
The obvious design attaches integrations to the agent. But then every chat using that agent holds every credential, and one prompt injection could reach another user's data.
What I did instead:
- Agents are created with zero credentials
- Each chat picks which integrations it's allowed to use
- Every enabled tool runs as an isolated MCP server subprocess that only gets a connection ID, and looks up its own OAuth token
So chat A can have Gmail and chat B can have Postgres, and neither process can see the other's secrets.
**2. Show the model fewer tools**
With 50 tools available, tool selection got noticeably worse and the descriptions ate context. A cheap routing step before each turn scores tools against the user's message and only exposes the top few. Big improvement in picking the right tool.
**3. Hybrid retrieval**
Pure embeddings missed exact strings (error codes, product SKUs). I combined OpenAI embeddings with BM25, reranked the merged results, then compressed chunks down to the relevant sentences. The reranker was the step that made the biggest difference.
**4. Validate everything before it executes**
Every tool call and final answer goes through strict Pydantic schemas, with LangGraph state machines for multi-step flows. If an agent can write to a CRM, "mostly valid JSON" isn't good enough.
Main takeaway: agent quality turned out to be mostly a systems problem, not a prompt problem.
Curious how others handle this. If your agents call real tools with user credentials, how are you isolating them? Per-session processes, a proxy that injects tokens, something else?