nicolargo/glances · error · RuntimeError

GlancesMcpServer: stats manager is not yet initialized. Call

Error message

GlancesMcpServer: stats manager is not yet initialized. Call set_stats() before serving MCP requests.

What it means

Every MCP resource/tool in GlancesMcpServer (plugins_list, all_stats, plugin_stats, plugin_history, plugin_limits, all_limits) calls _get_stats(), which raises RuntimeError if the stats manager was never injected via set_stats(). This is an internal wiring invariant: the server was constructed but not attached to a live Glances stats loop.

Source

Thrown at glances/outputs/glances_mcp.py:170

        Otherwise each hostname is converted to a "host:*" pattern (any port)
        and paired with http/https origin patterns.
        """
        if self.mcp_allowed_hosts == ["*"]:
            return TransportSecuritySettings(enable_dns_rebinding_protection=False)

        # Append :* to plain hostnames so the middleware matches any port.
        allowed_hosts = [h if ":" in h else f"{h}:*" for h in self.mcp_allowed_hosts]
        allowed_origins = [f"http://{h}" for h in allowed_hosts] + [f"https://{h}" for h in allowed_hosts]
        return TransportSecuritySettings(
            enable_dns_rebinding_protection=True,
            allowed_hosts=allowed_hosts,
            allowed_origins=allowed_origins,
        )

    def _get_stats(self):
        """Return the stats manager, raising RuntimeError if not yet set."""
        if self._stats is None:
            raise RuntimeError(
                "GlancesMcpServer: stats manager is not yet initialized. Call set_stats() before serving MCP requests."
            )
        return self._stats

    def _serialize(self, data) -> str:
        """Return a UTF-8 JSON string using Glances' custom serializer.

        Glances' json_dumps handles non-standard numeric types and datetime
        objects that the stdlib json module cannot encode out of the box.
        """
        return json_dumps(data).decode("utf-8")

    # ------------------------------------------------------------------
    # Resources
    # ------------------------------------------------------------------

    def _setup_resources(self):
        """Declare all MCP resources on the FastMCP instance."""

View on GitHub (pinned to a240d8dfb3)

Solutions

  1. Pass the stats manager when constructing: GlancesMcpServer(stats, args, config).
  2. Or call server.set_stats(stats_manager) before run()/serve().
  3. When using the documented --mint CLI path, ensure you're on a Glances version where the entrypoint wires stats automatically; report a bug if the CLI path hits this.

Example fix

# before
server = GlancesMcpServer(None, args, config)
server.run()

# after
server = GlancesMcpServer(stats, args, config)
# or: server.set_stats(stats)
server.run()
Defensive patterns

Strategy: validation

Validate before calling

assert server._stats is not None or stats_manager is not None, 'call set_stats() before serving'

Try / catch

try:
    stats = server._get_stats()
except RuntimeError:
    server.set_stats(stats_manager)  # late-bind then retry

Prevention

When it happens

Trigger: Programmatically creating GlancesMcpServer(stats=None) (or omitting stats) and then serving MCP requests; any integration that builds the server manually and forgets set_stats(). Note the constructor stores stats positionally, so passing None explicitly or constructing before stats exist triggers it.

Common situations: Embedding Glances' MCP server into a custom script; test harnesses instantiating the server standalone; version upgrades changing the init signature.

Related errors


AI-assisted analysis of nicolargo/glances@a240d8dfb3 (2026-08-27). Data as JSON: /api/errors/cefe7a61fac8385f. Report an issue: GitHub.