Sponsored by Deepsite.site

MCP + Ollama Local Tool Calling Example

Created By
rajeevchandra8 months ago
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 🚀

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Serper MCP ServerA Serper MCP Server
CursorThe AI Code Editor
ChatWiseThe second fastest AI chatbot™
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
Tavily Mcp
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Playwright McpPlaywright MCP server
DeepChatYour AI Partner on Desktop
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Amap Maps高德地图官方 MCP Server
WindsurfThe new purpose-built IDE to harness magic
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.