Financial AI agent
Two-person academic project, 2025
- My role
- Backend architecture, orchestration, persistence, delivery, and most frontend integration
- Stack
- Python, FastAPI, FastMCP, React, PostgreSQL, ChromaDB, Ollama, Docker
- Scope
- Proof of concept using projected historical market data
A chat app for managing a simulated portfolio, built as a set of real services. Responses stream in, answers can draw on retrieved documents, tools run through a standard protocol, sessions are saved, and every trade needs an explicit confirmation.
Problem
A chatbot you can look inside
Generating text about finance was only a small part of the job. The agent also had to look up relevant knowledge, tell a request for advice apart from an instruction, call portfolio tools, save state, stream its progress, and ask for confirmation before changing the simulated portfolio.
- 6 containersFive services plus a container for loading the knowledge base, with four persistent volumes.
- 8 MCP toolsPrices, price history, portfolio metrics, single and batch orders, and a reset, all behind a protocol boundary.
- 2-step tradesA trade command becomes a pending action that has to be confirmed.
Architecture
Separate reasoning, retrieval, and actions
The FastAPI orchestrator asks the model whether a message is a trade command or a question, and only treats it as a trade above 0.6 confidence. Questions go through retrieval: ChromaDB holds guides on six asset types and about 200 daily market snapshots, and a small cross-encoder reranks the results. The orchestrator, not the model, calls the MCP tools, so the model never touches the portfolio directly. PostgreSQL holds the portfolio and the chat sessions, and Ollama runs Qwen2.5 for chat and nomic-embed-text for embeddings on the local machine.
Responses and processing events reach the React client as server-sent events.
Ownership
What I did
We were two people on this school project. I was responsible for the backend architecture, agent orchestration, MCP integration, persistence, containers, and tests, and did most of the frontend integration. The commits in the repository are mine.
- Built and refined the FastAPI chat orchestrator, the trade-intent flow, guardrails, error handling, and the server-sent event protocol.
- Moved portfolio state from JSON to PostgreSQL and moved tool execution to FastMCP.
- Wired up RAG queries, citations, visible MCP tool calls, saved sessions, and the React view that shows each processing step.
- Containerized the frontend, inference API, MCP server, knowledge loader, PostgreSQL, and Ollama with Docker Compose.
- Wrote 18 tests for the guardrails, RAG price lookups, and the trade flow, plus database tests for the MCP server.
Engineering
Designed for safety and visibility
- Explicit confirmationA detected trade command becomes a pending action, so the agent can't change the simulated portfolio without the user seeing it.
- Protocol boundaryThe orchestrator calls typed MCP tools over SSE with an API key. The model only classifies and writes text, and never gets access to the database.
- Visible processingThe interface shows RAG queries, tool calls, citations, and progress as it streams, so the agent loop isn't hidden.
- Persistent statePostgreSQL stores portfolios, trades, and sessions. Health checks on the database, Ollama, and the MCP server make the stack start in the right order.
- Input controlsSimple keyword guardrails block off-topic requests and a known prompt-injection phrase. Tests cover them, price lookups, and the confirmation flow.
- ScopePrices are projected from historical data, which makes this a controlled proof of concept. It doesn't give live financial advice or make real trades.