Model Context Protocol (MCP) Python Implementation
May 24, 2025 ยท View on GitHub
This repository contains a Python implementation of the Model Context Protocol (MCP), demonstrating how to create both a server and client application that communicate using the MCP standard.
Overview
The MCP implementation consists of two main components:
-
MCP Server (
server.py) - A server that exposes:- Tools: Functions that can be called remotely
- Resources: Data that can be retrieved
- Prompts: Templates for generating prompts for language models
-
MCP Client (
client.py) - A client application that connects to the server and uses its features
Features
This implementation demonstrates several key MCP features:
Tools
completion- Generates text completions from AI models (simulated)add- Simple calculator that adds two numbers
Resources
models://- Returns information about available AI modelsgreeting://{name}- Returns a personalized greeting for a given name
Prompts
review_code- Generates a prompt for reviewing code
Installation
To use this MCP implementation, install the required packages:
pip install mcp-server mcp-client
Running the Server and Client
Starting the Server
Run the server in one terminal window:
python server.py
The server can also be run in development mode using the MCP CLI:
mcp dev server.py
Or installed in Claude Desktop (if available):
mcp install server.py
Running the Client
Run the client in another terminal window:
python client.py
This will connect to the server and demonstrate all available features.
Client Usage
The client (client.py) demonstrates all the MCP capabilities:
python client.py
This will connect to the server and exercise all features including tools, resources, and prompts. The output will show:
- Calculator tool result (5 + 7 = 12)
- Completion tool response to "What is the meaning of life?"
- List of available AI models
- Personalized greeting for "MCP Explorer"
- Code review prompt template
Implementation Details
The server is implemented using the FastMCP API, which provides high-level abstractions for defining MCP services. Here's a simplified example of how tools are defined:
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers together
Args:
a: First number
b: Second number
Returns:
The sum of the two numbers
"""
logger.info(f"Adding {a} and {b}")
return a + b
The client uses the MCP client library to connect to and call the server:
async with stdio_client(server_params) as (reader, writer):
async with ClientSession(reader, writer) as session:
await session.initialize()
result = await session.call_tool("add", arguments={"a": 5, "b": 7})
Learn More
For more information about MCP, visit: https://modelcontextprotocol.io/