Skip to main content

MCP client built on top of aiohttp

Project description

aiohttp-mcp-client

CI PyPI Python License: MIT

MCP (Model Context Protocol) client built on top of aiohttp. Supports the Streamable HTTP transport.

Features

  • Async context manager with automatic initialize/terminate lifecycle
  • Supports both JSON and SSE response modes
  • Accepts an external aiohttp.ClientSession for integration into existing apps
  • Typed result objects (frozen dataclasses)
  • Only 1 runtime dependency: aiohttp

Installation

pip install aiohttp-mcp-client

Or with uv:

uv add aiohttp-mcp-client

Quick Start

import asyncio
from aiohttp_mcp_client import MCPClient

async def main():
    async with MCPClient("http://localhost:8080/mcp") as client:
        # List available tools
        tools = await client.list_tools()
        for tool in tools:
            print(f"  {tool.name}: {tool.description}")

        # Call a tool
        result = await client.call_tool("my_tool", {"arg": "value"})
        for block in result.content:
            print(block.text)

asyncio.run(main())

Notification callbacks

The server can send log messages and progress updates during tool calls. You can handle these with async callbacks — set defaults at client level, or override per call:

from aiohttp_mcp_client import MCPClient, LogMessage, Progress

async def on_log(msg: LogMessage) -> None:
    print(f"[{msg.level}] {msg.data}")

async def on_progress(msg: Progress) -> None:
    pct = f"{msg.progress}/{msg.total}" if msg.total else f"{msg.progress}"
    print(f"Progress: {pct}")

async def main():
    # Client-level defaults
    async with MCPClient(url, on_log=on_log, on_progress=on_progress) as client:
        result = await client.call_tool("slow_task", {"steps": 10})

        # Per-call override
        result = await client.call_tool("other_task", on_log=my_other_handler)

Using an existing aiohttp session

import aiohttp
from aiohttp_mcp_client import MCPClient

async def main():
    async with aiohttp.ClientSession() as session:
        async with MCPClient("http://localhost:8080/mcp", session=session) as client:
            tools = await client.list_tools()

API

MCPClient(url, *, session=None, client_info=None, on_log=None, on_progress=None)

  • url — MCP server endpoint URL
  • session — Optional aiohttp.ClientSession (one is created if not provided)
  • client_info — Optional {"name": "...", "version": "..."} dict
  • on_log — Default async callback for LogMessage notifications
  • on_progress — Default async callback for Progress notifications

Methods

Method Description
list_tools() List available tools
call_tool(name, arguments) Call a tool
list_resources() List available resources
read_resource(uri) Read a resource by URI
list_resource_templates() List resource templates
list_prompts() List available prompts
get_prompt(name, arguments) Get a prompt by name
ping() Send a ping

All methods except ping() accept optional on_log and on_progress keyword arguments to override the client-level defaults for that call.

Future Plans

  • Automatic pagination helpers (fetch all pages)

Requirements

  • Python 3.11+
  • aiohttp >= 3.9.0

Development

uv venv && source .venv/bin/activate
uv sync --all-extras
make test
make lint

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

aiohttp_mcp_client-0.0.2.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

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

aiohttp_mcp_client-0.0.2-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_mcp_client-0.0.2.tar.gz.

File metadata

  • Download URL: aiohttp_mcp_client-0.0.2.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for aiohttp_mcp_client-0.0.2.tar.gz
Algorithm Hash digest
SHA256 c2d30781510fe0c51f626fb95b3699b3fc352c016096a18eedc4f7e9ca156334
MD5 ada1250fd6627fc30c7ca8150673fc07
BLAKE2b-256 7d7c76dfff77278d8fb66c8d9dba097221ff9328ce1fa0d646466b9e6867bda4

See more details on using hashes here.

File details

Details for the file aiohttp_mcp_client-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_mcp_client-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for aiohttp_mcp_client-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7c012f4ef6919e91f240fe875e3e2f992613014afcaab75fcda43db134b5807e
MD5 30869a74ec0c9b6ddfcf551b8789ba3d
BLAKE2b-256 d7543af2557e3dc55827f1c0e3a1ee88790d291e0b3eb67204d92dc2848a0732

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