This repository contains a Python implementation of the Model Context Protocol (MCP), showing how to create both a server and client application that communicate using the MCP standard.
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
This implementation demonstrates several key MCP features:
completion- Generates text completions from AI models (simulated)add- Simple calculator that adds two numbers
models://- Returns information about available AI modelsgreeting://{name}- Returns a personalized greeting for a given name
review_code- Generates a prompt for reviewing code
To use this MCP implementation, install the required packages:
pip install mcp-server mcp-clientRun the server in one terminal window:
python server.pyThe server can also be run in development mode using the MCP CLI:
mcp dev server.pyOr installed in Claude Desktop (if available):
mcp install server.pyRun the client in another terminal window:
python client.pyThis will connect to the server and demonstrate all available features.
The client (client.py) demonstrates all the MCP capabilities:
python client.pyThis 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
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 + bThe 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})For more information about MCP, visit: https://modelcontextprotocol.io/
Disclaimer:
This document has been translated using AI translation service Co-op Translator. While we strive for accuracy, please be aware that automated translations may contain errors or inaccuracies. The original document in its native language should be considered the authoritative source. For critical information, professional human translation is recommended. We are not liable for any misunderstandings or misinterpretations arising from the use of this translation.