get_stock_price
Retrieve real-time stock prices for crypto-related companies by providing a ticker symbol. Returns an ASCII table with ticker, company name, price, and timestamp.
Instructions
Get real-time price for a specific stock in a table format.
Args:
ticker (str): The stock ticker symbol (e.g., 'COIN' for Coinbase).
Returns:
str: An ASCII table string containing the ticker, company name, current price, and timestamp.
Returns an error message if the ticker is invalid.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| ticker | Yes |
Implementation Reference
- main.py:50-73 (handler)The handler function decorated with @mcp.tool() that implements the get_stock_price tool. It checks if the ticker is valid using CRYPTO_STOCKS, fetches the current price using yfinance.Ticker, and formats the result as an ASCII table using tabulate.@mcp.tool() def get_stock_price(ticker: str) -> str: """Get real-time price for a specific stock in a table format. Args: ticker (str): The stock ticker symbol (e.g., 'COIN' for Coinbase). Returns: str: An ASCII table string containing the ticker, company name, current price, and timestamp. Returns an error message if the ticker is invalid. """ if ticker not in CRYPTO_STOCKS: return f"Error: Unknown ticker {ticker}" stock = yf.Ticker(ticker) price = stock.info.get("regularMarketPrice", "N/A") table_data = [[ticker, CRYPTO_STOCKS[ticker], price, datetime.now().isoformat()]] return tabulate( table_data, headers=["Ticker", "Name", "Price", "Timestamp"], tablefmt="grid", stralign="left", floatfmt=".2f" )