Skip to main content

A Python client library for the Model Context Protocol (MCP) that provides clean, async connectivity to MCP servers

Project description

QV MCP Client

A Python client library for the Model Context Protocol (MCP) that provides clean, async connectivity to MCP servers.

Python MCP License

What is MCP?

The Model Context Protocol (MCP) is an open standard that enables AI assistants to securely connect to external data sources and tools. It provides a standardized way for AI models to interact with various systems including databases, file systems, APIs, and other services.

Features

  • Async Support: Full async/await pattern for non-blocking operations
  • Multiple Transports: Support for stdio and HTTP-based connections
  • Connection Management: Built-in manager for handling multiple server connections
  • Cross-Platform: Windows-specific command handling included
  • Error Handling: Comprehensive exception hierarchy with detailed error information
  • Type Safety: Complete type hints throughout the codebase
  • Modular Design: Clean separation of concerns for easy maintenance

Installation

pip install mcp
pip install -e .

Quick Start

Basic Client Usage

import asyncio
from qv_mcp_client import MCPClient, MCPServerConfig, MCPTransportType

async def main():
    config = MCPServerConfig(
        name="filesystem",
        transport=MCPTransportType.STDIO,
        command="npx",
        args=["-y", "@modelcontextprotocol/server-filesystem", "/tmp"]
    )
    
    client = MCPClient()
    await client.connect(config)
    
    # List available tools
    tools = await client.list_tools()
    print(f"Available tools: {[tool['name'] for tool in tools]}")
    
    # Call a tool
    result = await client.call_tool("read_file", {"path": "/tmp/test.txt"})
    print(f"Result: {result}")
    
    await client.disconnect()

asyncio.run(main())

Managing Multiple Connections

from qv_mcp_client import MCPManager

async def main():
    manager = MCPManager()
    
    # Add multiple servers
    await manager.add_server(MCPServerConfig(
        name="filesystem",
        transport=MCPTransportType.STDIO,
        command="npx",
        args=["-y", "@modelcontextprotocol/server-filesystem", "/tmp"]
    ))
    
    await manager.add_server(MCPServerConfig(
        name="api-server",
        transport=MCPTransportType.STREAMABLE_HTTP,
        url="http://localhost:3000/mcp"
    ))
    
    # Use specific client
    fs_client = manager.get_client("filesystem")
    if fs_client:
        tools = await fs_client.list_tools()
        print(f"Filesystem tools: {len(tools)}")
    
    await manager.disconnect_all()

Transport Types

STDIO Transport

For local subprocess-based servers (Python, Node.js, etc.)

config = MCPServerConfig(
    name="python-server",
    transport=MCPTransportType.STDIO,
    command="python",
    args=["-m", "my_mcp_server"],
    env={"DEBUG": "1"}
)

HTTP Transport

For remote or web-based MCP servers

config = MCPServerConfig(
    name="web-server",
    transport=MCPTransportType.STREAMABLE_HTTP,
    url="https://api.example.com/mcp"
)

API Overview

MCPClient Methods

Connection:

  • connect(config) - Connect to MCP server
  • disconnect() - Disconnect from server

Tools:

  • list_tools() - Get available tools
  • call_tool(name, arguments) - Execute a tool

Resources:

  • list_resources() - Get available resources
  • read_resource(uri) - Read resource content

Prompts:

  • list_prompts() - Get available prompts
  • get_prompt(name, arguments) - Retrieve formatted prompt

MCPManager Methods

  • add_server(config) - Add and connect to server
  • remove_server(name) - Remove server connection
  • get_client(name) - Get specific client instance
  • list_servers() - List connected servers
  • disconnect_all() - Close all connections

Error Handling

The library provides specific exception types for different error scenarios:

from qv_mcp_client.exceptions import MCPConnectionError, MCPToolError

try:
    await client.connect(config)
    result = await client.call_tool("example_tool", {})
except MCPConnectionError as e:
    print(f"Connection failed: {e}")
except MCPToolError as e:
    print(f"Tool execution failed: {e}")

Platform Support

  • Windows: Automatic handling of .cmd and .bat files
  • Linux/macOS: Direct command execution
  • Python 3.10+: Full compatibility

Project Structure

src/qv_mcp_client/
├── __init__.py          # Package exports
├── client.py            # Main MCPClient implementation
├── manager.py           # Multi-connection manager
├── config.py            # Configuration classes
├── utils.py             # Platform utilities
└── exceptions.py        # Custom exceptions

Examples

The examples/ directory contains working examples for different use cases:

  • Basic client usage with filesystem server
  • Multi-server management
  • Error handling patterns

Requirements

  • Python 3.10 or higher
  • mcp package (Model Context Protocol SDK)
  • Platform-specific requirements for server execution

MCP Server Compatibility

This client works with any MCP-compliant server including:

  • Official MCP servers (filesystem, database, etc.)
  • Custom Python MCP servers
  • Docker-based MCP services
  • HTTP/REST MCP endpoints

Development Status

This is a personal project developed for specific use cases. The code is provided as-is and will be maintained and improved based on personal requirements. While the library is functional and well-tested in its intended environments, it may not cover all edge cases or use scenarios.

License

MIT License - see LICENSE file for details.

Related Links

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

qv_mcp_client-0.1.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

qv_mcp_client-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file qv_mcp_client-0.1.0.tar.gz.

File metadata

  • Download URL: qv_mcp_client-0.1.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.5

File hashes

Hashes for qv_mcp_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2a96e45cbfab7ba5e992469de6357b6f2e5b4bfa3401c4228e32e4ea25d0208f
MD5 f6fe7a70e2c66047b008275437363336
BLAKE2b-256 e032dccbd32c9e8ed092400356fb8cb8a18b81cad66a39f1555dd1d68e03a3c1

See more details on using hashes here.

File details

Details for the file qv_mcp_client-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for qv_mcp_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 203ca00f15c736c8a2b9de19bec77d491f2929544c68f3dd5644223c41e12c04
MD5 6ac200864b7949d301a047a4a04e275a
BLAKE2b-256 e6736b7b6fc74de3fa65d6de7806efc9ec6764379e50b55ffb908f3e6bc59f46

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