Building an Intelligent Stock Analysis Agent with MCP, Groq LLM, and Multi-Source Data

 

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

┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │ User Query │───▶│ Intelligent │───▶│ MCP Server │ │ (Natural Lang.) │ │ Agent (LLM) │ │ (4 Tools) │ └─────────────────┘ └─────────────────┘ └─────────────────┘ │ │ ▼ ▼ ┌─────────────────┐ ┌─────────────────┐ │ Tavily API │ │ Yahoo Finance │ │ (News Search) │ │ (Stock Data) │ └─────────────────┘ └─────────────────┘ │ ▼ ┌─────────────────┐ │ Groq LLM │ │ (Planning + │ │ Summary) │ └─────────────────┘

✅ Components

  1. MCP Server (Port 8000)
    Hosts stock-related tools exposed via the Model Context Protocol.

  2. Agent Client (Port 8001)
    An LLM-powered agent that:

    • Understands user intent

    • Selects tools

    • Orchestrates workflow

    • Summarizes insights

  3. React UI (Port 3000)
    A simple frontend to query the agent.


🔧 Tools Exposed by the MCP Server

The MCP Server provides 4 main tools:

ToolWhat It Does
get_quoteReal-time stock price + basic metrics
get_stock_historyHistorical data (for trend analysis)
get_company_infoFundamentals, sector, market cap, description
company_newsDuckDuckGo-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

User Query → LLM Planning → Tool Execution → Data Fusion → AI Summary

Quick Start Guide

✅ Prerequisites


1️⃣ Clone the Repository

git clone https://github.com/kkvinodkumaran/mcp_agent_stock_demo cd mcp_agent_stock_demo

2️⃣ Configure .env

GROQ_API_KEY=your_groq_key TAVILY_API_KEY=your_tavily_key

3️⃣ Start With Docker

docker-compose up --build

Access the system:


🔌 API Usage

✅ Analyze Endpoint (LLM-Based)

curl -X POST "http://localhost:8001/analyze" \ -H "Content-Type: application/json" \ -d '{"query": "Analyze Tesla including recent trends"}'

Response includes:

  • LLM reasoning

  • Tools selected

  • Raw data

  • Final AI summary


🛠️ Local Development

MCP Server

cd mcp_stock_server uv sync uv run python server.py

Agent

cd agent_client uv sync uv run uvicorn app.main:app --host 0.0.0.0 --port 8001

UI

cd ui npm install npm start

🔍 How It All Works (Under the Hood)

✅ Step-by-step pipeline

  1. User sends a natural-language query

  2. Groq LLM interprets the intent

  3. Agent selects required MCP tools

  4. Tools fetch data (Yahoo Finance, Tavily, DuckDuckGo)

  5. Agent merges data from all sources

  6. 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:

docker-compose logs agent-client docker-compose logs mcp-server

🧰 Troubleshooting

LLM not selecting tools?

→ Check GROQ_API_KEY.

News not loading?

→ Check TAVILY_API_KEY.

MCP tools not available?

→ Check:

curl http://localhost:8000/list_tools

No comments:

Post a Comment

Model Context Protocol (MCP) — Complete Guide for Backend Engineers

  Model Context Protocol (MCP) — Complete Guide for Backend Engineers Build Tools, Resources, and AI-Driven Services Using LangChain Moder...

Featured Posts