Files
2026-07-13 13:31:35 +08:00

3.5 KiB

Model Context Protocol (MCP) Python Implementation

This repository contains a Python implementation of the Model Context Protocol (MCP), showing how to build both a server and client application that communicate using the MCP standard.

Overview

The MCP implementation includes two main parts:

  1. MCP Server (server.py) - A server that provides:

    • Tools: Functions that can be called remotely
    • Resources: Data that can be accessed
    • Prompts: Templates for generating prompts for language models
  2. MCP Client (client.py) - A client application that connects to the server and uses its features

Features

This implementation showcases 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 models
  • greeting://{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 test all features including tools, resources, and prompts. The output will show:

  1. Calculator tool result (5 + 7 = 12)
  2. Completion tool response to "What is the meaning of life?"
  3. List of available AI models
  4. Personalized greeting for "MCP Explorer"
  5. Code review prompt template

Implementation Details

The server is built using the FastMCP API, which offers 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/

Disclaimer:
This document has been translated using the 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.