Yahoo Finance MCP Server

repository·main·Indexed 41 days ago

https://github.com/narumiruna/yfinance-mcp

A Model Context Protocol (MCP) server that provides AI assistants with access to real-time and historical financial data via yfinance. It enables querying stock info, analyst price targets, financial statements, news, sector rankings, and options data. The server can generate professional financial charts (Candlestick, VWAP, volume profile) as WebP images and provide historical OHLCV data in markdown tables.

Tokens
7K
Snippets
29
Records
37
Agent score
52%

What's inside yfinance-mcp

  1. Overview of Yahoo Finance MCP Server

    main
    The Yahoo Finance MCP Server is a Model Context Protocol (MCP) server that enables AI assistants to access real-time and historical financial data from Yahoo Finance using the yfinance library. It allows users to query stock information, financial news, sector rankings, and generate professional financial charts directly through an AI chat interface.
  2. Key features of Yahoo Finance MCP Server

    main

    The server provides a wide range of financial data capabilities, including:

    • Stock Data: Company info, financials, valuation metrics, dividends, and trading data.
    • Analyst Data: Consensus targets, estimate/revision trends, recommendation history, and firm-level actions.
    • Financial Statements: Income statement and balance sheet with historical data (e.g., EBIT, Invested Capital).
    • Financial News: Recent news articles and press releases for specific tickers.
    • Search: Finding stocks, ETFs, and news across Yahoo Finance.
    • Sector Rankings: Top ETFs, mutual funds, companies, growth leaders, and top performers by sector.
    • Price History: Historical OHLCV data provided as markdown tables or professional charts.
    • Chart Generation: Candlestick, VWAP, and volume profile charts returned as WebP images.
    • Options Data: Option chains including calls, puts, strike prices, IV, and expiration dates.
    • Ownership Data: Major holders, institutional investors, mutual fund holders, and insider transactions.
    • Fund Look-Through: ETF and mutual-fund holdings, asset classes, sectors, ratings, and operating details.
    • Screeners: Predefined, equity, mutual-fund, and ETF query trees.
  3. Run development tasks (Lint, Format, Test)

    main

    Use the following commands to maintain code quality:

    Linting and Formatting:

    uv run ruff check .
    uv run ruff format .

    Type Checking:

    uv run ty check src tests

    Running Tests:

    uv run pytest -v -s --cov=src tests
  4. Install yfinance-mcp from source

    main

    To run from a local clone, first clone the repository and synchronize dependencies using uv:

    git clone https://github.com/narumiruna/yfinance-mcp.git
    cd yfinance-mcp
    uv sync

    Then, add the following to your MCP client configuration, ensuring you replace /path/to/yfinance-mcp with the absolute path to your local repository directory:

    {
      "mcpServers": {
        "yfmcp": {
          "command": "uv",
          "args": [
            "run",
            "--directory",
            "/path/to/yfinance-mcp",
            "yfmcp"
          ]
        }
      }
    }
  5. Install yfinance-mcp via Docker

    main

    You can run the server in a containerized environment using Docker. Add this configuration to your MCP client settings:

    {
      "mcpServers": {
        "yfmcp": {
          "command": "docker",
          "args": ["run", "-i", "--rm", "narumi/yfinance-mcp"]
        }
      }
    }
  6. Install yfinance-mcp via uv (recommended)

    main

    The recommended way to run the Yahoo Finance MCP server is using uvx. Add the following configuration to your MCP client settings to automatically fetch and run the latest version.

    {
      "mcpServers": {
        "yfmcp": {
          "command": "uvx",
          "args": ["yfmcp@latest"]
        }
      }
    }
  7. Manage option chains with `yfinance_get_option_chain` and `yfinance_get_option_dates`

    main

    To work with options, use these two tools in sequence:

    1. yfinance_get_option_dates(symbol): Returns a JSON array of available expiration dates in YYYY-MM-DD format.
    2. yfinance_get_option_chain(symbol, expiration_date, option_type): Fetches the actual chain data.

    yfinance_get_option_chain Parameters:

    • symbol (str): Stock ticker symbol.
    • expiration_date (str | null): A specific date from the list returned by get_option_dates. If omitted, fetches all available dates.
    • option_type (str, default: 'all'): Which options to return: 'calls', 'puts', or 'all'.
    // Step 1: Find dates
    const dates = await yfinance_get_option_dates({ symbol: 'AAPL' });
    
    // Step 2: Get calls for the first date
    const chain = await yfinance_get_option_chain({
      symbol: 'AAPL',
      expiration_date: dates[0],
      option_type: 'calls'
    });
  8. Avoid misclassifying rate-limit errors due to ticker symbols

    main

    Rate-limit text classifiers may incorrectly identify a ticker as a rate-limit error if the ticker symbol itself contains words associated with rate-limiting. This happens because str(yfinance_exception) includes the ticker symbol in its output.

    Fix: When possible, classify errors using the yfinance debug_info or rationale fields instead of parsing the full exception string.

  9. Display appropriate messages for YFRateLimitError

    main
    When encountering a retryable error that includes YFRateLimitError, avoid telling the user to check their internet connection. Instead, use wording that indicates a temporary network issue or an explicit rate-limit/backoff requirement.
  10. Handle errors in Yahoo Finance options tools

    main

    When using tools related to ticker.options or ticker.option_chain(), certain errors must be categorized specifically to avoid misleading the user.

    If a timeout, connection-related error, or YFRateLimitError occurs, the error must be reported as NETWORK_ERROR. Do not downgrade these specific errors to NO_DATA or a generic API_ERROR.

  11. Test the server with Codex CLI

    main

    If you have cloned the repository and run uv sync, the project includes a .codex/config.toml that registers the local yfmcp server. You can test the server by opening Codex CLI from the repository root and using natural language prompts like:

    • Show VOO ticker info
    • Show VOO price history for the last 5 days
    • Find the ticker symbol for Toyota
    • Get AAPL option expiration dates