mcp-server-yahoo-finance

mcp-server-yahoo-finance

0

The MCP Yahoo Finance Server is a tool for fetching and managing stock-related data using Yahoo Finance API. It supports real-time stock data retrieval and historical price analysis. A web-based UI enhances user interaction, combining Azure OpenAI for natural queries.

MCP Yahoo Finance Server and UI

The MCP Yahoo Finance Server is a tool designed to fetch real-time stock data, company information, and historical price data using the Yahoo Finance API. It integrates with the Multi-Client Protocol (MCP) framework to provide seamless communication between clients and the server. The project also includes a web-based UI built using FastAPI and DaisyUI components , enabling users to interact with the server through an intuitive interface

Before proceeding, ensure you have cloned the repository:

git clone https://github.com/quartermaine/mcp-server-yahoo-finance.git
cd <repository-directory>

Table of Contents

  1. Features
  2. Installation
  3. Docker Setup
  4. Usage
  5. Environment Configuration
  6. Integrating with GitHub Copilot
  7. Examples
  8. TODO

Features

  • Fetch current stock prices for any given stock symbol.
  • Retrieve detailed company information (e.g., sector, industry, market cap).
  • Get historical stock price data for a specific date range.
  • Built on the MCP framework for easy integration with clients.
  • Powered by Azure OpenAI for natural language query processing.
  • ncludes a web-based UI built using FastAPI and DaisyUI components for a user-friendly experience

Installation

Prerequisites

Before you begin, ensure you have the following tools installed:

  1. pipx: A tool to install Python applications in isolated environments.
  2. uv: A fast Python package and environment manager.

Steps to Install

1. Install pipx

Update your package list and install pipx:

sudo apt update
sudo apt install pipx
pipx ensurepath
2. Install uv

Install uv using the official installation script:

wget -qO- https://astral.sh/uv/install.sh | sh

After running the above command, make sure to source the environment variables by running:

source $HOME/.local/bin/env
3. Create a Project and Environment

Create a new project directory:

mkdir mcp-client
cd mcp-client

Create a virtual environment using uv:

uv venv

Activate the virtual environment:

source .venv/bin/activate

Install required dependencies:

uv add fastapi jinja2 uvicorn mcp anthropic python-dotenv openai yfinance

Docker Setup

To build and run the application using Docker, follow these steps:

  1. Build the Docker Image:

Use the docker build command to create a Docker image. Replace <tag_name> with a name of your choice (e.g., mcp-finance):

docker build -t <tag_name> .

Example:

docker build -t mcp-finance .
  1. Run the Docker Container :

Use the docker run command to start the container. Map port 8000 on your host machine to port 8000 in the container:

docker run -p 8000:8000 <tag_name>

Example:

docker run -p 8000:8000 mcp-finance
  1. Access the UI :

After running the container, navigate to the URL printed in the terminal logs (e.g., http://0.0.0.0:8000) to access the web-based

Usage

Running the Server

To run the Yahoo Finance MCP server, execute the following command in one terminal with the activated environment:

uv run yahoo_finance.py

Running the UI

In another terminal with the activated environment, start the FastAPI app (UI) using Uvicorn:

uvicorn main:app --reload

After running this command, Uvicorn will print a log message with the URL where the app is running, typically:

INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

Open the provided link (http://127.0.0.1:8000) in your browser to access the UI built with DaisyUI components

Environment Configuration

This project uses Azure OpenAI as the Language Model (LLM) provider. To configure the necessary parameters, follow these steps:

  1. Create a .env File: Copy the .env.example file to create a .env file:

    cp .env.example .env
    
  2. Set Azure OpenAI Parameters: Open the .env file and set the following environment variables:

    AZURE_OPEN_AI_API_VERSION=<your_api_version>
    AZURE_OPEN_AI_ENDPOINT=<your_endpoint>
    AZURE_OPEN_AI_API_KEY=<your_api_key>
    AZURE_OPEN_AI_DEPLOYMENT_MODEL=<your_deployment_model>
    

    Replace <your_api_version>, <your_endpoint>, <your_api_key>, and <your_deployment_model> with your actual Azure OpenAI credentials.

  3. Verify the .env File: Ensure the .env file is correctly formatted and contains all required parameters. Example:

    AZURE_OPEN_AI_API_VERSION=2023-05-15
    AZURE_OPEN_AI_ENDPOINT=https://<your-resource-name>.openai.azure.com/
    AZURE_OPEN_AI_API_KEY=12345abcdef67890ghijklmnopqrstuvwxyz
    AZURE_OPEN_AI_DEPLOYMENT_MODEL=gpt-4
    
  4. Load Environment Variables: The python-dotenv library automatically loads the .env file when running the server or client. Ensure the .env file is located in the root directory of your project.

Integrating with Github Copilot

To integrate the MCP Yahoo Finance Server with GitHub Copilot in Visual Studio Code, follow these steps:

  1. Create a start-mcp.sh script

Create a shell script named start-mcp.sh to activate the virtual environment and start the server. Add the following content to the file:

#!/bin/bash

# Navigate to the project directory
cd /<path_to>/mcp-server-yahoo-finance || { echo "Directory not found"; exit 1; }

# Activate the virtual environment
source .venv/bin/activate || { echo "Failed to activate virtual environment"; exit 1; }

# Run the server
uv run yahoo_finance.py || { echo "Failed to start server"; exit 1; }

Make the script executable:

chmod +x /<path_to>/start_mcp.sh
  1. Configure .vscode/mcp.json

Create a .vscode/mcp.json file in the root of your project directory with the following content:

{
    "servers": {
        "my-mcp-server": {
            "type": "stdio",
            "command": "/<path_to>/start-mcp.sh",
            "args": []
        }
    }
}

This configuration tells VS Code to use the start-mcp.sh script to start the MCP server.

  1. Add the MCP Server to GitHub Copilot

To add the the server to your copilot and use the tools in mcp server

  • Open Copilot in Visual Studio Code.
  • Swith to Agent mode in Chat view
  • Select Tools button to view the tools

see more in the official site


Examples

1. Get Current Stock Price

Query:

Query: Get current price of AAPL

Response:

The current price of AAPL is $202.52

2. Get Company Information

Query:

Query: Get company information for AAPL

Response:

Company Name: Apple Inc.
Sector: Technology
Industry: Consumer Electronics
Country: United States
Market Cap: $2,700,000,000,000
Employees: 164,000
Website: https://www.apple.com

3. Get Historical Data

Query:

Query: Get historical data for AAPL from 2023-01-01 to 2023-01-31

Response:

Date: 2023-01-03
Open: $130.00
High: $132.00
Low: $129.50
Close: $131.75
Volume: 1,234,567

---
Date: 2023-01-04
Open: $131.75
High: $133.00
Low: $130.50
Close: $132.50
Volume: 1,123,456

TODO

  • Enhance Error Handling:

    • Provide more descriptive error messages for invalid inputs or failed API calls.
  • Add Support for Additional APIs:

    • Integrate other financial APIs (e.g., Alpha Vantage, Finnhub) for broader data coverage.
  • Optimize Performance:

    • Cache frequently requested data to reduce API calls and improve response times.
  • Documentation:

    • Add detailed API documentation for developers who want to extend the functionality.

License

This project is licensed under the MIT License. See the file for details.