Friday, 17 October 2025

Model Context Protocol: Local MCP Vs Remote MCP


                            In the world of AI, the Model Context Protocol (MCP) is a game-changer. It's an open-source framework that lets large language models (LLMs) securely connect with external tools and data sources. Think of it as a universal remote for AI agents, allowing them to do more than just generate text—they can interact with the real world, like fetching data from a database, checking your calendar or sending an email. But when it comes to deploying an MCP server, you have two main options: Local MCP and Remote MCP. Which one should you choose?

This guide breaks down the core differences, including the pros and cons of each and provides step-by-step instructions on how to set up both.

NOTE: To get initial understanding on MCP server, refer the below blog post:
Understanding Model Context Protocol(MCP)


Local MCP vs. Remote MCP: The Core Differences

The fundamental difference lies in where the server is hosted and how it communicates with the AI client.

A Local MCP server runs on the same machine as the AI client (e.g., your desktop or laptop). It's great for development and personal use because it's fast, and all communication happens over a local connection, often using stdio (Standard Input/Output). You have full control over the data and tools it can access, as they are on your machine. However, it's limited to that single machine and isn't accessible over the internet.

A Remote MCP server is hosted in the cloud/common server, accessible over the internet/intranet via HTTP or HTTP+SSE. It’s designed for accessibility and scalability. Since it’s hosted, multiple users and clients can connect to it from anywhere. Think of it as the difference between a local desktop application and a web-based service. Remote MCP is the key to bringing powerful AI agents to a wider audience, including web apps and mobile devices and it often uses secure web standards like OAuth for authentication.

Local - Remote MCP

Creating a Local MCP Server

Setting up a local MCP server is the perfect way to get your hands dirty and test things out. We'll use a simple Python example, which is a common and straightforward approach.

Step 1: Setup your Environment

First, ensure you have Python installed and are using a virtual environment, which is standard practice in a professional IDE like PyCharm

Step 2: Install Dependencies

With your virtual environment activated, install the necessary packages.

pip install fastmcp

Step 3: Write the Server Logic

In your PyCharm project, create a file named server.py. In this file, you'll define the tools your MCP server will expose. Tools are just functions that your AI client can call.

A real-world example: an MCP tool that can fetch stock prices using a financial API.

from mcp.server.fastmcp import FastMCP
from typing import Any
import requests

# Initialize the MCP server
mcp = FastMCP("MyLocalStockBot")

# Define a tool to fetch the current stock price
@mcp.tool()
def get_stock_price(ticker: str) -> str:
    """Fetches the current price of a stock using a public API."""
    try:
        api_url = f"https://api.example.com/stocks/{ticker}/price"
        response = requests.get(api_url)
        response.raise_for_status() # Raise an exception 
        data = response.json()
        price = data.get("price")
        if price:
            return f"The current price for {ticker.upper()} is ${price}."
        else:
            return f"Price for {ticker.upper()} not found."
    except requests.exceptions.RequestException as e:
        return f"Error fetching stock price: {e}"

# Run the server
if __name__ == "__main__":
    mcp.run(transport='stdio')

Step 4: Run the Server

Now, you can run your server directly from PyCharm's terminal
python server.py
Your server will start and listen for requests from any MCP client configured to connect to it locally. Clients like Claude Desktop or certain IDEs can be configured to point to this script, allowing you to use your custom tools within your AI chat sessions.

Creating a Hosting a Remote MCP Server


This is where things get a bit more involved, as you're moving from a local machine to remote environment. The key is containerizing your application and using a platform that can host it as an remotely-accessible service.

Step 1: Prepare the Server for HTTP Transport

Modify your server.py to use an HTTP transport instead of stdio. This requires a web server framework. Let's stick with FastAPI, which integrates nicely with FastMCP.


from fastapi import FastAPI
from mcp.server.fastmcp import FastMCP
import uvicorn
import requests

app = FastAPI()

# Initialize the MCP server
mcp = FastMCP("MyRemoteStockBot")

# Define your tools here (same as the local example)
@mcp.tool()
def get_stock_price(ticker: str) -> str:
    """Fetches the current price of a stock using a public API."""
    try:
        api_url = f"https://api.example.com/stocks/{ticker}/price"
        response = requests.get(api_url)
        response.raise_for_status()
        data = response.json()
        price = data.get("price")
        if price:
            return f"The current price for {ticker.upper()} is ${price}."
        else:
            return f"Price for {ticker.upper()} not found."
    except requests.exceptions.RequestException as e:
        return f"Error fetching stock price: {e}"

# Add the MCP endpoint to your FastAPI app
app.include_router(mcp.get_router(), prefix='/mcp')

# A health check endpoint is always a good idea
@app.get('/health')
def health_check():
    return {"status": "ok"}

Step 2: Containerize your Application

To deploy on a remote server, it is good to package it into a Docker container. Create a Dockerfile in your project directory.

Dockerfile:

# Use a slim Python image
FROM python:3.12-slim

# Set the working directory
WORKDIR /app

# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy the application code
COPY server.py .

# Expose the port the app will run on
EXPOSE 8080

# Define the command to run the application
CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "8080"]


You'll also need a requirements.txt file listing your dependencies: fastapi, uvicorn, fastmcp and requests

Step 3: Deploy to a remote environment

If you are deploying in the corporate environment, you have the advantage of default layer of security. Here's how you can deploy your containerized application on a company server without using a public cloud provider.

  • Build the Docker Image: On the server, build your Docker image from the Dockerfile.
        docker build -t my-mcp-server .
  • Run the Container: Run the container on the server, mapping the internal port to a public port
        docker run -d --name mcp-server -p 8080:8080 my-mcp-server
  • Configure Access: The server is now running. For other users or services within the company to access it, they'll need to use the internal IP address or domain name of the server, along with the exposed port (e.g., http://internal-server-ip:8080/mcp). Access control can be handled through a company firewall or network policies.

Remote MCP servers are a powerful way to make your custom tools and data sources available to a wider range of AI applications. By containerizing and deploying to a server, you can build scalable, secure and always-on integrations for your AI workflows.



No comments:

Post a Comment