MCP_StockAssistant

AI株アシスタントは、LangGraphとClaude AIを活用し、自然言語クエリを専門のエージェントにルーティングすることで、株式市場の分析や企業情報を提供します。リアルタイムの金融データAPIを使用し、ユーザーの質問に対して迅速かつ正確な情報を提供することが特徴です。

GitHubスター

3

ユーザー評価

未評価

フォーク

0

イシュー

0

閲覧数

2

お気に入り

0

README
🏦 AI Stock Assistant

A comprehensive AI-powered stock market assistant built with LangGraph, Claude AI, and real-time financial data APIs. The system intelligently routes natural language queries to specialized agents, providing comprehensive stock analysis, company insights, and market data.

🎯 System Overview

This AI Stock Assistant features a multi-agent architecture that automatically routes queries to specialized agents:

  • 📈 Stock Agent: Real-time prices, comparisons, historical data, market movers, trading hours
  • 🏢 Equity Insights Agent: Company overviews, analyst ratings, news, insider trading
  • 🧠 Intelligent Router: AI-powered query classification and routing
  • 🔗 LangGraph Integration: Unified conversation flow and state management
🚀 Key Features
Dynamic Multi-Task Routing

The system intelligently handles complex multi-part queries, automatically routing to multiple agents and synthesizing results:

"What's AAPL stock price?" → 📈 Stock Agent
"Tell me about Apple company" → 🏢 Equity Insights Agent  
"Compare AAPL vs TSLA" → 📈 Stock Agent
"Show me insider trading for NVDA" → 🏢 Equity Insights Agent

🆕 MULTI-PART QUERIES:
"Show me top 5 insider trades for NVDA and compare with AMD" 
  → 🏢 Equity Insights (insider trading) + 📈 Stock Agent (comparison) 
  → 🔄 Synthesized into unified response

"What's Tesla price and recent news?" 
  → 📈 Stock Agent (price) + 🏢 Equity Insights (news)
  → 🔄 Combined comprehensive analysis
🧠 LLM-Powered Dynamic Routing
  • Intelligent Task Detection: Automatically identifies multiple tasks in complex queries
  • Dynamic Parameter Selection: Uses Claude AI to select optimal API parameters for any time range
  • Context-Aware Synthesis: Combines results from multiple agents into coherent responses
  • Precise Tool Selection: Only calls tools explicitly requested (no extra information)
🛠️ Available Tools (9 Total)
📈 Stock Agent Tools (5 tools)
Tool Purpose Example Queries 🆕 Dynamic Features
get_stock_data Real-time quotes "What's AAPL price?", "Tesla stock quote" Single stock precision
get_multiple_quotes Stock comparisons "Compare AAPL vs TSLA", "Show me tech stocks" Multi-symbol analysis
get_price_history Historical performance "NVDA performance last 6 months", "AMD yearly trend" 🎯 ANY time range: "past 2 weeks", "past 4 months", "past 18 months"
get_market_movers Top gainers/losers/volume "Show me top gainers", "Most active stocks" Market-wide screening
get_market_hours Trading schedules "Market hours today", "When does market close?" Real-time status
🏢 Equity Insights Agent Tools (4 tools)
Tool Purpose Example Queries 🆕 Enhanced Features
get_company_overview Company information "Tell me about Apple", "What sector is Tesla in?" Sector classification
get_analyst_ratings Analyst recommendations "Analyst ratings for NVDA", "Price targets for AMD" Recent rating changes
get_company_news Recent news articles "Recent news for Apple", "Show me 10 Tesla articles" 🎯 Custom limits: "top 5", "show me 15"
get_insider_trading Insider activity "Insider trading for AAPL", "Show me 5 AMD insider trades" 🎯 Custom limits: "top 5", "show me 10"
🎮 Sample Queries & Responses
🆕 Multi-Part Query Examples

Query: "Show me top 5 insider trades for NVDA and compare stock price with AMD"

