Repository navigation
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.
+1,140
−4
Open
Changes from 1 commit
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
Preserve borrowed AMQP channels during peer shutdown
- Loading branch information
commit cdf9c241821b0f22e9305bc00b4a6124e050327c
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
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,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" | ||
|
Kludex marked this conversation as resolved.
|
||
| ) | ||
| 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) | ||
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
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.