Creating a Python MCP Server: A Step-by-Step Guide
The Model Context Protocol (MCP) provides a standardized approach for connecting context sources to large language models (LLMs). This tutorial demonstrates how to build a functional MCP server using Python's MCP SDK, enabling you to expose data, tools, and templates to LLM applications.
What is MCP?
MCP creates a bridge between LLM applications and external context sources. It allows you to modularize different aspects of LLM interactions:
- Data provision through resources (similar to read-only endpoints)
- Action execution via tools (comparable to API functions)
- Template management using prompts for reusable interactions
Core MCP Components
MCP servers implement three fundamental building blocks, each serving different purposes:
Component | Controlled By | Purpose | Common Uses |
---|---|---|---|
Prompts | User | Interactive templates triggered by user selection | Command shortcuts, menu items |
Resources | Application | Data managed by the client for LLM context | File content, API data |
Tools | LLM | Functions the model can execute independently | Calculations, API calls, data modifications |
Understanding these distinctions helps you design effective MCP servers that properly separate concerns.
Server Feature Advertising
MCP servers announce their capabilities during startup, allowing clients to adapt their behavior:
Feature | Configuration Flag | What It Enables |
---|---|---|
prompts | listChanged | Dynamic prompt template updates |
resources | subscribe , listChanged | Data exposure with live updates |
tools | listChanged | Function discovery and execution |
logging | Default | Debug output configuration |
completion | Default | Argument suggestion support |
Getting Started
Requirements
Ensure your environment includes:
- Python 3.7+ (Python 3.11+ recommended)
- pip package manager
- Node.js 18.x
Installation Options
Choose one of these installation methods:
Standard pip installation:
Using uv (recommended for project management):
Project Structure
Organize your project as follows:
Implementation
Building Your Server
Create server.py
with the following foundation:
Testing Your Server
Using the Development Inspector
The most efficient way to test your server is through the built-in development interface:
This command:
- Launches your server with live reloading
- Opens a web interface at
http://localhost:6274/
- Provides interactive testing capabilities
Inspector Configuration
In the Inspector interface, configure the transport settings:
Click Connect to establish the connection.
Testing Each Component
Tool Testing
- Navigate to the Tools section
- Select the
add
tool - Input test values (e.g.,
a = 10
,b = 15
) - Execute and verify the result (
25
)
Resource Testing
- Go to Resources → Resource Templates
- Select
get_greeting
- Enter a name (e.g.,
Alice
) - Click Read Resource
- Verify the response:
"Hello, Alice!"
Prompt Testing
- Access Prompts → List prompts
- Select
review_code
- Input sample code:
print(1+1)
- Execute to see the formatted prompt output
Run this in a separate terminal while your server is active.
Understanding Server Behavior
When you run python server.py
directly, the server appears inactive because it uses stdio transport and waits for client connections. This is normal behavior - the server needs a client (like the Inspector or your custom client) to interact with it.
This server cannot be installed
A simple MCP server that provides a calculator tool, dynamic greeting resources, and code review prompts, demonstrating how to build standardized interfaces for LLM applications.
Related MCP Servers
- -securityFlicense-qualityAn MCP server that allows Claude to interact with local LLMs running in LM Studio, providing access to list models, generate text, and use chat completions through local models.Last updated -8Python
- AsecurityFlicenseAqualityAn MCP server that implements a structured workflow for LLM-based coding, guiding development through feature clarification, documentation generation, phased implementation, and progress tracking.Last updated -1014TypeScript
- -securityAlicense-qualityAn MCP server that helps novice developers deploy web applications through conversational interfaces, bridging the gap between LLMs and various hosting environments.Last updated -1PythonMIT License
- AsecurityAlicenseAqualityAn MCP server that enables LLMs to interact with Moodle platforms to manage courses, students, assignments, and quizzes through natural language commands.Last updated -713JavaScriptMIT License