📈 Stock Information:
📊 Stock Comparison:
🔴 NVDA: $173.46 (-0.28, -0.16%) Vol: 122,124,335
🟢 AMD: $166.83 (+4.71, +2.91%) Vol: 53,356,600

🏢 Company Insights:
👥 Insider Trading Activity for NVDA (Showing top 5 transactions)

🔴 Huang Jen Hsun 👑
Position: President and CEO
Transaction: Sale on Jul 21 '25
💵 Price: $172.42
📊 Shares: 75,000
💎 Total Value: $12,931,301

📈 Summary: 🟢 Buys: 0 🔴 Sells: 5 ⚡ Other: 0

Query: "What's Tesla price and give me recent news"

📈 Stock Information:
📈 TSLA Quote:
Price: $330.79 (-1.32, -0.40%)
High: $335.50, Low: $328.00, Open: $332.11
Trade Time: 2025-07-25T21:00:00

🏢 Company Insights:
📰 Recent News for TSLA

📄 Jul 25 '25
Tesla Reports Strong Q2 Earnings, Beats Expectations
📍 Source: Reuters

🚀 Jul 24 '25  
Tesla Unveils New Model 3 Refresh with Enhanced Features
📍 Source: TechCrunch
📈 Stock Market Data

Query: "What's Apple stock price?"

📈 AAPL Quote:
Price: $213.88 (-0.52, -0.24%)
High: $215.50, Low: $212.00, Open: $214.20
52W Range: $164.08 - $237.49
Trade Time: 2025-07-23T21:00:00

Query: "Compare AAPL vs TSLA"

📊 Stock Comparison:

🔴 AAPL: $213.88 (-0.52, -0.24%) Vol: 46,836,781
🔴 TSLA: $330.79 (-1.32, -0.40%) Vol: 81,906,154

Query: "Show me top gainers today"

🚀 Top Gainers ($SPX):

1. 🟢 NVDA: $167.03 (+12.45, +8.05%) Vol: 192,489,403
2. 🟢 TSLA: $332.11 (+8.62, +2.67%) Vol: 77,130,475
3. 🟢 AAPL: $214.40 (+1.92, +0.90%) Vol: 46,348,818
🏢 Company Insights

Query: "Tell me about Apple company"

🏢 AAPL - Apple Inc
Exchange: NASDAQ
Sector: ['Technology', 'Consumer Electronics', 'USA']
Company Profile: Available through Finviz

Query: "Show me insider trading for AMD"

👥 Insider Trading Activity for AMD

🔴 **Papermaster Mark D** 👔
   Position: Chief Technology Officer & EVP
   Transaction: Sale on Jul 15 '25
   💵 Price: $155.03
   📊 Shares: 17,998
   💎 Total Value: $2,790,187

📈 **Summary:**
🟢 Buys: 1  🔴 Sells: 4  ⚡ Other: 3
📊 Total Transactions: 8

Query: "What are analyst ratings for NVDA?"

📊 Analyst Ratings for NVDA

🔍 **Recent Analyst Actions:**

⬆️ **Jul 20 '25** | Goldman Sachs
   Upgrade • 🟢 Strong Buy
   💰 Price Target: $180.00

🔄 **Jul 18 '25** | Morgan Stanley  
   Reiterated • 🟢 Overweight
   💰 Price Target: $175.00

📈 **Summary:**
🟢 Buy/Outperform: 15  🟡 Hold: 3  🔴 Sell/Underperform: 0
📊 Total Ratings: 18
🏗️ Architecture
🆕 Dynamic Multi-Agent System with MCP Integration
                     User Query
                         │
┌────────────────────────▼────────────────────────────────┐
│              🧠 Intelligent Router                       │
│    • Multi-task detection and planning                  │
│    • Dynamic routing (single or multiple agents)        │
│    • LLM-powered parameter optimization                 │
└─────┬──────────────────────────────────────────┬───────┘
      │                                          │
      │ ┌──── Multi-Part Query Handling ─────┐   │
      │ │ 1. Task Detection                  │   │
      │ │ 2. Agent Selection                 │   │  
      │ │ 3. Sequential Execution            │   │
      │ │ 4. Result Synthesis                │   │
      │ └────────────────────────────────────┘   │
      │                                          │
