Skip to main content
Glama
dev484p

Agentic AI with MCP

by dev484p

yahoo_finance_search

Retrieve stock data and financial information from Yahoo Finance by entering a stock symbol and optional time period.

Instructions

Get stock information from Yahoo Finance.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
symbolYes
periodNo1mo

Implementation Reference

  • The handler function for 'yahoo_finance_search' tool. It fetches stock data from Yahoo Finance API using the provided symbol and period, extracts key metrics like current price, open, high, low, close, volume, and formats them into a readable string response. The @mcp.tool() decorator registers it with the MCP server.
    @mcp.tool()
    async def yahoo_finance_search(symbol: str, period: str = "1mo") -> str:
        """Get stock information from Yahoo Finance."""
        try:
            valid_periods = ["1d", "5d", "1mo", "3mo", "6mo", "1y", "5y"]
            if period not in valid_periods:
                return f"Invalid period. Must be one of: {', '.join(valid_periods)}"
            
            params = {
                "symbol": symbol,
                "range": period,
                "interval": "1d",
                "includePrePost": "false"
            }
            
            data = await make_api_request(f"{YAHOO_FINANCE_BASE}{symbol}", params=params)
            
            if not data or "chart" not in data or not data["chart"]["result"]:
                return f"Could not retrieve data for symbol {symbol}"
            
            result = data["chart"]["result"][0]
            meta = result["meta"]
            indicators = result["indicators"]["quote"][0]
            
            timestamps = result["timestamp"]
            dates = [datetime.fromtimestamp(ts).strftime('%Y-%m-%d') for ts in timestamps]
            
            latest_idx = -1
            latest_date = dates[latest_idx]
            latest_open = indicators["open"][latest_idx]
            latest_high = indicators["high"][latest_idx]
            latest_low = indicators["low"][latest_idx]
            latest_close = indicators["close"][latest_idx]
            latest_volume = indicators["volume"][latest_idx]
            
            response = [
                f"Stock: {meta['symbol']} ({meta['exchangeName']})",
                f"Currency: {meta['currency']}",
                f"Current Price: {meta['regularMarketPrice']}",
                f"Previous Close: {meta['chartPreviousClose']}",
                "\nLatest Trading Day:",
                f"Date: {latest_date}",
                f"Open: {latest_open}",
                f"High: {latest_high}",
                f"Low: {latest_low}",
                f"Close: {latest_close}",
                f"Volume: {latest_volume}"
            ]
            
            return "\n".join(response)
        except Exception as e:
            logger.error(f"Error in yahoo_finance_search: {e}")
            return f"Failed to retrieve finance data for {symbol} due to an internal error."
  • server.py:129-129 (registration)
    The @mcp.tool() decorator registers the yahoo_finance_search function as an MCP tool.
    @mcp.tool()

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/dev484p/AgenticAI_MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server