@nextblock-cms/cortex 0.20.3 → 0.21.1

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.
Files changed (100) hide show
  1. package/README.md +46 -0
  2. package/client.cjs +1 -0
  3. package/client.es.js +3 -7
  4. package/index.cjs +1 -0
  5. package/index.es.js +24 -277
  6. package/lib/ai-block-generation.cjs +4 -0
  7. package/lib/ai-block-generation.es.js +143 -182
  8. package/lib/ai-client.cjs +1 -0
  9. package/lib/ai-client.d.ts +1 -1
  10. package/lib/ai-client.es.js +101 -135
  11. package/lib/ai-config.cjs +1 -0
  12. package/lib/ai-config.es.js +89 -108
  13. package/lib/ai-cortex-widget-builder.cjs +1 -0
  14. package/lib/ai-cortex-widget-builder.es.js +56 -70
  15. package/lib/ai-global-agent-content-ops-tools.cjs +1 -0
  16. package/lib/ai-global-agent-content-ops-tools.d.ts +1943 -68
  17. package/lib/ai-global-agent-content-ops-tools.es.js +185 -199
  18. package/lib/ai-global-agent-custom-block-tools.cjs +1 -0
  19. package/lib/ai-global-agent-custom-block-tools.d.ts +2160 -95
  20. package/lib/ai-global-agent-custom-block-tools.es.js +226 -223
  21. package/lib/ai-global-agent-db-tools.cjs +1 -0
  22. package/lib/ai-global-agent-db-tools.d.ts +2708 -98
  23. package/lib/ai-global-agent-db-tools.es.js +1013 -986
  24. package/lib/ai-global-agent-ecommerce.cjs +1 -0
  25. package/lib/ai-global-agent-ecommerce.es.js +3 -7
  26. package/lib/ai-global-agent-site-tools.cjs +2 -0
  27. package/lib/ai-global-agent-site-tools.d.ts +10695 -500
  28. package/lib/ai-global-agent-site-tools.es.js +750 -757
  29. package/lib/ai-global-agent-theming-tools.cjs +1 -0
  30. package/lib/ai-global-agent-theming-tools.d.ts +2905 -105
  31. package/lib/ai-global-agent-theming-tools.es.js +326 -396
  32. package/lib/ai-global-agent-tools.cjs +15 -0
  33. package/lib/ai-global-agent-tools.d.ts +4 -2230
  34. package/lib/ai-global-agent-tools.es.js +3899 -4328
  35. package/lib/ai-key-crypto.cjs +1 -0
  36. package/lib/ai-key-crypto.es.js +63 -82
  37. package/lib/ai-key-verification.cjs +1 -0
  38. package/lib/ai-key-verification.es.js +74 -63
  39. package/lib/ai-model-catalog.cjs +1 -0
  40. package/lib/ai-model-catalog.es.js +18 -22
  41. package/lib/ai-model-registry.cjs +2 -0
  42. package/lib/ai-model-registry.es.js +246 -309
  43. package/lib/ai-seo-metadata.cjs +3 -0
  44. package/lib/ai-seo-metadata.es.js +143 -185
  45. package/lib/ai-vision.cjs +3 -0
  46. package/lib/ai-vision.d.ts +6 -6
  47. package/lib/ai-vision.es.js +161 -215
  48. package/lib/block-content-schemas.cjs +1 -0
  49. package/lib/block-content-schemas.es.js +324 -254
  50. package/lib/cortex-widget-registry.cjs +1 -0
  51. package/lib/cortex-widget-registry.es.js +35 -43
  52. package/lib/cortex-widget-schema.cjs +4 -0
  53. package/lib/cortex-widget-schema.es.js +280 -309
  54. package/lib/editor-document-from-html.cjs +1 -0
  55. package/lib/editor-document-from-html.es.js +334 -278
  56. package/lib/fetch-host-guard.cjs +1 -0
  57. package/lib/fetch-host-guard.d.ts +2 -0
  58. package/lib/fetch-host-guard.es.js +30 -0
  59. package/lib/mcp-server.cjs +1 -0
  60. package/lib/mcp-server.es.js +155 -173
  61. package/lib/mcp-tokens.cjs +1 -0
  62. package/lib/mcp-tokens.es.js +95 -107
  63. package/lib/mcp-tool-registry.cjs +5 -0
  64. package/lib/mcp-tool-registry.es.js +533 -411
  65. package/lib/site-brief.cjs +2 -0
  66. package/lib/site-brief.es.js +102 -108
  67. package/lib/vision-image-download.cjs +1 -0
  68. package/lib/vision-image-download.d.ts +43 -0
  69. package/lib/vision-image-download.es.js +120 -0
  70. package/lib/zod-config.cjs +1 -0
  71. package/lib/zod-config.es.js +5 -6
  72. package/package.json +13 -12
  73. package/client.cjs.js +0 -1
  74. package/index.cjs.js +0 -1
  75. package/lib/ai-block-generation.cjs.js +0 -5
  76. package/lib/ai-client.cjs.js +0 -1
  77. package/lib/ai-config.cjs.js +0 -1
  78. package/lib/ai-cortex-widget-builder.cjs.js +0 -1
  79. package/lib/ai-global-agent-content-ops-tools.cjs.js +0 -1
  80. package/lib/ai-global-agent-custom-block-tools.cjs.js +0 -1
  81. package/lib/ai-global-agent-db-tools.cjs.js +0 -1
  82. package/lib/ai-global-agent-ecommerce.cjs.js +0 -1
  83. package/lib/ai-global-agent-site-tools.cjs.js +0 -3
  84. package/lib/ai-global-agent-theming-tools.cjs.js +0 -3
  85. package/lib/ai-global-agent-tools.cjs.js +0 -15
  86. package/lib/ai-key-crypto.cjs.js +0 -1
  87. package/lib/ai-key-verification.cjs.js +0 -1
  88. package/lib/ai-model-catalog.cjs.js +0 -1
  89. package/lib/ai-model-registry.cjs.js +0 -2
  90. package/lib/ai-seo-metadata.cjs.js +0 -4
  91. package/lib/ai-vision.cjs.js +0 -3
  92. package/lib/block-content-schemas.cjs.js +0 -1
  93. package/lib/cortex-widget-registry.cjs.js +0 -1
  94. package/lib/cortex-widget-schema.cjs.js +0 -5
  95. package/lib/editor-document-from-html.cjs.js +0 -1
  96. package/lib/mcp-server.cjs.js +0 -1
  97. package/lib/mcp-tokens.cjs.js +0 -1
  98. package/lib/mcp-tool-registry.cjs.js +0 -6
  99. package/lib/site-brief.cjs.js +0 -2
  100. package/lib/zod-config.cjs.js +0 -1