┌─────▼──────┐                          ┌──────▼──────────┐
│ 📈 Stock    │                          │ 🏢 Equity        │
│   Agent     │                          │   Insights      │
│             │                          │   Agent         │
│ • Quotes    │                          │ • Company Info  │
│ • History   │ ◄──── Dynamic Params ────┤ • Analyst Data  │
│ • Movers    │       (LLM Selected)     │ • News          │
│ • Hours     │                          │ • Insider       │
└─────┬──────┘                          └──────┬──────────┘
      │                                        │
┌─────▼──────┐                          ┌──────▼──────────┐
│ 📊 Schwab  │                          │ 📰 Finviz       │
│   API      │                          │   API           │
└────────────┘                          └─────────────────┘
      │                                        │
      └──────────┬─── Results ─────┬───────────┘
                 │                 │
           ┌─────▼─────────────────▼─────┐
           │    🔄 Result Synthesizer    │
           │  • Combines multi-agent     │
           │  • Formats unified output   │
           │  • Context-aware responses  │
           └─────────────┬───────────────┘
                         │
                    Final Response
Project Structure
ai-stock-assistant/
├── frontend/                           # 🎨 React frontend application
│   ├── src/
│   │   ├── components/                 # React components
│   │   │   ├── Header.tsx              # App header with branding
│   │   │   ├── Message.tsx             # Chat message bubbles
│   │   │   ├── MessageInput.tsx        # Text input component
│   │   │   ├── QuickActions.tsx        # Suggested action buttons
│   │   │   └── TypingIndicator.tsx     # Loading animation
│   │   ├── hooks/
│   │   │   └── useChat.ts              # Chat state management
│   │   ├── utils/
│   │   │   └── api.ts                  # API communication
│   │   └── types/                      # TypeScript definitions
│   ├── public/                         # Static assets
│   ├── package.json                    # Frontend dependencies
│   └── README.md                       # Frontend documentation
├── backend/
│   ├── app/
│   │   ├── agents/
│   │   │   ├── stock_agent.py           # 📈 Stock market data agent
│   │   │   └── equity_insight_agent.py  # 🏢 Company insights agent
│   │   ├── services/
│   │   │   ├── schwab_client.py         # Schwab API integration
│   │   │   └── finviz_client.py         # Finviz data scraping
│   │   ├── graph/
│   │   │   ├── router_node.py           # 🧠 Intelligent routing
│   │   │   ├── stock_node.py            # Stock agent integration
│   │   │   ├── equity_insight_node.py   # Equity agent integration
│   │   │   └── build_graph.py           # LangGraph configuration
│   │   └── main.py                      # FastAPI + LangGraph server
│   └── requirements.txt
├── langgraph.json                       # LangGraph project config
├── dev.sh                              # Development startup script
├── logs/                               # Debug logs
└── README.md
🔧 Technical Implementation
🆕 Advanced MCP Architecture with Dynamic Routing
Dynamic Multi-Task Router
# Example: "Show me NVDA insider trades and compare with AMD"
router_response = await router_node({
    "input": query,
    "pending_tasks": [],
    "accumulated_results": {}
})
# Returns: ["equity_insights", "stock"] - Multiple tasks detected

# Sequential execution with state management:
# 1. equity_insights → Gets NVDA insider data
# 2. stock → Compares NVDA vs AMD  
# 3. synthesizer → Combines results
LLM-Powered Parameter Selection
# Dynamic time range handling - works with ANY period:
query = "AMD stock over past 7 days"
llm_params = await stock_agent.plan_tools(query)
# Auto-selects: periodType="day", period=10, frequencyType="minute"

