Featured
Understanding Model Context Protocol (MCP) : A Full Deep Dive + Working Code — Part 1
Introduction
Large Language Models (LLMs) like GPT-4, Claude, Gemini and Llama3 excel at reasoning , summarizing , and content creation .However, these models operate in isolation — they don’t have built-in access to real-time data, external APIs, or local files.This is where the Model Context Protocol (MCP) comes in.
What is Model Context Protocol (MCP) ?
Model Context Protocol (MCP) is an open protocol that allows Large Language Models (LLMs) to interact with external tools through structured API calls. Think of MCP as a USB-C port for our LLM — plugging it into external services like
- External APIs ,
- Local Database ,
- Local files
- Web Search
MCP acts as a universal interface — a bridge between LLM model and the real world — using simple HTTP requests and JSON responses.
Why MCP ?
MCP provides a simple, standardized way for an LLMs to:
- Discover available tools
- Understand the required input/output structure for each tool
- Call these external tools using structured JSON requests and receive responses
The key benefit of MCP is that it enables LLMs to interact with external tools in much the same way a human developer would call a REST API. This creates a consistent, structured approach for models to access external capabilities beyond their built-in knowledge.
Architecture of Model Context Protocol (MCP)
Core components of MCP is
- MCP Hosts — Applications such as Claude Desktop, IDEs, or AI agents that leverage MCP to interact with external tools and data.
- MCP Clients — Protocol clients that manage 1:1 connections with MCP servers, enabling secure and structured communication.
- MCP Servers — Lightweight, modular services that expose specific capabilities — such as file reading, API querying, or data analysis — via the standardized Model Context Protocol.
- Local Data Sources — Files, databases, and system services on our own machine that MCP servers can access securely to perform tasks.
- Remote Services — External systems or APIs available over the internet that MCP servers can connect to, such as weather APIs, search engines, or third-party data platforms.
How Model Context Protocol Works ?
At its core, MCP operates like a traditional API ecosystem — but tailored for use by LLMs instead of humans. The flow typically looks like this:
1. Tool Discovery: The model queries available tools and their capabilities.
2. Schema Understanding: MCP provides each tool’s input/output schema via JSON.
3. Structured Invocation: The model constructs a JSON payload and invokes the tool.
4. Response Handling: The model receives a structured response to continue reasoning.
API vs MCP: What’s the Difference?
APIs and the Model Context Protocol (MCP) might appear to do the same thing i.e they both provide a way to access external tools or services through structured data exchange .Let’s break it down difference
Traditional APIs Are Built for Humans and Code
APIs (Application Programming Interfaces) are foundational to software development. They let developers or applications exchange information over HTTP using well-defined endpoints and structured formats like JSON or XML.
For example, if we want to get weather data, we might hit this endpoint:
GET https://api.weather.com/v1/current?city=Atlanta&apikey=xyz
As a developer, we are expected to:
- Know the endpoint
- Format the request correctly
- Read the docs (usually Swagger/OpenAPI)
- Parse the response manually
MCP Is Built for Language Models to Use Tools Autonomously
Let us imagine if we’re working with an LLM like Gemma, GPT-4, or Claude and ask:
“What’s the weather in Atlanta?”
The model doesn’t have real-time weather data by default ,in our case using Ollama (Llama3.2) . However, with MCP, it can discover an external tool, understand its schema, format the input, make a structured request, and interpret the response — all on its own.
MCP turns that API into a model-friendly tool, complete with auto-discovery, a predictable schema, and structured interaction.
API is a Toolbox — MCP is a Smart Assistant Using the Toolbox
Difference between Function Calling vs Model Context Protocol (MCP)
In my previous article , I mentioned Function Calling allows model to act as a bridge between natural language and real-world actions and data. i.e Function calling describe external functions in JSON format, and the model will decide when and how to call them.
A basic function call example with OpenAI:
{
"name": "get_weather",
"description": "Returns weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string"}
},
"required": ["city"]
}
}
When we prompt the model with:
“What’s the weather like in Atlanta?”
It responds with:
{
"name": "get_weather",
"arguments": {
"city": "Atlanta"
}
}
Model Context Protocol takes this a step further. It defines a standardized way for LLMs to discover, call, and use tools, often hosted on local or remote servers, via HTTP + JSON RPC2.0.
Function calling is like giving LLM a calculator app.
MCP is like giving LLM an App Store — which can browse, discover, and use any available tool on its own.
When to use Function Calling vs Model Context Protocol (MCP) ?
Function Calling to be used when:
- We require simple tools
- We control the whole application stack
- We’re using cloud models like ChatGPT or Gemini
- We want tight integration in our application logic
MCP to be used when:
- We’re building agentic systems
- We want tools to be modular, reusable, discoverable
- We want local LLMs (like Ollama, Gemma, Mistral) to use external data
- We want scaling to multiple tools or toolchains
Implementation Guide
Let us break into 2 projects
- First — Simple MCP server & client (No LLM)
- Second — Flight Search MCP server + Ollama Client with Llama3
Lets focus on Simple MCP Server to demonstrate how MCP works without LLM
Pre-requisites
Python 3.8+ installed
Let us create Simple MCP Server.
Step 1 : Import required module
Install the FastMCP , llama-index , llama-index-tools-mcp library
pip install mcp llama-index llama-index-tools-mcp requests
What is FastMCP ?
FastMCP is a lightweight framework from the mcp library that:
- Makes it easy to register tools using decorators
- Automatically creates a tool manifest that LLMs can discover
- Exposes our tools over HTTP/SSE so models (or other clients) can call them
"""
Simple MCP server implementation.
"""
from mcp.server.fastmcp import FastMCP
import os
- mcp is the package/namespace for the Model Context Protocol SDK
- server is a submodule containing server-side implementations
- fastmcp is a specific module that provides a FastAPI-based implementation of MCP
- FastMCP is the main class that allows us to create an MCP-compliant server
Step 2 : Create MCP Server Instance
Creates an instance of an MCP server using the FastMCP class
# Create a simple MCP server
mcp = FastMCP("SimpleServer")
Step 3: Register to MCP tool
@mcp.tool()
def hello_world(name: str = "World"):
"""A simple hello world tool."""
return {"message": f"Hello, {name}!"}
@mcp.tool()
def add(a: int, b: int):
"""Add two numbers."""
return a + b
@mcp.tool() is decorator that registers the function below it as an MCP tool.
- Extracts the function name (hello_world) and (add)
- Reads the docstring for the description
- Auto-generates the input schema based on the function signature
- Prepares the tool to receive JSON requests and return JSON responses
Step 4: Start the Server using SSE( Server Side Events )
Start the MCP server using SSE protocol and use client script to test against
if __name__ == "__main__":
print("Starting MCP server on default port...")
mcp.run("sse")
Step 5 : LlamaIndex MCP Integration to connect MCP Server
LlamaIndex’s MCP integration to connect to your MCP server:
McpToolSpec This tool connects to MCP Servers and allows an Agent to call the tools provided by MCP Servers.
BasicMCPClient Handles the MCP connection and protocol details
pip install llama-index-tools-mcp
from llama_index.tools.mcp import BasicMCPClient, McpToolSpec
# MCP server configuration
MCP_URL = "http://localhost:8000/sse"
client = BasicMCPClient(MCP_URL)
print(f"Connecting to MCP server at {MCP_URL}...")
# Get available tools
tools_spec = McpToolSpec(client=client)
tools = await asyncio.wait_for(
tools_spec.to_tool_list_async(),
timeout=10
)
Step 5 : Using MCP Inspector — Visual Testing
The MCP Inspector is an interactive developer tool for testing and debugging MCP servers.
Debug responses using MCP Inspector by running below command
pip install 'mcp[cli]'
mcp dev server.py
GitHub Repo
Full code is available here: github.com/arjunprabhulal/mcp-simple-demo
In Part 2, we’ll take things further and integrate this with Ollama and LLaMA 3 — showing how to use MCP with a local LLM to create a real-world flight search assistant.