Applied AIMCPLLMsAI agentsPython

MCP (Model Context Protocol): what it is and how to build your first server

Pablo Alcalde García

Every time you want a language model to use a tool (query your database, read a repository, create a ticket) you have to write a custom integration. Multiply that by every application and every provider and you have a huge maintenance problem. The Model Context Protocol (MCP) was created to solve it: an open standard so AI applications can connect to tools and data in a common way.

What MCP is, in one sentence

MCP is an open protocol, introduced by Anthropic in late 2024, that defines how an AI application (the client) talks to a program that offers it capabilities (the server). It is often compared to a “USB-C for AI”: one common connector instead of a different cable for every device.

The three pieces a server exposes

PrimitiveWhat it isWho controls itExample
ToolsFunctions the model can invokeThe modelcreate_ticket(title)
ResourcesData the application can readThe applicationThe contents of a file
PromptsReusable instruction templatesThe user”Review this PR”

To get started, almost all the value is in tools.

How client and server communicate

The protocol supports several transports. The two you should know:

  • stdio: the client launches the server as a local process and they talk over standard input/output. Ideal for tools on your own machine.
  • HTTP (streamable): the server is remote and several clients connect over the network. This is what you need to share it with a team.

Your first MCP server in Python

The official Python SDK includes a simple interface (FastMCP):

pip install "mcp[cli]"
# demo_server.py
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("demo")

@mcp.tool()
def add(a: int, b: int) -> int:
    """Add two integers."""
    return a + b

@mcp.tool()
def order_status(order_id: str) -> str:
    """Return the status of an order from its identifier."""
    # The real query to your system would go here
    return f"Order {order_id}: shipped"

if __name__ == "__main__":
    mcp.run()  # uses stdio by default

The @mcp.tool() decorator publishes the function. The docstring and the types become the description and schema the model sees, so writing them well is not optional: it determines whether the model chooses and uses the tool correctly.

To try it with the official inspector:

mcp dev demo_server.py

Connecting it to a client

Compatible clients (editors, desktop assistants, agents) are configured with a small JSON that says how to launch the server. The usual shape:

{
  "mcpServers": {
    "demo": {
      "command": "python",
      "args": ["demo_server.py"]
    }
  }
}

The exact name of the configuration file and its location depend on each client; check its documentation.

Design best practices

  1. Few tools, well named. With dozens of similar tools, the model gets confused. Better 5 clear ones than 30 ambiguous ones.
  2. Descriptions written for the model, not for humans. Say when to use it and when not to.
  3. Return short, useful text. A dump of 50,000 characters consumes context and money.
  4. Minimum permissions. A tool that reads shouldn’t be able to write.
  5. Validate and limit. Never run SQL or commands the model gives you without restricting them.

Security: what you can’t ignore

An MCP server is a gateway into your systems. Before publishing one:

  • Only install servers you trust: they run code on your machine.
  • Apply the principle of least privilege and authentication on remote servers.
  • Remember that content a tool returns reaches the model: it may contain malicious instructions. More in security in LLM applications.

When is MCP worth it?

  • You want several AI applications to use the same tool without rewriting it.
  • You are building agents that need access to internal systems.
  • You want to clearly separate “what the model can do” from “what your systems do”.

If you only have one application and two functions, a direct tool call (function calling) is simpler. MCP pays off when there are several applications or several teams.

Conclusion

MCP doesn’t make the model smarter: it makes it easier and more orderly to give it access to your tools. Start with a small read-only server, measure, and expand permissions gradually.

If you want to integrate AI with your internal systems securely, let’s talk.

Call me now