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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

modelcontextprotocol_client-0.1.1-py3-none-any.whl (34.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modelcontextprotocol_client-0.1.1-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

Hashes for modelcontextprotocol_client-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f1b5553ff7033e0fff7e369e7ff00ed65f5e2b58a24559f54711dcc4c0e9a9f0
MD5 8b6a31e4c9afa21a8948024894dfc65c
BLAKE2b-256 43f809b635d0f99e36a6c210acd1690d9ee4fccabf37e129b336e969323be8b8

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