@konneal/engine 0.2.95 → 0.2.97

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.
@@ -991,12 +991,14 @@ async function callToolOnce(env, ctx, _req, tier, key, name, args, t0) {
991
991
  body: JSON.stringify({ ...args, stream: false })
992
992
  });
993
993
  const { runTool, TOOLS_REGISTRY: TOOLS_REGISTRY2 } = await import("../../tools-Z4HQH3QU.js");
994
+ let payload;
994
995
  if (TOOLS_REGISTRY2.some((t) => t.name === name && t.audiences.includes("mcp"))) {
995
996
  const r = await runTool({ ...env, DB: env.DB }, { name, args }, "mcp");
996
- return r ? { tool: r.name, query: r.query, result: r.output } : { error: { message: `tool ${name} returned nothing for the given arguments` } };
997
+ payload = r ? { tool: r.name, query: r.query, result: r.output } : { error: { message: `tool ${name} returned nothing for the given arguments` } };
998
+ } else {
999
+ const res = name === "ask" ? await (await import("../../ask-KKOQH7HK.js")).handleAsk(env, ctx, inner, tier, key) : await (await import("../../search-CGQFGYTR.js")).handleSearch(env, ctx, inner, tier, key);
1000
+ payload = await res.json().catch(() => ({ error: { message: "tool transport failed", status: res.status } }));
997
1001
  }
998
- const res = name === "ask" ? await (await import("../../ask-KKOQH7HK.js")).handleAsk(env, ctx, inner, tier, key) : await (await import("../../search-CGQFGYTR.js")).handleSearch(env, ctx, inner, tier, key);
999
- const payload = await res.json().catch(() => ({ error: { message: "tool transport failed", status: res.status } }));
1000
1002
  telemetry(env, ctx, tier, `mcp:${name}`, null, !payload?.error, JSON.stringify(payload).length, await sha256Hex(`${name}:${JSON.stringify(args ?? {})}`), void 0, void 0, { durationMs: Date.now() - t0 });
1001
1003
  return payload;
1002
1004
  }
@@ -1413,12 +1415,14 @@ async function healthRoute(c) {
1413
1415
  }
1414
1416
  async function tierFor(c) {
1415
1417
  const isApi = c.path.startsWith("/v1/");
1418
+ const isMcp = c.path === "/mcp";
1419
+ const bearer = c.req.headers.get("authorization")?.replace(/^Bearer\s+/i, "").trim();
1416
1420
  let key = null;
1417
- if (isApi) {
1421
+ if (isApi || isMcp && bearer) {
1418
1422
  key = await authenticate(c.env, c.req);
1419
1423
  if (!key) return err(401, "unauthorized", `Provide a valid API key: Authorization: Bearer ${P().publisher.id}_...`);
1420
1424
  }
1421
- let tier = isApi ? "key" : "anon";
1425
+ let tier = key ? "key" : "anon";
1422
1426
  if (!isApi && await sessionFrom(c.req, c.env)) tier = "member";
1423
1427
  else if (!isApi && await opTokenMember(c.env, opCfg(c.env), c.req)) tier = "member";
1424
1428
  return { tier, key };
package/package.json CHANGED
@@ -27,7 +27,7 @@
27
27
  "playwright": "^1.62.1",
28
28
  "yaml": "^2.9.1"
29
29
  },
30
- "version": "0.2.95",
30
+ "version": "0.2.97",
31
31
  "description": "The Konneal engine: the publisher-agnostic build pipeline and API plane for standards intelligence (retrieval, answer contract, verdicts, evaluation).",
32
32
  "license": "BSD-3-Clause",
33
33
  "type": "module",
@@ -186,12 +186,18 @@ async function healthRoute(c: RouteContext): Promise<Response> {
186
186
  * key. */
187
187
  async function tierFor(c: RouteContext): Promise<{ tier: "anon" | "key" | "member"; key: ApiKey | null } | Response> {
188
188
  const isApi = c.path.startsWith("/v1/");
189
+ // /mcp rides the same key tiering: a bearer presented to the JSON-RPC
190
+ // endpoint authenticates like /v1 (key tier, the caller's quota and
191
+ // telemetry) — but the endpoint stays open to anonymous agents, so an
192
+ // ABSENT bearer is not an error, only a PRESENT-but-invalid one is.
193
+ const isMcp = c.path === "/mcp";
194
+ const bearer = c.req.headers.get("authorization")?.replace(/^Bearer\s+/i, "").trim();
189
195
  let key: ApiKey | null = null;
190
- if (isApi) {
196
+ if (isApi || (isMcp && bearer)) {
191
197
  key = await authenticate(c.env, c.req);
192
198
  if (!key) return err(401, "unauthorized", `Provide a valid API key: Authorization: Bearer ${P().publisher.id}_...`);
193
199
  }
194
- let tier: "anon" | "key" | "member" = isApi ? "key" : "anon";
200
+ let tier: "anon" | "key" | "member" = key ? "key" : "anon";
195
201
  // No SESSION_SECRET precondition: the delegated bearer (the platform
196
202
  // bubble's OP-minted JWT, delegated.ts) admits the member lane on its
197
203
  // own — sessionFrom returns null honestly when neither lane applies.
@@ -68,14 +68,16 @@ async function callToolOnce(
68
68
  // registry tools (certificates.search, units.get, …) dispatch through
69
69
  // the SAME handlers the API would — the no-drift rule, now structural
70
70
  const { runTool, TOOLS_REGISTRY } = await import("./tools.ts");
71
+ let payload: any;
71
72
  if (TOOLS_REGISTRY.some((t) => t.name === name && t.audiences.includes("mcp"))) {
72
73
  const r = await runTool({ ...env, DB: (env as any).DB }, { name, args }, "mcp");
73
- return r ? { tool: r.name, query: r.query, result: r.output } : { error: { message: `tool ${name} returned nothing for the given arguments` } };
74
+ payload = r ? { tool: r.name, query: r.query, result: r.output } : { error: { message: `tool ${name} returned nothing for the given arguments` } };
75
+ } else {
76
+ const res = name === "ask"
77
+ ? await (await import("./ask")).handleAsk(env, ctx as any, inner, tier, key)
78
+ : await (await import("./search")).handleSearch(env, ctx as any, inner, tier, key);
79
+ payload = await res.json().catch(() => ({ error: { message: "tool transport failed", status: res.status } }));
74
80
  }
75
- const res = name === "ask"
76
- ? await (await import("./ask")).handleAsk(env, ctx as any, inner, tier, key)
77
- : await (await import("./search")).handleSearch(env, ctx as any, inner, tier, key);
78
- const payload: any = await res.json().catch(() => ({ error: { message: "tool transport failed", status: res.status } }));
79
81
  telemetry(env, ctx, tier, `mcp:${name}`, null, !payload?.error, JSON.stringify(payload).length, await sha256Hex(`${name}:${JSON.stringify(args ?? {})}`), undefined, undefined, { durationMs: Date.now() - t0 });
80
82
  return payload;
81
83
  }