ollama-agent-with-mcp

saurav-samantray/ollama-agent-with-mcp

3.2

If you are the rightful owner of ollama-agent-with-mcp and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

The MCP Server and Ollama AI Agent project demonstrates an AI pattern where an autonomous agent uses external tools to answer questions.

Tools
  1. get_weather

    Fetches weather information for a specified location.

  2. get_stock_price

    Retrieves the current stock price for a specified company.

  3. currency_converter

    Converts currency values based on current exchange rates.

  4. calculator

    Performs basic arithmetic calculations.

MCP Server and Ollama AI Agent

This project demonstrates a powerful AI pattern: an autonomous agent that uses external tools to answer questions. It consists of two applications:

  1. MCP Server: A FastAPI server that provides a set of tools (Weather, Stocks, Currency, Calculator) via a REST API.
  2. AI Agent: A command-line agent powered by a local Ollama LLM that can intelligently decide which tool to use to respond to a user's query.

Prerequisites

  1. Python: You must have Python 3.8+ installed.
  2. Ollama: You must have Ollama installed and running on your machine.
  3. Ollama Model: You need to have a model pulled. We recommend llama3. You can get it by running:
    ollama run llama3
    
    Once you see the >>> prompt, you can type /bye to exit. The model is now available for the agent to use.

Step 1: Set Up and Run the MCP Server

The server provides the tools for the agent.

  1. Navigate to the server directory:

    cd mcp_server
    
  2. Create a virtual environment and install dependencies:

    python -m venv venv
    source venv/bin/activate  # On Windows, use `venv\Scripts\activate`
    pip install -r requirements.txt
    
  3. Run the server:

    python server.py
    

You should see output indicating the server is running on http://127.0.0.1:8000. Keep this terminal window open. You can verify it's working by opening http://127.0.0.1:8000/docs in your browser.

Step 2: Set Up and Run the AI Agent

The agent will connect to the server you just started.

  1. Open a new terminal window.

  2. Navigate to the agent directory:

    cd ai_agent
    
  3. Create a virtual environment and install dependencies:

    python -m venv venv
    source venv/bin/activate  # On Windows, use `venv\Scripts\activate`
    pip install -r requirements.txt
    
  4. Run the agent:

    python agent.py
    

Step 3: Interact with the Agent

Once the agent is running, you will see a > prompt. You can now ask it questions that require it to use its tools. The agent will print its "thought process" in real-time.

Example Questions:

  • What is the weather like in London? (Uses the get_weather tool)
  • What is the price of Apple stock? (Uses the get_stock_price tool)
  • How many US dollars is 100 Euros? (This requires two steps: first getting the exchange rate, then using the calculator)
  • What is 55 times 3.14? (Uses the calculator tool)
  • What is the weather in Tokyo and what is the price of Microsoft stock? (Uses multiple tools)
  • Who is the president of the United States? (The agent should answer this from its own knowledge, without using a tool)