MCP Python Client
A reusable Python client for interacting with MCP (Machine Conversation Protocol) servers and LLMs.
Features
- Connect to and interact with multiple MCP servers
- Call tools provided by MCP servers
- Process user queries using various LLM models via LiteLLM
- Support for synchronous and asynchronous operations
- Streaming response support
Installation
pip install mcp-python-client
Quick Start
import asyncio
from mcp_python_client import MCPClient
async def main():
client = MCPClient(
model="anthropic/claude-3-sonnet-20240229",
api_key="your_api_key_here"
)
await client.connect_to_all_servers()
query = "What is the current weather in New York?"
async for chunk in client.aprocess_query(query):
print(chunk, end="", flush=True)
await client.cleanup()
asyncio.run(main())
Configuration
The client looks for a configuration file in the following locations:
- Path specified when creating the client
~/.config/mcp-client/config.json
~/.mcp-client.json
./mcp-client.json
./config.json
Example configuration file:
{
"mcpServers": {
"shell-server": {
"command": "mcp-shell-server",
"args": ["--use_cache"],
"env": {
"SHELL_SERVER_CACHE_DIR": "/tmp/shell-server-cache"
}
},
"python-server": {
"command": "mcp-python-server",
"args": []
}
}
}
Advanced Usage
See the documentation for more advanced usage examples and API details.
Publishing
uv build
uv publish
License
MIT