Skip to main content

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

modelcontextprotocol_client-0.1.3.tar.gz (91.2 kB view details)

Uploaded Source

Built Distribution

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

modelcontextprotocol_client-0.1.3-py3-none-any.whl (47.0 kB view details)

Uploaded Python 3

File details

Details for the file modelcontextprotocol_client-0.1.3.tar.gz.

File metadata

  • Download URL: modelcontextprotocol_client-0.1.3.tar.gz
  • Upload date:
  • Size: 91.2 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

Hashes for modelcontextprotocol_client-0.1.3.tar.gz
Algorithm Hash digest
SHA256 dda28a575e740de5b186dd3f938d09c559131990905dcfcb075346085782055f
MD5 6d67c29bd038f67799f831c36818f95e
BLAKE2b-256 3770536b88db1708b10a14c334e801d0b875b0cc4973eb411d984216ed5060e3

See more details on using hashes here.

File details

Details for the file modelcontextprotocol_client-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: modelcontextprotocol_client-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 47.0 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

Hashes for modelcontextprotocol_client-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 449cca5496212c53cb98cc39b1b9f2b2e0e112e1b4bd9c47b95a1c505b887620
MD5 6ea059c04441d129c249a48b84829f6c
BLAKE2b-256 a8c7dafc8c66d70cf6b23adcc1f608229c790a047d49d8c42abda31473b3eb46

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