Nexus-Agent is a modular agentic AI system that combines Model Context Protocol (MCP), LangGraph, semantic RAG, FAISS, SQL reasoning, and FastAPI behind a unified tool-execution architecture.
Instead of hard-coding application logic for every question, the agent dynamically discovers available MCP tools and decides which capabilities are required to answer a user's request.
It can retrieve information from documents, perform structured queries against a finance database, or use multiple tools in a single reasoning workflow.
- MCP-native tool execution
- Dynamic tool discovery through MCP
- LangGraph agent orchestration
- Semantic RAG using FAISS
- Local embeddings with FastEmbed
- SQL MCP server with read-only protection
- Multi-MCP tool selection
- Groq LLM integration
- Recovery from malformed Groq/Llama tool calls
- FastAPI REST API
- Deterministic offline test suite
- 193 automated tests passing
User
│
▼
┌─────────────┐
│ FastAPI │
│ /query │
└──────┬──────┘
│
▼
┌─────────────┐
│ LangGraph │
│ Agent │
└──────┬──────┘
│
▼
┌─────────────┐
│ Groq LLM │
└──────┬──────┘
│
Dynamic MCP Tool Selection
│
┌────────────┴────────────┐
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Document MCP │ │ SQL MCP │
└──────┬───────┘ └──────┬───────┘
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│ RAG / FAISS │ │ SQLite │
└──────┬───────┘ └──────┬───────┘
│ │
└────────────┬───────────┘
│
▼
Tool Results
│
▼
LangGraph
│
▼
Groq LLM
│
▼
Final Answer
The agent is intentionally decoupled from the underlying RAG and database implementations.
LangGraph Agent
│
▼
MCP Client
│
▼
MCP Server
│
▼
Domain implementation
The agent decides which tool to use, while the MCP layer handles tool execution.
Nexus-Agent provides both lexical and semantic document retrieval.
list_documents()
get_document(filename)
search_documents(query)
semantic_search(query, top_k)
Documents
│
▼
Ingestion
│
▼
Chunking
│
▼
Embeddings
│
▼
FAISS Index
│
▼
Semantic Search
│
▼
Relevant Context
│
▼
LLM
│
▼
Grounded Answer
The default embedding configuration uses:
BAAI/bge-small-en-v1.5- 384-dimensional embeddings
- FastEmbed / ONNX Runtime
- FAISS
IndexFlatIP - normalized vectors, making inner product equivalent to cosine similarity
A deterministic hashing embedding provider is also available for fully offline testing.
The RAG pipeline generates a SHA-256 fingerprint from:
- document contents
- chunking configuration
- embedding configuration
The existing FAISS index is reused when the fingerprint is unchanged, avoiding unnecessary re-embedding.
LangGraph manages the agent's reasoning loop.
START
│
▼
Agent Node
│
├── Final answer ───────────► END
│
└── Tool call
│
▼
Tools Node
│
▼
MCP Client
│
▼
Tool Result
│
└──────────────► Agent Node
The graph supports:
- dynamic tool selection
- sequential tool calls
- multiple tool calls
- tool validation
- MCP failure handling
- LLM failure handling
- maximum reasoning rounds
- graceful final responses
Nexus-Agent exposes multiple independent MCP servers.
Provides:
- document listing
- document retrieval
- lexical search
- semantic RAG search
Provides:
query_database(sql)
The LangGraph agent dynamically selects the appropriate MCP tool based on the user's question.
It can also use both MCP servers within the same workflow.
For example:
Explain RAG using the documents and tell me which company has the highest return.
The workflow can become:
Document MCP → RAG information
│
├──────────────┐
│ │
SQL MCP → Financial calculation
│ │
└───────┬──────┘
▼
Combined Answer
The SQL MCP server is intentionally read-only.
Every query passes through two independent layers of protection.
query_guard.py rejects:
INSERTUPDATEDELETEREPLACEDROPALTERCREATETRUNCATEATTACHDETACHPRAGMAVACUUM- transaction-control statements
- multiple SQL statements
- malformed/non-SELECT queries
Forbidden keywords are checked against the raw SQL before comment stripping, preventing prohibited operations from being hidden inside SQL comments.
The database connection is configured with SQLite read-only protections and PRAGMA query_only = ON.
This provides defense in depth rather than relying solely on LLM behavior.
The project includes a deterministic local finance database.
companies
├── company_id
├── ticker
├── name
└── sector
prices
├── price_id
├── ticker
├── trade_date
├── close_price
└── volume
The database is generated deterministically and is intentionally excluded from Git.
For the demo dataset, stock return is calculated as:
((latest_close - earliest_close) / earliest_close) × 100
Using this definition, TSLA has the highest return at 18.0%.
Nexus-Agent uses Groq as its LLM provider.
The LLM provider is abstracted so the agent does not depend directly on a specific API implementation.
The provider also handles malformed tool-call formats occasionally produced by Llama/Groq, including Pythonic function-call syntax such as:
<function=tool_name={...}>
The recovery mechanism is implemented generically rather than being tied to a specific MCP tool.
Nexus-Agent exposes the agent through:
POST /query
{
"question": "What does RAG mean according to the documents?"
}{
"answer": "...",
"metadata": {
"question": "What does RAG mean according to the documents?"
}
}The API delegates to the LangGraph agent rather than duplicating the agent logic.
Nexus-Agent can combine information from multiple MCP tools in a single request.
What does RAG mean according to the documents?
Example result:
RAG stands for Retrieval-Augmented Generation.
It combines retrieval from an external knowledge base
with generation from a language model.
Which company has the highest return?
Example result:
TSLA — Tesla, Inc. — 18.0%
Explain RAG using the documents, and also tell me
which company has the highest return and its return percentage.
This can require both:
Document MCP → RAG information
SQL MCP → Financial calculation
│
▼
Combined final answer
Start the API:
python -m uvicorn nexusai.api.app:app --reloadThen:
curl -X POST http://127.0.0.1:8000/query \
-H "Content-Type: application/json" \
-d "{\"question\":\"Explain RAG using the documents, and also tell me which company has the highest return and its return percentage.\"}"Example response:
{
"answer": "RAG, or Retrieval-Augmented Generation, is ... The company with the highest return is TSLA with a return percentage of 18.0.",
"metadata": {
"question": "Explain RAG using the documents, and also tell me which company has the highest return and its return percentage."
}
}Nexus-Agent/
│
├── data/
│ ├── documents/
│ │ ├── langgraph_notes.txt
│ │ ├── mcp_overview.txt
│ │ └── rag_basics.txt
│ │
│ └── eval/
│ └── rag_eval_dataset.json
│
├── src/
│ └── nexusai/
│ ├── agent/
│ │ ├── langgraph_agent.py
│ │ └── tool_agent.py
│ │
│ ├── api/
│ │ └── app.py
│ │
│ ├── client/
│ │ └── mcp_client.py
│ │
│ ├── db/
│ │ ├── query_guard.py
│ │ └── sql_database.py
│ │
│ ├── llm/
│ │ └── provider.py
│ │
│ ├── mcp_servers/
│ │ ├── document_server.py
│ │ └── sql_server.py
│ │
│ └── rag/
│ ├── chunking.py
│ ├── config.py
│ ├── embeddings.py
│ ├── evaluation.py
│ ├── ingestion.py
│ ├── pipeline.py
│ └── vector_store.py
│
├── tests/
├── .env.example
├── .gitignore
├── requirements.txt
└── README.md
- Python 3.11+
- Groq API key for live agent execution
Create and activate a virtual environment:
python -m venv .venv
.venv\Scripts\activatepython -m venv .venv
source .venv/bin/activateInstall dependencies:
pip install -r requirements.txtCreate the environment file:
copy .env.example .envcp .env.example .envAdd your Groq API key:
GROQ_API_KEY=your_key_here
Never commit .env to Git.
Set the source path.
set PYTHONPATH=srcexport PYTHONPATH=srcRun the LangGraph agent:
python -m nexusai.agent.langgraph_agentStart FastAPI:
python -m uvicorn nexusai.api.app:app --reloadThe API will be available at:
http://127.0.0.1:8000
The complete automated test suite currently passes:
193 passed
Run:
PYTHONPATH=src pytest tests/ -vThe test suite covers:
- MCP client and server behavior
- Dynamic tool discovery
- LangGraph orchestration
- Multi-MCP workflows
- RAG ingestion and retrieval
- Chunking
- Embeddings
- FAISS vector search
- RAG caching
- RAG evaluation
- SQL database operations
- Read-only SQL validation
- Groq provider behavior
- Malformed tool-call recovery
- FastAPI endpoints
The tests use deterministic and mocked components where appropriate, allowing the majority of the suite to run without a live Groq API call.
Secrets and generated artifacts are excluded from Git:
.env
nexs/
data/index/
data/nexus.db
.pytest_cache/
__pycache__/
The repository contains only .env.example for configuration reference.
The SQL layer additionally applies read-only validation before database execution.
The agent decides which tool to use, while MCP controls how the tool is executed.
Agent
│
▼
MCP Client
│
▼
MCP Server
│
▼
Domain Implementation
RAG, database access, LLM interaction, MCP transport, agent orchestration, and API serving are separated into independent modules.
Core functionality can be tested without relying on live network services or paid APIs.
The SQL layer combines query validation with database-level read-only controls.
The agent communicates through an LLM provider abstraction rather than being tightly coupled to a single API implementation.
The current project intentionally keeps its scope focused.
Current limitations include:
- No persistent conversational memory
- Single-agent LangGraph architecture
- SQLite rather than a production database
- FAISS
IndexFlatIPis intended for the current small corpus - No authentication layer for the FastAPI endpoint
- No production deployment configuration
- No distributed MCP infrastructure
These are deliberate scope boundaries for the current implementation.
This project is intended as a portfolio and learning project.
