A minimal, cross-platform Python client for the Model Context Protocol (MCP).
MCP is an emerging standard for securely exposing local tools, data, and capabilities to AI models. Rather than hard-coding integrations, a client negotiates what a server offers (tools, prompts, resources) at runtime. This repository provides a compact, readable example of that handshake plus a thin OpenAI-powered chat layer that can decide when to call server tools.
Modern AI workflows increasingly need structured access to local functionality (query files, run utilities, generate domain-specific responses) without leaking arbitrary system control. MCP defines a permissioned vocabulary for that. This project shows:
- How to launch an MCP server over stdio in a cross-platform way (no POSIX shell required)
- How to enumerate and display offered capabilities ("members")
- How to let an LLM invoke those tools via OpenAI function/tool calling
- How to keep the implementation small and approachable for learning
CLI Args → MCPClient → Start server (stdio) → Initialize ClientSession
↓
List members (--members) OR Chat loop (--chat)
↓
Chat handler → OpenAI model → (optional) tool calls → MCP tool execution
↓
Aggregated response printed to terminal
- Cross-platform: Avoid shells; use
sys.executable. - Minimal surface: One client class, one sample server, a handler for tool-aware chat.
- Explicit errors: Fail fast if server script or API key missing.
- Educational: Clear separation of concerns (
cli.py,mcp_client.py,handlers.py).
- Start a local MCP server script via stdio (Windows/macOS/Linux)
- List server members: tools, prompts, and resources
- Chat mode that can call MCP tools using OpenAI
- Clean CLI interface via
python -m mcp_clientor themcp-clientconsole script
- Python 3.10+
mcp>=1.22.0openai>=2.8.1- An OpenAI API key (
OPENAI_API_KEY)
Using uv (recommended):
# From the project root
uv pip install .Or with pip:
pip install .Set your OpenAI API key, then run the client against the included server:
# Set for current PowerShell session
$env:OPENAI_API_KEY = "sk-..."
# List members
uv run python -m mcp_client mcp_server/mcp_server.py --members
# Chat mode (calls tools when appropriate)
uv run python -m mcp_client mcp_server/mcp_server.py --chatAlternatively, after installing the package, use the console script (if on PATH):
mcp-client mcp_server/mcp_server.py --members
mcp-client mcp_server/mcp_server.py --chat- PowerShell activation for a local venv:
.\\.venv\\Scripts\\Activate.ps1 - You do not need to activate a venv when using
uv run—it manages the environment automatically.
The CLI supports:
--members: Print tools, prompts, and resources exposed by the server--chat: Start a simple chat loop that may call MCP tools
Examples:
# Show help (module form)
uv run python -m mcp_client --help
# With console script (after install)
mcp-client --helpmain.py
pyproject.toml
mcp_client/
__init__.py
__main__.py
chat.py
cli.py
handlers.py
mcp_client.py
mcp_server/
mcp_server.py
mcp_server/mcp_server.py: Example MCP server (FastMCP) running on stdiomcp_client/mcp_client.py: Client that starts the server and opens an MCP sessionmcp_client/chat.py: Hanldes chat loop for applicationmcp_client/cli.py: Parses CLI argumentsmcp_client/handlers.py: Chat handler integrating OpenAI and MCP toolsmcp_client/__main__.py: CLI entry (module form)pyproject.toml: Package metadata and console script mapping
OPENAI_API_KEYmust be set. Options:- Session-only:
$env:OPENAI_API_KEY = "sk-..." - Persist for your user:
[System.Environment]::SetEnvironmentVariable('OPENAI_API_KEY', 'sk-...', 'User')(restart terminal)
- Session-only:
Run locally without installing:
# Members
uv run python -m mcp_client mcp_server/mcp_server.py --members
# Chat
uv run python -m mcp_client mcp_server/mcp_server.py --chatOptional venv activation in PowerShell:
.\\.venv\\Scripts\\Activate.ps1
# work with python/pip
deactivate- This client avoids POSIX shell dependencies and uses
sys.executableto start the server script for cross-platform compatibility. - If you rely on newer typing features (e.g.,
typing.Self), use Python 3.11+ or addtyping-extensions.
This project takes the base code from Build a Python MCP Client to Test Servers From Your Terminal. Modifications were made and extra comments added to help facilitate learning and to remove dependency on POSIX shell.