fix: UNIFI_SITE environment variable not passed to exporter#59
Conversation
The UNIFI_SITE environment variable is configured and loaded but never passed to the unifi_exporter.py script. This causes the exporter to default to the 'default' site, which breaks functionality for UniFi controllers with multiple sites. Changes: - Add --site parameter to exporter command in fetch_unifi_data() - Site parameter now properly passed from environment to exporter - Defaults to 'default' if UNIFI_SITE not set (backward compatible) Fixes multi-site controller support where queries were returning wrong or empty data due to site mismatch.
There was a problem hiding this comment.
Pull request overview
Fixes UniFi multi-site support by ensuring the configured site is used when invoking unifi_exporter.py, rather than always defaulting to the controller’s default site.
Changes:
- Adds
--siteargument to the UniFi exporter subprocess invocation. - (Intended) wires
UNIFI_SITEconfiguration through the MCP UniFi server to the exporter.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| "--site", | ||
| UNIFI_SITE, |
There was a problem hiding this comment.
UNIFI_SITE is referenced in the exporter command but is never defined in this module (no os.getenv/load). This will raise a NameError at runtime. Define UNIFI_SITE (e.g., from os.getenv with a default) before using it here.
| # Load .env with security hardening | ||
| UNIFI_ALLOWED_VARS = COMMON_ALLOWED_ENV_VARS | { | ||
| "UNIFI_HOST", | ||
| "UNIFI_API_KEY", | ||
| } | ||
|
|
||
| # Only load env file at module level if not in unified mode | ||
| if not os.getenv("MCP_UNIFIED_MODE"): | ||
| load_env_file(ENV_FILE, allowed_vars=UNIFI_ALLOWED_VARS, strict=True) | ||
|
|
||
| UNIFI_EXPORTER_PATH = SCRIPT_DIR / "unifi_exporter.py" | ||
| UNIFI_HOST = os.getenv("UNIFI_HOST", "192.168.1.1") | ||
| UNIFI_API_KEY = os.getenv("UNIFI_API_KEY", "") | ||
|
|
There was a problem hiding this comment.
UNIFI_SITE is not included in UNIFI_ALLOWED_VARS and is not loaded from the .env file. With strict allowlisting, a UNIFI_SITE entry in .env will be ignored, so site selection won’t work even after adding the CLI argument. Add UNIFI_SITE to the allowlist and load it (defaulting to "default").
| UNIFI_EXPORTER_PATH = SCRIPT_DIR / "unifi_exporter.py" | ||
| UNIFI_HOST = os.getenv("UNIFI_HOST", "192.168.1.1") | ||
| UNIFI_API_KEY = os.getenv("UNIFI_API_KEY", "") | ||
|
|
There was a problem hiding this comment.
This introduces support for configuring a UniFi site via UNIFI_SITE, but the repo’s configuration templates/docs currently only mention UNIFI_API_KEY and UNIFI_HOST. Please update the relevant documentation (e.g., .env.example / README) to include UNIFI_SITE and its default behavior so users can discover it.
UNIFI_SITE was used in the exporter command but never added to UNIFI_ALLOWED_VARS or loaded via os.getenv, causing a NameError at runtime. Add both the allowlist entry and the variable definition. Addresses Copilot review feedback on PR bjeans#59.
|
@claude can you review and let me know if this is safe to merge, or if there are additional updates needed. |
|
I'll analyze this and get back to you. |

Pull Request: Fix UNIFI_SITE Environment Variable Not Passed to Exporter
Summary
The
UNIFI_SITEenvironment variable is configured and loaded but never passed to theunifi_exporter.pyscript. This causes the exporter to default to the "default" site, which breaks functionality for UniFi controllers with multiple sites.Issue Description
Current Behavior
UNIFI_SITEis defined in allowed environment variablesos.getenv("UNIFI_SITE", "default")Expected Behavior
UNIFI_SITEshould be passed to the exporter via--siteargumentRoot Cause
File:
unifi_mcp_optimized.pyCurrent Code (Lines 140-153)
Evidence of Configuration
Proposed Fix
File:
unifi_mcp_optimized.py(Lines 140-153)Impact
Before Fix
UNIFI_SITEenvironment variable is silently ignoredAfter Fix
Testing
Test Case: Multi-Site Controller
Setup:
Steps:
list_devices,list_clients)Expected Result:
Verification:
Check container logs for exporter command:
Files Changed
unifi_mcp_optimized.py--site UNIFI_SITEto exporter command argumentsBackward Compatibility
✅ Fully backward compatible
UNIFI_SITEnot set: defaults to "default" (existing behavior)UNIFI_SITEis set: uses specified site (new functionality)Additional Context
This appears to be an oversight in the original implementation or a regression from the v3.0.0 FastMCP migration. The infrastructure for site selection exists but the final connection to pass it to the exporter was missed.
Checklist