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 ...
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.