query = "Tesla performance past 18 months" 
llm_params = await stock_agent.plan_tools(query)
# Auto-selects: periodType="year", period=2, frequencyType="daily"
State Management & Synthesis
# Multi-agent coordination with accumulated results:
state = {
    "original_query": "NVDA price and insider trades",
    "pending_tasks": ["stock", "equity_insights"],
    "completed_tasks": set(),
    "accumulated_results": {}
}

# After execution:
final_state = {
    "accumulated_results": {
        "stock": "📈 NVDA: $173.46 (-0.28, -0.16%)",
        "equity_insights": "👥 Insider Trading Activity..."
    },
    "completed_tasks": {"stock", "equity_insights"}
}
Enhanced MCP Agent Features
  • Intelligent Planning: LLM analyzes queries and selects optimal tool combinations
  • Dynamic Parameters: Real-time parameter optimization for Schwab API constraints
  • Precise Tool Selection: Only calls explicitly requested tools (no extra data)
  • Result Synthesis: Context-aware combination of multi-agent responses
Enhanced Debug Logging

Every query shows detailed execution flow:

🔧 ROUTER - Input query: 'What's AAPL stock price?'
🔧 ROUTER - Raw LLM response: 'stock'
🔧 ROUTER - Final routing decision: 'stock'

🔧 STOCK AGENT - Input received: 'What's AAPL stock price?'
🔧 STOCK AGENT - Executing TOOL: get_stock_data
   📋 Tool Description: 📈 Get real-time quote and price data for a single stock
   ⚙️  Parameters: {'symbol': 'AAPL'}
   ✅ Tool completed successfully - Got quote for AAPL
🔧 STOCK AGENT - Final output length: 155 chars
🎉 STOCK AGENT - Successfully processed query using 1 tool(s)
Advanced Features
  • JSON Extraction: Robust parsing of LLM responses with fallback logic
  • Intelligent Routing: Context-aware query classification
  • Tool Descriptions: Human-readable tool explanations in debug output
  • Error Handling: Comprehensive error recovery and user feedback
  • Auto-Reload: Development mode with hot reloading
🚀 Getting Started
Prerequisites
  • Python 3.8+
  • Charles Schwab Developer Account
  • Claude API Key
  • LangGraph CLI
Installation
# Clone the repository
git clone <repository-url>
cd ai-stock-assistant

# Install dependencies
pip install -r backend/requirements.txt
pip install langgraph-cli

# Configure environment variables
cp .env.example .env
# Edit .env with your API keys:
# SCHWAB_CLIENT_ID=your_client_id
# SCHWAB_CLIENT_SECRET=your_client_secret
# CLAUDE_API_KEY=your_claude_key
Quick Start (One Command)
# Start all services with one command
./dev.sh

This script automatically:

  • Starts Stock Agent (port 8020)
  • Starts Equity Insights Agent (port 8001)
  • Starts React Frontend (port 3000)
  • Starts LangGraph Development Server (port 2024)
  • Opens LangGraph Studio in your browser
Manual Setup (4 Terminals)
# Terminal 1 - Stock Agent
cd backend
uvicorn app.agents.stock_agent:app --reload --port 8020

# Terminal 2 - Equity Insights Agent  
cd backend
uvicorn app.agents.equity_insight_agent:app --reload --port 8001

# Terminal 3 - Frontend
cd frontend
npm install && npm start

# Terminal 4 - LangGraph Dev Server
langgraph dev
Access Points
🧪 Test Queries
Stock Market Data (Routes to Stock Agent)
# Single stock quotes
"What's AAPL stock price?"
"Give me Tesla quote"
"Show me NVDA current price"

# Stock comparisons  
"Compare AAPL vs TSLA"
"Show me MSFT versus GOOGL"
"Compare Apple, Tesla, and NVIDIA"

# Historical performance
"AAPL performance over last 6 months"
"How has Tesla performed this year?"
"Show me NVDA price history"

# Market movers
"Show me top gainers today"
"What are the biggest losers?"
"Most active stocks by volume"

