Skip to main content

MCP Server example with add and multiply tools

Project description

MCP + Ollama Local Tool Calling Example

This project demonstrates how a local AI agent can understand user queries and automatically call Python functions using:

  • Model Context Protocol (MCP)
  • Ollama for running a local LLM (e.g., Llama3)
  • Python MCP Client and Server

🔗 Sequence Diagram

sequenceDiagram
    participant User
    participant MCP_Client
    participant Ollama_LLM
    participant MCP_Server

    User->>MCP_Client: 1) User types: "What is 5 + 8?"
    MCP_Client->>Ollama_LLM: 2) Send available tools + user query
    Ollama_LLM->>Ollama_LLM: 3) Understand query & tool descriptions
    Ollama_LLM->>Ollama_LLM: 4) Select tool: add(a=5, b=8)
    Ollama_LLM->>MCP_Client: 5) Return tool_call
    MCP_Client->>MCP_Server: 6) Execute add(a=5, b=8)
    MCP_Server-->>MCP_Client: 7) Return result: 13
    MCP_Client-->>User: 8) Show final answer: 13

📚 Project Structure

.
├── math_server.py      # MCP Server exposing add() and multiply() tools
├── ollama_client.py    # MCP Client interacting with Ollama
├── README.md           # Project documentation

🛠️ Setup Instructions

1. Install Requirements

pip install "mcp[cli] @ git+https://github.com/awslabs/mcp.git" openai==0.28 httpx

Make sure you have Ollama installed and running.

2. Pull or run an LLM model

ollama run llama3

(Ensure the model you run supports tool calling.)

3. Run the MCP Server

python math_server.py

The server exposes two simple tools:

  • add(a: int, b: int) -> int
  • multiply(a: int, b: int) -> int

4. Run the MCP Client

python ollama_client.py math_server.py

5. Interact!

Example queries:

Query: What is 5 + 8?
Response: 13

Query: Multiply 7 and 9
Response: 63

The MCP client sends the query and available tools to Ollama. The LLM internally decides which tool to use based on the tool descriptions and user intent.


🚀 How It Works

  • MCP Client lists available tools.
  • Sends tools + user query to Ollama LLM.
  • LLM reasons about the best matching tool.
  • LLM generates a tool_call.
  • MCP Client invokes the function via the MCP Server.
  • Final result is returned and displayed.

✅ No manual hardcoding! ✅ Everything runs locally! ✅ Fully autonomous!


📢 Why This Matters

This pattern enables building smart local AI agents that:

  • Understand user intent
  • Dynamically select the correct actions
  • Operate fully offline and locally

It opens doors for:

  • Autonomous developers
  • Local intelligent assistants
  • Secure AI workflows

🏷️ Hashtags for Sharing

#MCP #ModelContextProtocol #Ollama #LocalLLM #FunctionCalling #Python #AI #DeveloperTools #AIEngineering #AutonomousAgents

🙌 Credits


"Smarter AI agents start with understanding how they think!"


Next Steps: Add Streamlit UI or Dockerize this project 🚀

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file iflow_mcp_rajeevchandra_client_server-0.1.0.tar.gz.

File metadata

  • Download URL: iflow_mcp_rajeevchandra_client_server-0.1.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_rajeevchandra_client_server-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a7add3f74ff220f4c1267b9fc5617f4f2873bf0d53a84a8c607e57c332ff6f9f
MD5 21d15fc1bcb8c62e6b21f78245462713
BLAKE2b-256 9ab62f9efb0a7d794bf3af6a3dcb4b485c13a26b2e7c8bb6151b3d13e01400a0

See more details on using hashes here.

File details

Details for the file iflow_mcp_rajeevchandra_client_server-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: iflow_mcp_rajeevchandra_client_server-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_rajeevchandra_client_server-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ba38d408a3de51663d36d90466db8e331ec66b0063ff0f2761959f83130377cc
MD5 8a4b54e207a85e631e2c5e8f6f0f6293
BLAKE2b-256 52032bfad07c66bac8c7aff86bd8acd067c3289001968d3f59fd9192fa71297c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page