Skip to main content

Python client for interacting with MCP servers and LLMs

Project description

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():
    # Create a client with your preferred LLM settings
    client = MCPClient(
        model="anthropic/claude-3-sonnet-20240229",
        api_key="your_api_key_here"  # Or set via env var
    )
    
    # Connect to MCP servers defined in your config
    await client.connect_to_all_servers()
    
    # Process a query
    query = "What is the current weather in New York?"
    
    # Stream response
    async for chunk in client.aprocess_query(query):
        print(chunk, end="", flush=True)
    
    # Clean up
    await client.cleanup()

# Run the example
asyncio.run(main())

Configuration

The client looks for a configuration file in the following locations:

  1. Path specified when creating the client
  2. ~/.config/mcp-client/config.json
  3. ~/.mcp-client.json
  4. ./mcp-client.json
  5. ./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

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

mcp_python_client-0.1.5.tar.gz (105.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcp_python_client-0.1.5-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file mcp_python_client-0.1.5.tar.gz.

File metadata

  • Download URL: mcp_python_client-0.1.5.tar.gz
  • Upload date:
  • Size: 105.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.8

File hashes

Hashes for mcp_python_client-0.1.5.tar.gz
Algorithm Hash digest
SHA256 7d0480207306e50881ddb6fffd9de5f23206ea7fcf9d234d8c930cabcad95067
MD5 59fd6d7ea1e4b7a3f5ddef6978180a0f
BLAKE2b-256 79e9bb6f37331b0e55b8dc455160e801eaa2847dbcb9e24fc88c9f80156e36fa

See more details on using hashes here.

File details

Details for the file mcp_python_client-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for mcp_python_client-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 991139166f49d03f52701fad706a9dcc8969a3f9a6a8a7e9a1d0d02980c7c76f
MD5 b19dc2e3666a941efc596fa527d0aff3
BLAKE2b-256 e7cc9b59f44104e05083d3397ec9d508dcbbccdb2b7f39820c2efb7e7159e9bd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page