Stores and queries stock price data (OHLCV) and financial news articles with sentiment analysis, using separate collections per stock symbol for efficient data retrieval.
Alpha Vantage MCP Server
A Model Context Protocol (MCP) server that provides access to Alpha Vantage stock price and financial news data through MongoDB storage. This project demonstrates a systematic approach to building production-ready MCP servers.
๐บ Video Tutorial
Watch the complete tutorial on YouTube: Building MCP Servers with Bun
๐ฏ What This Project Does
This MCP server provides two powerful tools for querying financial data:
get_stock_prices- Query stock price data (OHLCV) with automatic API fetching when data is missingget_news- Query financial news articles with sentiment analysis and keyword filtering
The server automatically pulls missing data from the Alpha Vantage API, stores it in MongoDB for fast future queries, and provides graceful error handling with helpful messages.
๐๏ธ Building an MCP Server: The Common Pattern
This project follows a systematic 6-step approach to building MCP servers. You can use this pattern for any MCP server project:
Step 1: Research & Preparation
Identify the data source or API you want to integrate
Study the API documentation and data structures
Plan your data storage strategy (MongoDB, SQLite, files, etc.)
Define what tools your MCP server will expose
In this project:
API: Alpha Vantage API for stocks and news
Storage: MongoDB with separate collections per stock symbol
Tools:
get_stock_pricesandget_news
Step 2: Install Dependencies
Set up your runtime environment (Bun, Node.js, etc.)
Install required packages for MCP, database, and API access
Configure environment variables
Commands:
Step 3: Create Utility Functions
Build database connection and CRUD operations
Create reusable helper functions
Implement proper error handling and type safety
Files created:
src/mongo.ts- MongoDB utilities (connect, findDocuments, upsertDocument, etc.)
Step 4: Implement Core Business Logic
Create functions to interact with your data source/API
Implement data fetching, parsing, and storage
Handle rate limiting and API key rotation
Files created:
src/business.ts- Alpha Vantage API integration (pullStock, pullNews, getKey)
Step 5: Create CLI Tool
Build a command-line interface for testing and manual operations
Implement all major operations as CLI commands
This helps validate your business logic before building the MCP server
Files created:
src/run-stocks.ts- CLI tool using yargs
Step 6: Build the MCP Server
Use the bun-mcp-template as a starting point
Implement tool handlers that leverage your business logic
Add both stdio and SSE transport support
Include comprehensive error handling
Files created:
src/mcp.ts- MCP server implementation
๐ Quick Start
Prerequisites
Bun runtime installed
MongoDB running (default:
mongodb://localhost:27017)Alpha Vantage API key(s) (get free keys at https://www.alphavantage.co/support/#api-key)
Installation
See SETUP-KEYLIST.md for detailed .keylist setup instructions.
๐งช Testing
Run All Tests
Test Specific Features
๐ฅ๏ธ Using the CLI Tool
The CLI provides direct access to all functionality for testing and manual operations.
Pull Data from API
Query Data from MongoDB
CLI Help
CLI Examples
๐ Installing the MCP Server
The MCP server can be installed in various MCP clients like Cursor, Claude Desktop, and Claude Code CLI.
Option 1: Install in Cursor
Add this configuration to your ~/.cursor/mcp.json file:
Note: Update the path to match your actual project location.
Option 2: Install in Claude Desktop
Add this configuration to your Claude Desktop MCP settings file:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%/Claude/claude_desktop_config.json
Option 3: Install in Claude Code CLI
Using the claude mcp add command:
This will automatically configure the MCP server in Claude Code. You can verify it's installed:
Configuring Environment Variables
After installation, you may need to set environment variables:
For Cursor/Claude Desktop: Add to the env object in the JSON config:
For Claude Code CLI: Environment variables are automatically loaded from your project's .env file.
๐ ๏ธ Running the MCP Server Directly
You can also run the MCP server directly for testing:
Stdio Mode (for MCP clients)
SSE Mode (for HTTP clients)
Test MCP Server Manually
๐ MCP Tools Reference
Tool 1: get_stock_prices
Query stock price data with automatic API fetching.
Parameters:
symbol(string, required) - Stock ticker symbol (e.g., "AAPL", "MSFT", "GOOGL")date(string, optional) - Specific date in YYYYMMDD format (e.g., "20251223")
Behavior:
Queries MongoDB first
If not found, automatically pulls from Alpha Vantage API
Returns specific date data or latest 100 prices
Indicates if data was "freshly pulled from API"
Example:
Tool 2: get_news
Query financial news articles with sentiment analysis.
Parameters:
from(string, required) - Start date in YYYYMMDD formatto(string, required) - End date in YYYYMMDD formatkeyword(string, optional) - Filter by keyword in title/summary
Behavior:
Queries news collection in MongoDB
Filters by date range and optional keyword
Returns articles with sentiment scores and ticker sentiment
Example:
๐ฏ Key Features
Auto-Pull Feature
The MCP server automatically fetches missing stock data from the Alpha Vantage API when queried. This provides a seamless experience - users can query any stock symbol without manually pulling data first.
How it works:
User queries a stock symbol
Server checks MongoDB
If not found, automatically calls Alpha Vantage API
Stores data in MongoDB
Returns the data with "(freshly pulled from API)" indicator
Intelligent Error Handling
Missing
.keylistfile: Provides setup instructions and manual pull alternativeInvalid API keys: Clear error messages with documentation links
Rate limit errors: Helpful guidance on API limits
All errors include actionable next steps
Path Resolution
The .keylist file is located using absolute path resolution based on the source file location. This ensures the server works regardless of the working directory.
Rate Limiting
Automatic 5-second delay after API calls
Round-robin key rotation from
.keylistfileRespects Alpha Vantage free tier limits (5 calls/minute, 500 calls/day)
๐ Project Structure
๐ง Configuration
Environment Variables
Create a .env file:
API Keys
Create a .keylist file with your Alpha Vantage API keys (one per line):
See SETUP-KEYLIST.md for detailed setup instructions.
๐ Additional Documentation
MCP-SERVER-README.md - Detailed MCP server documentation
SETUP-KEYLIST.md - Complete .keylist setup guide
AUTO-PULL-FEATURE.md - Auto-pull feature documentation
ERROR-HANDLING-UPDATE.md - Error handling details
PATH-RESOLUTION-FIX.md - Path resolution implementation
๐ค Contributing
This project follows a systematic development pattern that can be adapted for other MCP servers. Feel free to use it as a template!
๐ License
This project was created using bun init in bun v1.2.20. Bun is a fast all-in-one JavaScript runtime.
๐ Resources
๐ก Tips
Start with the CLI - Build and test your business logic with a CLI tool before creating the MCP server
Use the template - The bun-mcp-template provides a solid foundation
Test thoroughly - Create test scripts for each component (MongoDB, API, MCP tools)
Document errors - Provide helpful error messages with actionable guidance
Handle edge cases - Consider rate limits, missing data, network errors, etc.
๐ Troubleshooting
MCP Server Not Connecting
Verify MongoDB is running:
mongo --versionor check Docker containerCheck
.envfile has correctMONGODB_CONNECTION_STRINGEnsure the path in MCP config matches your actual project location
Check MCP client logs for specific errors
Auto-Pull Not Working
Verify
.keylistfile exists in project rootCheck API keys are valid (test with CLI:
bun src/run-stocks.ts --pull-stock AAPL)Ensure you haven't hit rate limits (5 calls/minute, 500 calls/day)
Check server logs for specific error messages
Path Resolution Issues
The .keylist file must be in the project root. If you get "file not found" errors, verify:
The server uses absolute path resolution, so it should work from any directory.
Built with โค๏ธ using Bun and the Model Context Protocol