get_silence
Retrieve details of an alert silence by providing its unique identifier. Use this to inspect silence configurations and status.
Instructions
Get a silence by its ID
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| silence_id | Yes |
Implementation Reference
- src/alertmanager_mcp_server/server.py:400-414 (registration)Tool registration via @mcp.tool decorator for get_silence
@mcp.tool(description="Get a silence by its ID") async def get_silence(silence_id: str): """Get a silence by its ID Parameters ---------- silence_id : str The ID of the silence to be retrieved. Returns ------- dict: The Silence object from Alertmanager instance. """ return make_request(method="GET", route=url_join("/api/v2/silences/", silence_id)) - Handler function get_silence - makes GET request to /api/v2/silences/{silence_id}
@mcp.tool(description="Get a silence by its ID") async def get_silence(silence_id: str): """Get a silence by its ID Parameters ---------- silence_id : str The ID of the silence to be retrieved. Returns ------- dict: The Silence object from Alertmanager instance. """ return make_request(method="GET", route=url_join("/api/v2/silences/", silence_id)) - make_request helper that actually executes the HTTP request
def make_request(method="GET", route="/", **kwargs): """Make HTTP request and return a requests.Response object. Parameters ---------- method : str HTTP method to use for the request. route : str (Default value = "/") This is the url we are making our request to. **kwargs : dict Arbitrary keyword arguments. Returns ------- dict: The response from the Alertmanager API. This is a dictionary containing the response data. """ try: route = url_join(config.url, route) auth = ( requests.auth.HTTPBasicAuth(config.username, config.password) if config.username and config.password else None ) # Add X-Scope-OrgId header for multi-tenant setups # Priority: 1) Request header from caller (via ContextVar), 2) Static config tenant headers = kwargs.get("headers", {}) tenant_id = _current_scope_org_id.get() or config.tenant_id if tenant_id: headers["X-Scope-OrgId"] = tenant_id if headers: kwargs["headers"] = headers response = requests.request( method=method.upper(), url=route, auth=auth, timeout=60, **kwargs ) response.raise_for_status() result = response.json() # Ensure we always return something (empty list is valid but might cause issues) if result is None: return {"message": "No data returned"} return result except requests.exceptions.RequestException as e: return {"error": str(e)} - url_join helper used to construct the API route with silence_id
def url_join(base: str, path: str) -> str: """Join a base URL with a path, preserving the base URL's path component. Unlike urllib.parse.urljoin, this function preserves the path in the base URL when the path argument starts with '/'. This is useful for APIs hosted at subpaths (e.g., http://localhost:8080/alertmanager). Examples -------- >>> url_join("http://localhost:8080/alertmanager", "/api/v2/alerts") 'http://localhost:8080/alertmanager/api/v2/alerts' >>> url_join("http://localhost:8080/alertmanager/", "/api/v2/alerts") 'http://localhost:8080/alertmanager/api/v2/alerts' >>> url_join("http://localhost:8080", "/api/v2/alerts") 'http://localhost:8080/api/v2/alerts' Parameters ---------- base : str The base URL which may include a path component path : str The path to append, which may or may not start with '/' Returns ------- str The combined URL with both base path and appended path """ # Remove trailing slash from base if present base = base.rstrip('/') # Remove leading slash from path if present path = path.lstrip('/') # Combine with a single slash return f"{base}/{path}"