meetsimform/mcp-stock-market
If you are the rightful owner of mcp-stock-market and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to dayong@mcphub.com.
The MCP Stock Market server is a PHP-based Model Context Protocol server that provides real-time stock market data using the Alpha Vantage API, designed for seamless integration with VS Code and other MCP-compatible clients.
MCP Stock Market
A Model Context Protocol (MCP) server written in PHP that provides real-time stock market data using the Alpha Vantage API. This server is designed to integrate seamlessly with VS Code and other MCP-compatible clients.
🚀 Features
📊 Stock Market Tools
get_stock_quote- Get real-time stock quotes with current price, change, and percentagesearch_stocks- Search for stocks by company name or symbolget_company_overview- Get detailed company information, financials, and metricsget_intraday_data- Get intraday price data with configurable intervals (1min to 60min)get_market_status- Check current market status and trading hoursget_top_gainers_losers- Get top performing and worst performing stocks
🔧 Technical Features
- Pure PHP implementation (no external dependencies)
- JSON-RPC 2.0 protocol compliance
- STDIO transport for VS Code integration
- Comprehensive error handling and validation
- Alpha Vantage API rate limit handling
- Structured JSON responses
📋 Requirements
- PHP 7.4+ with the following extensions:
json(for JSON processing)curl(for HTTP requests)
- Internet connection (for Alpha Vantage API access)
- VS Code with MCP extension (for IDE integration)
🛠️ Installation & Setup
1. Clone or Download Files
# Navigate to your project directory
cd path/to/mcp-stock-market
2. Configure Environment Variables
# Copy the environment template
cp .env.example .env
# Edit the .env file and add your Alpha Vantage API key
# Get your free API key at: https://www.alphavantage.co/support/#api-key
nano .env
3. Test PHP Setup
php test.php
This will verify:
- PHP version and extensions
- File permissions
- Environment configuration
- Alpha Vantage API connectivity
- JSON-RPC functionality
4. Make Server Executable
chmod +x server.php
🔌 VS Code Integration
Method 1: Using VS Code MCP Extension
- Open VS Code
- Press
Ctrl+Shift+P(orCmd+Shift+Pon Mac) - Search for "MCP" and select "MCP: Add Server"
- Configure the server:
- Name:
mcp-stock-market - Command:
php - Args:
["./server.php"](or full path to your server.php) - Working Directory: Your project directory path
- Name:
Method 2: Manual Configuration
Add to your VS Code settings.json:
{
"mcp.servers": {
"mcp-stock-market": {
"command": "php",
"args": ["./server.php"],
"cwd": "${workspaceFolder}"
}
}
}
Method 3: Using MCP Configuration File
Use the provided mcp-config.json:
{
"mcpServers": {
"mcp-stock-market": {
"command": "php",
"args": ["./server.php"],
"cwd": "."
}
}
}
📖 Usage Examples
Get Stock Quote
{
"tool": "get_stock_quote",
"parameters": {
"symbol": "AAPL"
}
}
Response:
{
"symbol": "AAPL",
"price": "150.25",
"change": "+2.15",
"changePercent": "+1.45%",
"lastUpdated": "2025-10-07"
}
Search Stocks
{
"tool": "search_stocks",
"parameters": {
"query": "Apple"
}
}
Get Company Overview
{
"tool": "get_company_overview",
"parameters": {
"symbol": "TSLA"
}
}
Get Intraday Data
{
"tool": "get_intraday_data",
"parameters": {
"symbol": "GOOGL",
"interval": "5min"
}
}
Get Market Status
{
"tool": "get_market_status",
"parameters": {}
}
Get Top Gainers/Losers
{
"tool": "get_top_gainers_losers",
"parameters": {}
}
🧪 Testing the Server
Manual Testing
# Start the server
php server.php
# In another terminal, send test requests
echo '{"jsonrpc":"2.0","method":"initialize","id":1,"params":{"protocolVersion":"2024-11-05","capabilities":{}}}' | php server.php
Automated Testing
php test.php
⚙️ Configuration
Alpha Vantage API
- API Key: Configured via
ALPHA_VANTAGE_API_KEYenvironment variable (falls back to demo key if not set) - Base URL:
https://www.alphavantage.co/query - Rate Limits:
- Free: 25 requests/day
- Standard: 75 requests/day
- Premium: 1,200+ requests/day
Server Settings
- Timeout: 10 seconds for API requests
- Data Limit: 20 latest data points for intraday data
- Error Handling: Comprehensive with proper JSON-RPC error codes
🔧 Troubleshooting
Common Issues
-
"Extension not loaded" error
sudo apt-get install php-curl php-json -
"Permission denied" error
chmod +x server.php -
"API Rate Limit" error
- Wait for rate limit reset
- Consider upgrading Alpha Vantage plan
-
"Connection refused" error
- Check internet connection
- Verify firewall settings
Debug Mode
The server logs debug information to STDERR. Monitor with:
php server.php 2> debug.log
📁 File Structure
mcp-stock-market/
├── server.php # Main MCP server implementation
├── test.php # Test and validation script
├── composer.json # PHP project configuration
├── mcp-config.json # MCP client configuration
├── claude_desktop_config.json # Claude Desktop configuration
├── .vscode/
│ ├── settings.json # VS Code workspace settings
│ └── mcp.json # MCP VS Code configuration
└── README.md # This documentation
🔐 Security Considerations
✅ Security Features Implemented:
- Environment Variables: API key is loaded from environment variables (
.envfile or system environment) - Fallback Protection: Uses demo key if no environment variable is set (with warning message)
- Configuration Separation: Sensitive data separated from source code
⚠️ Additional Security Considerations:
- SSL Verification: CURL SSL verification is disabled for development. Enable it for production:
CURLOPT_SSL_VERIFYPEER => true, - Authentication: No authentication is implemented. Add proper authentication for production environments.
- Input Validation: Basic input validation is implemented. Consider enhanced validation for production use.
- Rate Limiting: No client-side rate limiting. Implement rate limiting to avoid API quota exhaustion.
📋 Environment Setup:
# Set via environment variable
export ALPHA_VANTAGE_API_KEY="your-api-key-here"
# Or create .env file (recommended)
cp .env.example .env
# Edit .env and add your API key
📝 API Documentation
MCP Protocol Compliance
- Protocol Version:
2024-11-05 - Transport: STDIO
- Format: JSON-RPC 2.0
- Capabilities: Tools support
Error Codes
-32700Parse error-32601Method not found-32603Internal error- Custom error codes for API-specific issues
🚀 Production Deployment
For production use, consider:
- Environment variables for API keys
- SSL certificate verification
- Rate limiting and caching
- Authentication and authorization
- Comprehensive logging
- Health checks and monitoring
📞 Support
- Alpha Vantage API: Documentation
- MCP Protocol: Specification
🎯 Quick Start Checklist
- Run
php test.phpto verify setup - Make
server.phpexecutable withchmod +x server.php - Add server to VS Code using
Ctrl+Shift+P→ "MCP: Add Server" - Test with a stock quote:
get_stock_quotewith{"symbol": "AAPL"} - Verify real-time data is being returned
🎉 Ready to get real-time stock data in VS Code!