ecommerce-mcp-chat-server
by Pkaran26
README.md
# Ecommerce MCP Chat Server Demo with Local LLM
A complete demonstration of connecting a local Large Language Model (LLM) to a NestJS database backend using the **Model Context Protocol (MCP)**. This project allows an AI agent to autonomously execute tools to query a MySQL database in real-time during a chat session.
## 🚀 Features
* **NestJS Backend:** A modular architecture managing Users, Products, and Orders.
* **Database Integration:** Sequelize ORM connected to a local MySQL database.
* **MCP Server:** Exposes database queries natively to AI agents using `@nestjs-mcp/server`.
* **Local AI Agent:** A Node.js CLI chat interface powered by the **Vercel AI SDK** and **Ollama** (`llama3.1:8b`).
* **Agentic Tool Calling:** The LLM autonomously decides when to query the database to answer user questions accurately.
## đź“‹ Prerequisites
Before you begin, ensure you have the following installed:
* **Node.js** (v24+)
* **MySQL** (Running locally on port 3306)
* **Ollama** (Running locally with the `llama3.1:8b` model pulled)
> **Note:** To pull the required model, run `ollama pull llama3.1:8b` in your terminal.
## 🛠️ Installation & Setup
1. **Clone the repository and install dependencies:**
```bash
npm install
```
2. **Database Setup:** Ensure your local MySQL server is running. Create an empty database named **mcp_demo**.
```bash
CREATE DATABASE my_db;
```
(Update the database credentials in src/app.module.ts and src/seed.ts if your MySQL username is not root or if you have a password).
3. **Seed the Database:** Generate dummy data (50 users, 50 products, and 50 orders) to test the AI's querying capabilities.
```bash
npx ts-node src/seed.ts
```
## đź’» Running the Application
This project requires two terminal windows to run simultaneously—one for the NestJS MCP Server, and one for the AI Chat Client.
**Terminal 1:** Start the NestJS MCP Server
Start the backend server so it can expose the database tools via the MCP Streamable HTTP transport.
```bash
npm run start
```
(The server runs on http://localhost:3000 with MCP available at /mcp)
**Terminal 2:** Start the AI Chat Client
Start the interactive command-line interface. The client will connect to the NestJS server, discover the tools, and allow you to chat with the local LLM.
```bash
npx ts-node src/chat.ts
```
## đź’¬ Usage Examples
Once the chat client is running, try asking the agent questions that require database knowledge:
*"What products do we have available?"*
*"Can you give me the details for user ID 5?"*
*"How many orders are in the system?"*
*"Check the price of product ID 12 and tell me if it's more than $500."*
The LLM will pause, call the appropriate NestJS tool, read the database results, and formulate a natural language response.
## đź“‚ Project Structure
*src/user/, src/product/, src/order/ - NestJS modules containing Controllers, Services, Models, and MCP Resolvers.*
**src/chat.ts** - The Vercel AI SDK client implementing the conversational loop.
**src/seed.ts** - Database seeding script.