Skip to main content
Glama
neeraj-somani

Tavily Web Search MCP Server

get_stock_ohlc

Retrieve Open, High, Low, Close stock price data for a specific symbol on a given date or the last trading day.

Instructions

Get Open, High, Low, Close (OHLC) data for a stock symbol for the last working day or specified date

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
symbolYes
dateNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • server.py:25-29 (handler)
    MCP tool handler for 'get_stock_ohlc', registered via @mcp.tool() decorator. Executes by calling get_stock_data from polygon_api.
    @mcp.tool()
    def get_stock_ohlc(symbol: str, date: str = None) -> str:
        """Get Open, High, Low, Close (OHLC) data for a stock symbol for the last working day or specified date"""
        return get_stock_data(symbol, date)
  • Core helper method in PolygonStockAPI class that performs the actual API call to Polygon.io to retrieve OHLC stock data.
    def get_stock_ohlc(self, symbol: str, date: Optional[str] = None) -> Dict:
        """
        Fetch Open, High, Low, Close (OHLC) data for a stock symbol.
        
        Args:
            symbol: Stock symbol (e.g., 'AAPL', 'MSFT', 'GOOGL')
            date: Date in YYYY-MM-DD format. If not provided, uses last working day.
        
        Returns:
            Dictionary containing OHLC data and metadata
        """
        if not date:
            date = self.get_last_working_day()
        
        # Validate symbol format
        symbol = symbol.upper().strip()
        
        # Polygon.io API endpoint for daily bars
        url = f"{self.base_url}/v2/aggs/ticker/{symbol}/prev"
        
        params = {
            "adjusted": "true",
            "apikey": self.api_key
        }
        
        try:
            response = requests.get(url, params=params, timeout=10)
            response.raise_for_status()
            
            data = response.json()
            
            if data.get("status") != "OK":
                return {
                    "error": f"API Error: {data.get('status', 'Unknown error')}",
                    "symbol": symbol,
                    "date": date
                }
            
            results = data.get("results", [])
            if not results:
                return {
                    "error": f"No data found for symbol {symbol}",
                    "symbol": symbol,
                    "date": date
                }
            
            # Extract OHLC data from the first result
            result = results[0]
            
            return {
                "symbol": symbol,
                "date": date,
                "open": result.get("o"),
                "high": result.get("h"),
                "low": result.get("l"),
                "close": result.get("c"),
                "volume": result.get("v"),
                "volume_weighted_average_price": result.get("vw"),
                "number_of_transactions": result.get("n"),
                "status": "success"
            }
            
        except requests.exceptions.RequestException as e:
            return {
                "error": f"Network error: {str(e)}",
                "symbol": symbol,
                "date": date
            }
        except Exception as e:
            return {
                "error": f"Unexpected error: {str(e)}",
                "symbol": symbol,
                "date": date
            }
  • Wrapper function imported by server.py that instantiates the API client, fetches data, formats it, and returns the string result.
    def get_stock_data(symbol: str, date: Optional[str] = None) -> str:
        """
        Convenience function to get formatted stock data.
        
        Args:
            symbol: Stock symbol (e.g., 'AAPL', 'MSFT', 'GOOGL')
            date: Date in YYYY-MM-DD format. If not provided, uses last working day.
        
        Returns:
            Formatted string with stock OHLC data
        """
        try:
            api = PolygonStockAPI()
            data = api.get_stock_ohlc(symbol, date)
            return api.format_stock_data(data)
        except Exception as e:
            return f"Error initializing API: {str(e)}"
  • Helper method to determine the last working day (weekday) if no date is specified.
    def get_last_working_day(self) -> str:
        """
        Get the last working day (excluding weekends).
        
        Returns:
            Date string in YYYY-MM-DD format
        """
        today = datetime.now()
        
        # Go back to find the last working day
        days_back = 1
        while True:
            check_date = today - timedelta(days=days_back)
            # Monday = 0, Sunday = 6
            if check_date.weekday() < 5:  # Monday to Friday
                return check_date.strftime("%Y-%m-%d")
            days_back += 1
  • Helper method to format the raw stock data dictionary into a human-readable string.
        def format_stock_data(self, data: Dict) -> str:
            """
            Format stock data into a readable string.
            
            Args:
                data: Stock data dictionary from get_stock_ohlc
                
            Returns:
                Formatted string with stock information
            """
            if "error" in data:
                return f"Error fetching data for {data.get('symbol', 'Unknown')}: {data['error']}"
            
            return f"""
    Stock Symbol: {data['symbol']}
    Date: {data['date']}
    Open: ${data['open']:.2f}
    High: ${data['high']:.2f}
    Low: ${data['low']:.2f}
    Close: ${data['close']:.2f}
    Volume: {data['volume']:,}
    VWAP: ${data['volume_weighted_average_price']:.2f}
    Transactions: {data['number_of_transactions']:,}
    """
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It mentions the tool retrieves data but does not specify whether it requires authentication, has rate limits, what happens if the date is invalid or the symbol doesn't exist, or if it's a read-only operation. This leaves significant gaps in understanding the tool's behavior.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, well-structured sentence that efficiently conveys the tool's purpose and key usage details without any unnecessary words. It is front-loaded with the main action and resource, making it easy to understand quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (2 parameters, no annotations, but with an output schema), the description is mostly complete. It covers the purpose and basic usage, and the presence of an output schema means return values are documented elsewhere. However, it lacks details on error handling or behavioral constraints, which could be important for robust use.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The description adds some meaning by clarifying that 'date' is optional and defaults to the last working day, which is not evident from the schema alone (schema description coverage is 0%). However, it does not explain the format of the 'symbol' parameter (e.g., ticker format) or the 'date' parameter (e.g., YYYY-MM-DD), leaving room for ambiguity.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Get') and resource ('Open, High, Low, Close (OHLC) data for a stock symbol'), with precise scope ('for the last working day or specified date'). It effectively distinguishes from sibling tools (roll_dice, web_search) by focusing on financial data retrieval rather than random generation or web search.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides clear context for when to use this tool (to retrieve OHLC data for stocks, either for the last working day or a specified date). However, it does not explicitly state when not to use it or name alternatives for similar financial data queries, which prevents a perfect score.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/neeraj-somani/AIE8-MCP-S13'

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