Skip to main content
Glama
maxscheijen

MCP Yahoo Finance

by maxscheijen

get_recommendations

Retrieve analyst recommendations for a specific stock symbol using Yahoo Finance data to inform investment decisions.

Instructions

Get analyst recommendations for a given symbol.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
symbolYesStock symbol in Yahoo Finance format.

Implementation Reference

  • The main handler function for the 'get_recommendations' tool. It takes a stock symbol, fetches the Ticker from yfinance, retrieves analyst recommendations, and returns them as a JSON string if it's a DataFrame.
    def get_recommendations(self, symbol: str) -> str:
        """Get analyst recommendations for a given symbol.
    
        Args:
            symbol (str): Stock symbol in Yahoo Finance format.
        """
        stock = Ticker(ticker=symbol, session=self.session)
        recommendations = stock.get_recommendations()
        print(recommendations)
        if isinstance(recommendations, pd.DataFrame):
            return f"{recommendations.to_json(orient='records', indent=2)}"
        return f"{recommendations}"
  • Registers all tools including 'get_recommendations' by generating Tool objects using generate_tool and returning them in list_tools().
    @server.list_tools()
    async def list_tools() -> list[Tool]:
        return [
            generate_tool(yf.get_current_stock_price),
            generate_tool(yf.get_stock_price_by_date),
            generate_tool(yf.get_stock_price_date_range),
            generate_tool(yf.get_historical_stock_prices),
            generate_tool(yf.get_dividends),
            generate_tool(yf.get_income_statement),
            generate_tool(yf.get_cashflow),
            generate_tool(yf.get_earning_dates),
            generate_tool(yf.get_news),
            generate_tool(yf.get_recommendations),
            generate_tool(yf.get_option_expiration_dates),
            generate_tool(yf.get_option_chain),
        ]
  • The dispatch case in call_tool() that handles invocation of 'get_recommendations' by calling the handler with arguments and returning the result as TextContent.
    case "get_recommendations":
        recommendations = yf.get_recommendations(**args)
        return [TextContent(type="text", text=recommendations)]
  • Helper function that dynamically generates the MCP Tool schema (including inputSchema from function signature and docstring) for 'get_recommendations' and other tools.
    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/maxscheijen/mcp-yahoo-finance'

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