Skip to main content

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 🚀

Metadata

Release files for iflow-mcp-rajeevchandra-mcp-client-server-example 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for iflow-mcp-rajeevchandra-mcp-client-server-example 0.1.1
File Size Uploaded
iflow_mcp_rajeevchandra_mcp_client_server_example-0.1.1.tar.gz 4.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for iflow-mcp-rajeevchandra-mcp-client-server-example 0.1.1
File Interpreter ABI Platform
iflow_mcp_rajeevchandra_mcp_client_server_example-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 14.6 kB

Release files / iflow_mcp_rajeevchandra_mcp_client_server_example-0.1.1.tar.gz

Download URL iflow_mcp_rajeevchandra_mcp_client_server_example-0.1.1.tar.gz
Size 4.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b14c6ec9ee7b1be2f5a94df4ecf0e3140779218582f994226b2851bca94db1a9
BLAKE2b-256 checksum
How to use checksums
0ad369a327668df1988a09700b52b1b599fc9c39e402ce6e89fc7316dbce990f
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release files / iflow_mcp_rajeevchandra_mcp_client_server_example-0.1.1-py3-none-any.whl

Download URL iflow_mcp_rajeevchandra_mcp_client_server_example-0.1.1-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
359fda8c74a7f2ac8cb3d0407b9f3a658edba8c4a7febcaf76269c811bc60d40
BLAKE2b-256 checksum
How to use checksums
025df1a56762001e6f060ffebabb6fa214af17cdb3cf2ae9e593eace76934a6c
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page