diff --git a/README.md b/README.md index 57af167..768424b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/rabbitmq/module.py b/src/rabbitmq/module.py index 28f32be..a2188fc 100644 --- a/src/rabbitmq/module.py +++ b/src/rabbitmq/module.py @@ -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 @@ -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()