-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Add an AMQP 0.9.1 transport example #3521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kludex
wants to merge
5
commits into
transport-mqtt
Choose a base branch
from
transport-amqp
base: transport-mqtt
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bbaffce
Add an AMQP 0.9.1 transport example
Kludex 4a9d98a
Remove postponed annotations from the AMQP example
Kludex 36a5f02
Reserve AMQP topology provisioning for the server
Kludex cdf9c24
Preserve borrowed AMQP channels during peer shutdown
Kludex 3c3a2d3
Document manual AMQP cleanup after process crashes
Kludex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| { | ||
| "users": [ | ||
| {"name": "server", "password_hash": "dGVzdISQMYLFRDkVmIyH/2iPR3elfgPHcZO7uFXGqTE0UeU9", "hashing_algorithm": "rabbit_password_hashing_sha256", "tags": []}, | ||
| {"name": "alice", "password_hash": "dGVzdMhyAjoCEMSETc4cmiL+/OInoknje5+9BVEZ8SaVlj+Z", "hashing_algorithm": "rabbit_password_hashing_sha256", "tags": []}, | ||
| {"name": "bob", "password_hash": "dGVzdFu3/YOufljrGKLUVvAqIh2R9WeQ87Ql039pWvRL9HZO", "hashing_algorithm": "rabbit_password_hashing_sha256", "tags": []} | ||
| ], | ||
| "vhosts": [{"name": "/"}], | ||
| "permissions": [ | ||
| {"user": "server", "vhost": "/", "configure": ".*", "write": ".*", "read": ".*"}, | ||
| {"user": "alice", "vhost": "/", "configure": "^$", "write": "^mcp\\.alice\\.[^.]+\\.requests\\.exchange$", "read": "^mcp\\.alice\\.[^.]+\\.responses$"}, | ||
| {"user": "bob", "vhost": "/", "configure": "^$", "write": "^mcp\\.bob\\.[^.]+\\.requests\\.exchange$", "read": "^mcp\\.bob\\.[^.]+\\.responses$"} | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| definitions.import_backend = local_filesystem | ||
| definitions.local.path = /etc/rabbitmq/definitions.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| """Exercise the AMQP 0.9.1 adapter against the local RabbitMQ broker.""" | ||
|
|
||
| import os | ||
| from contextlib import AsyncExitStack | ||
|
|
||
| import aio_pika | ||
| import anyio | ||
| from aio_pika.abc import AbstractChannel | ||
| from mcp.shared.transport import Transport | ||
|
|
||
| from demo_common import verify | ||
| from mcp_transport_examples.amqp import amqp_transport | ||
|
|
||
|
|
||
| async def open_transport(stack: AsyncExitStack, principal: str, session: str, server_side: bool) -> Transport: | ||
| user = "server" if server_side else principal | ||
| connection = await aio_pika.connect( | ||
| host="127.0.0.1", | ||
| port=int(os.environ.get("AMQP_TEST_PORT", "15673")), | ||
| login=user, | ||
| password=f"test-{user}-password", | ||
| ) | ||
| await stack.enter_async_context(connection) | ||
| channel = await connection.channel(publisher_confirms=True, on_return_raises=True) | ||
| stack.push_async_callback(channel.close) | ||
| queue = f"mcp.{principal}.{session}" | ||
| if server_side: | ||
| await provision(stack, channel, queue) | ||
| incoming, outgoing = ("requests", "responses") if server_side else ("responses", "requests") | ||
| return amqp_transport(channel, incoming_queue=f"{queue}.{incoming}", outgoing_queue=f"{queue}.{outgoing}") | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| for highlevel in (False, True): | ||
| for mode in ("legacy", "auto", "2026-07-28"): | ||
| with anyio.fail_after(5): | ||
| await verify(open_transport, kind="amqp", highlevel=highlevel, mode=mode) | ||
|
|
||
|
|
||
| async def provision(stack: AsyncExitStack, channel: AbstractChannel, prefix: str) -> None: | ||
| """Keep exchanges until every transport entered after this setup has exited.""" | ||
| for direction in ("requests", "responses"): | ||
| name = f"{prefix}.{direction}" | ||
| destination = await channel.declare_queue( | ||
| name, | ||
| auto_delete=True, | ||
| arguments={"x-expires": 60_000, "x-max-length": 256, "x-overflow": "reject-publish"}, | ||
| ) | ||
| exchange = await channel.declare_exchange(f"{name}.exchange", auto_delete=False) | ||
| stack.push_async_callback(exchange.delete, if_unused=False) | ||
| await destination.bind(exchange, routing_key=name) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| anyio.run(main) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| """Check AMQP close ordering, routing readiness, and the remote-loss timeout boundary.""" | ||
|
|
||
| import os | ||
| from contextlib import AsyncExitStack | ||
| from uuid import uuid4 | ||
|
|
||
| import aio_pika | ||
| import anyio | ||
| from mcp import Client, MCPError | ||
| from mcp.server.mcpserver import MCPServer | ||
| from mcp.shared.transport import SessionMessage | ||
| from mcp.types import CONNECTION_CLOSED, REQUEST_TIMEOUT, JSONRPCRequest | ||
|
|
||
| from demo_amqp import provision | ||
| from mcp_transport_examples.amqp import amqp_transport | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| """Exercise real broker behavior, including peer loss that requires a configured request deadline.""" | ||
| port = int(os.environ.get("AMQP_TEST_PORT", "15673")) | ||
| for scenario in ("server-first", "client-first", "queued", "return", "raise-return", "peer-loss"): | ||
| with anyio.fail_after(5): | ||
| async with AsyncExitStack() as stack: | ||
| owner = await stack.enter_async_context( | ||
| await aio_pika.connect( | ||
| host="127.0.0.1", | ||
| port=port, | ||
| login="server", | ||
| password="test-server-password", | ||
| ) | ||
| ) | ||
| setup = await owner.channel() | ||
| stack.push_async_callback(setup.close) | ||
| prefix = f"mcp.alice.{uuid4().hex}" | ||
| await provision(stack, setup, prefix) | ||
| server_connection = await stack.enter_async_context( | ||
| await aio_pika.connect( | ||
| host="127.0.0.1", | ||
| port=port, | ||
| login="server", | ||
| password="test-server-password", | ||
| ) | ||
| ) | ||
| client_connection = await stack.enter_async_context( | ||
| await aio_pika.connect( | ||
| host="127.0.0.1", | ||
| port=port, | ||
| login="alice", | ||
| password="test-alice-password", | ||
| ) | ||
| ) | ||
| server_channel = await server_connection.channel() | ||
| stack.push_async_callback(server_channel.close) | ||
| client_channel = await client_connection.channel( | ||
| publisher_confirms=True, on_return_raises=scenario != "return" | ||
| ) | ||
| stack.push_async_callback(client_channel.close) | ||
| server_transport = amqp_transport( | ||
| server_channel, | ||
| incoming_queue=f"{prefix}.requests", | ||
| outgoing_queue=f"{prefix}.responses", | ||
| expiry=120, | ||
| ) | ||
| client_transport = amqp_transport( | ||
| client_channel, | ||
| incoming_queue=f"{prefix}.responses", | ||
| outgoing_queue=f"{prefix}.requests", | ||
| ) | ||
| if scenario in ("server-first", "client-first"): | ||
| first, second = ( | ||
| (server_transport, client_transport) | ||
| if scenario == "server-first" | ||
| else (client_transport, server_transport) | ||
| ) | ||
| async with second as (receive, write): | ||
| async with first: | ||
| pass | ||
| try: | ||
| await receive.receive() | ||
| except anyio.EndOfStream: | ||
| pass | ||
| else: | ||
| raise AssertionError("The peer close must end the read stream") | ||
| try: | ||
| await write.send(SessionMessage(JSONRPCRequest(jsonrpc="2.0", id=1, method="ping"))) | ||
| except anyio.ClosedResourceError: | ||
| pass | ||
| else: | ||
| raise AssertionError("The peer close must also close the writer") | ||
| elif scenario == "queued": | ||
| message = SessionMessage( | ||
| JSONRPCRequest( | ||
| jsonrpc="2.0", | ||
| id=1, | ||
| method="initialize", | ||
| params={ | ||
| "protocolVersion": "2025-11-25", | ||
| "capabilities": {}, | ||
| "clientInfo": {"name": "lifecycle", "version": "1"}, | ||
| }, | ||
| ) | ||
| ) | ||
| async with client_transport as (_, write): | ||
| await write.send(message) | ||
| async with server_transport as (read, _): | ||
| received = await read.receive() | ||
| assert isinstance(received, SessionMessage) | ||
| assert received.message == message.message | ||
| elif scenario in ("return", "raise-return"): | ||
| queue = await setup.get_queue(f"{prefix}.requests", ensure=False) | ||
| exchange = await setup.get_exchange(f"{prefix}.requests.exchange", ensure=False) | ||
| await queue.unbind(exchange, routing_key=f"{prefix}.requests") | ||
| async with Client(client_transport, mode="2026-07-28") as client: | ||
| try: | ||
| await client.list_tools() | ||
| except MCPError as exc: | ||
| assert exc.code == CONNECTION_CLOSED | ||
| else: | ||
| raise AssertionError("An unroutable publish must fail immediately") | ||
| else: | ||
| entered = anyio.Event() | ||
| finished = anyio.Event() | ||
| server = MCPServer("AMQP peer loss") | ||
|
|
||
| @server.tool() | ||
| async def hold() -> str: | ||
| entered.set() | ||
| await anyio.sleep_forever() | ||
| raise NotImplementedError | ||
|
|
||
| runtime = await stack.enter_async_context(server.serve()) | ||
| await runtime.connect(server_transport) | ||
| # Remote-idle loss is not signalled by AMQP; the deadline is the behavior under test. | ||
| async with Client(client_transport, mode="2026-07-28", read_timeout_seconds=1) as client: | ||
|
|
||
| async def call() -> None: | ||
| try: | ||
| await client.call_tool("hold") | ||
| except MCPError as exc: | ||
| assert exc.code == REQUEST_TIMEOUT | ||
| else: | ||
| raise AssertionError("The remote-loss deadline must settle the request") | ||
| finished.set() | ||
|
|
||
| async with anyio.create_task_group() as tg: | ||
| tg.start_soon(call) | ||
| await entered.wait() | ||
| assert not finished.is_set() | ||
| await server_connection.close() | ||
| await finished.wait() | ||
| assert not client_channel.is_closed | ||
| await client_channel.set_qos(prefetch_count=16) | ||
| if scenario != "peer-loss": | ||
| assert not server_channel.is_closed | ||
| await server_channel.set_qos(prefetch_count=16) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| anyio.run(main) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.