🏛️ Politician Trading Data Assistant

Ask questions about politician trading data from capitoltrades.com

GitHub

👋 Welcome! I can help you explore politician trading data. Try asking:

"Show me statistics for AAPL"
"What are the assets with high buy momentum?"
"Get politician stats for Nancy Pelosi"
"What are the top traded stocks in the last 90 days?"

🏗️ System Architecture

💻
Frontend UI
GitHub Pages
Static HTML/CSS/JS
→
⚙️
Backend Service
Express.js Server
Hosted on Render
↔
🤖
Hugging Face LLM
Mistral-7B-Instruct
Two-Stage Processing
Uses Conversation History
↕
🤖
Hugging Face LLM
Mistral-7B-Instruct
Two-Stage Processing
Uses Conversation History
📊
Model Context Protocol (MCP)
stdio Transport
capitoltrades.com

How It Works

  1. User Input: You type a question in the chat interface
  2. Security Check: Backend validates API key and CORS headers to ensure requests come from authorized origins
  3. Backend Processing: Express.js server receives the request and loads conversation history from the current session
  4. LLM Stage 1 - Function Detection: Hugging Face LLM analyzes your question along with conversation history to determine if MCP data is needed and which function to call
  5. MCP Data Fetch: If data is needed, backend connects to the MCP Capitol Trades server via stdio transport (spawned as child process). The MCP server web scrapes capitoltrades.com to retrieve politician trading data. If JSON parsing fails, a keyword-based fallback mechanism attempts to detect the appropriate function.
  6. LLM Stage 2 - Data Analysis: The LLM analyzes the scraped data along with the original question and generates a natural language response
  7. History Storage: The conversation (question + response) is saved server-side to history.json file, keyed by user ID. This history is loaded and included in future LLM prompts for context continuity within a session
  8. Display: The formatted response is sent back to the frontend and displayed in the chat

🔒 Security Features

  • CORS Protection: Backend restricts requests to configured GitHub Pages domains only
  • API Key Validation: All requests must include a valid API key in the x-api-key header
  • Rate Limiting: 100 requests per 15 minutes per IP address to prevent abuse

📊 Data Sources

  • MCP Capitol Trades: Web scrapes capitoltrades.com to fetch real-time politician trading data
  • MCP Connection: Backend connects to MCP server via stdio (standard input/output) transport. The MCP server process is spawned by the backend as a child process and communicates through stdin/stdout pipes, enabling direct process-to-process communication without network overhead. Uses a singleton pattern - one MCP client connection is reused across requests.
  • Conversation History: Stored server-side in history.json file, keyed by user ID (from x-user-id header). Each user's history is limited to the last 30 messages. History is loaded and passed to the LLM for context in each request, enabling conversation continuity.
  • Two-Stage LLM Workflow: The system uses a two-stage approach: (1) LLM detects if MCP data is needed and which function to call, (2) If data is fetched, LLM analyzes the data and generates the final response. This separation allows for better error handling and more accurate responses.
  • Fallback Mechanism: If the LLM's JSON response cannot be parsed, the system falls back to keyword-based detection to attempt to identify the appropriate MCP function to call.

Technologies Used

GitHub Pages Render Express.js Hugging Face MCP Protocol Node.js Web Scraping