AI agent · Model Context Protocol
Multiple MCP Tools
An agent that takes a sales CSV and a question, loads the data into MongoDB through MCP and answers with numbers computed by the database.
- Two MCP servers and a local tool in the same agent
- Aggregation in MongoDB instead of math done by the model
- Instrumented run: time, calls, tokens and cost
- Type
- Personal project
- Period
- Dec 2026
- Stack
- TypeScript, LangGraph, MCP, MongoDB

Architecture overview
01Input
POST /chat — Input
CSV and a free-text question
02Intent
intentParser — Language model
structured output with Zod: intent, fileContent, fileType
not understood → ends with a clear message, no tools
03Agent
ReAct agent — Language model
sequence set by the system prompt; 60-step limit
04Tools
csv_to_json — Deterministic code
local tool
MongoDB MCP — Data
insert-many in batches of 25, count, aggregate
Filesystem MCP — Service or infrastructure
write_file in reports/
05Output
Report — Output
ranking, criterion used, ties and caveats
- Input and output
- Deterministic code
- Language model
- Data
- Service or infrastructure
01Context and problem
Pasting a CSV into a prompt and asking for “the top 5 best-selling products” tends to produce a well-written, confident answer with the wrong sum. A language model is not a calculator.
02Goal
Have the model orchestrate real tools instead of doing the math: the numbers come from database aggregations, and the model only interprets the result and writes the report.
03How it works
- 1.
Separate the question from the data
intentParsercalls the model with a Zod schema and gets back a typed object with the intent, the file content and the type (csv,jsonorunknown). No tool is exposed in this node. - 2.
Load the tools
The MongoDB and filesystem MCP servers run as stdio subprocesses, started on demand, and are loaded with
@langchain/mcp-adapterstogether with the localcsv_to_jsontool. - 3.
Run the sequence
The agent clears the collection, converts the CSV, inserts the records in batches of 25 while checking the count between them, queries with
$groupand$sortand saves the report to disk. - 4.
Report with caveats
The report states the criterion used — for example, that without a quantity column each row counts as one unit — and the ties at the edge of the ranking.
Inside one run
- Total time
- 41.7 s
- Model calls
- 15
- Tool uses
- 16
- Cost
- US$0.0086
Measured in a real run with LangChain callbacks, as recorded in the project README.
04Technical architecture
A two-node StateGraph served by Fastify. The model configuration lives in a single file, with a fallback list on OpenRouter.
Graph
intentParser→agent. If interpretation fails, the flow ends before any tool is called.MultiServerMCPClient
Combines
mongodb-mcp-serverand@modelcontextprotocol/server-filesystem, exposing 47 tools to the model — fromfindandaggregatetowrite_file.Agent
LangChain’s
createAgentwith the system prompt versioned insrc/prompts/v1/,maxTokensof 8192 and a recursion limit of 60 steps.Infrastructure
MongoDB 8 and mongo-express via Docker Compose; optional tracing in LangSmith to inspect every node, model call and tool call.
05Technologies
- Agent
- LangGraph · LangChain · @langchain/mcp-adapters · Zod
- MCP
- mongodb-mcp-server · server-filesystem
- Server and data
- Node 24 · Fastify 5 · MongoDB 8 · Docker Compose
- Model
- OpenRouter · DeepSeek V4.1 Flash
06Engineering decisions
Numbers come from the database
The agent writes the aggregation pipeline and MongoDB does the math. Two independent runs, with different collection names and paths, reached the same top 5 and the same totals: the wording changes, the numbers don’t.
Intent before action
Separating interpretation from execution keeps an ambiguous message from triggering tools. If the intent node doesn’t understand, the flow stops with a clear message.
Verified batches
Inserts go in batches of 25 documents, with the count checked before the next batch, instead of a single insert with every record.
07Technical challenges
aggregate only accepted vector search
The MongoDB MCP server declared every pipeline stage with
required: ["$vectorSearch"], which made a$groupinvalid. ThefixMongoAggregateSchemafunction rewrites the stage schema when the tools are loaded.JSON truncated mid-insert
With 100 records in a single argument, the model’s response went past the token limit and arrived cut off (
Unterminated string in JSON). The fix combined smaller batches in the prompt with a highermaxTokens.Agent recursion limit
createAgentstops at 25 steps by default, and the batches plus the discovery calls went past that. The limit was raised to 60.Exhausted quota that looked like slowness
With no quota left, the API answered 429 and the client retried with backoff: 279 seconds stalled before the error showed up. The diagnosis is documented in the project’s troubleshooting table.
08What was implemented
- Intent node with structured output
- Agent with two MCP servers and a local tool
- Inserts in verified batches
- Fixes for the aggregate schema and the recursion limit
- Report saved to disk by the agent itself
- Local environment with MongoDB and mongo-express in Docker
09Possible next steps
Future improvements — not implemented yet
- Expose to the model only the tools the flow uses — today 47 schemas are resent on every call.
- A deterministic ingestion tool, so the CSV reaches MongoDB without the model rewriting the records.
- Restrict the filesystem MCP to a dedicated folder.
- Graph tests with a simulated model.
10Code and demo
- Source code on GitHub (opens in a new tab)
No public demo. The repository README explains how to run the project locally.
