feature/loki-log-queries #7

Merged
torjus merged 2 commits from feature/loki-log-queries into master 2026-02-05 20:06:34 +00:00
Owner

Summary

Add Loki log query support to the lab-monitoring MCP server. Three new tools are registered when LOKI_URL is configured (opt-in):

  • query_logs — Execute LogQL range queries against Loki with configurable time range, limit (capped at 5000), and direction
  • list_labels — List available label names from Loki
  • list_label_values — List values for a specific label

Changes

  • Loki HTTP client (internal/monitoring/loki.go) — LokiClient with QueryRange, Labels, and LabelValues methods, following the same pattern as PrometheusClient
  • Handlers (internal/monitoring/handlers.go) — RegisterHandlers now accepts an optional *LokiClient; tools are conditionally registered when non-nil. Includes parseTimeArg helper supporting relative durations, RFC3339, and Unix epoch
  • Formatting (internal/monitoring/format.go) — formatLogStreams, formatLabels, formatLabelValues for markdown output
  • CLI commands (cmd/lab-monitoring/main.go) — logs and labels subcommands, --loki-url global flag / LOKI_URL env var
  • NixOS module (nix/lab-monitoring-module.nix) — lokiUrl option (nullOr str, default null), conditionally sets LOKI_URL env var
  • Version bump — lab-monitoring 0.1.00.2.0

Security hardening

  • Log query limit parameter capped at 5000 to prevent unbounded memory usage
  • direction parameter validated server-side against allowed values

Tests

  • 5 new Loki client tests (loki_test.go)
  • 4 new handler tests (QueryLogs, ListLabels, ListLabelValues, ToolCountWithLoki)
  • All existing tests unaffected

Verification

  • go test ./... -short — all pass
  • golangci-lint run ./... — 0 issues
  • go vet ./... — clean
  • nix build .#lab-monitoring — builds successfully
## Summary Add Loki log query support to the lab-monitoring MCP server. Three new tools are registered when `LOKI_URL` is configured (opt-in): - **`query_logs`** — Execute LogQL range queries against Loki with configurable time range, limit (capped at 5000), and direction - **`list_labels`** — List available label names from Loki - **`list_label_values`** — List values for a specific label ### Changes - **Loki HTTP client** (`internal/monitoring/loki.go`) — `LokiClient` with `QueryRange`, `Labels`, and `LabelValues` methods, following the same pattern as `PrometheusClient` - **Handlers** (`internal/monitoring/handlers.go`) — `RegisterHandlers` now accepts an optional `*LokiClient`; tools are conditionally registered when non-nil. Includes `parseTimeArg` helper supporting relative durations, RFC3339, and Unix epoch - **Formatting** (`internal/monitoring/format.go`) — `formatLogStreams`, `formatLabels`, `formatLabelValues` for markdown output - **CLI commands** (`cmd/lab-monitoring/main.go`) — `logs` and `labels` subcommands, `--loki-url` global flag / `LOKI_URL` env var - **NixOS module** (`nix/lab-monitoring-module.nix`) — `lokiUrl` option (nullOr str, default null), conditionally sets `LOKI_URL` env var - **Version bump** — lab-monitoring `0.1.0` → `0.2.0` ### Security hardening - Log query `limit` parameter capped at 5000 to prevent unbounded memory usage - `direction` parameter validated server-side against allowed values ### Tests - 5 new Loki client tests (`loki_test.go`) - 4 new handler tests (QueryLogs, ListLabels, ListLabelValues, ToolCountWithLoki) - All existing tests unaffected ### Verification - `go test ./... -short` — all pass - `golangci-lint run ./...` — 0 issues - `go vet ./...` — clean - `nix build .#lab-monitoring` — builds successfully
torjus added 2 commits 2026-02-05 20:02:59 +00:00
Add 3 opt-in Loki tools (query_logs, list_labels, list_label_values)
that are registered when LOKI_URL is configured. Includes Loki HTTP
client, CLI commands (logs, labels), NixOS module option, formatting,
and tests.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Prevent unbounded memory usage by capping the limit parameter to 5000.
Validate direction against allowed values instead of passing through
to Loki unchecked.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
torjus merged commit 9dfe61e170 into master 2026-02-05 20:06:34 +00:00
torjus deleted branch feature/loki-log-queries 2026-02-05 20:06:34 +00:00
This repo is archived. You cannot comment on pull requests.
No Reviewers
No Label
1 Participants
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: torjus/labmcp#7