what is Stock Price MCP Server?
The Stock Price MCP Server is a simple Model Context Protocol (MCP) server that provides stock price information using the yfinance library.
how to use Stock Price MCP Server?
To use the Stock Price MCP Server, clone the repository, install the required dependencies, and run the server using the command python stock_price_server.py
. You can then access stock information through various tools or resource-based interfaces.
key features of Stock Price MCP Server?
- Fetch current stock prices for valid ticker symbols.
- Retrieve historical stock data in CSV format.
- Compare prices between different stocks.
- Expose stock data as resources using the pattern
stock://{symbol}
.
use cases of Stock Price MCP Server?
- Retrieving the latest stock price for investment decisions.
- Analyzing historical stock performance over time.
- Comparing stock prices to identify investment opportunities.
FAQ from Stock Price MCP Server?
- What programming language is used for the server?
The server is built using Python 3.7+.
- How do I handle errors when retrieving stock data?
The server includes robust error handling for invalid symbols, network issues, and market closures.
- Is there a license for using this server?
Yes, the server is licensed under the MIT License.
Stock Price MCP Server
A simple Model Context Protocol (MCP) server that provides stock price information using the yfinance library.
Overview
This MCP server offers tools to retrieve and compare stock prices. It demonstrates how to build a functional MCP server with multiple capabilities:
- Fetch current stock prices
- Retrieve historical stock data
- Compare prices between different stocks
- Expose stock data as resources
Features
Stock Price Retrieval
Retrieve the latest stock price for any valid ticker symbol. The server handles market closures and invalid symbols gracefully.
Historical Data Access
Get historical stock data in CSV format for different time periods (1 month by default, but customizable).
Stock Comparison
Compare prices between two different stocks with a human-readable output.
Resource Exposure
Access stock information through a resource-based interface using the pattern stock://{symbol}
.
Installation
- Clone this repository
- Install the required dependencies:
pip install -r requirements.txt
Usage
Starting the Server
Run the server with:
python stock_price_server.py
Available Tools
The server provides the following tools:
get_stock_price(symbol)
: Returns the current price of a stock as a floatget_stock_history(symbol, period)
: Returns historical stock data in CSV formatcompare_stocks(symbol1, symbol2)
: Compares prices between two stocks
Using Resources
Resources are accessible using the pattern stock://{symbol}
which returns formatted price information.
Error Handling
All functions include robust error handling that returns meaningful error messages when data retrieval fails:
- Invalid symbols return appropriate error messages
- Network issues are properly handled
- Market closures are managed with fallback price information
Requirements
- Python 3.7+
- mcp-server (Model Context Protocol)
- yfinance
- FastAPI
- Uvicorn