Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ Use uvx directly in your MCP client config
- `--http-auth-audience`: Audience for FastMCP Bearer Auth Provider.
- `--http-auth-required-scopes`: Required scopes for FastMCP Bearer Auth Provider.

#### Environment Variables

If `RABBITMQ_HOST`, `RABBITMQ_USERNAME`, and `RABBITMQ_PASSWORD` are set, the server connects automatically on startup — no `initialize_connection` tool call required. Useful when running behind MCP proxies that spawn a fresh process per session.

| Variable | Description | Default |
|----------|-------------|---------|
| `RABBITMQ_HOST` | Broker hostname | _(required)_ |
| `RABBITMQ_USERNAME` | Auth username | _(required)_ |
| `RABBITMQ_PASSWORD` | Auth password | _(required)_ |
| `RABBITMQ_PORT` | AMQP port | `5671` |
| `RABBITMQ_MANAGEMENT_PORT` | Management API port | `443` (TLS) / `15672` (non-TLS) |
| `RABBITMQ_USE_TLS` | `true` or `false` | `true` |
| `RABBITMQ_ALIAS` | Broker alias for multi-broker setups | hostname |

## Usage Examples

### Strands Agent Example
Expand Down
50 changes: 50 additions & 0 deletions src/rabbitmq/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# This file is part of the awslabs namespace.
# It is intentionally minimal to support PEP 420 namespace packages.

import os
from typing import Any

from mcp.server.fastmcp import FastMCP
Expand Down Expand Up @@ -104,6 +105,55 @@ def register_rabbitmq_management_tools(self, allow_mutative_tools: bool = False)
self.__register_read_only_tools()
if allow_mutative_tools:
self.__register_mutative_tools()
self._auto_connect_from_env()

def _auto_connect_from_env(self) -> None:
"""Connect to a broker at startup if RABBITMQ_* env vars are present.

In some MCP aggregator setups the server subprocess does not stay
running between calls, so connection state set via
initialize_connection does not persist. Configuring credentials
through the environment makes the tools usable on every spawn.

Env vars:
RABBITMQ_HOST – broker hostname (required)
RABBITMQ_USERNAME – auth username (required)
RABBITMQ_PASSWORD – auth password (required)
RABBITMQ_PORT – AMQP port (default: 5671)
RABBITMQ_MANAGEMENT_PORT – management API port; falls back to
--management-port CLI arg, then to
443 / 15672 depending on TLS
RABBITMQ_USE_TLS – "true" or "false" (default: "true")
RABBITMQ_ALIAS – broker alias (default: hostname)
"""
host = os.environ.get("RABBITMQ_HOST", "")
user = os.environ.get("RABBITMQ_USERNAME", "")
pw = os.environ.get("RABBITMQ_PASSWORD", "")
if not (host and user and pw):
return
port = int(os.environ.get("RABBITMQ_PORT", "5671") or "5671")
mgmt_port_str = os.environ.get("RABBITMQ_MANAGEMENT_PORT", "")
mgmt_port = int(mgmt_port_str) if mgmt_port_str else self.default_management_port
use_tls = os.environ.get("RABBITMQ_USE_TLS", "true").lower() in ("true", "1", "yes")
try:
rmq = RabbitMQConnection(
hostname=host, username=user, password=pw,
port=port, use_tls=use_tls,
)
rmq_admin = RabbitMQAdmin(
hostname=host, username=user, password=pw,
use_tls=use_tls, port=mgmt_port,
)
rmq_admin.test_connection()
alias = os.environ.get("RABBITMQ_ALIAS", host)
self.brokers[alias] = {
"rmq": rmq,
"rmq_admin": rmq_admin,
"hostname": host,
}
self.active_alias = alias
except Exception:
pass # Fall back to manual initialization via tool call

def __register_critical_tools(self):
@self.mcp.tool()
Expand Down
Loading