Prior CoderTech Studio
By Editor
Admin

Model Context Protocol (MCP): What It Is and How to Use It with Ollama

Artificial Intelligence is quickly moving beyond simple chat interfaces. Today, developers want AI systems that can read files, query databases, call APIs, and automate workflows. This is where Model Context Protocol (MCP) comes in.


In this article, you’ll learn:

  • What MCP is (in simple terms)

  • Why it matters

  • How it works

  • A practical example

  • How to use MCP with Ollama



What is MCP?


MCP (Model Context Protocol) is a standard that allows AI models to interact with external tools and data sources.


Without MCP:

  • AI only knows what’s in the prompt

With MCP:

  • AI can fetch real-time data

  • Access databases

  • Read/write files

  • Execute actions



Why MCP is Important


Traditional AI:

  • Static

  • Limited to conversation

MCP-enabled AI:

  • Dynamic

  • Action-driven

  • Context-aware



Lets understand with basic example


Without MCP:

“Show my latest orders”

AI: “I don’t have access to your system.”


With MCP:


AI:

  • Connects to your database

  • Fetches orders

  • Analyzes them

  • Returns insights



How MCP Works


MCP systems typically have three components:


1. Model

The AI model (e.g., CodeGemma, DeepSeek)

2. MCP Server

Provides tools like:

  • File system access

  • Database queries

  • APIs

3. Client / Interface

Connects the model with MCP tools




Flow Diagram (Conceptual)

User → AI Model → MCP Server → Tool/Data → Response → User


Example: MCP in Action

Let’s say you build a system where AI can read local files.

User Prompt:

“Summarize my project README file”

What happens:

  1. AI understands intent

  2. MCP triggers file access tool

  3. Reads README.md

  4. AI summarizes content

  5. Returns response


This is not possible with plain LLM alone.




Using MCP with Ollama


Now let’s get practical.

Step 1: Run Ollama

ollama run deepseek-coder:6.7b

This starts your local AI model.




Step 2: Create a Simple MCP Server (Python Example)

Here’s a basic example of exposing a tool (file reader):

from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/read-file', methods=['POST'])
def read_file():
    data = request.json
    file_path = data.get("path")

    try:
        with open(file_path, "r") as f:
            content = f.read()
        return jsonify({"content": content})
    except Exception as e:
        return jsonify({"error": str(e)})

app.run(port=5001)

This acts as your MCP tool server




Step 3: Connect Ollama via API

Ollama provides a local API:

http://localhost:11434/api/generate

Example request:

curl http://localhost:11434/api/generate -d '{
  "model": "deepseek-coder:6.7b",
  "prompt": "Summarize file at /path/to/file",
  "stream": false
}'


Step 4: Add Tool Logic (MCP Layer)

You create a middleware that:

  1. Detects intent (e.g., “read file”)

  2. Calls MCP server (/read-file)

  3. Sends result back to model

  4. Model generates final answer



Real-World Use Cases


With MCP + Ollama, you can build:


  1. AI Business Dashboard

    1. Connect to MySQL/PostgreSQL

    2. Ask: “Show revenue this month”

  2. AI Coding Assistant

    1. Read your project files

    2. Suggest improvements

  3. File Automation Agent

    1. Organize files

    2. Summarize documents

  4. API Automation Bot

    1. Call external APIs

    2. Combine responses intelligently


Answers & discussion

Sign in to comment.

No comments yet.