Model Context Protocol Client
A Python client implementation for the Model Context Protocol (MCP). This library allows you to connect to MCP servers, list tools, resources, and prompts, and execute tool calls.
Installation
pip install modelcontextprotocol-client
Or from source:
pip install .
Features
- Support for Stdio, SSE, HTTP, and WebSocket transports
- Full MCP protocol implementation (Client capabilities)
- Type-safe interactions with Pydantic models
- Async/Await support
Hello World Example
Here is a simple example of how to use the client to connect to a local MCP server (using stdio transport) and list available tools.
This example assumes you have an MCP server running or available to run (e.g., a simple python script or a CLI tool).
1. Basic Usage
import asyncio
from mcp.client import Client
async def main():
# Define server configuration
# This example connects to a local git MCP server (requires 'uv' and 'mcp-server-git')
config = {
"mcpServers": {
"git-server": {
"transport": "stdio",
"command": "uvx",
"args": ["mcp-server-git", "--repository", "."]
}
}
}
# Initialize the client
client = Client.from_config(config)
try:
# Create a session with the 'git-server'
print("Connecting to server...")
session = await client.create_session("git-server")
print("Connected!")
# Initialize the connection (handshake)
# Note: create_session already handles connect() and initialize()
# List available tools
print("\nFetching tools...")
tools = await session.list_tools()
print(f"Found {len(tools.tools)} tools:")
for tool in tools.tools:
print(f"- {tool.name}: {tool.description}")
# Example: Call a tool (if applicable)
# await session.call_tool(...)
finally:
# Close all sessions
await client.close_all_sessions()
if __name__ == "__main__":
asyncio.run(main())
2. Using an SSE Server
import asyncio
from mcp.client import Client
async def main():
config = {
"mcpServers": {
"remote-server": {
"transport": "sse",
"url": "http://localhost:8000/sse"
}
}
}
client = Client(config)
# ... rest of the logic is the same
Configuration
The client accepts a configuration dictionary or path to a JSON config file.
# Load from file
client = Client.from_config_file("mcp_config.json")
The config file structure:
{
"mcpServers": {
"my-server": {
"command": "python",
"args": ["server.py"],
"env": { "MY_VAR": "value" }
}
}
}
License
[Add License Here]
Metadata
Release files for modelcontextprotocol-client 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| modelcontextprotocol_client-0.1.3.tar.gz | 91.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| modelcontextprotocol_client-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 138.2 kB
Release files / modelcontextprotocol_client-0.1.3.tar.gz
| Download URL | modelcontextprotocol_client-0.1.3.tar.gz |
|---|---|
| Size | 91.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dda28a575e740de5b186dd3f938d09c559131990905dcfcb075346085782055f
|
|
BLAKE2b-256 checksum How to use checksums |
3770536b88db1708b10a14c334e801d0b875b0cc4973eb411d984216ed5060e3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / modelcontextprotocol_client-0.1.3-py3-none-any.whl
| Download URL | modelcontextprotocol_client-0.1.3-py3-none-any.whl |
|---|---|
| Size | 47.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
449cca5496212c53cb98cc39b1b9f2b2e0e112e1b4bd9c47b95a1c505b887620
|
|
BLAKE2b-256 checksum How to use checksums |
a8c7dafc8c66d70cf6b23adcc1f608229c790a047d49d8c42abda31473b3eb46
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|