Skip to main content
Glama
narumiruna

Yahoo Finance MCP Server

get_top

Retrieve top ETFs, mutual funds, companies, growth companies, or performing companies in a specific sector using input parameters like sector type and number of results.

Instructions

Get top entities (ETFs, mutual funds, companies, growth companies, or performing companies) in a sector.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sectorYesThe sector to get
top_nNoNumber of top entities to retrieve (limit the results)
top_typeYesType of top companies to retrieve

Implementation Reference

  • The core handler function for the 'get_top' MCP tool. Registered via @mcp.tool() decorator. Dispatches to specific helper functions based on the top_type parameter using input schemas Sector and TopType.
    @mcp.tool()
    def get_top(
        sector: Annotated[Sector, Field(description="The sector to get")],
        top_type: Annotated[TopType, Field(description="Type of top companies to retrieve")],
        top_n: Annotated[int, Field(description="Number of top entities to retrieve (limit the results)")] = 10,
    ) -> str:
        """Get top entities (ETFs, mutual funds, companies, growth companies, or performing companies) in a sector."""
        match top_type:
            case "top_etfs":
                return get_top_etfs(sector, top_n)
            case "top_mutual_funds":
                return get_top_mutual_funds(sector, top_n)
            case "top_companies":
                return get_top_companies(sector, top_n)
            case "top_growth_companies":
                return get_top_growth_companies(sector, top_n)
            case "top_performing_companies":
                return get_top_performing_companies(sector, top_n)
            case _:
                return "Invalid top_type"
  • Literal type defining the allowed values for the 'top_type' parameter in the get_top tool.
    TopType = Literal[
        "top_etfs",
        "top_mutual_funds",
        "top_companies",
        "top_growth_companies",
        "top_performing_companies",
    ]
  • Literal type defining the allowed values for the 'sector' parameter in the get_top tool.
    Sector = Literal[
        "Basic Materials",
        "Communication Services",
        "Consumer Cyclical",
        "Consumer Defensive",
        "Energy",
        "Financial Services",
        "Healthcare",
        "Industrials",
        "Real Estate",
        "Technology",
        "Utilities",
    ]
  • Helper function called by get_top for top_type='top_companies'. Fetches top companies DataFrame from yf.Sector and returns JSON.
    def get_top_companies(
        sector: Annotated[Sector, Field(description="The sector to get")],
        top_n: Annotated[int, Field(description="Number of top companies to retrieve")],
    ) -> str:
        """Get top companies in a sector with name, analyst rating, and market weight as JSON array."""
        if top_n < 1:
            return "top_n must be greater than 0"
    
        try:
            s = yf.Sector(sector)
            df = s.top_companies
        except Exception as e:
            return json.dumps({"error": f"Failed to get top companies for sector '{sector}': {e}"})
        if df is None:
            return json.dumps({"error": f"No top companies available for {sector} sector."})
        return df.iloc[:top_n].to_json(orient="records")
Install Server

Other Tools

Related 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/narumiruna/yfinance-mcp'

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