@kealthas-dev/opencode-mcp-loki 1.0.13 → 1.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -82,4 +82,4 @@ The default config file is already in place inside that shell — `docker-entryp
82
82
 
83
83
  **Direct-to-Loki path verified end-to-end and automated.** `loki.test.ts` covers tool listing, `loki_labels`/`loki_label_values` finding pushed test data, `loki_query_range` finding a pushed log line by content, an empty-result query returning cleanly (not an error), and a malformed LogQL query returning a clean error — driven over the real Streamable HTTP transport against the sandbox's `loki` service. Wired into `deploy/opencode.json.example` (`mcp.loki`, `type: "remote"`, `enabled: true`).
84
84
 
85
- **`LOKI_VIA_GRAFANA` verified only against a fake stand-in HTTP server** (confirms the request lands on `/api/datasources/proxy/<id>/...` with the right Basic Auth header) — not against a real Grafana instance, and not covered by `loki.test.ts`. See `mcp-servers/TODO.md` for adding a real Grafana fixture to the sandbox.
85
+ **`LOKI_VIA_GRAFANA` verified against a fake stand-in HTTP server, now automated in `loki.test.ts`** (confirms the request lands on `/api/datasources/proxy/<id>/...` with the right Basic Auth header, and that a redirect-to-login response — real Grafana's actual failure mode for rejected auth, a 302, not a 401 — surfaces as a clean `{success: false}` diagnostic instead of an opaque JSON-parse error) — still not against a real Grafana instance. See `mcp-servers/TODO.md` for adding a real Grafana fixture to the sandbox.
package/dist/server.js CHANGED
@@ -53,6 +53,8 @@ function loadServerConfig() {
53
53
  }
54
54
  }
55
55
  // config.json by default, config-<name>.json when LOKI_CONFIG_ENV is set.
56
+ // Only LOKI_BASE_URL is required - Loki is commonly reachable unauthenticated
57
+ // on an internal LAN, unlike mcp-servers/oracle.
56
58
  function loadLokiConfig() {
57
59
  const envName = process.env.LOKI_CONFIG_ENV;
58
60
  const resolvedPath = join(CONFIG_DIR, envName ? `config-${envName}.json` : "config.json");
@@ -83,23 +85,15 @@ function loadLokiConfig() {
83
85
  }
84
86
  }
85
87
  const serverConfig = loadServerConfig();
86
- // Computing a correct start/end by hand (an exact RFC3339 offset, or -
87
- // worse - a 19-digit nanosecond epoch) is real friction for whatever's
88
- // calling this tool, model or human. This resolves three friendlier
89
- // forms into whatever Loki actually accepts, so most callers never have
90
- // to touch epoch math at all:
91
- // - "now" / "now-<duration>" (duration is <n><unit> pairs, unit one of
92
- // s/m/h/d, e.g. "now-1h", "now-30m", "now-1d") - the same relative-time
93
- // convention Grafana itself uses for Loki/Prometheus time ranges, not
94
- // an invented one.
95
- // - a bare "YYYY-MM-DDTHH:MM:SS" or "YYYY-MM-DD HH:MM:SS" with no
96
- // timezone - assumed to be in LOKI_DEFAULT_TZ_OFFSET and qualified
97
- // before being sent.
98
- // Anything else (already a full RFC3339 offset, or a bare epoch number)
99
- // passes straight through unchanged - both were already correct inputs
100
- // before this existed, and stay correct. An unrecognized shape also
101
- // passes straight through rather than being rejected here - Loki's own
102
- // parser is the final judge and returns its own clean error, same
88
+ // Hand-computing a correct start/end (an RFC3339 offset, or a 19-digit
89
+ // nanosecond epoch) is real friction, so this resolves two friendlier forms
90
+ // into what Loki accepts:
91
+ // - "now"/"now-<duration>" (s/m/h/d units, e.g. "now-1h") - Grafana's own
92
+ // relative-time convention, not an invented one.
93
+ // - a bare "YYYY-MM-DDTHH:MM:SS"/"YYYY-MM-DD HH:MM:SS" with no timezone -
94
+ // assumed to be LOKI_DEFAULT_TZ_OFFSET and qualified before sending.
95
+ // Anything else (already RFC3339, a bare epoch, or unrecognized) passes
96
+ // straight through - Loki's own parser is the final judge, same
103
97
  // full-passthrough philosophy as everywhere else in this server.
104
98
  const RELATIVE_TIME_RE = /^now(?:-((?:\d+[smhd])+))?$/;
105
99
  const DURATION_PART_RE = /(\d+)([smhd])/g;
@@ -121,14 +115,10 @@ function resolveTimeParam(value, tzOffset) {
121
115
  return `${naive[1]}T${naive[2]}${tzOffset}`;
122
116
  return value;
123
117
  }
124
- // Loki's query API (what every tool below hits) has no write side at all -
125
- // unlike Oracle there's no executeQuery()-style connection lifecycle or
126
- // auditQuery() gate to design around here. Every call is a plain,
127
- // stateless GET; this helper just centralizes URL-building, the optional
128
- // auth headers, and turning a non-2xx/network failure into a clean
129
- // {success: false} instead of a thrown error - mirroring the shape
130
- // executeQuery() returns in mcp-servers/oracle/src/server.ts, for the same reason:
131
- // tool results should never surface as a raw MCP protocol error.
118
+ // Loki's API is read-only, so unlike Oracle there's no connection lifecycle
119
+ // or audit gate here - just a stateless GET. Centralizes URL-building, auth
120
+ // headers, and turning a non-2xx/network failure into {success: false}
121
+ // rather than a thrown error, so tool results never surface as a raw MCP error.
132
122
  async function lokiFetch(path, params) {
133
123
  const lokiConfig = loadLokiConfig();
134
124
  const effectivePath = lokiConfig.LOKI_VIA_GRAFANA
@@ -146,11 +136,22 @@ async function lokiFetch(path, params) {
146
136
  if (lokiConfig.LOKI_ORG_ID)
147
137
  headers["X-Scope-OrgID"] = lokiConfig.LOKI_ORG_ID;
148
138
  try {
149
- const response = await fetch(url, { headers });
150
- // Read as text first, not response.json() directly - Loki's error
151
- // responses aren't guaranteed to be the same JSON shape as its
152
- // success responses (sometimes plain text), so parsing is only safe
153
- // once response.ok is known.
139
+ // Manual redirect handling: a Grafana instance rejecting the request
140
+ // (missing/expired auth, or LOKI_VIA_GRAFANA off when it should be on)
141
+ // responds with a 302 to its own login page, not a 401. Node's fetch
142
+ // follows that by default, landing on the login page's HTML with a
143
+ // misleadingly "successful" 200 status - the JSON.parse below would
144
+ // then fail with an opaque syntax error instead of a diagnosable one.
145
+ const response = await fetch(url, { headers, redirect: "manual" });
146
+ if (response.status >= 300 && response.status < 400) {
147
+ const location = response.headers.get("location") ?? "(no Location header)";
148
+ const hint = lokiConfig.LOKI_VIA_GRAFANA
149
+ ? "check LOKI_USERNAME/LOKI_PASSWORD (must be a real Grafana user's Basic Auth) and LOKI_GRAFANA_DATASOURCE_ID"
150
+ : "if this Loki is only reachable through Grafana's datasource proxy, set LOKI_VIA_GRAFANA: true (see README's Configuration section)";
151
+ return { success: false, error: `Request was redirected (${response.status}) to ${location} instead of returning data - ${hint}.` };
152
+ }
153
+ // Text first, not response.json() - Loki's error responses aren't
154
+ // guaranteed the same JSON shape as success (sometimes plain text).
154
155
  const text = await response.text();
155
156
  if (!response.ok) {
156
157
  return { success: false, error: `Loki returned ${response.status}: ${text || response.statusText}` };
@@ -260,11 +261,9 @@ function createMcpServer() {
260
261
  });
261
262
  return server;
262
263
  }
263
- // Stateless mode (sessionIdGenerator: undefined) with a fresh Server +
264
- // transport pair per request - same shell as mcp-servers/oracle/src/server.ts, for the
265
- // same reason (the SDK's own reference stateless Streamable HTTP server;
266
- // no session state worth sharing between calls, and sharing one pair would
267
- // just mean concurrent requests fighting over the same transport).
264
+ // Stateless mode: fresh Server+transport pair per request, same shape and
265
+ // reason as oracle/src/server.ts - no session state to share, and reusing
266
+ // one pair would mean requests fighting over it.
268
267
  const httpServer = http.createServer(async (req, res) => {
269
268
  const url = new URL(req.url ?? "/", `http://${req.headers.host ?? "localhost"}`);
270
269
  if (url.pathname !== "/mcp") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kealthas-dev/opencode-mcp-loki",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "type": "module",
5
5
  "description": "MCP server exposing read-only Loki query/label tools - see README.md for design and the config.json-driven config",
6
6
  "main": "dist/server.js",