Skip to main content

Tools for building Model Context Protocol (MCP) servers on top of aiohttp

Project description

aiohttp-mcp

GitHub Actions Workflow Status codecov pre-commit.ci status PyPI - Version PyPI Downloads PyPI - Python Version GitHub License

Tools for building Model Context Protocol (MCP) servers on top of aiohttp.

Features

  • Easy integration with aiohttp web applications
  • Support for Model Context Protocol (MCP) tools
  • Async-first design
  • Type hints support
  • Debug mode for development
  • Flexible routing options

Installation

With uv package manager:

uv add aiohttp-mcp

Or with pip:

pip install aiohttp-mcp

Quick Start

Basic Server Setup

Create a simple MCP server with a custom tool:

import datetime
from zoneinfo import ZoneInfo

from aiohttp import web

from aiohttp_mcp import AiohttpMCP, build_mcp_app

# Initialize MCP
mcp = AiohttpMCP()


# Define a tool
@mcp.tool()
def get_time(timezone: str) -> str:
    """Get the current time in the specified timezone."""
    tz = ZoneInfo(timezone)
    return datetime.datetime.now(tz).isoformat()


# Create and run the application
app = build_mcp_app(mcp, path="/mcp")
web.run_app(app)

Using as a Sub-Application

You can also use aiohttp-mcp as a sub-application in your existing aiohttp server:

import datetime
from zoneinfo import ZoneInfo

from aiohttp import web

from aiohttp_mcp import AiohttpMCP, setup_mcp_subapp

mcp = AiohttpMCP()


# Define a tool
@mcp.tool()
def get_time(timezone: str) -> str:
    """Get the current time in the specified timezone."""
    tz = ZoneInfo(timezone)
    return datetime.datetime.now(tz).isoformat()


# Create your main application
app = web.Application()

# Add MCP as a sub-application
setup_mcp_subapp(app, mcp, prefix="/mcp")

web.run_app(app)

Using Streamable HTTP Transport

For production deployments requiring advanced session management, you can use the streamable HTTP transport mode:

import datetime
from zoneinfo import ZoneInfo

from aiohttp import web

from aiohttp_mcp import AiohttpMCP, TransportMode, build_mcp_app

# Initialize MCP
mcp = AiohttpMCP()


# Define a tool
@mcp.tool()
def get_time(timezone: str) -> str:
    """Get the current time in the specified timezone."""
    tz = ZoneInfo(timezone)
    return datetime.datetime.now(tz).isoformat()


# Create application with streamable transport
app = build_mcp_app(mcp, path="/mcp", transport_mode=TransportMode.STREAMABLE_HTTP, stateless=True)
web.run_app(app)

Client Example

Here's how to create a client that interacts with the MCP server:

import asyncio

from mcp import ClientSession
from mcp.client.sse import sse_client


async def main():
    # Connect to the MCP server
    async with sse_client("http://localhost:8080/mcp") as (read_stream, write_stream):
        async with ClientSession(read_stream, write_stream) as session:
            # Initialize the session
            await session.initialize()

            # List available tools
            tools = await session.list_tools()
            print("Available tools:", [tool.name for tool in tools.tools])

            # Call a tool
            result = await session.call_tool("get_time", {"timezone": "UTC"})
            print("Current time in UTC:", result.content)


if __name__ == "__main__":
    asyncio.run(main())

More Examples

For more examples, check the examples directory.

Development

Setup Development Environment

  1. Clone the repository:
git clone https://github.com/kulapard/aiohttp-mcp.git
cd aiohttp-mcp
  1. Create and activate a virtual environment:
uv venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
  1. Install development dependencies:
uv sync --all-extras

Running Tests

uv run pytest

Requirements

  • Python 3.10 or higher
  • aiohttp >= 3.9.0, < 4.0.0
  • aiohttp-sse >= 2.2.0, < 3.0.0
  • anyio >= 4.9.0, < 5.0.0
  • mcp >= 1.8.0, < 2.0.0

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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-0.5.0rc2.tar.gz (21.9 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-0.5.0rc2-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp_mcp-0.5.0rc2.tar.gz.

File metadata

  • Download URL: aiohttp_mcp-0.5.0rc2.tar.gz
  • Upload date:
  • Size: 21.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for aiohttp_mcp-0.5.0rc2.tar.gz
Algorithm Hash digest
SHA256 97481bcdad5f9ff73a464c09e4f6894d5335e99bdeb2b4b820ef608bee2200b7
MD5 92c07541530bc6038ecddb4a0b3ebf95
BLAKE2b-256 b9f19a18c3eab243ed3b8aa07ea81eb6d13ce1e3f71ce8e5e377d33f26fa56b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiohttp_mcp-0.5.0rc2.tar.gz:

Publisher: publish.yml on kulapard/aiohttp-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file aiohttp_mcp-0.5.0rc2-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_mcp-0.5.0rc2-py3-none-any.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for aiohttp_mcp-0.5.0rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 c4efe5670c2d9bf5e521e523cf1224fb251baab10d105cfb84dbf7d31585fdfe
MD5 6f118a4955724b753f13ece8a07de296
BLAKE2b-256 bed644e36b04f670f5fee9602f6f48c7c6674f6b8f70b25b03787b34829a33af

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiohttp_mcp-0.5.0rc2-py3-none-any.whl:

Publisher: publish.yml on kulapard/aiohttp-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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