ModelContextProtocol Client
Project description
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]
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file modelcontextprotocol_client-0.1.2.tar.gz.
File metadata
- Download URL: modelcontextprotocol_client-0.1.2.tar.gz
- Upload date:
- Size: 36.4 kB
- Tags: Source
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ff2c55233257fdd592dd4667c514ba2c46873f3e7057446583d12c20e2a8a3e
|
|
| MD5 |
4efb3488b2395033043838655e155693
|
|
| BLAKE2b-256 |
250a6e7193f11c6351135597278b23d8b0fe7c945932df65e8bb85f5fa6a9be7
|
File details
Details for the file modelcontextprotocol_client-0.1.2-py3-none-any.whl.
File metadata
- Download URL: modelcontextprotocol_client-0.1.2-py3-none-any.whl
- Upload date:
- Size: 34.8 kB
- Tags: Python 3
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d1a844c31f8f3e2e7429708471552608d58e2576fc32ca1dfd93c13478c9179
|
|
| MD5 |
eaf7b370648215ad1df70aa448c3a3d4
|
|
| BLAKE2b-256 |
f6d3555b85094c77e8e461df72316c95396bffad0bb589f4440e8e3158c98d41
|