Skip to main content
Glama
jyotibasm
by jyotibasm
README.md
# AI Agent with MCP and Ollama: MCP Server for Calculator Application

## Introduction

This project demonstrates how to build a simple **AI Agent using Model Context Protocol (MCP) and Ollama**. The application uses **Ollama to run an LLM locally** and **MCP to connect the AI agent with calculator tools** exposed by an MCP server.

An **AI Agent** is an application that can understand a user's request, determine what action is required, select an appropriate tool, execute that tool, and use the tool's result to generate a response.

**Model Context Protocol (MCP)** provides a standardized way for AI applications to discover and interact with external tools and services. In this project, the MCP server exposes calculator operations such as addition, subtraction, multiplication, and division.

**Ollama** runs the LLM locally and acts as the intelligence behind the agent. Based on the user's request, the LLM can select the appropriate calculator tool and provide the required arguments. The MCP client then invokes that tool on the MCP server and sends the result back to the LLM.

### Project Flow

```text
User
  |
  v
AI Agent / MCP Client
  |
  v
Ollama (Local LLM)
  |
  | Selects appropriate tool
  v
MCP Client
  |
  | Tool call
  v
MCP Server
  |
  +-- add
  +-- subtract
  +-- multiply
  +-- divide
  |
  v
Tool Result
  |
  v
Ollama
  |
  v
Final Answer
```

This project provides a basic understanding of how **LLMs, AI Agents, MCP Clients, and MCP Servers work together** to create tool-using AI applications.

---

## 1. Setup

### Prerequisites

Install:

* Python 3.10+
* Ollama
* Git (optional)

Create a virtual environment:

```powershell
python -m venv venv
```

Activate it:

```powershell
.\venv\Scripts\Activate.ps1
```

Install dependencies:

```powershell
python -m pip install -r requirements.txt
```

---

## 2. Ollama — Run LLM Locally

Ollama allows you to run open-source LLMs locally without requiring an external API.

Pull the model:

```powershell
ollama pull qwen3:8b
```

Verify the model:

```powershell
ollama list
```

You can also run the model directly:

```powershell
ollama run qwen3:8b
```

The Python application communicates with the locally running Ollama service.

---

## 3. Project Folder Structure

```text
MCP_Calculator/
│
├── server.py
├── client.py
├── requirements.txt
├── README.md
└── venv/
```

---

## 4. MCP Server

`server.py` creates the MCP server and exposes calculator operations as tools.

```python
mcp = MCPServer("Calculator")
```

Tools are registered using:

```python
@mcp.tool()
```

The server provides four tools:

```text
add
subtract
multiply
divide
```

For example:

```python
@mcp.tool()
def add(a: float, b: float) -> float:
    return a + b
```

The MCP server is responsible for **hosting and executing the tools**.

---

## 5. MCP Client

`client.py` acts as the bridge between the **Ollama LLM and the MCP server**.

The client:

1. Starts the MCP server.
2. Establishes an MCP session.
3. Discovers available tools using `list_tools()`.
4. Converts MCP tools into tools understood by Ollama.
5. Sends the user's question and available tools to Ollama.
6. Receives the selected tool and arguments from the LLM.
7. Calls the selected MCP tool.
8. Sends the tool result back to Ollama.
9. Displays the final answer.

The LLM does not directly execute the Python functions.

Instead:

```text
Ollama / LLM
     |
     v
MCP Client
     |
     v
MCP Server
     |
     v
Calculator Tool
```

---

## 6. Run the Application

Make sure Ollama is installed and the model is available:

```powershell
ollama list
```

Then run:

```powershell
python client.py
```

Enter a question:

```text
You: What is 10 + 30?
```

The LLM can select:

```text
add
```

with arguments:

```text
a = 10
b = 30
```

The MCP client calls:

```text
add(10, 30)
```

The MCP server returns:

```text
40
```

The result is sent back to Ollama, which generates the final response:

```text
Assistant: 10 + 30 = 40
```

---

## 7. How It Works

```text
                 User
                   |
                   v
              MCP Client
                   |
                   v
                Ollama
                   |
            Tool Selection
                   |
                   v
              MCP Client
                   |
                 stdio
                   |
                   v
              MCP Server
                   |
        +----------+----------+
        |          |          |
       add      subtract   multiply
                              |
                            divide
                   |
                   v
               Result
                   |
                   v
                Ollama
                   |
                   v
              Final Answer
```

### Key Concept

The main components have different responsibilities:

```text
Ollama / LLM
    ↓
Understands the user's request
and selects an appropriate tool.

MCP Client
    ↓
Connects the LLM with MCP tools
and invokes the selected tool.

MCP Server
    ↓
Exposes and executes the actual tools.

Calculator Tools
    ↓
Perform the required calculation.
```

Together, these components form a simple **tool-using AI Agent architecture**.