loading...

3
13 Oct 2025
Model Context Protocol (MCP) Demystified

                                                                     

1. Introduction

Even before we begin and deep dive into MCP, we need to understand why MCP matters in the AI ecosystem. Today's AI ecosystem is a mess of brilliant but isolated systems. Every powerful AI Assistant (Claude, GPT, custom models) needs to use external Tools (APIs, databases, services) and the challenge is that each tool speaks a different language which forces the developers to create custom integration codes. This is where exactly MCP comes in and solves the problem, MCP defines a single, standardized way of Tool Discovery and Model communication, it is a foundational standard designed to bring order to tool interactions. (Not limited to tools, you can talk to Agents as well, will explore this later when we talk about MCP and A2A)

Coming back to the technical details...

It is an open standard that defines

How models can discover tools (like databases, APIs, file systems).

How to exchange context between models and external systems.

How to handle communication over different transports (local process, WebSocket, HTTP)



2. Setting Up an MCP Server with FastMCP

We will setup the simple MCP server using FastMCP that will be helping us to run terminal commands in your system.

Later, we'll connect this server to a front-end application (like the Claude desktop app). This will enable you to run complex terminal actions simply by describing them in natural language.

I'm assuming you've already completed setting up your Python virtual environment and installing the necessary system packages, if not follow this https://www.datacamp.com/tutorial/building-mcp-server-client-fastmcp

Lets see the code  ...

import os
import subprocess
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("terminal")
DEFAULT_WORKSPACE = os.path.expanduser("~m/mcp/workspace")


@mcp.tool()
async def run_command(command: str) -> str:
    """
    Run a terminal command inside the workspace directory.
    If a terminal command can accomplish a task,
    tell the user you'll use this tool to accomplish it,
    even though you cannot directly do it

    Args:
        command: The shell command to run.
   
    Returns:
        The command output or an error message.
    """
    try:
        result = subprocess.run(command, shell=True, cwd=DEFAULT_WORKSPACE, capture_output=True, text=True)
        return result.stdout or result.stderr
    except Exception as e:
        return str(e)
   

if __name__ == "__main__":
    mcp.run(transport='stdio')


Let us breakdown this code and what it does ....

  • Registration: The @mcp.tool() decorator registers the function as a callable MCP tool.
  • Function: run_command(command: str) accepts the shell command as a string input.
  • Execution: subprocess.run(...) executes the command within the defined cwd=DEFAULT_WORKSPACE.
  • Output: Returns the command's standard output (stdout) or any error messages (stderr or exception).
  • mcp.run(transport='stdio'): Starts the MCP server, communicating over standard input/output (stdio)—the common interface for connecting to an external AI application.

4. Modes of Transport in MCP

Understanding MCP transports (process, WebSocket, HTTP, etc.)

  • Streaming HTTP (SSE/HTTStandard I/O (stdio): This is the simplest, most common mode, ideal for local integration (like your CLI client connecting to your server). It uses standard input and output streams to exchange JSON-RPC messages between the client and server.
  • Streaming HTTP (SSE/HTTP): For remote or web-based applications, MCP supports HTTP/SSE. This allows clients (like a browser or remote application) to connect, enabling real-time, asynchronous communication for continuous data exchange.
  • Asynchronous Communication: Regardless of the mode, MCP transports are designed for the non-blocking exchange of structured messages (JSON-RPC), ensuring low-latency communication that handles the complexities of AI tool calling and data flow efficiently.

5. Connecting MCP with Claude

Why Claude + MCP is powerful

How to link an MCP server to Claude

Testing Claude’s interaction with MCP tools

Example workflow: Claude querying your custom MCP server


6. Real-World Applications

Practical use cases of MCP with AI assistants

Extending MCP to multiple tools and systems

Future possibilities of MCP in multi-agent setups


aiwithharsh © 2025