@@ -0,0 +1,30 @@
1
+ //#region src/lib/fetch-host-guard.ts
2
+ function e(e) {
3
+ let t = e.match(/^::ffff:(.+)$/i);
4
+ if (!t) return null;
5
+ let n = t[1];
6
+ if (/^\d{1,3}(\.\d{1,3}){3}$/.test(n)) return n;
7
+ let r = n.match(/^([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i);
8
+ if (!r) return null;
9
+ let i = Number.parseInt(r[1], 16), a = Number.parseInt(r[2], 16);
10
+ return [
11
+ i >> 8 & 255,
12
+ i & 255,
13
+ a >> 8 & 255,
14
+ a & 255
15
+ ].join(".");
16
+ }
17
+ function t(n) {
18
+ let r = n.trim().toLowerCase().replace(/\.$/, "").replace(/^\[|\]$/g, "");
19
+ if (!r || r === "localhost" || r.endsWith(".localhost") || r.endsWith(".local") || r.endsWith(".internal") || r === "metadata.google.internal" || r === "0.0.0.0" || r === "::" || r === "::1" || r.startsWith("fe80:") || r.startsWith("fc") || r.startsWith("fd")) return !0;
20
+ let i = e(r);
21
+ if (i) return t(i);
22
+ let a = r.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
23
+ if (a) {
24
+ let e = Number(a[1]), t = Number(a[2]);
25
+ if (e === 10 || e === 127 || e === 0 || e === 192 && t === 168 || e === 169 && t === 254 || e === 172 && t >= 16 && t <= 31) return !0;
26
+ }
27
+ return !1;
28
+ }
29
+ //#endregion
30
+ export { t as isBlockedFetchHost };
@@ -0,0 +1 @@
1
+ const e=require("./mcp-tool-registry.cjs");var t=`nextblock-cortex-ai`,n=`NextBlock Cortex AI`,r=`2025-06-18`,i=`2025-11-25`,a=`2026-07-28`,o=[a,i,`2025-06-18`,`2025-03-26`,`2024-11-05`],s=-32700,c=-32600,l=-32601,u=-32602,d=-32603;function f(e,t){return{body:{id:e,jsonrpc:`2.0`,result:t},status:200}}function p(e,t,n,r=200,i){return{body:{error:{code:t,message:n,...i===void 0?{}:{data:i}},id:e,jsonrpc:`2.0`},status:r}}var m={body:null,status:202};function h(e){return typeof e.method==`string`&&e.id===void 0}function g(e,t){let n=e.params?._meta;if(n&&typeof n==`object`){let e=n[`io.modelcontextprotocol/protocolVersion`];if(typeof e==`string`&&e>=`2026-07-28`)return!0}return!!(t&&t>=`2026-07-28`)}function _(e){return typeof e!=`string`||!e?r:o.includes(e)?e:i}function v(e,r){let i=e.scopes.includes(`write`);return{capabilities:{prompts:{},resources:{},tools:{}},instructions:e.instructions??[`NextBlock Cortex AI exposes this CMS through typed tools.`,`Call get_database_schema first when you are unsure what exists.`,`Conventions: a page or post feature_image_id renders as a full-width title banner above the content and is its share preview — give posts one, never the home page or a page with its own hero section (use update_site_identity social_image for the site-wide preview); mark hero sections is_hero:true; slider:true plus slides makes a carousel.`,i?`Page builds should go through generate_jsonb_layout, which stages a reviewable Live Draft rather than publishing directly.`:`This connection is READ-ONLY. Mutating tools are not available on this token.`].join(` `),protocolVersion:_(r),serverInfo:{name:t,title:n,version:e.serverVersion,websiteUrl:`https://nextblock.dev`}}}function y(e){return{authentication:{header:`Authorization`,instructions:"POST JSON-RPC 2.0 messages with `Authorization: Bearer <token>`. Create the token in the NextBlock CMS (Settings → Cortex AI → MCP server access) or set MCP_BEARER_TOKEN in the environment. Cortex AI must be active on the site.",required:!0,schemes:[`bearer`]},capabilities:{prompts:{},resources:{},tools:{}},endpoint:{transport:`streamable-http`,url:e.endpointUrl},name:t,protocolVersions:o,serverCard:e.serverCardUrl,status:`ok`,title:n,version:e.serverVersion,websiteUrl:`https://nextblock.dev`}}function b(e){return!!(e&&e.toLowerCase().includes(`text/event-stream`))}async function x(t,n){if(typeof t!=`object`||!t||Array.isArray(t))return p(null,c,`Expected a single JSON-RPC message.`,400);if(typeof t.method!=`string`)return`result`in t||`error`in t?m:p(t.id??null,c,`Missing "method".`,400);let{method:r}=t,i=t.id??null,a=h(t);if(r.startsWith(`notifications/`))return m;switch(r){case`initialize`:return a?m:f(i,v(n,t.params?.protocolVersion));case`ping`:return a?m:f(i,{});case`tools/list`:return a?m:f(i,{tools:e.buildCortexMcpToolDefinitions({context:n.context,scopes:n.scopes})});case`tools/call`:{if(a)return m;let r=t.params?.name;if(typeof r!=`string`||!r)return p(i,u,`tools/call requires params.name.`);try{return f(i,await e.callCortexMcpTool({args:t.params?.arguments,context:n.context,name:r,scopes:n.scopes}))}catch(t){return t instanceof e.CortexMcpUnknownToolError||t instanceof e.CortexMcpForbiddenToolError?p(i,u,t.message):p(i,d,t instanceof Error?t.message:`Tool execution failed.`)}}case`resources/list`:return a?m:f(i,{resources:e.CORTEX_MCP_RESOURCES});case`resources/templates/list`:return a?m:f(i,{resourceTemplates:[]});case`resources/read`:{if(a)return m;let r=t.params?.uri;if(typeof r!=`string`||!r)return p(i,u,`resources/read requires params.uri.`);try{let t=await e.readCortexMcpResource({context:n.context,uri:r});return t?f(i,{contents:[t]}):p(i,u,`Unknown resource: ${r}`)}catch(e){return p(i,d,e instanceof Error?e.message:`Resource read failed.`)}}case`prompts/list`:return a?m:f(i,{prompts:e.CORTEX_MCP_PROMPTS});case`prompts/get`:{if(a)return m;let n=t.params?.name;if(typeof n!=`string`||!n)return p(i,u,`prompts/get requires params.name.`);let r=t.params?.arguments,o={};if(r&&typeof r==`object`)for(let[e,t]of Object.entries(r))typeof t==`string`&&(o[e]=t);let s=e.getCortexMcpPrompt({args:o,name:n});return s?f(i,s):p(i,u,`Unknown prompt: ${n}`)}default:return a?m:p(i,l,`Method not found: ${r}`)}}exports.CORTEX_MCP_DEFAULT_PROTOCOL_VERSION=r,exports.CORTEX_MCP_LATEST_LEGACY_PROTOCOL_VERSION=i,exports.CORTEX_MCP_MODERN_PROTOCOL_VERSION=a,exports.CORTEX_MCP_SERVER_NAME=t,exports.CORTEX_MCP_SERVER_TITLE=n,exports.CORTEX_MCP_SUPPORTED_PROTOCOL_VERSIONS=o,exports.JSON_RPC_INTERNAL_ERROR=d,exports.JSON_RPC_INVALID_PARAMS=u,exports.JSON_RPC_INVALID_REQUEST=c,exports.JSON_RPC_METHOD_NOT_FOUND=l,exports.JSON_RPC_PARSE_ERROR=s,exports.buildCortexMcpDiscoveryDocument=y,exports.handleCortexMcpMessage=x,exports.isJsonRpcNotification=h,exports.isModernEraMessage=g,exports.wantsMcpEventStream=b;
@@ -1,182 +1,164 @@
1
- import { getCortexMcpPrompt as x, readCortexMcpResource as M, callCortexMcpTool as P, CortexMcpUnknownToolError as b, CortexMcpForbiddenToolError as g, buildCortexMcpToolDefinitions as v, CORTEX_MCP_PROMPTS as S, CORTEX_MCP_RESOURCES as y } from "./mcp-tool-registry.es.js";
2
- const E = "nextblock-cortex-ai", d = "NextBlock Cortex AI", N = "2025-06-18", O = "2025-11-25", p = "2026-07-28", m = [
3
- p,
4
- O,
5
- "2025-06-18",
6
- "2025-03-26",
7
- "2024-11-05"
8
- ], V = -32700, R = -32600, w = -32601, l = -32602, h = -32603;
9
- function u(t, o) {
10
- return { body: { id: t, jsonrpc: "2.0", result: o }, status: 200 };
1
+ import { CORTEX_MCP_PROMPTS as e, CORTEX_MCP_RESOURCES as t, CortexMcpForbiddenToolError as n, CortexMcpUnknownToolError as r, buildCortexMcpToolDefinitions as i, callCortexMcpTool as a, getCortexMcpPrompt as o, readCortexMcpResource as s } from "./mcp-tool-registry.es.js";
2
+ //#region src/lib/mcp-server.ts
3
+ var c = "nextblock-cortex-ai", l = "NextBlock Cortex AI", u = "2025-06-18", d = "2025-11-25", f = "2026-07-28", p = [
4
+ f,
5
+ d,
6
+ "2025-06-18",
7
+ "2025-03-26",
8
+ "2024-11-05"
9
+ ], m = -32700, h = -32600, g = -32601, _ = -32602, v = -32603;
10
+ function y(e, t) {
11
+ return {
12
+ body: {
13
+ id: e,
14
+ jsonrpc: "2.0",
15
+ result: t
16
+ },
17
+ status: 200
18
+ };
11
19
  }
12
- function a(t, o, i, e = 200, c) {
13
- return {
14
- body: {
15
- error: { code: o, message: i },
16
- id: t,
17
- jsonrpc: "2.0"
18
- },
19
- status: e
20
- };
20
+ function b(e, t, n, r = 200, i) {
21
+ return {
22
+ body: {
23
+ error: {
24
+ code: t,
25
+ message: n,
26
+ ...i === void 0 ? {} : { data: i }
27
+ },
28
+ id: e,
29
+ jsonrpc: "2.0"
30
+ },
31
+ status: r
32
+ };
21
33
  }
22
- const s = { body: null, status: 202 };
23
- function A(t) {
24
- return typeof t.method == "string" && t.id === void 0;
34
+ var x = {
35
+ body: null,
36
+ status: 202
37
+ };
38
+ function S(e) {
39
+ return typeof e.method == "string" && e.id === void 0;
25
40
  }
26
- function D(t, o) {
27
- const i = t.params?._meta;
28
- if (i && typeof i == "object") {
29
- const e = i["io.modelcontextprotocol/protocolVersion"];
30
- if (typeof e == "string" && e >= p)
31
- return !0;
32
- }
33
- return !!(o && o >= p);
41
+ function C(e, t) {
42
+ let n = e.params?._meta;
43
+ if (n && typeof n == "object") {
44
+ let e = n["io.modelcontextprotocol/protocolVersion"];
45
+ if (typeof e == "string" && e >= "2026-07-28") return !0;
46
+ }
47
+ return !!(t && t >= "2026-07-28");
34
48
  }
35
- function k(t) {
36
- return typeof t != "string" || !t ? N : m.includes(t) ? t : O;
49
+ function w(e) {
50
+ return typeof e != "string" || !e ? u : p.includes(e) ? e : d;
37
51
  }
38
- function I(t, o) {
39
- const i = t.scopes.includes("write");
40
- return {
41
- capabilities: {
42
- // listChanged is deliberately absent: the tool set is static per token, so
43
- // advertising change notifications we never send would be a lie a client
44
- // could wait on.
45
- prompts: {},
46
- resources: {},
47
- tools: {}
48
- },
49
- instructions: t.instructions ?? [
50
- "NextBlock Cortex AI exposes this CMS through typed tools.",
51
- "Call get_database_schema first when you are unsure what exists.",
52
- "Conventions: a page or post feature_image_id renders as a full-width title banner above the content and is its share preview — give posts one, never the home page or a page with its own hero section (use update_site_identity social_image for the site-wide preview); mark hero sections is_hero:true; slider:true plus slides makes a carousel.",
53
- i ? "Page builds should go through generate_jsonb_layout, which stages a reviewable Live Draft rather than publishing directly." : "This connection is READ-ONLY. Mutating tools are not available on this token."
54
- ].join(" "),
55
- protocolVersion: k(o),
56
- serverInfo: {
57
- name: E,
58
- title: d,
59
- version: t.serverVersion,
60
- websiteUrl: "https://nextblock.dev"
61
- }
62
- };
52
+ function T(e, t) {
53
+ let n = e.scopes.includes("write");
54
+ return {
55
+ capabilities: {
56
+ prompts: {},
57
+ resources: {},
58
+ tools: {}
59
+ },
60
+ instructions: e.instructions ?? [
61
+ "NextBlock Cortex AI exposes this CMS through typed tools.",
62
+ "Call get_database_schema first when you are unsure what exists.",
63
+ "Conventions: a page or post feature_image_id renders as a full-width title banner above the content and is its share preview — give posts one, never the home page or a page with its own hero section (use update_site_identity social_image for the site-wide preview); mark hero sections is_hero:true; slider:true plus slides makes a carousel.",
64
+ n ? "Page builds should go through generate_jsonb_layout, which stages a reviewable Live Draft rather than publishing directly." : "This connection is READ-ONLY. Mutating tools are not available on this token."
65
+ ].join(" "),
66
+ protocolVersion: w(t),
67
+ serverInfo: {
68
+ name: c,
69
+ title: l,
70
+ version: e.serverVersion,
71
+ websiteUrl: "https://nextblock.dev"
72
+ }
73
+ };
63
74
  }
64
- function U(t) {
65
- return {
66
- authentication: {
67
- header: "Authorization",
68
- instructions: "POST JSON-RPC 2.0 messages with `Authorization: Bearer <token>`. Create the token in the NextBlock CMS (Settings → Cortex AI → MCP server access) or set MCP_BEARER_TOKEN in the environment. Cortex AI must be active on the site.",
69
- required: !0,
70
- schemes: ["bearer"]
71
- },
72
- capabilities: { prompts: {}, resources: {}, tools: {} },
73
- endpoint: { transport: "streamable-http", url: t.endpointUrl },
74
- name: E,
75
- protocolVersions: m,
76
- serverCard: t.serverCardUrl,
77
- status: "ok",
78
- title: d,
79
- version: t.serverVersion,
80
- websiteUrl: "https://nextblock.dev"
81
- };
75
+ function E(e) {
76
+ return {
77
+ authentication: {
78
+ header: "Authorization",
79
+ instructions: "POST JSON-RPC 2.0 messages with `Authorization: Bearer <token>`. Create the token in the NextBlock CMS (Settings → Cortex AI → MCP server access) or set MCP_BEARER_TOKEN in the environment. Cortex AI must be active on the site.",
80
+ required: !0,
81
+ schemes: ["bearer"]
82
+ },
83
+ capabilities: {
84
+ prompts: {},
85
+ resources: {},
86
+ tools: {}
87
+ },
88
+ endpoint: {
89
+ transport: "streamable-http",
90
+ url: e.endpointUrl
91
+ },
92
+ name: c,
93
+ protocolVersions: p,
94
+ serverCard: e.serverCardUrl,
95
+ status: "ok",
96
+ title: l,
97
+ version: e.serverVersion,
98
+ websiteUrl: "https://nextblock.dev"
99
+ };
82
100
  }
83
- function j(t) {
84
- return !!(t && t.toLowerCase().includes("text/event-stream"));
101
+ function D(e) {
102
+ return !!(e && e.toLowerCase().includes("text/event-stream"));
85
103
  }
86
- async function J(t, o) {
87
- if (t === null || typeof t != "object" || Array.isArray(t))
88
- return a(null, R, "Expected a single JSON-RPC message.", 400);
89
- if (typeof t.method != "string")
90
- return "result" in t || "error" in t ? s : a(t.id ?? null, R, 'Missing "method".', 400);
91
- const { method: i } = t, e = t.id ?? null, c = A(t);
92
- if (i.startsWith("notifications/"))
93
- return s;
94
- switch (i) {
95
- case "initialize":
96
- return c ? s : u(e, I(o, t.params?.protocolVersion));
97
- case "ping":
98
- return c ? s : u(e, {});
99
- case "tools/list":
100
- return c ? s : u(e, {
101
- tools: v({ context: o.context, scopes: o.scopes })
102
- });
103
- case "tools/call": {
104
- if (c)
105
- return s;
106
- const n = t.params?.name;
107
- if (typeof n != "string" || !n)
108
- return a(e, l, "tools/call requires params.name.");
109
- try {
110
- const r = await P({
111
- args: t.params?.arguments,
112
- context: o.context,
113
- name: n,
114
- scopes: o.scopes
115
- });
116
- return u(e, r);
117
- } catch (r) {
118
- return r instanceof b || r instanceof g ? a(e, l, r.message) : a(
119
- e,
120
- h,
121
- r instanceof Error ? r.message : "Tool execution failed."
122
- );
123
- }
124
- }
125
- case "resources/list":
126
- return c ? s : u(e, { resources: y });
127
- case "resources/templates/list":
128
- return c ? s : u(e, { resourceTemplates: [] });
129
- case "resources/read": {
130
- if (c)
131
- return s;
132
- const n = t.params?.uri;
133
- if (typeof n != "string" || !n)
134
- return a(e, l, "resources/read requires params.uri.");
135
- try {
136
- const r = await M({ context: o.context, uri: n });
137
- return r ? u(e, { contents: [r] }) : a(e, l, `Unknown resource: ${n}`);
138
- } catch (r) {
139
- return a(
140
- e,
141
- h,
142
- r instanceof Error ? r.message : "Resource read failed."
143
- );
144
- }
145
- }
146
- case "prompts/list":
147
- return c ? s : u(e, { prompts: S });
148
- case "prompts/get": {
149
- if (c)
150
- return s;
151
- const n = t.params?.name;
152
- if (typeof n != "string" || !n)
153
- return a(e, l, "prompts/get requires params.name.");
154
- const r = t.params?.arguments, f = {};
155
- if (r && typeof r == "object")
156
- for (const [T, C] of Object.entries(r))
157
- typeof C == "string" && (f[T] = C);
158
- const _ = x({ args: f, name: n });
159
- return _ ? u(e, _) : a(e, l, `Unknown prompt: ${n}`);
160
- }
161
- default:
162
- return c ? s : a(e, w, `Method not found: ${i}`);
163
- }
104
+ async function O(c, l) {
105
+ if (typeof c != "object" || !c || Array.isArray(c)) return b(null, h, "Expected a single JSON-RPC message.", 400);
106
+ if (typeof c.method != "string") return "result" in c || "error" in c ? x : b(c.id ?? null, h, "Missing \"method\".", 400);
107
+ let { method: u } = c, d = c.id ?? null, f = S(c);
108
+ if (u.startsWith("notifications/")) return x;
109
+ switch (u) {
110
+ case "initialize": return f ? x : y(d, T(l, c.params?.protocolVersion));
111
+ case "ping": return f ? x : y(d, {});
112
+ case "tools/list": return f ? x : y(d, { tools: i({
113
+ context: l.context,
114
+ scopes: l.scopes
115
+ }) });
116
+ case "tools/call": {
117
+ if (f) return x;
118
+ let e = c.params?.name;
119
+ if (typeof e != "string" || !e) return b(d, _, "tools/call requires params.name.");
120
+ try {
121
+ return y(d, await a({
122
+ args: c.params?.arguments,
123
+ context: l.context,
124
+ name: e,
125
+ scopes: l.scopes
126
+ }));
127
+ } catch (e) {
128
+ return e instanceof r || e instanceof n ? b(d, _, e.message) : b(d, v, e instanceof Error ? e.message : "Tool execution failed.");
129
+ }
130
+ }
131
+ case "resources/list": return f ? x : y(d, { resources: t });
132
+ case "resources/templates/list": return f ? x : y(d, { resourceTemplates: [] });
133
+ case "resources/read": {
134
+ if (f) return x;
135
+ let e = c.params?.uri;
136
+ if (typeof e != "string" || !e) return b(d, _, "resources/read requires params.uri.");
137
+ try {
138
+ let t = await s({
139
+ context: l.context,
140
+ uri: e
141
+ });
142
+ return t ? y(d, { contents: [t] }) : b(d, _, `Unknown resource: ${e}`);
143
+ } catch (e) {
144
+ return b(d, v, e instanceof Error ? e.message : "Resource read failed.");
145
+ }
146
+ }
147
+ case "prompts/list": return f ? x : y(d, { prompts: e });
148
+ case "prompts/get": {
149
+ if (f) return x;
150
+ let e = c.params?.name;
151
+ if (typeof e != "string" || !e) return b(d, _, "prompts/get requires params.name.");
152
+ let t = c.params?.arguments, n = {};
153
+ if (t && typeof t == "object") for (let [e, r] of Object.entries(t)) typeof r == "string" && (n[e] = r);
154
+ let r = o({
155
+ args: n,
156
+ name: e
157
+ });
158
+ return r ? y(d, r) : b(d, _, `Unknown prompt: ${e}`);
159
+ }
160
+ default: return f ? x : b(d, g, `Method not found: ${u}`);
161
+ }
164
162
  }
165
- export {
166
- N as CORTEX_MCP_DEFAULT_PROTOCOL_VERSION,
167
- O as CORTEX_MCP_LATEST_LEGACY_PROTOCOL_VERSION,
168
- p as CORTEX_MCP_MODERN_PROTOCOL_VERSION,
169
- E as CORTEX_MCP_SERVER_NAME,
170
- d as CORTEX_MCP_SERVER_TITLE,
171
- m as CORTEX_MCP_SUPPORTED_PROTOCOL_VERSIONS,
172
- h as JSON_RPC_INTERNAL_ERROR,
173
- l as JSON_RPC_INVALID_PARAMS,
174
- R as JSON_RPC_INVALID_REQUEST,
175
- w as JSON_RPC_METHOD_NOT_FOUND,
176
- V as JSON_RPC_PARSE_ERROR,
177
- U as buildCortexMcpDiscoveryDocument,
178
- J as handleCortexMcpMessage,
179
- A as isJsonRpcNotification,
180
- D as isModernEraMessage,
181
- j as wantsMcpEventStream
182
- };
163
+ //#endregion
164
+ export { u as CORTEX_MCP_DEFAULT_PROTOCOL_VERSION, d as CORTEX_MCP_LATEST_LEGACY_PROTOCOL_VERSION, f as CORTEX_MCP_MODERN_PROTOCOL_VERSION, c as CORTEX_MCP_SERVER_NAME, l as CORTEX_MCP_SERVER_TITLE, p as CORTEX_MCP_SUPPORTED_PROTOCOL_VERSIONS, v as JSON_RPC_INTERNAL_ERROR, _ as JSON_RPC_INVALID_PARAMS, h as JSON_RPC_INVALID_REQUEST, g as JSON_RPC_METHOD_NOT_FOUND, m as JSON_RPC_PARSE_ERROR, E as buildCortexMcpDiscoveryDocument, O as handleCortexMcpMessage, S as isJsonRpcNotification, C as isModernEraMessage, D as wantsMcpEventStream };
@@ -0,0 +1 @@
1
+ let e=require("node:crypto");var t=`Cortex AI MCP token utilities can only be imported from server-side code.`;function n(){if(typeof window<`u`)throw Error(t)}var r=`cortex_ai_mcp_settings`,i=`mcp_access_tokens`,a=`nbmcp_`,o=32,s=[`read`,`write`],c={allowLocalhostWithoutToken:!0,enabled:!1};function l(e){let t=e&&typeof e==`object`&&!Array.isArray(e)?e:{};return{allowLocalhostWithoutToken:typeof t.allowLocalhostWithoutToken==`boolean`?t.allowLocalhostWithoutToken:c.allowLocalhostWithoutToken,enabled:typeof t.enabled==`boolean`?t.enabled:c.enabled}}async function u(e){if(e)try{let{data:t}=await e.from(`site_settings`).select(`value`).eq(`key`,r).maybeSingle();if(t?.value)return l(t.value)}catch{}return{...c}}function d(t){return n(),(0,e.createHash)(`sha256`).update(t.trim(),`utf8`).digest(`hex`)}function f(){n();let t=`${a}${(0,e.randomBytes)(o).toString(`base64url`)}`;return{token:t,tokenHash:d(t),tokenPrefix:t.slice(0,14)}}function p(e){let t=e?.trim();if(!t)return null;let n=/^Bearer\s+(.+)$/i.exec(t);return(n?n[1]:t).trim()||null}function m(e){let t=(Array.isArray(e)?e:[]).filter(e=>s.includes(e));return t.length>0?Array.from(new Set(t)):[`read`]}async function h(t,r,a=new Date){n();let o=r?.trim();if(!o||o.length<16)return{reason:`malformed`,valid:!1};let s=d(o),{data:c,error:l}=await t.from(i).select(`id, name, scopes, token_prefix, created_at, created_by, expires_at, last_used_at, revoked_at`).eq(`token_hash`,s).maybeSingle();if(l||!c)return{reason:`unknown`,valid:!1};let u=Buffer.from(s,`utf8`);if(!(0,e.timingSafeEqual)(u,Buffer.from(d(o),`utf8`)))return{reason:`unknown`,valid:!1};let f=c;return f.revoked_at?{reason:`revoked`,valid:!1}:f.expires_at&&new Date(f.expires_at).getTime()<=a.getTime()?{reason:`expired`,valid:!1}:{scopes:m(f.scopes),token:f,valid:!0}}async function g(e,t,n=new Date){try{await e.from(i).update({last_used_at:n.toISOString()}).eq(`id`,t)}catch{}}var _=new Set([`localhost`,`127.0.0.1`,`[::1]`,`::1`,`0.0.0.0`]);function v(e){let t=e?.trim().toLowerCase();if(!t)return!1;let n=t.startsWith(`[`)?t.replace(/\]:\d+$/,`]`):t.replace(/:\d+$/,``);return _.has(n)||n.endsWith(`.localhost`)}function y(e){return!e.settings.allowLocalhostWithoutToken||process.env.NODE_ENV===`production`?!1:v(e.hostHeader)}var b=`MCP_BEARER_TOKEN`,x=32;function S(e=process.env){let t=e[b]?.trim();return!t||t.length<32?null:t}function C(t,r){n();let i=t?.trim(),a=r?.trim();return!i||!a||a.length<32?!1:(0,e.timingSafeEqual)(Buffer.from(d(i),`utf8`),Buffer.from(d(a),`utf8`))}exports.CORTEX_AI_MCP_ENV_TOKEN_MIN_LENGTH=x,exports.CORTEX_AI_MCP_ENV_TOKEN_VAR=b,exports.CORTEX_AI_MCP_SCOPES=s,exports.CORTEX_AI_MCP_SETTINGS_DEFAULTS=c,exports.CORTEX_AI_MCP_SETTINGS_KEY=r,exports.CORTEX_AI_MCP_TOKENS_TABLE=i,exports.CORTEX_AI_MCP_TOKEN_PREFIX=a,exports.hashCortexAiMcpToken=d,exports.isLocalhostHost=v,exports.matchesCortexAiMcpEnvToken=C,exports.mintCortexAiMcpToken=f,exports.normalizeCortexAiMcpSettings=l,exports.parseBearerToken=p,exports.readCortexAiMcpEnvToken=S,exports.resolveCortexAiMcpSettings=u,exports.shouldTrustLocalMcpRequest=y,exports.touchCortexAiMcpToken=g,exports.verifyCortexAiMcpToken=h;
@@ -1,121 +1,109 @@
1
- import { createHash as E, timingSafeEqual as f, randomBytes as m } from "node:crypto";
2
- const p = "Cortex AI MCP token utilities can only be imported from server-side code.";
3
- function a() {
4
- if (!(typeof window > "u"))
5
- throw new Error(p);
1
+ import { createHash as e, randomBytes as t, timingSafeEqual as n } from "node:crypto";
2
+ //#region src/lib/mcp-tokens.ts
3
+ var r = "Cortex AI MCP token utilities can only be imported from server-side code.";
4
+ function i() {
5
+ if (typeof window < "u") throw Error(r);
6
6
  }
7
- const C = "cortex_ai_mcp_settings", _ = "mcp_access_tokens", u = "nbmcp_", A = 32, k = 8, S = ["read", "write"], i = {
8
- allowLocalhostWithoutToken: !0,
9
- enabled: !1
7
+ var a = "cortex_ai_mcp_settings", o = "mcp_access_tokens", s = "nbmcp_", c = 32, l = ["read", "write"], u = {
8
+ allowLocalhostWithoutToken: !0,
9
+ enabled: !1
10
10
  };
11
- function O(e) {
12
- const t = e && typeof e == "object" && !Array.isArray(e) ? e : {};
13
- return {
14
- allowLocalhostWithoutToken: typeof t.allowLocalhostWithoutToken == "boolean" ? t.allowLocalhostWithoutToken : i.allowLocalhostWithoutToken,
15
- enabled: typeof t.enabled == "boolean" ? t.enabled : i.enabled
16
- };
11
+ function d(e) {
12
+ let t = e && typeof e == "object" && !Array.isArray(e) ? e : {};
13
+ return {
14
+ allowLocalhostWithoutToken: typeof t.allowLocalhostWithoutToken == "boolean" ? t.allowLocalhostWithoutToken : u.allowLocalhostWithoutToken,
15
+ enabled: typeof t.enabled == "boolean" ? t.enabled : u.enabled
16
+ };
17
17
  }
18
- async function R(e) {
19
- if (e)
20
- try {
21
- const { data: t } = await e.from("site_settings").select("value").eq("key", C).maybeSingle();
22
- if (t?.value)
23
- return O(t.value);
24
- } catch {
25
- }
26
- return { ...i };
18
+ async function f(e) {
19
+ if (e) try {
20
+ let { data: t } = await e.from("site_settings").select("value").eq("key", a).maybeSingle();
21
+ if (t?.value) return d(t.value);
22
+ } catch {}
23
+ return { ...u };
27
24
  }
28
- function s(e) {
29
- return a(), E("sha256").update(e.trim(), "utf8").digest("hex");
25
+ function p(t) {
26
+ return i(), e("sha256").update(t.trim(), "utf8").digest("hex");
30
27
  }
31
- function g() {
32
- a();
33
- const e = `${u}${m(A).toString(
34
- "base64url"
35
- )}`;
36
- return {
37
- token: e,
38
- tokenHash: s(e),
39
- tokenPrefix: e.slice(
40
- 0,
41
- u.length + k
42
- )
43
- };
28
+ function m() {
29
+ i();
30
+ let e = `${s}${t(c).toString("base64url")}`;
31
+ return {
32
+ token: e,
33
+ tokenHash: p(e),
34
+ tokenPrefix: e.slice(0, 14)
35
+ };
44
36
  }
45
- function N(e) {
46
- const t = e?.trim();
47
- if (!t)
48
- return null;
49
- const o = /^Bearer\s+(.+)$/i.exec(t);
50
- return (o ? o[1] : t).trim() || null;
37
+ function h(e) {
38
+ let t = e?.trim();
39
+ if (!t) return null;
40
+ let n = /^Bearer\s+(.+)$/i.exec(t);
41
+ return (n ? n[1] : t).trim() || null;
51
42
  }
52
- function M(e) {
53
- const o = (Array.isArray(e) ? e : []).filter(
54
- (n) => S.includes(n)
55
- );
56
- return o.length > 0 ? Array.from(new Set(o)) : ["read"];
43
+ function g(e) {
44
+ let t = (Array.isArray(e) ? e : []).filter((e) => l.includes(e));
45
+ return t.length > 0 ? Array.from(new Set(t)) : ["read"];
57
46
  }
58
- async function I(e, t, o = /* @__PURE__ */ new Date()) {
59
- a();
60
- const n = t?.trim();
61
- if (!n || n.length < 16)
62
- return { reason: "malformed", valid: !1 };
63
- const c = s(n), { data: l, error: h } = await e.from(_).select("id, name, scopes, token_prefix, created_at, created_by, expires_at, last_used_at, revoked_at").eq("token_hash", c).maybeSingle();
64
- if (h || !l)
65
- return { reason: "unknown", valid: !1 };
66
- const T = Buffer.from(c, "utf8");
67
- if (!f(T, Buffer.from(s(n), "utf8")))
68
- return { reason: "unknown", valid: !1 };
69
- const r = l;
70
- return r.revoked_at ? { reason: "revoked", valid: !1 } : r.expires_at && new Date(r.expires_at).getTime() <= o.getTime() ? { reason: "expired", valid: !1 } : { scopes: M(r.scopes), token: r, valid: !0 };
47
+ async function _(e, t, r = /* @__PURE__ */ new Date()) {
48
+ i();
49
+ let a = t?.trim();
50
+ if (!a || a.length < 16) return {
51
+ reason: "malformed",
52
+ valid: !1
53
+ };
54
+ let s = p(a), { data: c, error: l } = await e.from(o).select("id, name, scopes, token_prefix, created_at, created_by, expires_at, last_used_at, revoked_at").eq("token_hash", s).maybeSingle();
55
+ if (l || !c) return {
56
+ reason: "unknown",
57
+ valid: !1
58
+ };
59
+ let u = Buffer.from(s, "utf8");
60
+ if (!n(u, Buffer.from(p(a), "utf8"))) return {
61
+ reason: "unknown",
62
+ valid: !1
63
+ };
64
+ let d = c;
65
+ return d.revoked_at ? {
66
+ reason: "revoked",
67
+ valid: !1
68
+ } : d.expires_at && new Date(d.expires_at).getTime() <= r.getTime() ? {
69
+ reason: "expired",
70
+ valid: !1
71
+ } : {
72
+ scopes: g(d.scopes),
73
+ token: d,
74
+ valid: !0
75
+ };
71
76
  }
72
- async function L(e, t, o = /* @__PURE__ */ new Date()) {
73
- try {
74
- await e.from(_).update({ last_used_at: o.toISOString() }).eq("id", t);
75
- } catch {
76
- }
77
+ async function v(e, t, n = /* @__PURE__ */ new Date()) {
78
+ try {
79
+ await e.from(o).update({ last_used_at: n.toISOString() }).eq("id", t);
80
+ } catch {}
77
81
  }
78
- const v = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "[::1]", "::1", "0.0.0.0"]);
79
- function y(e) {
80
- const t = e?.trim().toLowerCase();
81
- if (!t)
82
- return !1;
83
- const o = t.startsWith("[") ? t.replace(/\]:\d+$/, "]") : t.replace(/:\d+$/, "");
84
- return v.has(o) || o.endsWith(".localhost");
82
+ var y = /* @__PURE__ */ new Set([
83
+ "localhost",
84
+ "127.0.0.1",
85
+ "[::1]",
86
+ "::1",
87
+ "0.0.0.0"
88
+ ]);
89
+ function b(e) {
90
+ let t = e?.trim().toLowerCase();
91
+ if (!t) return !1;
92
+ let n = t.startsWith("[") ? t.replace(/\]:\d+$/, "]") : t.replace(/:\d+$/, "");
93
+ return y.has(n) || n.endsWith(".localhost");
85
94
  }
86
- function P(e) {
87
- return !e.settings.allowLocalhostWithoutToken || process.env.NODE_ENV === "production" ? !1 : y(e.hostHeader);
95
+ function x(e) {
96
+ return !e.settings.allowLocalhostWithoutToken || process.env.NODE_ENV === "production" ? !1 : b(e.hostHeader);
88
97
  }
89
- const w = "MCP_BEARER_TOKEN", d = 32;
90
- function b(e = process.env) {
91
- const t = e[w]?.trim();
92
- return !t || t.length < d ? null : t;
98
+ var S = "MCP_BEARER_TOKEN", C = 32;
99
+ function w(e = process.env) {
100
+ let t = e[S]?.trim();
101
+ return !t || t.length < 32 ? null : t;
93
102
  }
94
- function B(e, t) {
95
- a();
96
- const o = e?.trim(), n = t?.trim();
97
- return !o || !n || n.length < d ? !1 : f(
98
- Buffer.from(s(o), "utf8"),
99
- Buffer.from(s(n), "utf8")
100
- );
103
+ function T(e, t) {
104
+ i();
105
+ let r = e?.trim(), a = t?.trim();
106
+ return !r || !a || a.length < 32 ? !1 : n(Buffer.from(p(r), "utf8"), Buffer.from(p(a), "utf8"));
101
107
  }
102
- export {
103
- d as CORTEX_AI_MCP_ENV_TOKEN_MIN_LENGTH,
104
- w as CORTEX_AI_MCP_ENV_TOKEN_VAR,
105
- S as CORTEX_AI_MCP_SCOPES,
106
- i as CORTEX_AI_MCP_SETTINGS_DEFAULTS,
107
- C as CORTEX_AI_MCP_SETTINGS_KEY,
108
- _ as CORTEX_AI_MCP_TOKENS_TABLE,
109
- u as CORTEX_AI_MCP_TOKEN_PREFIX,
110
- s as hashCortexAiMcpToken,
111
- y as isLocalhostHost,
112
- B as matchesCortexAiMcpEnvToken,
113
- g as mintCortexAiMcpToken,
114
- O as normalizeCortexAiMcpSettings,
115
- N as parseBearerToken,
116
- b as readCortexAiMcpEnvToken,
117
- R as resolveCortexAiMcpSettings,
118
- P as shouldTrustLocalMcpRequest,
119
- L as touchCortexAiMcpToken,
120
- I as verifyCortexAiMcpToken
121
- };
108
+ //#endregion
109
+ export { C as CORTEX_AI_MCP_ENV_TOKEN_MIN_LENGTH, S as CORTEX_AI_MCP_ENV_TOKEN_VAR, l as CORTEX_AI_MCP_SCOPES, u as CORTEX_AI_MCP_SETTINGS_DEFAULTS, a as CORTEX_AI_MCP_SETTINGS_KEY, o as CORTEX_AI_MCP_TOKENS_TABLE, s as CORTEX_AI_MCP_TOKEN_PREFIX, p as hashCortexAiMcpToken, b as isLocalhostHost, T as matchesCortexAiMcpEnvToken, m as mintCortexAiMcpToken, d as normalizeCortexAiMcpSettings, h as parseBearerToken, w as readCortexAiMcpEnvToken, f as resolveCortexAiMcpSettings, x as shouldTrustLocalMcpRequest, v as touchCortexAiMcpToken, _ as verifyCortexAiMcpToken };