mcp-client-server-python
This project demonstrates a simple client-server Model Context Protocol (MCP) implementation using Python.
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
-
Environment Variables
Create a
.env
file in the project directory:OPENAI_API_KEY=your-openai-api-key MCP_SSE_URL=http://localhost:8080/sse
-
Start the Server
uv run server.py --host 0.0.0.0 --port 8080
The server exposes tools and resources via SSE at
/sse
. -
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.
Related MCP Servers
View all developer_tools servers ācontext7
by upstash
Context7 MCP provides up-to-date, version-specific documentation and code examples directly into your prompt, enhancing the capabilities of LLMs by ensuring they use the latest information.
git-mcp
by idosal
GitMCP is a free, open-source, remote Model Context Protocol (MCP) server that transforms GitHub projects into documentation hubs, enabling AI tools to access up-to-date documentation and code.
exa-mcp-server
by exa-labs
A Model Context Protocol (MCP) server allows AI assistants to use the Exa AI Search API for real-time web searches in a secure manner.
Sequential Thinking
by modelcontextprotocol
An MCP server implementation that provides a tool for dynamic and reflective problem-solving through a structured thinking process.
Everything MCP Server
by modelcontextprotocol
The Everything MCP Server is a comprehensive test server designed to demonstrate the full capabilities of the Model Context Protocol (MCP). It is not intended for production use but serves as a valuable tool for developers building MCP clients.
gateway
by centralmind
CentralMind Gateway is a tool designed to expose databases to AI agents via MCP or OpenAPI protocols, providing secure, LLM-optimized APIs.
mcpdoc
by langchain-ai
MCP LLMS-TXT Documentation Server provides a structured way to manage and retrieve LLM documentation using the Model Context Protocol.