Skip to main content
Glama
marckwei

MCP Yahoo Finance

by marckwei

get_current_stock_price

Retrieve current stock prices using Yahoo Finance data by providing a stock symbol. This tool enables users to access real-time financial information for investment decisions.

Instructions

Get the current stock price based on stock symbol.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
symbolYesStock symbol in Yahoo Finance format.

Implementation Reference

  • The handler function implementing the logic to fetch and format the current stock price for the given symbol using yfinance.Ticker.
    def get_current_stock_price(self, symbol: str) -> str:
        """Get the current stock price based on stock symbol.
    
        Args:
            symbol (str): Stock symbol in Yahoo Finance format.
        """
        stock = Ticker(ticker=symbol, session=self.session).info
        current_price = stock.get(
            "regularMarketPrice", stock.get("currentPrice", "N/A")
        )
        return (
            f"{current_price:.4f}"
            if current_price
            else f"Couldn't fetch {symbol} current price"
        )
  • Registers the get_current_stock_price tool in the MCP server's list_tools() method using generate_tool to create the Tool object.
    generate_tool(yf.get_current_stock_price),
  • Dispatches calls to the get_current_stock_price tool in the MCP server's call_tool() method.
    case "get_current_stock_price":
        price = yf.get_current_stock_price(**args)
        return [TextContent(type="text", text=price)]
  • Helper function that generates the MCP Tool schema (including input schema) from the handler function's signature and docstring, used for get_current_stock_price.
    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