Building an Intelligent Stock Analysis Agent with MCP, Groq LLM, and Multi-Source Data
A complete walkthrough of my MCP-powered AI agent for real-time stock insights
GitHub Repo: https://github.com/kkvinodkumaran/mcp_agent_stock_demo
🧩 Introduction
In the era of LLM-powered automation, we're moving beyond simple “question → answer” chatbots. Modern AI agents plan, reason, select the right tools, and combine multiple data sources to generate deep, actionable insights.
This project — MCP Stock Analysis Agent — demonstrates how to build a fully intelligent stock-analysis workflow using:
✅ Model Context Protocol (MCP)
✅ Groq LLM (ultra-fast inference)
✅ Multi-API data fusion (Yahoo Finance, Tavily, DuckDuckGo)
✅ LLM-driven tool selection and planning
✅ React UI + FastAPI backend + MCP server
It combines real-time market data, historical trends, company fundamentals, and news sentiment into a single, adaptive AI agent.
What Problem Does This Solve?
Traditional stock apps give you raw data: prices, charts, company descriptions, or scattered news articles. But they don’t answer real questions like:
-
“How is Tesla performing lately?”
-
“What’s the recent news about Apple?”
-
“Show me Microsoft’s long-term price trends.”
-
“Give me a full analysis of Nvidia today.”
Users don’t want to assemble multiple APIs or charts themselves.
✅ We solve this by creating an AI agent that understands your query and automatically chooses the right combination of tools.
This agent:
-
Interprets your natural language
-
Determines what data you actually need
-
Calls the right MCP tools and APIs
-
Combines results
-
Generates a clean, human-level summary
Architecture: How the Intelligent Agent Works
✅ Components
-
MCP Server (Port 8000)
Hosts stock-related tools exposed via the Model Context Protocol. -
Agent Client (Port 8001)
An LLM-powered agent that:-
Understands user intent
-
Selects tools
-
Orchestrates workflow
-
Summarizes insights
-
-
React UI (Port 3000)
A simple frontend to query the agent.
🔧 Tools Exposed by the MCP Server
The MCP Server provides 4 main tools:
| Tool | What It Does |
|---|---|
get_quote | Real-time stock price + basic metrics |
get_stock_history | Historical data (for trend analysis) |
get_company_info | Fundamentals, sector, market cap, description |
company_news | DuckDuckGo-based recent news |
Additionally, the agent can directly call:
✅ Tavily API (Enhanced news search with relevance ranking)
🤖 How the Agent Thinks (LLM-Based Planning & Tool Selection)
The heart of the system is Groq LLM, which interprets queries and builds a plan.
Example 1 — Comprehensive Analysis
User: “Give me a complete analysis of Tesla”
Agent reasoning:
“I need price, company fundamentals, historical trends, and recent news.”
✅ Tools Selected
-
get_quote -
get_company_info -
get_stock_history -
search_news_tavily
Example 2 — News-Focused Query
User: “What’s the recent news about Apple?”
Agent reasoning:
“The user only needs news. No market data required.”
✅ Tool Selected
-
search_news_tavily
Example 3 — Technical Analysis
User: “Show me Microsoft price trends.”
Agent reasoning:
“Trend analysis requires historical + current price.”
✅ Tools Selected
-
get_stock_history -
get_quote
🏗️ System Architecture Overview
✅ Services
-
MCP Server → provides stock tools
-
Agent Client → coordinates LLM and tools
-
React UI → user interface
✅ Workflow
Quick Start Guide
✅ Prerequisites
-
Docker & Docker Compose
-
Groq API Key
-
Tavily API Key
Note
Get Groq API key from https://console.groq.com
Get Tavily API key from https://tavily.com
1️⃣ Clone the Repository
2️⃣ Configure .env
3️⃣ Start With Docker
Access the system:
-
Agent → http://localhost:8001
-
MCP Server → http://localhost:8000
🔌 API Usage
✅ Analyze Endpoint (LLM-Based)
Response includes:
-
LLM reasoning
-
Tools selected
-
Raw data
-
Final AI summary
🛠️ Local Development
MCP Server
Agent
UI
🔍 How It All Works (Under the Hood)
✅ Step-by-step pipeline
-
User sends a natural-language query
-
Groq LLM interprets the intent
-
Agent selects required MCP tools
-
Tools fetch data (Yahoo Finance, Tavily, DuckDuckGo)
-
Agent merges data from all sources
-
Groq LLM generates the final summary
📈 Intelligent Behaviors (Live Examples)
✅ “How is Tesla performing?”
Agent chooses:
-
get_quote -
search_news_tavily
✅ “Give me Tesla's financial details”
Agent chooses:
-
get_company_info -
get_quote
✅ “Analyze Tesla’s price trends”
Agent chooses:
-
get_stock_history -
get_quote
🧱 Why MCP?
MCP (Model Context Protocol) is designed for:
✅ Standardized tools
✅ Dynamic discovery
✅ LLM-friendly interfaces
✅ Easy extensibility
This project shows how to expose your own tools for an AI agent.
🐳 Docker Deployment
-
All three services run in isolated containers
-
Health checks ensure reliability
-
Logs available via:
🧰 Troubleshooting
LLM not selecting tools?
→ Check GROQ_API_KEY.
News not loading?
→ Check TAVILY_API_KEY.
MCP tools not available?
→ Check:
No comments:
Post a Comment