# Trading hours
"What are market hours today?"
"Is the market open?"
"When does the market close?"
Company Insights (Routes to Equity Insights Agent)
# Company overviews
"Tell me about Apple company"
"What sector is Tesla in?"
"Give me company overview of NVDA"

# Analyst ratings
"What are analyst ratings for AAPL?"
"Show me Tesla recommendations"
"NVDA price targets"

# Company news
"Recent news for Apple"
"Show me 10 Tesla articles"
"Latest NVDA news"

# Insider trading
"Insider trading for AAPL"
"Show me AMD insider activity"
"Give me 5 insider trades for Tesla"
🆕 Dynamic Time Range Queries
# ANY time period supported - LLM selects optimal API parameters
"AMD stock price over the past 2 weeks"
"NVDA performance over the past 4 months"  
"Tesla stock over the past 18 months"
"Apple stock price over the past 3 days"
"Microsoft performance year to date"
"GOOGL stock over the past 2 years"

# Custom limit queries
"Show me top 3 insider trades for AAPL"
"Give me 15 recent news articles for Tesla"
"Show me only 2 analyst ratings for NVDA"
Complex Multi-Agent Queries
# Multi-part queries automatically route to multiple agents
"Show me AAPL price and recent analyst ratings"
"Compare Tesla vs Ford and give me insider trading for both"
"What's NVDA performance over 6 months and show me company news"

# Full analysis (uses all equity tools)
"Give me a full analysis of Apple"
"Tell me everything about Tesla"
"Complete information on NVDA"
📊 API Integration
Data Sources
  • Charles Schwab Market Data API: Real-time quotes, historical data, market movers, trading hours
  • Finviz: Company overviews, analyst ratings, news, insider trading
  • Claude AI: Query understanding and tool orchestration
Supported Assets
  • US Stocks: All NYSE, NASDAQ, OTC equities
  • ETFs: Exchange-traded funds
  • Indices: $SPX, $DJI, $COMPX, NASDAQ
  • International: Major international stocks (limited)
🎯 Use Cases
Individual Investors
  • Quick price checks and comparisons
  • Company research and due diligence
  • Market trend monitoring
  • Trading schedule awareness
Financial Professionals
  • Multi-stock analysis and screening
  • Client portfolio reviews
  • Market intelligence gathering
  • Real-time market monitoring
Developers
  • Financial API integration examples
  • AI agent architecture patterns
  • LangGraph multi-agent systems
  • MCP protocol implementation
🔮 Future Enhancements
Planned Features
  • Options Analysis: Options chains, Greeks, expiration dates
  • Technical Analysis: Chart patterns, indicators, signals
  • Portfolio Tracking: Holdings management and performance
  • Alerts System: Price alerts and news notifications
  • Fundamental Analysis: Financial ratios, earnings data
  • Sector Analysis: Industry comparisons and trends
Technical Improvements
  • Caching Layer: Redis caching for frequently accessed data
  • Rate Limiting: API quota management and optimization
  • Authentication: User management and API key handling
  • WebSocket Support: Real-time data streaming
  • Mobile App: React Native companion app
📈 Performance
  • Query Response Time: < 3 seconds average
  • Concurrent Users: Supports multiple simultaneous queries
  • API Rate Limits: Intelligent request throttling
  • Uptime: 99.9% availability with fallback systems
🛡️ Security
  • API Key Management: Secure environment variable handling
  • Input Validation: Query sanitization and validation
  • Error Handling: Safe error messages without data exposure
  • Access Control: Configurable authentication options

Built with: Python, LangGraph, Claude AI, FastAPI, Charles Schwab API, Finviz, MCP Protocol, asyncio

Created by: Advanced AI system architecture with intelligent multi-agent coordination

🚀 Ready to analyze the markets? Start with: ./dev.sh

作者情報

0

フォロワー

16

リポジトリ

0

Gist

5

貢献数

トップ貢献者

スレッド