drizzle-cube 0.4.19 → 0.4.20
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/dist/adapters/express/index.cjs +6 -6
- package/dist/adapters/express/index.js +75 -74
- package/dist/adapters/fastify/index.cjs +5 -5
- package/dist/adapters/fastify/index.js +91 -90
- package/dist/adapters/{handler-BV4JuWNW.js → handler-CbDMdSY5.js} +49 -42
- package/dist/adapters/{handler-D4MVKkVy.cjs → handler-DtdjM1Vx.cjs} +17 -13
- package/dist/adapters/hono/index.cjs +6 -6
- package/dist/adapters/hono/index.js +66 -65
- package/dist/adapters/nextjs/index.cjs +5 -5
- package/dist/adapters/nextjs/index.js +116 -115
- package/dist/server/index.cjs +5 -1
- package/dist/server/index.d.ts +12 -0
- package/dist/server/index.js +8 -1
- package/package.json +1 -1
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const R=require("express"),M=require("cors"),c=require("../mcp-transport-8u9G5oNa.cjs"),n=require("../utils.cjs");function $(g){const{cubes:h,drizzle:w,schema:P,extractSecurityContext:
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const R=require("express"),M=require("cors"),c=require("../mcp-transport-8u9G5oNa.cjs"),n=require("../utils.cjs");function $(g){const{cubes:h,drizzle:w,schema:P,extractSecurityContext:p,engineType:A,cors:x,basePath:f="/cubejs-api/v1",jsonLimit:S="10mb",cache:H,mcp:C={enabled:!0},agent:v}=g;if(!h||h.length===0)throw new Error("At least one cube must be provided in the cubes array");const l=R.Router();x&&l.use(M(x)),l.use(R.json({limit:S})),l.use(R.urlencoded({extended:!0,limit:S}));const u=new c.SemanticLayerCompiler({drizzle:w,schema:P,engineType:A,cache:H});if(h.forEach(r=>{u.registerCube(r)}),l.post(`${f}/load`,async(r,t)=>{try{const e=r.body.query||r.body,o=await p(r,t),s=u.validateQuery(e);if(!s.isValid)return t.status(400).json(n.formatErrorResponse(`Query validation failed: ${s.errors.join(", ")}`,400));const a=r.headers["x-cache-control"]==="no-cache",i=await u.executeMultiCubeQuery(e,o,{skipCache:a});t.json(n.formatCubeResponse(e,i,u))}catch(e){console.error("Query execution error:",e),t.status(500).json(n.formatErrorResponse(e instanceof Error?e.message:"Query execution failed",500))}}),l.get(`${f}/load`,async(r,t)=>{try{const e=r.query.query;if(!e)return t.status(400).json(n.formatErrorResponse("Query parameter is required",400));let o;try{o=JSON.parse(e)}catch{return t.status(400).json(n.formatErrorResponse("Invalid JSON in query parameter",400))}const s=await p(r,t),a=u.validateQuery(o);if(!a.isValid)return t.status(400).json(n.formatErrorResponse(`Query validation failed: ${a.errors.join(", ")}`,400));const i=r.headers["x-cache-control"]==="no-cache",y=await u.executeMultiCubeQuery(o,s,{skipCache:i});t.json(n.formatCubeResponse(o,y,u))}catch(e){console.error("Query execution error:",e),t.status(500).json(n.formatErrorResponse(e instanceof Error?e.message:"Query execution failed",500))}}),l.post(`${f}/batch`,async(r,t)=>{try{const{queries:e}=r.body;if(!e||!Array.isArray(e))return t.status(400).json(n.formatErrorResponse('Request body must contain a "queries" array',400));if(e.length===0)return t.status(400).json(n.formatErrorResponse("Queries array cannot be empty",400));const o=await p(r,t),s=r.headers["x-cache-control"]==="no-cache",a=await n.handleBatchRequest(e,o,u,{skipCache:s});t.json(a)}catch(e){console.error("Batch execution error:",e),t.status(500).json(n.formatErrorResponse(e instanceof Error?e.message:"Batch execution failed",500))}}),l.get(`${f}/meta`,(r,t)=>{try{const e=u.getMetadata();t.json(n.formatMetaResponse(e))}catch(e){console.error("Metadata error:",e),t.status(500).json(n.formatErrorResponse(e instanceof Error?e.message:"Failed to fetch metadata",500))}}),l.post(`${f}/sql`,async(r,t)=>{try{const e=r.body,o=await p(r,t),s=u.validateQuery(e);if(!s.isValid)return t.status(400).json(n.formatErrorResponse(`Query validation failed: ${s.errors.join(", ")}`,400));const a=e.measures?.[0]||e.dimensions?.[0];if(!a)return t.status(400).json(n.formatErrorResponse("No measures or dimensions specified",400));const i=a.split(".")[0],y=await u.generateSQL(i,e,o);t.json(n.formatSqlResponse(e,y))}catch(e){console.error("SQL generation error:",e),t.status(500).json(n.formatErrorResponse(e instanceof Error?e.message:"SQL generation failed",500))}}),l.get(`${f}/sql`,async(r,t)=>{try{const e=r.query.query;if(!e)return t.status(400).json(n.formatErrorResponse("Query parameter is required",400));const o=JSON.parse(e),s=await p(r,t),a=u.validateQuery(o);if(!a.isValid)return t.status(400).json(n.formatErrorResponse(`Query validation failed: ${a.errors.join(", ")}`,400));const i=o.measures?.[0]||o.dimensions?.[0];if(!i)return t.status(400).json(n.formatErrorResponse("No measures or dimensions specified",400));const y=i.split(".")[0],j=await u.generateSQL(y,o,s);t.json(n.formatSqlResponse(o,j))}catch(e){console.error("SQL generation error:",e),t.status(500).json(n.formatErrorResponse(e instanceof Error?e.message:"SQL generation failed",500))}}),l.post(`${f}/dry-run`,async(r,t)=>{try{const e=r.body.query||r.body,o=await p(r,t),s=await n.handleDryRun(e,o,u);t.json(s)}catch(e){console.error("Dry-run error:",e),t.status(400).json({error:e instanceof Error?e.message:"Dry-run validation failed",valid:!1})}}),l.get(`${f}/dry-run`,async(r,t)=>{try{const e=r.query.query;if(!e)return t.status(400).json({error:"Query parameter is required",valid:!1});const o=JSON.parse(e),s=await p(r,t),a=await n.handleDryRun(o,s,u);t.json(a)}catch(e){console.error("Dry-run error:",e),t.status(400).json({error:e instanceof Error?e.message:"Dry-run validation failed",valid:!1})}}),l.post(`${f}/explain`,async(r,t)=>{try{const e=r.body.query||r.body,o=r.body.options||{},s=await p(r,t),a=u.validateQuery(e);if(!a.isValid)return t.status(400).json({error:`Query validation failed: ${a.errors.join(", ")}`});const i=await u.explainQuery(e,s,o);t.json(i)}catch(e){console.error("Explain error:",e),t.status(500).json({error:e instanceof Error?e.message:"Explain query failed"})}}),v&&l.post(`${f}/agent/chat`,async(r,t)=>{try{const{handleAgentChat:e}=await Promise.resolve().then(()=>require("../handler-DtdjM1Vx.cjs")),{message:o,sessionId:s,history:a}=r.body;if(!o||typeof o!="string")return t.status(400).json({error:"message is required and must be a string"});let i=(v.apiKey||"").trim();if(v.allowClientApiKey){const d=r.headers["x-agent-api-key"];d&&(i=d.trim())}if(!i)return t.status(401).json({error:"No API key configured. Set agent.apiKey in server config or send X-Agent-Api-Key header."});const y=await p(r,t),j=v.buildSystemContext?.(y);t.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"});try{const d=e({message:o,sessionId:s,history:a,semanticLayer:u,securityContext:y,agentConfig:v,apiKey:i,systemContext:j});for await(const m of d)t.write(`data: ${JSON.stringify(m)}
|
|
2
2
|
|
|
3
|
-
`)}catch(
|
|
3
|
+
`)}catch(d){const m={type:"error",data:{message:d instanceof Error?d.message:"Stream failed"}};t.write(`data: ${JSON.stringify(m)}
|
|
4
4
|
|
|
5
|
-
`)}finally{t.end()}}catch(e){console.error("Agent chat error:",e),t.headersSent||t.status(500).json({error:e instanceof Error?e.message:"Agent chat failed"})}}),
|
|
5
|
+
`)}finally{t.end()}}catch(e){console.error("Agent chat error:",e),t.headersSent||t.status(500).json({error:e instanceof Error?e.message:"Agent chat failed"})}}),C.enabled!==!1){const r=C.basePath??"/mcp";l.post(`${r}`,async(t,e)=>{const o=c.validateOriginHeader(t.headers.origin,C.allowedOrigins?{allowedOrigins:C.allowedOrigins}:{});if(!o.valid)return e.status(403).json(c.buildJsonRpcError(null,-32600,o.reason));const s=t.headers.accept;if(!c.validateAcceptHeader(s))return e.status(400).json(c.buildJsonRpcError(null,-32600,"Accept header must include both application/json and text/event-stream"));const a=c.negotiateProtocol(t.headers);if(!a.ok)return e.status(426).json({error:"Unsupported MCP protocol version",supported:a.supported});const i=c.parseJsonRpc(t.body);if(!i)return e.status(400).json(c.buildJsonRpcError(null,-32600,"Invalid JSON-RPC 2.0 request"));const y=c.wantsEventStream(s),j=i.method==="initialize";try{const d=await c.dispatchMcpMethod(i.method,i.params,{semanticLayer:u,extractSecurityContext:p,rawRequest:t,rawResponse:e,negotiatedProtocol:a.negotiated});if(c.isNotification(i))return e.status(202).end();const m=j&&d&&typeof d=="object"&&"sessionId"in d?d.sessionId:void 0;m&&e.setHeader(c.MCP_SESSION_ID_HEADER,m);const b=c.buildJsonRpcResult(i.id??null,d);if(y){const E=c.primeEventId();return e.status(200),e.setHeader("Content-Type","text/event-stream"),e.setHeader("Cache-Control","no-cache"),e.setHeader("Connection","keep-alive"),e.write(`id: ${E}
|
|
6
6
|
|
|
7
|
-
`),e.write(c.serializeSseEvent(
|
|
7
|
+
`),e.write(c.serializeSseEvent(b,E)),e.end()}return e.json(b)}catch(d){if(c.isNotification(i))return console.error("MCP notification processing error:",d),e.status(202).end();console.error("MCP RPC error:",d);const m=d?.code??-32603,b=d?.data,E=d.message||"MCP request failed",Q=c.buildJsonRpcError(i.id??null,m,E,b);if(y){const q=c.primeEventId();return e.status(200),e.setHeader("Content-Type","text/event-stream"),e.setHeader("Cache-Control","no-cache"),e.setHeader("Connection","keep-alive"),e.write(`id: ${q}
|
|
8
8
|
|
|
9
|
-
`),e.write(c.serializeSseEvent(Q,q)),e.end()}return e.status(200).json(Q)}}),
|
|
9
|
+
`),e.write(c.serializeSseEvent(Q,q)),e.end()}return e.status(200).json(Q)}}),l.get(`${r}`,async(t,e)=>{const o=c.primeEventId();e.status(200),e.setHeader("Content-Type","text/event-stream"),e.setHeader("Cache-Control","no-cache"),e.setHeader("Connection","keep-alive"),e.write(c.serializeSseEvent({jsonrpc:"2.0",method:"mcp/ready",params:{protocol:"streamable-http"}},o,15e3));const s=setInterval(()=>{e.write(`: keep-alive
|
|
10
10
|
|
|
11
|
-
`)},15e3);t.on("close",()=>{clearInterval(s)})}),
|
|
11
|
+
`)},15e3);t.on("close",()=>{clearInterval(s)})}),l.delete(`${r}`,(t,e)=>e.status(405).json({error:"Session termination not supported"}))}return l.use((r,t,e,o)=>{console.error("Express adapter error:",r),e.headersSent||e.status(500).json(n.formatErrorResponse(r,500))}),l}function I(g,h){const w=$(h);return g.use("/",w),g}function N(g){const h=R();return I(h,g)}exports.createCubeApp=N;exports.createCubeRouter=$;exports.mountCubeRoutes=I;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import E, { Router as J } from "express";
|
|
2
2
|
import L from "cors";
|
|
3
3
|
import { S as D, v as _, b as w, a as K, n as V, p as z, w as T, d as B, i as P, M as F, c as U, e as S, s as Q } from "../mcp-transport-m1X1GtwG.js";
|
|
4
|
-
import { formatErrorResponse as
|
|
4
|
+
import { formatErrorResponse as d, formatCubeResponse as A, handleBatchRequest as X, formatMetaResponse as G, formatSqlResponse as H, handleDryRun as M } from "../utils.js";
|
|
5
5
|
function W(h) {
|
|
6
6
|
const {
|
|
7
7
|
cubes: m,
|
|
@@ -13,13 +13,13 @@ function W(h) {
|
|
|
13
13
|
basePath: p = "/cubejs-api/v1",
|
|
14
14
|
jsonLimit: $ = "10mb",
|
|
15
15
|
cache: O,
|
|
16
|
-
mcp:
|
|
17
|
-
agent:
|
|
16
|
+
mcp: v = { enabled: !0 },
|
|
17
|
+
agent: g
|
|
18
18
|
} = h;
|
|
19
19
|
if (!m || m.length === 0)
|
|
20
20
|
throw new Error("At least one cube must be provided in the cubes array");
|
|
21
|
-
const
|
|
22
|
-
R &&
|
|
21
|
+
const u = J();
|
|
22
|
+
R && u.use(L(R)), u.use(E.json({ limit: $ })), u.use(E.urlencoded({ extended: !0, limit: $ }));
|
|
23
23
|
const i = new D({
|
|
24
24
|
drizzle: x,
|
|
25
25
|
schema: N,
|
|
@@ -28,27 +28,27 @@ function W(h) {
|
|
|
28
28
|
});
|
|
29
29
|
if (m.forEach((r) => {
|
|
30
30
|
i.registerCube(r);
|
|
31
|
-
}),
|
|
31
|
+
}), u.post(`${p}/load`, async (r, t) => {
|
|
32
32
|
try {
|
|
33
33
|
const e = r.body.query || r.body, o = await y(r, t), a = i.validateQuery(e);
|
|
34
34
|
if (!a.isValid)
|
|
35
|
-
return t.status(400).json(
|
|
35
|
+
return t.status(400).json(d(
|
|
36
36
|
`Query validation failed: ${a.errors.join(", ")}`,
|
|
37
37
|
400
|
|
38
38
|
));
|
|
39
39
|
const n = r.headers["x-cache-control"] === "no-cache", s = await i.executeMultiCubeQuery(e, o, { skipCache: n });
|
|
40
40
|
t.json(A(e, s, i));
|
|
41
41
|
} catch (e) {
|
|
42
|
-
console.error("Query execution error:", e), t.status(500).json(
|
|
42
|
+
console.error("Query execution error:", e), t.status(500).json(d(
|
|
43
43
|
e instanceof Error ? e.message : "Query execution failed",
|
|
44
44
|
500
|
|
45
45
|
));
|
|
46
46
|
}
|
|
47
|
-
}),
|
|
47
|
+
}), u.get(`${p}/load`, async (r, t) => {
|
|
48
48
|
try {
|
|
49
49
|
const e = r.query.query;
|
|
50
50
|
if (!e)
|
|
51
|
-
return t.status(400).json(
|
|
51
|
+
return t.status(400).json(d(
|
|
52
52
|
"Query parameter is required",
|
|
53
53
|
400
|
|
54
54
|
));
|
|
@@ -56,107 +56,107 @@ function W(h) {
|
|
|
56
56
|
try {
|
|
57
57
|
o = JSON.parse(e);
|
|
58
58
|
} catch {
|
|
59
|
-
return t.status(400).json(
|
|
59
|
+
return t.status(400).json(d(
|
|
60
60
|
"Invalid JSON in query parameter",
|
|
61
61
|
400
|
|
62
62
|
));
|
|
63
63
|
}
|
|
64
64
|
const a = await y(r, t), n = i.validateQuery(o);
|
|
65
65
|
if (!n.isValid)
|
|
66
|
-
return t.status(400).json(
|
|
66
|
+
return t.status(400).json(d(
|
|
67
67
|
`Query validation failed: ${n.errors.join(", ")}`,
|
|
68
68
|
400
|
|
69
69
|
));
|
|
70
|
-
const s = r.headers["x-cache-control"] === "no-cache",
|
|
71
|
-
t.json(A(o,
|
|
70
|
+
const s = r.headers["x-cache-control"] === "no-cache", l = await i.executeMultiCubeQuery(o, a, { skipCache: s });
|
|
71
|
+
t.json(A(o, l, i));
|
|
72
72
|
} catch (e) {
|
|
73
|
-
console.error("Query execution error:", e), t.status(500).json(
|
|
73
|
+
console.error("Query execution error:", e), t.status(500).json(d(
|
|
74
74
|
e instanceof Error ? e.message : "Query execution failed",
|
|
75
75
|
500
|
|
76
76
|
));
|
|
77
77
|
}
|
|
78
|
-
}),
|
|
78
|
+
}), u.post(`${p}/batch`, async (r, t) => {
|
|
79
79
|
try {
|
|
80
80
|
const { queries: e } = r.body;
|
|
81
81
|
if (!e || !Array.isArray(e))
|
|
82
|
-
return t.status(400).json(
|
|
82
|
+
return t.status(400).json(d(
|
|
83
83
|
'Request body must contain a "queries" array',
|
|
84
84
|
400
|
|
85
85
|
));
|
|
86
86
|
if (e.length === 0)
|
|
87
|
-
return t.status(400).json(
|
|
87
|
+
return t.status(400).json(d(
|
|
88
88
|
"Queries array cannot be empty",
|
|
89
89
|
400
|
|
90
90
|
));
|
|
91
91
|
const o = await y(r, t), a = r.headers["x-cache-control"] === "no-cache", n = await X(e, o, i, { skipCache: a });
|
|
92
92
|
t.json(n);
|
|
93
93
|
} catch (e) {
|
|
94
|
-
console.error("Batch execution error:", e), t.status(500).json(
|
|
94
|
+
console.error("Batch execution error:", e), t.status(500).json(d(
|
|
95
95
|
e instanceof Error ? e.message : "Batch execution failed",
|
|
96
96
|
500
|
|
97
97
|
));
|
|
98
98
|
}
|
|
99
|
-
}),
|
|
99
|
+
}), u.get(`${p}/meta`, (r, t) => {
|
|
100
100
|
try {
|
|
101
101
|
const e = i.getMetadata();
|
|
102
102
|
t.json(G(e));
|
|
103
103
|
} catch (e) {
|
|
104
|
-
console.error("Metadata error:", e), t.status(500).json(
|
|
104
|
+
console.error("Metadata error:", e), t.status(500).json(d(
|
|
105
105
|
e instanceof Error ? e.message : "Failed to fetch metadata",
|
|
106
106
|
500
|
|
107
107
|
));
|
|
108
108
|
}
|
|
109
|
-
}),
|
|
109
|
+
}), u.post(`${p}/sql`, async (r, t) => {
|
|
110
110
|
try {
|
|
111
111
|
const e = r.body, o = await y(r, t), a = i.validateQuery(e);
|
|
112
112
|
if (!a.isValid)
|
|
113
|
-
return t.status(400).json(
|
|
113
|
+
return t.status(400).json(d(
|
|
114
114
|
`Query validation failed: ${a.errors.join(", ")}`,
|
|
115
115
|
400
|
|
116
116
|
));
|
|
117
117
|
const n = e.measures?.[0] || e.dimensions?.[0];
|
|
118
118
|
if (!n)
|
|
119
|
-
return t.status(400).json(
|
|
119
|
+
return t.status(400).json(d(
|
|
120
120
|
"No measures or dimensions specified",
|
|
121
121
|
400
|
|
122
122
|
));
|
|
123
|
-
const s = n.split(".")[0],
|
|
124
|
-
t.json(H(e,
|
|
123
|
+
const s = n.split(".")[0], l = await i.generateSQL(s, e, o);
|
|
124
|
+
t.json(H(e, l));
|
|
125
125
|
} catch (e) {
|
|
126
|
-
console.error("SQL generation error:", e), t.status(500).json(
|
|
126
|
+
console.error("SQL generation error:", e), t.status(500).json(d(
|
|
127
127
|
e instanceof Error ? e.message : "SQL generation failed",
|
|
128
128
|
500
|
|
129
129
|
));
|
|
130
130
|
}
|
|
131
|
-
}),
|
|
131
|
+
}), u.get(`${p}/sql`, async (r, t) => {
|
|
132
132
|
try {
|
|
133
133
|
const e = r.query.query;
|
|
134
134
|
if (!e)
|
|
135
|
-
return t.status(400).json(
|
|
135
|
+
return t.status(400).json(d(
|
|
136
136
|
"Query parameter is required",
|
|
137
137
|
400
|
|
138
138
|
));
|
|
139
139
|
const o = JSON.parse(e), a = await y(r, t), n = i.validateQuery(o);
|
|
140
140
|
if (!n.isValid)
|
|
141
|
-
return t.status(400).json(
|
|
141
|
+
return t.status(400).json(d(
|
|
142
142
|
`Query validation failed: ${n.errors.join(", ")}`,
|
|
143
143
|
400
|
|
144
144
|
));
|
|
145
145
|
const s = o.measures?.[0] || o.dimensions?.[0];
|
|
146
146
|
if (!s)
|
|
147
|
-
return t.status(400).json(
|
|
147
|
+
return t.status(400).json(d(
|
|
148
148
|
"No measures or dimensions specified",
|
|
149
149
|
400
|
|
150
150
|
));
|
|
151
|
-
const
|
|
152
|
-
t.json(H(o,
|
|
151
|
+
const l = s.split(".")[0], j = await i.generateSQL(l, o, a);
|
|
152
|
+
t.json(H(o, j));
|
|
153
153
|
} catch (e) {
|
|
154
|
-
console.error("SQL generation error:", e), t.status(500).json(
|
|
154
|
+
console.error("SQL generation error:", e), t.status(500).json(d(
|
|
155
155
|
e instanceof Error ? e.message : "SQL generation failed",
|
|
156
156
|
500
|
|
157
157
|
));
|
|
158
158
|
}
|
|
159
|
-
}),
|
|
159
|
+
}), u.post(`${p}/dry-run`, async (r, t) => {
|
|
160
160
|
try {
|
|
161
161
|
const e = r.body.query || r.body, o = await y(r, t), a = await M(e, o, i);
|
|
162
162
|
t.json(a);
|
|
@@ -166,7 +166,7 @@ function W(h) {
|
|
|
166
166
|
valid: !1
|
|
167
167
|
});
|
|
168
168
|
}
|
|
169
|
-
}),
|
|
169
|
+
}), u.get(`${p}/dry-run`, async (r, t) => {
|
|
170
170
|
try {
|
|
171
171
|
const e = r.query.query;
|
|
172
172
|
if (!e)
|
|
@@ -182,7 +182,7 @@ function W(h) {
|
|
|
182
182
|
valid: !1
|
|
183
183
|
});
|
|
184
184
|
}
|
|
185
|
-
}),
|
|
185
|
+
}), u.post(`${p}/explain`, async (r, t) => {
|
|
186
186
|
try {
|
|
187
187
|
const e = r.body.query || r.body, o = r.body.options || {}, a = await y(r, t), n = i.validateQuery(e);
|
|
188
188
|
if (!n.isValid)
|
|
@@ -196,46 +196,47 @@ function W(h) {
|
|
|
196
196
|
error: e instanceof Error ? e.message : "Explain query failed"
|
|
197
197
|
});
|
|
198
198
|
}
|
|
199
|
-
}),
|
|
199
|
+
}), g && u.post(`${p}/agent/chat`, async (r, t) => {
|
|
200
200
|
try {
|
|
201
|
-
const { handleAgentChat: e } = await import("../handler-
|
|
201
|
+
const { handleAgentChat: e } = await import("../handler-CbDMdSY5.js"), { message: o, sessionId: a, history: n } = r.body;
|
|
202
202
|
if (!o || typeof o != "string")
|
|
203
203
|
return t.status(400).json({ error: "message is required and must be a string" });
|
|
204
|
-
let s = (
|
|
205
|
-
if (
|
|
206
|
-
const
|
|
207
|
-
|
|
204
|
+
let s = (g.apiKey || "").trim();
|
|
205
|
+
if (g.allowClientApiKey) {
|
|
206
|
+
const c = r.headers["x-agent-api-key"];
|
|
207
|
+
c && (s = c.trim());
|
|
208
208
|
}
|
|
209
209
|
if (!s)
|
|
210
210
|
return t.status(401).json({
|
|
211
211
|
error: "No API key configured. Set agent.apiKey in server config or send X-Agent-Api-Key header."
|
|
212
212
|
});
|
|
213
|
-
const
|
|
213
|
+
const l = await y(r, t), j = g.buildSystemContext?.(l);
|
|
214
214
|
t.writeHead(200, {
|
|
215
215
|
"Content-Type": "text/event-stream",
|
|
216
216
|
"Cache-Control": "no-cache",
|
|
217
217
|
Connection: "keep-alive"
|
|
218
218
|
});
|
|
219
219
|
try {
|
|
220
|
-
const
|
|
220
|
+
const c = e({
|
|
221
221
|
message: o,
|
|
222
222
|
sessionId: a,
|
|
223
223
|
history: n,
|
|
224
224
|
semanticLayer: i,
|
|
225
|
-
securityContext:
|
|
226
|
-
agentConfig:
|
|
227
|
-
apiKey: s
|
|
225
|
+
securityContext: l,
|
|
226
|
+
agentConfig: g,
|
|
227
|
+
apiKey: s,
|
|
228
|
+
systemContext: j
|
|
228
229
|
});
|
|
229
|
-
for await (const
|
|
230
|
-
t.write(`data: ${JSON.stringify(
|
|
230
|
+
for await (const f of c)
|
|
231
|
+
t.write(`data: ${JSON.stringify(f)}
|
|
231
232
|
|
|
232
233
|
`);
|
|
233
|
-
} catch (
|
|
234
|
-
const
|
|
234
|
+
} catch (c) {
|
|
235
|
+
const f = {
|
|
235
236
|
type: "error",
|
|
236
|
-
data: { message:
|
|
237
|
+
data: { message: c instanceof Error ? c.message : "Stream failed" }
|
|
237
238
|
};
|
|
238
|
-
t.write(`data: ${JSON.stringify(
|
|
239
|
+
t.write(`data: ${JSON.stringify(f)}
|
|
239
240
|
|
|
240
241
|
`);
|
|
241
242
|
} finally {
|
|
@@ -246,12 +247,12 @@ function W(h) {
|
|
|
246
247
|
error: e instanceof Error ? e.message : "Agent chat failed"
|
|
247
248
|
});
|
|
248
249
|
}
|
|
249
|
-
}),
|
|
250
|
-
const r =
|
|
251
|
-
|
|
250
|
+
}), v.enabled !== !1) {
|
|
251
|
+
const r = v.basePath ?? "/mcp";
|
|
252
|
+
u.post(`${r}`, async (t, e) => {
|
|
252
253
|
const o = _(
|
|
253
254
|
t.headers.origin,
|
|
254
|
-
|
|
255
|
+
v.allowedOrigins ? { allowedOrigins: v.allowedOrigins } : {}
|
|
255
256
|
);
|
|
256
257
|
if (!o.valid)
|
|
257
258
|
return e.status(403).json(w(null, -32600, o.reason));
|
|
@@ -267,9 +268,9 @@ function W(h) {
|
|
|
267
268
|
const s = z(t.body);
|
|
268
269
|
if (!s)
|
|
269
270
|
return e.status(400).json(w(null, -32600, "Invalid JSON-RPC 2.0 request"));
|
|
270
|
-
const
|
|
271
|
+
const l = T(a), j = s.method === "initialize";
|
|
271
272
|
try {
|
|
272
|
-
const
|
|
273
|
+
const c = await B(
|
|
273
274
|
s.method,
|
|
274
275
|
s.params,
|
|
275
276
|
{
|
|
@@ -282,22 +283,22 @@ function W(h) {
|
|
|
282
283
|
);
|
|
283
284
|
if (P(s))
|
|
284
285
|
return e.status(202).end();
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
const C = U(s.id ?? null,
|
|
288
|
-
if (
|
|
286
|
+
const f = j && c && typeof c == "object" && "sessionId" in c ? c.sessionId : void 0;
|
|
287
|
+
f && e.setHeader(F, f);
|
|
288
|
+
const C = U(s.id ?? null, c);
|
|
289
|
+
if (l) {
|
|
289
290
|
const b = S();
|
|
290
291
|
return e.status(200), e.setHeader("Content-Type", "text/event-stream"), e.setHeader("Cache-Control", "no-cache"), e.setHeader("Connection", "keep-alive"), e.write(`id: ${b}
|
|
291
292
|
|
|
292
293
|
`), e.write(Q(C, b)), e.end();
|
|
293
294
|
}
|
|
294
295
|
return e.json(C);
|
|
295
|
-
} catch (
|
|
296
|
+
} catch (c) {
|
|
296
297
|
if (P(s))
|
|
297
|
-
return console.error("MCP notification processing error:",
|
|
298
|
-
console.error("MCP RPC error:",
|
|
299
|
-
const
|
|
300
|
-
if (
|
|
298
|
+
return console.error("MCP notification processing error:", c), e.status(202).end();
|
|
299
|
+
console.error("MCP RPC error:", c);
|
|
300
|
+
const f = c?.code ?? -32603, C = c?.data, b = c.message || "MCP request failed", q = w(s.id ?? null, f, b, C);
|
|
301
|
+
if (l) {
|
|
301
302
|
const I = S();
|
|
302
303
|
return e.status(200), e.setHeader("Content-Type", "text/event-stream"), e.setHeader("Cache-Control", "no-cache"), e.setHeader("Connection", "keep-alive"), e.write(`id: ${I}
|
|
303
304
|
|
|
@@ -305,7 +306,7 @@ function W(h) {
|
|
|
305
306
|
}
|
|
306
307
|
return e.status(200).json(q);
|
|
307
308
|
}
|
|
308
|
-
}),
|
|
309
|
+
}), u.get(`${r}`, async (t, e) => {
|
|
309
310
|
const o = S();
|
|
310
311
|
e.status(200), e.setHeader("Content-Type", "text/event-stream"), e.setHeader("Cache-Control", "no-cache"), e.setHeader("Connection", "keep-alive"), e.write(Q({
|
|
311
312
|
jsonrpc: "2.0",
|
|
@@ -320,11 +321,11 @@ function W(h) {
|
|
|
320
321
|
t.on("close", () => {
|
|
321
322
|
clearInterval(a);
|
|
322
323
|
});
|
|
323
|
-
}),
|
|
324
|
+
}), u.delete(`${r}`, (t, e) => e.status(405).json({ error: "Session termination not supported" }));
|
|
324
325
|
}
|
|
325
|
-
return
|
|
326
|
-
console.error("Express adapter error:", r), e.headersSent || e.status(500).json(
|
|
327
|
-
}),
|
|
326
|
+
return u.use((r, t, e, o) => {
|
|
327
|
+
console.error("Express adapter error:", r), e.headersSent || e.status(500).json(d(r, 500));
|
|
328
|
+
}), u;
|
|
328
329
|
}
|
|
329
330
|
function Y(h, m) {
|
|
330
331
|
const x = W(m);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
"use strict";var O=Object.create;var j=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var L=Object.getPrototypeOf,D=Object.prototype.hasOwnProperty;var H=(
|
|
1
|
+
"use strict";var O=Object.create;var j=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var J=Object.getOwnPropertyNames;var L=Object.getPrototypeOf,D=Object.prototype.hasOwnProperty;var H=(l,n,v,R)=>{if(n&&typeof n=="object"||typeof n=="function")for(let f of J(n))!D.call(l,f)&&f!==v&&j(l,f,{get:()=>n[f],enumerable:!(R=k(n,f))||R.enumerable});return l};var T=(l,n,v)=>(v=l!=null?O(L(l)):{},H(n||!l||!l.__esModule?j(v,"default",{value:l,enumerable:!0}):v,l));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("../mcp-transport-8u9G5oNa.cjs"),a=require("../utils.cjs"),q=function(n,v,R){const{cubes:f,drizzle:I,schema:A,extractSecurityContext:g,engineType:M,cors:P,basePath:h="/cubejs-api/v1",bodyLimit:C=10485760,cache:N,mcp:x={enabled:!0},agent:w}=v;if(!f||f.length===0)return R(new Error("At least one cube must be provided in the cubes array"));P&&n.register(import("@fastify/cors"),P),n.addHook("onRequest",async(r,t)=>{r.method==="POST"&&(r.body=void 0)});const d=new u.SemanticLayerCompiler({drizzle:I,schema:A,engineType:M,cache:N});if(f.forEach(r=>{d.registerCube(r)}),n.post(`${h}/load`,{bodyLimit:C,schema:{body:{type:"object",additionalProperties:!0}}},async(r,t)=>{try{const e=r.body,o=e.query||e,s=await g(r),i=d.validateQuery(o);if(!i.isValid)return t.status(400).send(a.formatErrorResponse(`Query validation failed: ${i.errors.join(", ")}`,400));const c=r.headers["x-cache-control"]==="no-cache",p=await d.executeMultiCubeQuery(o,s,{skipCache:c});return a.formatCubeResponse(o,p,d)}catch(e){return r.log.error(e,"Query execution error"),t.status(500).send(a.formatErrorResponse(e instanceof Error?e.message:"Query execution failed",500))}}),n.get(`${h}/load`,{schema:{querystring:{type:"object",properties:{query:{type:"string"}},required:["query"]}}},async(r,t)=>{try{const{query:e}=r.query;let o;try{o=JSON.parse(e)}catch{return t.status(400).send(a.formatErrorResponse("Invalid JSON in query parameter",400))}const s=await g(r),i=d.validateQuery(o);if(!i.isValid)return t.status(400).send(a.formatErrorResponse(`Query validation failed: ${i.errors.join(", ")}`,400));const c=r.headers["x-cache-control"]==="no-cache",p=await d.executeMultiCubeQuery(o,s,{skipCache:c});return a.formatCubeResponse(o,p,d)}catch(e){return r.log.error(e,"Query execution error"),t.status(500).send(a.formatErrorResponse(e instanceof Error?e.message:"Query execution failed",500))}}),n.post(`${h}/batch`,{bodyLimit:C,schema:{body:{type:"object",required:["queries"],properties:{queries:{type:"array",items:{type:"object"}}}}}},async(r,t)=>{try{const{queries:e}=r.body;if(!e||!Array.isArray(e))return t.status(400).send(a.formatErrorResponse('Request body must contain a "queries" array',400));if(e.length===0)return t.status(400).send(a.formatErrorResponse("Queries array cannot be empty",400));const o=await g(r),s=r.headers["x-cache-control"]==="no-cache";return await a.handleBatchRequest(e,o,d,{skipCache:s})}catch(e){return r.log.error(e,"Batch execution error"),t.status(500).send(a.formatErrorResponse(e instanceof Error?e.message:"Batch execution failed",500))}}),n.get(`${h}/meta`,async(r,t)=>{try{const e=d.getMetadata();return a.formatMetaResponse(e)}catch(e){return r.log.error(e,"Metadata error"),t.status(500).send(a.formatErrorResponse(e instanceof Error?e.message:"Failed to fetch metadata",500))}}),n.post(`${h}/sql`,{bodyLimit:C,schema:{body:{type:"object",additionalProperties:!0}}},async(r,t)=>{try{const e=r.body,o=await g(r),s=d.validateQuery(e);if(!s.isValid)return t.status(400).send(a.formatErrorResponse(`Query validation failed: ${s.errors.join(", ")}`,400));const i=e.measures?.[0]||e.dimensions?.[0];if(!i)return t.status(400).send(a.formatErrorResponse("No measures or dimensions specified",400));const c=i.split(".")[0],p=await d.generateSQL(c,e,o);return a.formatSqlResponse(e,p)}catch(e){return r.log.error(e,"SQL generation error"),t.status(500).send(a.formatErrorResponse(e instanceof Error?e.message:"SQL generation failed",500))}}),n.get(`${h}/sql`,{schema:{querystring:{type:"object",properties:{query:{type:"string"}},required:["query"]}}},async(r,t)=>{try{const{query:e}=r.query,o=JSON.parse(e),s=await g(r),i=d.validateQuery(o);if(!i.isValid)return t.status(400).send(a.formatErrorResponse(`Query validation failed: ${i.errors.join(", ")}`,400));const c=o.measures?.[0]||o.dimensions?.[0];if(!c)return t.status(400).send(a.formatErrorResponse("No measures or dimensions specified",400));const p=c.split(".")[0],E=await d.generateSQL(p,o,s);return a.formatSqlResponse(o,E)}catch(e){return r.log.error(e,"SQL generation error"),t.status(500).send(a.formatErrorResponse(e instanceof Error?e.message:"SQL generation failed",500))}}),n.post(`${h}/dry-run`,{bodyLimit:C,schema:{body:{type:"object",additionalProperties:!0}}},async(r,t)=>{try{const e=r.body,o=e.query||e,s=await g(r);return await a.handleDryRun(o,s,d)}catch(e){return r.log.error(e,"Dry-run error"),t.status(400).send({error:e instanceof Error?e.message:"Dry-run validation failed",valid:!1})}}),n.get(`${h}/dry-run`,{schema:{querystring:{type:"object",properties:{query:{type:"string"}},required:["query"]}}},async(r,t)=>{try{const{query:e}=r.query,o=JSON.parse(e),s=await g(r);return await a.handleDryRun(o,s,d)}catch(e){return r.log.error(e,"Dry-run error"),t.status(400).send({error:e instanceof Error?e.message:"Dry-run validation failed",valid:!1})}}),n.post(`${h}/explain`,{bodyLimit:C,schema:{body:{type:"object",additionalProperties:!0}}},async(r,t)=>{try{const e=r.body,o=e.query||e,s=e.options||{},i=await g(r),c=d.validateQuery(o);return c.isValid?await d.explainQuery(o,i,s):t.status(400).send({error:`Query validation failed: ${c.errors.join(", ")}`})}catch(e){return r.log.error(e,"Explain error"),t.status(500).send({error:e instanceof Error?e.message:"Explain query failed"})}}),w&&n.post(`${h}/agent/chat`,{bodyLimit:C,schema:{body:{type:"object",additionalProperties:!0}}},async(r,t)=>{try{const{handleAgentChat:e}=await Promise.resolve().then(()=>require("../handler-DtdjM1Vx.cjs")),o=r.body,{message:s,sessionId:i,history:c}=o;if(!s||typeof s!="string")return t.status(400).send({error:"message is required and must be a string"});let p=(w.apiKey||"").trim();if(w.allowClientApiKey){const m=r.headers["x-agent-api-key"];m&&(p=m.trim())}if(!p)return t.status(401).send({error:"No API key configured. Set agent.apiKey in server config or send X-Agent-Api-Key header."});const E=await g(r),y=w.buildSystemContext?.(E);t.raw.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"});try{const m=e({message:s,sessionId:i,history:c,semanticLayer:d,securityContext:E,agentConfig:w,apiKey:p,systemContext:y});for await(const b of m)t.raw.write(`data: ${JSON.stringify(b)}
|
|
2
2
|
|
|
3
|
-
`)}catch(
|
|
3
|
+
`)}catch(m){const b={type:"error",data:{message:m instanceof Error?m.message:"Stream failed"}};t.raw.write(`data: ${JSON.stringify(b)}
|
|
4
4
|
|
|
5
|
-
`)}finally{t.raw.end()}}catch(e){if(r.log.error(e,"Agent chat error"),!t.raw.headersSent)return t.status(500).send({error:e instanceof Error?e.message:"Agent chat failed"})}}),
|
|
5
|
+
`)}finally{t.raw.end()}}catch(e){if(r.log.error(e,"Agent chat error"),!t.raw.headersSent)return t.status(500).send({error:e instanceof Error?e.message:"Agent chat failed"})}}),x.enabled!==!1){const r=x.basePath??"/mcp";n.post(`${r}`,{bodyLimit:C,schema:{body:{type:"object",additionalProperties:!0}}},async(t,e)=>{const o=u.validateOriginHeader(t.headers.origin,x.allowedOrigins?{allowedOrigins:x.allowedOrigins}:{});if(!o.valid)return e.status(403).send(u.buildJsonRpcError(null,-32600,o.reason));const s=t.headers.accept;if(!u.validateAcceptHeader(s))return e.status(400).send(u.buildJsonRpcError(null,-32600,"Accept header must include both application/json and text/event-stream"));const i=u.negotiateProtocol(t.headers);if(!i.ok)return e.status(426).send({error:"Unsupported MCP protocol version",supported:i.supported});const c=u.parseJsonRpc(t.body);if(!c)return e.status(400).send(u.buildJsonRpcError(null,-32600,"Invalid JSON-RPC 2.0 request"));const p=u.wantsEventStream(s),E=c.method==="initialize";try{const y=await u.dispatchMcpMethod(c.method,c.params,{semanticLayer:d,extractSecurityContext:g,rawRequest:t,rawResponse:e,negotiatedProtocol:i.negotiated});if(u.isNotification(c))return e.status(202).send();const m=E&&y&&typeof y=="object"&&"sessionId"in y?y.sessionId:void 0;m&&e.header(u.MCP_SESSION_ID_HEADER,m);const b=u.buildJsonRpcResult(c.id??null,y);if(p){const S=u.primeEventId();e.header("Content-Type","text/event-stream").header("Cache-Control","no-cache").header("Connection","keep-alive").send(`id: ${S}
|
|
6
6
|
|
|
7
|
-
${u.serializeSseEvent(
|
|
7
|
+
${u.serializeSseEvent(b,S)}`);return}return e.send(b)}catch(y){if(u.isNotification(c))return t.log.error(y,"MCP notification processing error"),e.status(202).send();t.log.error(y,"MCP RPC error");const m=y?.code??-32603,b=y?.data,S=y.message||"MCP request failed",Q=u.buildJsonRpcError(c.id??null,m,S,b);if(p){const $=u.primeEventId();e.header("Content-Type","text/event-stream").header("Cache-Control","no-cache").header("Connection","keep-alive").send(`id: ${$}
|
|
8
8
|
|
|
9
9
|
${u.serializeSseEvent(Q,$)}`);return}return e.send(Q)}}),n.get(`${r}`,async(t,e)=>{const o=u.primeEventId();e.raw.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),e.raw.write(u.serializeSseEvent({jsonrpc:"2.0",method:"mcp/ready",params:{protocol:"streamable-http"}},o,15e3));const s=setInterval(()=>{e.raw.write(`: keep-alive
|
|
10
10
|
|
|
11
|
-
`)},15e3);t.raw.on("close",()=>{clearInterval(s)})}),n.delete(`${r}`,async(t,e)=>e.status(405).send({error:"Session termination not supported"}))}n.setErrorHandler(async(r,t,e)=>{t.log.error(r,"Fastify cube adapter error"),e.statusCode<400&&e.status(500);const o=r instanceof Error?r:String(r);return a.formatErrorResponse(o,e.statusCode)}),
|
|
11
|
+
`)},15e3);t.raw.on("close",()=>{clearInterval(s)})}),n.delete(`${r}`,async(t,e)=>e.status(405).send({error:"Session termination not supported"}))}n.setErrorHandler(async(r,t,e)=>{t.log.error(r,"Fastify cube adapter error"),e.statusCode<400&&e.status(500);const o=r instanceof Error?r:String(r);return a.formatErrorResponse(o,e.statusCode)}),R()};async function z(l,n){await l.register(q,n)}function K(l){const n=require("fastify")({logger:!0});return n.register(q,l),n}exports.createCubeApp=K;exports.cubePlugin=q;exports.registerCubeRoutes=z;
|