MCP Client-Server Python Example

by sofianhw

127 downloads
Not rated
GitHub

About

This project demonstrates a simple client-server implementation of the Model Context Protocol (MCP), an open protocol by Anthropic that enables large language models to interact with external tools and resources. It is intended for developers learning how to build MCP servers…

Details

Author
sofianhw
Downloads
127
Categories
Other

- Exposes tools (e.g., addition) and resources (e.g., greetings) via SSE.
- Client connects to server and lists available tools.
- Uses OpenAI GPT models to process queries and call tools.
- Built with Python 3.12+ and the MCP Python SDK.
- Uses SSE (Server-Sent Events) for transport.
- Managed with uv for fast dependency resolution.

Setting up with Highlight

This MCP is not yet compatible with Highlight’s one-click setup. However, you can still use it with Highlight by following these steps:

  1. Download and install Highlight from highlightai.com/download
  2. Navigate to the plugins tab and select "Add Custom Plugin"
  3. Configure the plugin with the settings below
    Plugin Name MCP Client-Server Python Example
    Command (node, npx, python, etc.)

    Please refer to the README for specific instructions on how to obtain API keys or other required environment variables.

  4. Enable "Start Automatically" if you want the plugin to start when Highlight launches

From the repository

Install dependencies with uv sync, create a .env file with OPENAI_API_KEY and MCP_SSE_URL, start the server with uv run server.py --host 0.0.0.0 --port 8080, then run the client with uv run client.py. The client will list available tools and start an interactive chat loop. Type quit to exit.

Claude Desktop / Cursor

Paste into your MCP client config file to install this server.

{
    "mcpServers": {
        "mcp client-server python example": {
            "mcp-client-server-python": {
                "command": "uv",
                "args": [
                    "sync"
                ]
            }
        }
    }
}

McpServers

{
    "mcp-client-server-python": {
        "command": "uv",
        "args": [
            "sync"
        ]
    }
}

MCP Client-Server Python Example

This project demonstrates a simple client-server MCP.

---

What is MCP (Model Context Protocol)?

> MCP is an open protocol introduced by Anthropic to enable large language models (LLMs) to interact with external tools, APIs, and resources in a standardized, extensible way.
> It facilitates secure, multi-channel communication between AI models and external systems, supporting advanced agentic workflows and tool use.
>
> 🔗 Anthropic's announcement
> 🔗 MCP documentation

---

Features

- MCP Server: Exposes tools (e.g., addition) and resources (e.g., greetings) via SSE.
- MCP Client: Connects to the server, lists available tools, and interacts using OpenAI's chat completions.
- OpenAI Integration: Uses OpenAI's GPT models to process user queries and call server tools as needed.

---

Requirements

- Python 3.12+
- MCP Python SDK
- OpenAI Python SDK
- Uvicorn (for running the server)
- python-dotenv (for loading environment variables)
- uv (fast Python package installer and resolver)

Project Structure

.
├── client.py      # MCP client implementation
├── server.py      # MCP server implementation
├── pyproject.toml # Project metadata and dependencies
├── .env           # Environment variables (not committed)
└── README.md      # This file

Install dependencies with uv:

uv sync

(This will install all dependencies as specified in uv.lock.)

---

Setup

1. Environment Variables

Create a .env file in the project directory:

   OPENAI_API_KEY=your-openai-api-key
MCP_SSE_URL=http://localhost:8080/sse

2. Start the Server

   uv run server.py --host 0.0.0.0 --port 8080
   
The server exposes tools and resources via SSE at /sse.

3. Run the Client

In another terminal:

   uv run client.py

The client will connect to the server, list available tools, and start an interactive chat loop.

---

Usage

- Type your queries in the client prompt.
- The client will use OpenAI to process your query and call server tools if needed.
- Type quit to exit the client.

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.