What Is MCP?
The Model Context Protocol (MCP) is an open standard introduced by Anthropic that provides a uniform way for AI models to connect to external data sources and tools. Think of it as a USB-C port for AI — instead of writing custom integration code for every LLM and every tool, you build one MCP server and any compatible AI client can use it.
MCP servers expose three types of capabilities:
- Resources — read-only data sources (files, database records, API responses)
- Tools — callable functions the model can invoke
- Prompts — pre-built prompt templates the client can select
Why MCP Matters
Before MCP, connecting an LLM to your internal systems meant:
- Writing custom function-calling schemas per model API
- Maintaining different integration logic for Claude, GPT, Gemini, etc.
- No standard for authentication, error handling, or streaming
MCP solves all three. Write the server once; every MCP-compatible client works with it.
Setting Up Your Environment
# Create a virtual environment
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
# Install the MCP SDK
pip install mcp
Your First MCP Server
The simplest possible MCP server — a calculator with one tool:
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp import types
app = Server("calculator-server")
@app.list_tools()
async def list_tools() -> list[types.Tool]:
return [
types.Tool(
name="calculate",
description="Evaluate a mathematical expression and return the result.",
inputSchema={
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "A safe math expression, e.g. '(12 + 5) * 3'"
}
},
"required": ["expression"]
}
)
]
@app.call_tool()
async def call_tool(name: str, arguments: dict) -> list[types.TextContent]:
if name != "calculate":
raise ValueError(f"Unknown tool: {name}")
expression = arguments.get("expression", "")
# Use a safe evaluator in production (e.g. numexpr or simpleeval)
try:
result = eval(expression, {"__builtins__": {}}, {})
return [types.TextContent(type="text", text=str(result))]
except Exception as e:
return [types.TextContent(type="text", text=f"Error: {e}")]
if __name__ == "__main__":
import asyncio
asyncio.run(stdio_server(app))
Run it: python server.py
Exposing Resources
Resources let the model read data from your system without calling a tool. They are ideal for configuration files, documentation, or database records:
@app.list_resources()
async def list_resources() -> list[types.Resource]:
return [
types.Resource(
uri="file:///docs/api-reference",
name="API Reference",
description="Internal API documentation",
mimeType="text/markdown"
)
]
@app.read_resource()
async def read_resource(uri: str) -> str:
if uri == "file:///docs/api-reference":
with open("docs/api-reference.md", "r") as f:
return f.read()
raise ValueError(f"Unknown resource: {uri}")
Connecting to Claude Desktop
Add your server to Claude Desktop's configuration file:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
{
"mcpServers": {
"calculator": {
"command": "python",
"args": ["/absolute/path/to/server.py"]
}
}
}
Restart Claude Desktop and your tools will appear automatically.
Production Considerations
Before deploying an MCP server in production:
- Authentication — validate API keys or OAuth tokens on every request
- Input sanitization — never pass raw user input to
eval(), shell commands, or SQL queries - Rate limiting — protect backend services from runaway AI loops
- Logging — record every tool invocation for debugging and audit trails
- Timeouts — enforce a maximum execution time per tool call
MCP is designed to be secure by default, but the security of your tools is entirely your responsibility. The protocol trusts that you validate everything the model sends you.
What to Build Next
Good candidates for MCP servers in a developer workflow:
- Database query server — let the model run read-only SQL queries on your analytics DB
- GitHub server — expose PRs, issues, and code search
- Documentation server — index your internal docs with embeddings for RAG
- Monitoring server — surface Datadog/CloudWatch alerts directly in your AI assistant
MCP is quickly becoming the backbone of the agentic software ecosystem. Mastering it now gives you a significant edge.
Share