Skip to main content

A Simple Python MCP Solution

Project description

aiomcp

A Simple Python MCP Solution

Mission of aiomcp

Start with a smooth experience, end with a compliant solution.

Tutorial

A simple McpServer

def func(a: int, b: int):
    return a + b

impl = ToolImpl()

mcp_server = McpServer("mcp-server-name")
await mcp_server.register_tool(func)
await mcp_server.register_tool(impl.method, alias="tool_alias")
await mcp_server.register_tool(ToolImpl.class_method)
await mcp_server.host("http://127.0.0.1:8000/mcp")

A simple McpClient

mcp_client = McpClient("mcp-client-name")
await mcp_client.initialize("http://127.0.0.1:8000/mcp")
# await mcp_client.initialize(McpHttpTransport("127.0.0.1", 8000, "/mcp"))
# await mcp_client.initialize(McpStdioClientTransport([sys.executable, "server.py"]))
# await mcp_client.initialize(mcp_server) for quick testing without hosting a server
sum_value = await mcp_client.invoke("name", {"a": 1, "b": 2})
# standard MCP functions
# await mcp_client.mcp_tools_list() -> list[McpTool]
# await mcp_client.mcp_tools_call(tool: McpTool, request: McpCallToolRequest) -> McpResponse | McpError

Using McpTransports

While McpClient and McpServer handle client/server behavior, they are implemented in a platform-agnostic way by using McpClientTransport/McpServerTransport to handle message streams. Client and server transports usually come in pairs as McpTransport implementations.

mcp_server = McpServer()
await mcp_server.host(McpHttpTransport("127.0.0.1", 8000, "/mcp"))
# await mcp_server.host("http://127.0.0.1:8000/mcp")
mcp_client = McpClient()
await mcp_client.initialize(McpHttpTransport("127.0.0.1", 8000, "/mcp"))
# await mcp_client.initialize("http://127.0.0.1:8000/mcp")

Supported McpTransport implementations:

  • McpDirectTransport: Simple client driven transport that invokes McpServer with minimal MCP protocol overhead.
  • McpMemoryTransport: Single event loop, in-memory transport.
  • McpHttpTransport
  • McpStdioClientTransport / McpStdioServerTransport: stdio based transport

Sample: Using McpMemoryTransport for local debugging.

transport = McpMemoryTransport()

mcp_server = McpServer()
asyncio.create_task(mcp_server.host(transport))

mcp_client = McpClient()
await mcp_client.initialize(transport)

Tool metadata

Use Annotated with Pydantic Field for parameter descriptions, and pass MCP tool annotations when registering tools.

from typing import Annotated

from pydantic import Field


async def get_current_time(
    timezone: Annotated[
        str,
        Field(
            description="IANA timezone name, e.g. 'Europe/London'. Use '{CURRENT_TIMEZONE}' as local timezone."
        ),
    ],
) -> dict[str, str]:
    return {"timezone": timezone}


await mcp_server.register_tool(
    get_current_time,
    annotations={
        "readOnlyHint": True,
        "destructiveHint": False,
        "idempotentHint": True,
        "openWorldHint": False,
    },
    format_map={"CURRENT_TIMEZONE": "America/Los_Angeles"},
)

Authorization

Connect to an OAuth 2.1 protected remote MCP server.

from aiomcp import McpClient, McpAuthorizationClient

authorization = await McpAuthorizationClient.discover("http://remote-server/mcp")

# Or use a bearer token
# authorization = McpAuthorizationClient("your-access-token")

client = McpClient("mcp-client-name")
await client.initialize("http://remote-server/mcp", authorization=authorization)

Use GitHub as the OAuth provider to connect to GitHub's MCP server.

from aiomcp import McpClient, McpAuthorizationClient

server_url = "https://api.githubcopilot.com/mcp/"

authorization = await McpAuthorizationClient.discover(
    server_url,
    client_id="{YOUR_CLIENT_ID}",
    client_secret="{YOUR_CLIENT_SECRET}",
)

client = McpClient("mcp-client-name")
await client.initialize(server_url, authorization=authorization)

Protect your MCP server with built-in OAuth 2.1 + Dynamic Client Registration.

from aiomcp import McpServer, McpAuthorizationServer

server = McpServer("mcp-server-name")
await server.register_tool(add, alias="add")

authorization = McpAuthorizationServer()
await server.host("http://127.0.0.1:8000/mcp", authorization=authorization)

Compatibility flags

aiomcp defaults to broad compatibility, so it can connect to a wider range of MCP clients and servers. Compatibility flags let you opt into stricter protocol checks when you want closer MCP enforcement.

from aiomcp import McpClient, McpServer

mcp_client = McpClient(
    flags={"enforce_mcp_tool_result_content": True}
)

mcp_server = McpServer(
    flags={"enforce_mcp_initialize_sequence": True}
)

Available client flags

  • enforce_mcp_tools_capability: require the server to advertise the tools capability before the client sends tools/list.
  • enforce_mcp_tool_result_content: reject tool results that omit content.
  • enforce_mcp_version_negotiation: reject unsupported negotiated protocol versions.
  • enforce_mcp_session_header: require HTTP session headers where applicable.
  • enforce_mcp_protocol_header: require HTTP protocol version headers where applicable.
  • enforce_mcp_transport_version_consistency: require HTTP header and initialize body protocol versions to match.

Available server flags

  • enforce_mcp_initialize_sequence: reject requests sent before MCP initialization completes.
  • enforce_mcp_version_negotiation: negotiate only supported protocol versions.
  • enforce_mcp_session_header: require HTTP session headers where applicable.
  • enforce_mcp_protocol_header: require HTTP protocol version headers where applicable.

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

aiomcp-0.0.9.tar.gz (43.2 kB view details)

Uploaded Source

Built Distribution

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

aiomcp-0.0.9-py3-none-any.whl (36.4 kB view details)

Uploaded Python 3

File details

Details for the file aiomcp-0.0.9.tar.gz.

File metadata

  • Download URL: aiomcp-0.0.9.tar.gz
  • Upload date:
  • Size: 43.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for aiomcp-0.0.9.tar.gz
Algorithm Hash digest
SHA256 20d1175cefb6efc2230f4887760cb4786c32ff6636980ea22ff1e415cdecce3b
MD5 d14bd3e0bb7411acf17c356a49aa62b6
BLAKE2b-256 9fb3df7120f1ad3cca09b22b2f8bfbcbe6c3a4884187602a5529e2f2eb188918

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiomcp-0.0.9.tar.gz:

Publisher: publish.yml on yangyuan/aiomcp

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

File details

Details for the file aiomcp-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: aiomcp-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 36.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for aiomcp-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 e0f45e5e834de922bbeaffb36abb4f5d1179a8840b5857db0781f7769fd957cb
MD5 3a67ae761c3b97d935f4dc72b6b4c10d
BLAKE2b-256 e93c762bfe1f634dac77f12a8a64cfa96f628c1a06fc8feeb6cd65c887cc8ecf

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiomcp-0.0.9-py3-none-any.whl:

Publisher: publish.yml on yangyuan/aiomcp

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