Stock Analyzer using MCP Server + AI
AI-powered stock analysis platform that connects financial market data with intelligent natural language insights using OpenAI GPT. Built with a FastAPI MCP server backend and a Next.js frontend, this system analyzes stock trends, displays financial metrics, and generates downloadable PDF stock reports with AI summaries.
⚡ Quick Start Guide
✅ Start Backend (FastAPI MCP Server)
cd stock_analyzer
python3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
uvicorn mcp_server.server:app --reload --port 8000✅ Start Frontend (Next.js UI)
cd frontend
npm install
npm run devNow open your browser ➝ http://localhost:3000
✅ Features
| Feature | Description |
|---|---|
| Stock Metrics | SMA, RSI, Returns, Volatility |
| Data Source | Yahoo Finance (yfinance) |
| AI Summary | GPT-4o-mini (OpenAI) |
| PDF Report | Auto-generated report with chart |
| Frontend UI | Built in Next.js + Tailwind CSS |
| MCP Backend | FastAPI HTTP endpoints |
| Stock Options | RELIANCE, TCS, INFY, HDFCBANK, SBIN |
| Time Periods | 3mo, 6mo, 1y, 5y |
🏗️ Architecture Overview
Frontend (Next.js UI)
│
▼
Backend API (FastAPI MCP Server)
├─ /analyze_stock
├─ /download_report
│
┌──────┴─────────┐
│ │
Yahoo Finance OpenAI GPT📦 Project Directory Structure
stock_analyzer/
├── analyzer.py # Stock analysis logic
├── llm_reporter.py # AI summary using GPT
├── report_pdf.py # PDF report generator
│
├── mcp_server/
│ ├── server.py # FastAPI backend
│ ├── templates.py # Prompt templates
│ ├── traders.py # Buy/Hold/Sell logic
│ ├── mcp_params.py # Config
│
├── frontend/ # Next.js frontend
│ ├── app/page.tsx
│ ├── components/AnalyzerForm.tsx
│ ├── package.json
│
├── reports/ # Generated PDFs + charts
├── .env # Environment file
stock_analyzer/
│
├── analyzer.py
├── llm_reporter.py
├── main.py
│
├── mcp_server/
│ ├── __init__.py
│ ├── server.py # UPDATED (adds CORS + /download_report)
│ ├── mcp_params.py
│ ├── templates.py
│ └── traders.py
│
├── report_pdf.py # NEW (PDF generation helper)
├── reports/ # charts + cached PDFs
│
├── requirements.txt # ensure: fastapi, uvicorn, reportlab
├── .env
│
└── frontend/ # NEW Next.js 14 app
├── package.json
├── next.config.js
├── postcss.config.js
├── tailwind.config.ts
├── tsconfig.json
├── app/
│ ├── globals.css
│ └── page.tsx
└── components/
└── AnalyzerForm.tsx
└── README.md🔧 Environment Setup
Create .env file inside project root:
OPENAI_API_KEY=your_openai_api_key
DEFAULT_STOCK=RELIANCE.NS
DEFAULT_PERIOD=3mo🔌 API Endpoints (Backend)
| Endpoint | Description | Method |
|---|---|---|
/analyze_stock | Returns stock metrics + AI summary | GET |
/download_report | Returns PDF stock report | GET |
Example request:
http://localhost:8000/analyze_stock?symbol=TCS.NS&period=6mo📄 Sample PDF Output
✅ AI Summary ✅ Stock Chart ✅ Metrics Table ✅ Time Period ✅ Downloadable & Shareable Report
📎 *Reports are saved inside /reports*
🚀 Deployment
Deploy Backend
You can deploy the FastAPI server to:
- Render.com
- Railway.app
- Azure App Service
- AWS EC2 or Lightsail
Deploy Frontend (Recommended)
npm install -g vercel
vercel deployUpdate API URL:
NEXT_PUBLIC_API_BASE=https://your-api-url.com🛠️ Troubleshooting
| Problem | Solution |
|---|---|
Address already in use :8000 | Run: lsof -i :8000 then `kill -9 |
| ` | |
| OpenAI API error | Check .env and billing |
| Chart save error | Make sure /reports folder exists |
| CORS error frontend-backend | Allow origin in server.py |
| No stock data | Use .NS suffix for NSE stocks |
✅ Roadmap
- [ ] Deploy on Vercel + Render
- [ ] Add multi-stock comparison
- [ ] Add Bollinger Bands & MACD
- [ ] Add database for user history
- [ ] Add authentication
- [ ] Host as AI microservice
👨💻 Author
Built by Anish Roychowdhury For AI engineering, education & financial data insights.
📜 License
MIT License – Free to use, learn & modify.
