Skip to main content
Glama
marckwei

MCP Yahoo Finance

by marckwei

get_stock_price_by_date

Retrieve historical stock prices for specific dates using Yahoo Finance data. Enter a stock symbol and date to get the closing price for that trading day.

Instructions

Get the stock price for a given stock symbol on a specific date.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
symbolYesStock symbol in Yahoo Finance format.
dateYesThe date in YYYY-MM-DD format.

Implementation Reference

  • The handler function in the YahooFinance class that implements the tool logic: creates a yfinance Ticker, fetches 1-day history starting from the given date, and returns the closing price formatted to 4 decimal places.
    def get_stock_price_by_date(self, symbol: str, date: str) -> str:
        """Get the stock price for a given stock symbol on a specific date.
    
        Args:
            symbol (str): Stock symbol in Yahoo Finance format.
            date (str): The date in YYYY-MM-DD format.
        """
        stock = Ticker(ticker=symbol, session=self.session)
        price = stock.history(start=date, period="1d")
        return f"{price.iloc[0]['Close']:.4f}"
  • Registers the get_stock_price_by_date tool in the MCP server's list_tools() by passing the handler method to generate_tool.
    generate_tool(yf.get_stock_price_by_date),
  • In the server's call_tool() dispatcher, matches the tool name and invokes the handler with arguments, returning the result as TextContent.
    case "get_stock_price_by_date":
        price = yf.get_stock_price_by_date(**args)
        return [TextContent(type="text", text=price)]
  • Helper function that generates the MCP Tool object (including schema inferred from function signature, docstring, and type annotations) used to register all tools including get_stock_price_by_date.
    def generate_tool(func: Any) -> Tool:
        """Generates a tool schema from a Python function."""
        signature = inspect.signature(func)
        docstring = inspect.getdoc(func) or ""
        param_descriptions = parse_docstring(docstring)
    
        schema = {
            "name": func.__name__,
            "description": docstring.split("Args:")[0].strip(),
            "inputSchema": {
                "type": "object",
                "properties": {},
            },
        }
    
        for param_name, param in signature.parameters.items():
            param_type = (
                "number"
                if param.annotation is float
                else "string"
                if param.annotation is str
                else "string"
            )
            schema["inputSchema"]["properties"][param_name] = {
                "type": param_type,
                "description": param_descriptions.get(param_name, ""),
            }
    
            if "required" not in schema["inputSchema"]:
                schema["inputSchema"]["required"] = [param_name]
            else:
                if "=" not in str(param):
                    schema["inputSchema"]["required"].append(param_name)
    
        return Tool(**schema)

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/marckwei/no-use-tools'

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