@brickkit/core 0.1.3 → 0.2.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.
- package/README.md +70 -0
- package/dist/chunks/client.d.ts +950 -0
- package/dist/chunks/client.d.ts.map +1 -0
- package/dist/chunks/client.js +311 -0
- package/dist/chunks/client.js.map +1 -0
- package/dist/client.d.ts +2 -0
- package/dist/client.js +2 -0
- package/dist/index.d.ts +55 -634
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +206 -95
- package/dist/index.js.map +1 -1
- package/package.json +13 -3
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":["
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":["TSchema","TSchema","TSchema"],"sources":["../src/db/index.ts","../src/service.ts","../src/action.ts","../src/resource.ts","../src/rpc.ts","../src/registry.ts","../src/typebox.ts","../src/openapi.ts","../src/tool.ts","../src/mcp.ts"],"mappings":";;;;;iBA6CiB,eACf,gBAAgB,0BAA0B,iCAGxC,KAAK,qBAAqB,qBAC1B,KAAK,uBAAuB;EAC9B,SACI,qBAAqB,qBACrB,uBAAuB;;EAE3B;;EAEA;;YAGU,cACV,gBAAgB,0BAA0B,2BACxC,kBAAkB,WAAW,iBAAiB;;;;;;wBAOlC,eACd,gBAAgB,0BAA0B,yBAE1C,SAAQ,eAAe,WACtB,kBAAkB,WAAW,iBAAiB;;wBAmBjC,eAAe,KAC7B,SAAS,QAAQ,WACjB,UAAU,eAAe,OACxB;;;;;wBASa,uBAAuB;;;;;;;wBAiCvB,uBACd,YACA;EAAQ;EAAqB;;;;;;;;wBAgEf,sBACd,SACA,YACA;EACE;EACA;EACA;;;;;;;wBAoEY,gBACd,SACA,YACA;;;;;;wBA6Dc,mBAAmB;;;;;;wBAuBnB,WACd,iBAAiB,2BAA2B,sBAAsB,SAClE,mBACC;;;qBC3UU,YACX,MAAM,kBAAkB,0BACxB,wBAA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S1D,mBAAmB,UAAU,SAC3C,QAAQ,eACR;EAEI;EAAe,MAAM,OAAO;;EAC5B;EAAgB,QAAQ;;;;UCnBb;EACf;EACA;EACA;EACA;EACA,WAAW;EACX,WAAW;;wBAGG,oBACd,UAAS,0BACR;;;wBCLa,WACd,qBAAqBC,mCACrB,sBAAsBA,mCACtB,gBAAgB,eAAe,yBAAyB,eAEtD,wBAEF,iBAAiB,gBAAgB,eAEjC,QAAQ,wBACN,cACA,eACA,SACA,YAED,KAAK,cAAc,eAAe,SAAS;wBAE9B,WACd,qBAAqBA,mCACrB,sBAAsBA,mCACtB,gBAAgB,eAAe,yBAAyB,eAEtD,wBAEF,iBAAiB,gBAAgB,eAEjC,QAAQ,2BACN,cACA,eACA,SACA,YAED,KAAK,cAAc,eAAe,SAAS;wBAW9B,OAAO,iBAAiB,SAAS;;;UCnDhC;EACf;EACA;EACA;EACA;EACA,aAAa;;UAGE;EACf,oBAAoB,KAAK;EACzB,iBAAiB;;UAGF;EACf,aAAa;EACb,SACE,cACA,OAAO,yBACP,MAAM,QAAQ,iBACb;;qBAGQ,yBAAyB;EACxB,YAAA;;;wBAOE,kBAAkB,SAAS,qBAAqB;UAsC/C;EACf,UAAU;EACV;IAAc;IAAc;IAAiB;;;EAE7C;;;wBAcc,iBAAiB,SAAS,qBAKtC,SAAS,SACT,MAAM,QAAQ,mBACb,QAAQ"}
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { a as isBrickTransportError, i as isBrickServerError, n as BrickTransportError, o as ActionExecutionError, r as createBrickClient, s as ValidationError, t as BrickServerError } from "./chunks/client.js";
|
|
1
2
|
import { createRequire } from "node:module";
|
|
2
3
|
import { Type } from "@sinclair/typebox";
|
|
3
4
|
import { TypeCompiler } from "@sinclair/typebox/compiler";
|
|
@@ -7,46 +8,8 @@ import { drizzle } from "drizzle-orm/postgres-js";
|
|
|
7
8
|
import { Column, SQL, Table, and, and as and$1, asc, asc as asc$1, desc, desc as desc$1, entityKind, eq, eq as eq$1, getTableColumns, getTableColumns as getTableColumns$1, getTableName, getTableName as getTableName$1, gt, gt as gt$1, gte, gte as gte$1, ilike, ilike as ilike$1, inArray, is, is as is$1, isNotNull, isNull, like, like as like$1, lt, lt as lt$1, lte, lte as lte$1, ne, not, notInArray, or, or as or$1, relations, sql, sql as sql$1 } from "drizzle-orm";
|
|
8
9
|
import { mkdirSync } from "node:fs";
|
|
9
10
|
import { dirname } from "node:path";
|
|
10
|
-
//#region
|
|
11
|
-
var
|
|
12
|
-
status;
|
|
13
|
-
errors;
|
|
14
|
-
constructor(message, errors = [], status = 400) {
|
|
15
|
-
super(message);
|
|
16
|
-
this.name = "ValidationError";
|
|
17
|
-
this.status = status;
|
|
18
|
-
this.errors = errors;
|
|
19
|
-
}
|
|
20
|
-
toJSON() {
|
|
21
|
-
return {
|
|
22
|
-
name: this.name,
|
|
23
|
-
message: this.message,
|
|
24
|
-
status: this.status,
|
|
25
|
-
errors: this.errors
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
var ActionExecutionError = class extends Error {
|
|
30
|
-
code;
|
|
31
|
-
status;
|
|
32
|
-
details;
|
|
33
|
-
constructor(code, message, status = 400, details) {
|
|
34
|
-
super(message);
|
|
35
|
-
this.name = "ActionExecutionError";
|
|
36
|
-
this.code = code;
|
|
37
|
-
this.status = status;
|
|
38
|
-
this.details = details;
|
|
39
|
-
}
|
|
40
|
-
toJSON() {
|
|
41
|
-
return {
|
|
42
|
-
name: this.name,
|
|
43
|
-
code: this.code,
|
|
44
|
-
message: this.message,
|
|
45
|
-
status: this.status,
|
|
46
|
-
details: this.details
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
};
|
|
11
|
+
//#region \0rolldown/runtime.js
|
|
12
|
+
var __require = /* #__PURE__ */ (() => createRequire(import.meta.url))();
|
|
50
13
|
//#endregion
|
|
51
14
|
//#region src/typebox.ts
|
|
52
15
|
const compilerCache = /* @__PURE__ */ new WeakMap();
|
|
@@ -253,6 +216,17 @@ function defineAction(config) {
|
|
|
253
216
|
return actionFn;
|
|
254
217
|
}
|
|
255
218
|
//#endregion
|
|
219
|
+
//#region src/tool.ts
|
|
220
|
+
function defineTool(config) {
|
|
221
|
+
if (config.input && config.input.type !== "object") throw new Error(`Tool '${config.name}' input must be an object schema`);
|
|
222
|
+
const tool = defineAction(config);
|
|
223
|
+
Object.defineProperty(tool, "kind", { value: "tool" });
|
|
224
|
+
return tool;
|
|
225
|
+
}
|
|
226
|
+
function isTool(value) {
|
|
227
|
+
return typeof value === "function" && value.kind === "tool";
|
|
228
|
+
}
|
|
229
|
+
//#endregion
|
|
256
230
|
//#region src/db/load.ts
|
|
257
231
|
function loadDriver(id) {
|
|
258
232
|
return (globalThis.require ?? createRequire(import.meta.url))(id);
|
|
@@ -272,7 +246,7 @@ function createSQLiteDatabase(config = {}) {
|
|
|
272
246
|
let drizzle;
|
|
273
247
|
try {
|
|
274
248
|
({Database} = loadDriver("bun:sqlite"));
|
|
275
|
-
({drizzle} =
|
|
249
|
+
({drizzle} = __require("drizzle-orm/bun-sqlite"));
|
|
276
250
|
} catch (err) {
|
|
277
251
|
throw new Error(`[Brick-TS DB] SQLite engine requires the Bun runtime ('bun:sqlite' is unavailable here). Original error: ${err?.message ?? err}`);
|
|
278
252
|
}
|
|
@@ -294,7 +268,7 @@ function createSQLiteDatabase(config = {}) {
|
|
|
294
268
|
*/
|
|
295
269
|
function createPostgresDatabase(config = {}) {
|
|
296
270
|
const connectionUrl = config.url ?? config.connectionString ?? (typeof process !== "undefined" ? process.env?.DATABASE_URL : void 0);
|
|
297
|
-
const client = connectionUrl ? postgres(connectionUrl, {
|
|
271
|
+
const client = config.client ?? (connectionUrl ? postgres(connectionUrl, {
|
|
298
272
|
max: config.max,
|
|
299
273
|
prepare: config.prepare,
|
|
300
274
|
ssl: config.ssl
|
|
@@ -307,7 +281,7 @@ function createPostgresDatabase(config = {}) {
|
|
|
307
281
|
max: config.max,
|
|
308
282
|
prepare: config.prepare,
|
|
309
283
|
ssl: config.ssl
|
|
310
|
-
});
|
|
284
|
+
}));
|
|
311
285
|
return drizzle(client, {
|
|
312
286
|
schema: config.schema,
|
|
313
287
|
...config.schemaName ? { schemaFilter: [config.schemaName] } : {}
|
|
@@ -477,8 +451,7 @@ function defineDatabase(options = {}) {
|
|
|
477
451
|
"path",
|
|
478
452
|
"filename",
|
|
479
453
|
"wal",
|
|
480
|
-
"foreignKeys"
|
|
481
|
-
"client"
|
|
454
|
+
"foreignKeys"
|
|
482
455
|
];
|
|
483
456
|
for (const key of forbidden) if (key in options) throw new Error(`Database option "${key}" is not valid for ${engine}`);
|
|
484
457
|
const declared = options.tables ?? {};
|
|
@@ -501,6 +474,7 @@ function defineDatabase(options = {}) {
|
|
|
501
474
|
"name",
|
|
502
475
|
"engine"
|
|
503
476
|
].includes(key)) continue;
|
|
477
|
+
if (engine === "postgres" && options.client && key !== "client" && key !== "schemaName") continue;
|
|
504
478
|
config[key] = value instanceof SecretRef ? value.require() : value;
|
|
505
479
|
}
|
|
506
480
|
if (engine === "sqlite") {
|
|
@@ -544,7 +518,7 @@ function generateSQLiteTableDdl(table) {
|
|
|
544
518
|
const tableName = getTableName$1(table);
|
|
545
519
|
const columns = getTableColumns$1(table);
|
|
546
520
|
const colDefs = [];
|
|
547
|
-
for (const [
|
|
521
|
+
for (const [, col] of Object.entries(columns)) {
|
|
548
522
|
let def = `"${col.name}" ${col.getSQLType().toUpperCase()}`;
|
|
549
523
|
if (col.primary) def += " PRIMARY KEY";
|
|
550
524
|
if (col.notNull) def += " NOT NULL";
|
|
@@ -739,7 +713,7 @@ function getColumn(table, fieldName) {
|
|
|
739
713
|
if (table[fieldName]) return table[fieldName];
|
|
740
714
|
const cols = getTableColumns$1(table);
|
|
741
715
|
if (cols[fieldName]) return cols[fieldName];
|
|
742
|
-
for (const
|
|
716
|
+
for (const col of Object.values(cols)) if (col.name === fieldName) return col;
|
|
743
717
|
}
|
|
744
718
|
const RESERVED_LIST_KEYS = /* @__PURE__ */ new Set([
|
|
745
719
|
"limit",
|
|
@@ -1661,6 +1635,13 @@ function defineResource(service, config) {
|
|
|
1661
1635
|
service.action(deleteAction);
|
|
1662
1636
|
service.actions.set(`delete${capitalizedName}`, deleteAction);
|
|
1663
1637
|
}
|
|
1638
|
+
const actions = {
|
|
1639
|
+
list: listAction,
|
|
1640
|
+
get: getAction,
|
|
1641
|
+
create: createAction,
|
|
1642
|
+
update: updateAction,
|
|
1643
|
+
delete: deleteAction
|
|
1644
|
+
};
|
|
1664
1645
|
return {
|
|
1665
1646
|
name: resourceName,
|
|
1666
1647
|
serviceName: service.name,
|
|
@@ -1670,18 +1651,8 @@ function defineResource(service, config) {
|
|
|
1670
1651
|
pluralName,
|
|
1671
1652
|
config,
|
|
1672
1653
|
plan,
|
|
1673
|
-
actions
|
|
1674
|
-
|
|
1675
|
-
get: getAction,
|
|
1676
|
-
create: createAction,
|
|
1677
|
-
update: updateAction,
|
|
1678
|
-
delete: deleteAction
|
|
1679
|
-
},
|
|
1680
|
-
list: listAction,
|
|
1681
|
-
get: getAction,
|
|
1682
|
-
create: createAction,
|
|
1683
|
-
update: updateAction,
|
|
1684
|
-
delete: deleteAction
|
|
1654
|
+
actions,
|
|
1655
|
+
...actions
|
|
1685
1656
|
};
|
|
1686
1657
|
}
|
|
1687
1658
|
//#endregion
|
|
@@ -1700,6 +1671,7 @@ var ServiceImpl = class {
|
|
|
1700
1671
|
name;
|
|
1701
1672
|
options;
|
|
1702
1673
|
actions = /* @__PURE__ */ new Map();
|
|
1674
|
+
tools = /* @__PURE__ */ new Map();
|
|
1703
1675
|
tables = /* @__PURE__ */ new Map();
|
|
1704
1676
|
resources = /* @__PURE__ */ new Map();
|
|
1705
1677
|
database;
|
|
@@ -1739,12 +1711,12 @@ var ServiceImpl = class {
|
|
|
1739
1711
|
}
|
|
1740
1712
|
return this;
|
|
1741
1713
|
}
|
|
1742
|
-
resource(config) {
|
|
1714
|
+
resource = ((config) => {
|
|
1743
1715
|
const resourceInstance = defineResource(this, config);
|
|
1744
1716
|
this.resources.set(resourceInstance.name, resourceInstance);
|
|
1745
1717
|
getGlobalRegistry().touch();
|
|
1746
1718
|
return resourceInstance;
|
|
1747
|
-
}
|
|
1719
|
+
});
|
|
1748
1720
|
getResource(name) {
|
|
1749
1721
|
return this.resources.get(name);
|
|
1750
1722
|
}
|
|
@@ -1756,6 +1728,7 @@ var ServiceImpl = class {
|
|
|
1756
1728
|
const isActionFunction = typeof actionOrConfig === "function" && "config" in actionOrConfig;
|
|
1757
1729
|
if (isActionFunction) actionInstance = actionOrConfig;
|
|
1758
1730
|
else actionInstance = defineAction(actionOrConfig);
|
|
1731
|
+
if (isTool(actionInstance)) actionInstance.config.http = true;
|
|
1759
1732
|
actionInstance.serviceName = this.name;
|
|
1760
1733
|
actionInstance.service = this;
|
|
1761
1734
|
this.actions.set(actionInstance.name, actionInstance);
|
|
@@ -1763,6 +1736,23 @@ var ServiceImpl = class {
|
|
|
1763
1736
|
if (isActionFunction) return this;
|
|
1764
1737
|
return actionInstance;
|
|
1765
1738
|
}
|
|
1739
|
+
tool(toolOrConfig) {
|
|
1740
|
+
const existing = isTool(toolOrConfig);
|
|
1741
|
+
const tool = existing ? toolOrConfig : defineTool(toolOrConfig);
|
|
1742
|
+
if (this.tools.has(tool.name)) throw new Error(`Duplicate tool: ${tool.name}`);
|
|
1743
|
+
tool.serviceName = this.name;
|
|
1744
|
+
tool.service = this;
|
|
1745
|
+
this.tools.set(tool.name, tool);
|
|
1746
|
+
if (tool.config.http) this.action(tool);
|
|
1747
|
+
getGlobalRegistry().touch();
|
|
1748
|
+
return existing ? this : tool;
|
|
1749
|
+
}
|
|
1750
|
+
getTool(name) {
|
|
1751
|
+
return this.tools.get(name);
|
|
1752
|
+
}
|
|
1753
|
+
listTools() {
|
|
1754
|
+
return Array.from(this.tools.values());
|
|
1755
|
+
}
|
|
1766
1756
|
getAction(name) {
|
|
1767
1757
|
return this.actions.get(name);
|
|
1768
1758
|
}
|
|
@@ -1902,9 +1892,12 @@ function generateOpenApiSpec(options = {}) {
|
|
|
1902
1892
|
for (const service of services) {
|
|
1903
1893
|
tagsSet.add(service.name);
|
|
1904
1894
|
for (const action of service.listActions()) {
|
|
1905
|
-
const
|
|
1895
|
+
const routePath = action.config.path ?? `${prefix}/${service.name}/${action.name}`;
|
|
1896
|
+
const method = (action.config.method ?? "POST").toLowerCase();
|
|
1897
|
+
const pathParams = [...routePath.matchAll(/:([^/]+)/g)].map((match) => match[1]);
|
|
1898
|
+
const actionPath = routePath.replace(/:([^/]+)/g, "{$1}");
|
|
1906
1899
|
if (!paths[actionPath]) paths[actionPath] = {};
|
|
1907
|
-
|
|
1900
|
+
if (paths[actionPath][method]) continue;
|
|
1908
1901
|
const hasAuth = Boolean(action.config.authorize);
|
|
1909
1902
|
if (hasAuth) hasAnyAuthorizedAction = true;
|
|
1910
1903
|
const responses = {};
|
|
@@ -1964,44 +1957,41 @@ function generateOpenApiSpec(options = {}) {
|
|
|
1964
1957
|
};
|
|
1965
1958
|
}
|
|
1966
1959
|
responses["500"] = { description: "Internal server error" };
|
|
1967
|
-
const
|
|
1968
|
-
operationId: `${service.name}_${action.name}
|
|
1960
|
+
const operation = {
|
|
1961
|
+
operationId: `${service.name}_${action.name}_${method}`,
|
|
1969
1962
|
summary: `${service.name}.${action.name}`,
|
|
1970
1963
|
description: action.config.description ?? `Execute action '${action.name}' on service '${service.name}'`,
|
|
1971
1964
|
tags: action.config.tags && action.config.tags.length > 0 ? action.config.tags : [service.name],
|
|
1972
1965
|
responses
|
|
1973
1966
|
};
|
|
1974
|
-
|
|
1967
|
+
const inputSchema = action.config.input;
|
|
1968
|
+
const properties = inputSchema?.properties ?? {};
|
|
1969
|
+
const parameters = [...new Set(pathParams)].map((key) => ({
|
|
1970
|
+
name: key,
|
|
1971
|
+
in: "path",
|
|
1975
1972
|
required: true,
|
|
1976
|
-
|
|
1977
|
-
};
|
|
1978
|
-
if (
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
responses
|
|
1987
|
-
};
|
|
1988
|
-
if (action.config.input && typeof action.config.input === "object") {
|
|
1989
|
-
const inputSchema = action.config.input;
|
|
1990
|
-
if (inputSchema.properties) {
|
|
1991
|
-
const parameters = [];
|
|
1992
|
-
const requiredFields = inputSchema.required || [];
|
|
1993
|
-
for (const [key, propSchema] of Object.entries(inputSchema.properties)) parameters.push({
|
|
1994
|
-
name: key,
|
|
1995
|
-
in: "query",
|
|
1996
|
-
required: requiredFields.includes(key),
|
|
1997
|
-
schema: propSchema
|
|
1998
|
-
});
|
|
1999
|
-
if (parameters.length > 0) getOperation.parameters = parameters;
|
|
2000
|
-
}
|
|
2001
|
-
}
|
|
2002
|
-
if (hasAuth) getOperation.security = [{ bearerAuth: [] }];
|
|
2003
|
-
paths[actionPath].get = getOperation;
|
|
1973
|
+
schema: properties[key] ?? { type: "string" }
|
|
1974
|
+
}));
|
|
1975
|
+
if (method === "get" || method === "delete") for (const [key, schema] of Object.entries(properties)) {
|
|
1976
|
+
if (pathParams.includes(key)) continue;
|
|
1977
|
+
parameters.push({
|
|
1978
|
+
name: key,
|
|
1979
|
+
in: "query",
|
|
1980
|
+
required: inputSchema?.required?.includes(key) ?? false,
|
|
1981
|
+
schema
|
|
1982
|
+
});
|
|
2004
1983
|
}
|
|
1984
|
+
else if (inputSchema) operation.requestBody = {
|
|
1985
|
+
required: true,
|
|
1986
|
+
content: { "application/json": { schema: pathParams.length && inputSchema.properties ? {
|
|
1987
|
+
...inputSchema,
|
|
1988
|
+
properties: Object.fromEntries(Object.entries(properties).filter(([key]) => !pathParams.includes(key))),
|
|
1989
|
+
required: (inputSchema.required ?? []).filter((key) => !pathParams.includes(key))
|
|
1990
|
+
} : inputSchema } }
|
|
1991
|
+
};
|
|
1992
|
+
if (parameters.length) operation.parameters = parameters;
|
|
1993
|
+
if (hasAuth) operation.security = [{ bearerAuth: [] }];
|
|
1994
|
+
paths[actionPath][method] = operation;
|
|
2005
1995
|
}
|
|
2006
1996
|
}
|
|
2007
1997
|
const spec = {
|
|
@@ -2026,6 +2016,127 @@ function generateOpenApiSpec(options = {}) {
|
|
|
2026
2016
|
return spec;
|
|
2027
2017
|
}
|
|
2028
2018
|
//#endregion
|
|
2029
|
-
|
|
2019
|
+
//#region src/mcp.ts
|
|
2020
|
+
var UnknownToolError = class extends Error {
|
|
2021
|
+
constructor(name) {
|
|
2022
|
+
super(`unknown tool: ${name}`);
|
|
2023
|
+
this.name = "UnknownToolError";
|
|
2024
|
+
}
|
|
2025
|
+
};
|
|
2026
|
+
/** Collect only explicitly defined tools, without exposing ordinary actions. */
|
|
2027
|
+
function createMcpRegistry(options) {
|
|
2028
|
+
function toolsByName() {
|
|
2029
|
+
const tools = /* @__PURE__ */ new Map();
|
|
2030
|
+
for (const tool of [...options.tools ?? [], ...(options.services ?? []).flatMap((service) => service.listTools())]) {
|
|
2031
|
+
const existing = tools.get(tool.name);
|
|
2032
|
+
if (existing && existing !== tool) throw new Error(`Duplicate MCP tool: ${tool.name}`);
|
|
2033
|
+
tools.set(tool.name, tool);
|
|
2034
|
+
}
|
|
2035
|
+
return tools;
|
|
2036
|
+
}
|
|
2037
|
+
toolsByName();
|
|
2038
|
+
return {
|
|
2039
|
+
listTools: () => Array.from(toolsByName().values(), (tool) => ({
|
|
2040
|
+
name: tool.name,
|
|
2041
|
+
...tool.config.title ?? tool.config.annotations?.title ? { title: tool.config.title ?? tool.config.annotations?.title } : {},
|
|
2042
|
+
description: tool.config.description ?? "",
|
|
2043
|
+
inputSchema: tool.config.input ?? {
|
|
2044
|
+
type: "object",
|
|
2045
|
+
properties: {},
|
|
2046
|
+
additionalProperties: false
|
|
2047
|
+
},
|
|
2048
|
+
annotations: tool.config.annotations ?? {}
|
|
2049
|
+
})),
|
|
2050
|
+
async callTool(name, args = {}, ctx) {
|
|
2051
|
+
const tool = toolsByName().get(name);
|
|
2052
|
+
if (!tool) throw new UnknownToolError(name);
|
|
2053
|
+
return tool({
|
|
2054
|
+
input: args,
|
|
2055
|
+
ctx
|
|
2056
|
+
});
|
|
2057
|
+
}
|
|
2058
|
+
};
|
|
2059
|
+
}
|
|
2060
|
+
function result(id, value) {
|
|
2061
|
+
return Response.json({
|
|
2062
|
+
jsonrpc: "2.0",
|
|
2063
|
+
id,
|
|
2064
|
+
result: value
|
|
2065
|
+
});
|
|
2066
|
+
}
|
|
2067
|
+
function rpcError(id, code, message) {
|
|
2068
|
+
return Response.json({
|
|
2069
|
+
jsonrpc: "2.0",
|
|
2070
|
+
id,
|
|
2071
|
+
error: {
|
|
2072
|
+
code,
|
|
2073
|
+
message
|
|
2074
|
+
}
|
|
2075
|
+
});
|
|
2076
|
+
}
|
|
2077
|
+
function isRecord(value) {
|
|
2078
|
+
return !!value && typeof value === "object" && !Array.isArray(value);
|
|
2079
|
+
}
|
|
2080
|
+
/** Stateless MCP JSON-RPC POST handler. Wrap with the app's authentication. */
|
|
2081
|
+
function createMcpHandler(options) {
|
|
2082
|
+
const versions = options.protocolVersions ?? ["2025-06-18"];
|
|
2083
|
+
if (!versions.length) throw new Error("At least one MCP protocol version is required");
|
|
2084
|
+
return async (request, ctx) => {
|
|
2085
|
+
if (request.method !== "POST") return new Response(null, {
|
|
2086
|
+
status: 405,
|
|
2087
|
+
headers: { Allow: "POST" }
|
|
2088
|
+
});
|
|
2089
|
+
let body;
|
|
2090
|
+
try {
|
|
2091
|
+
body = await request.json();
|
|
2092
|
+
} catch {
|
|
2093
|
+
return rpcError(null, -32700, "parse error");
|
|
2094
|
+
}
|
|
2095
|
+
if (!isRecord(body) || body.jsonrpc !== "2.0" || typeof body.method !== "string" || body.id !== void 0 && body.id !== null && typeof body.id !== "string" && typeof body.id !== "number" || body.params !== void 0 && !isRecord(body.params)) return rpcError(null, -32600, "invalid request");
|
|
2096
|
+
if (body.id === void 0) return new Response(null, { status: 202 });
|
|
2097
|
+
const { id, method } = body;
|
|
2098
|
+
const params = body.params;
|
|
2099
|
+
switch (method) {
|
|
2100
|
+
case "initialize": {
|
|
2101
|
+
const requested = params?.protocolVersion;
|
|
2102
|
+
return result(id, {
|
|
2103
|
+
capabilities: { tools: {} },
|
|
2104
|
+
protocolVersion: typeof requested === "string" && versions.includes(requested) ? requested : versions[0],
|
|
2105
|
+
serverInfo: options.serverInfo
|
|
2106
|
+
});
|
|
2107
|
+
}
|
|
2108
|
+
case "ping": return result(id, {});
|
|
2109
|
+
case "tools/list": try {
|
|
2110
|
+
return result(id, { tools: options.registry.listTools() });
|
|
2111
|
+
} catch {
|
|
2112
|
+
return rpcError(id, -32603, "internal error");
|
|
2113
|
+
}
|
|
2114
|
+
case "tools/call":
|
|
2115
|
+
if (typeof params?.name !== "string" || params.arguments !== void 0 && !isRecord(params.arguments)) return rpcError(id, -32602, "invalid tool parameters");
|
|
2116
|
+
try {
|
|
2117
|
+
const value = await options.registry.callTool(params.name, params.arguments, {
|
|
2118
|
+
...ctx,
|
|
2119
|
+
request
|
|
2120
|
+
});
|
|
2121
|
+
return result(id, { content: [{
|
|
2122
|
+
type: "text",
|
|
2123
|
+
text: typeof value === "string" ? value : JSON.stringify(value, null, 2) ?? "null"
|
|
2124
|
+
}] });
|
|
2125
|
+
} catch (error) {
|
|
2126
|
+
if (error instanceof UnknownToolError) return rpcError(id, -32602, error.message);
|
|
2127
|
+
return result(id, {
|
|
2128
|
+
content: [{
|
|
2129
|
+
type: "text",
|
|
2130
|
+
text: error instanceof Error ? error.message : String(error)
|
|
2131
|
+
}],
|
|
2132
|
+
isError: true
|
|
2133
|
+
});
|
|
2134
|
+
}
|
|
2135
|
+
default: return rpcError(id, -32601, `method not found: ${method}`);
|
|
2136
|
+
}
|
|
2137
|
+
};
|
|
2138
|
+
}
|
|
2139
|
+
//#endregion
|
|
2140
|
+
export { ActionExecutionError, BrickServerError, BrickTransportError, SecretRef, ServiceImpl, ServiceRegistry, Table, Type, Type as t, UnknownToolError, ValidationError, and, asc, attachDatabase, blob, buildErrorHelpers, buildFtsMatchQuery, buildResourcePlan, createBrickClient, createDatabase, createMcpHandler, createMcpRegistry, createPostgresDatabase, createSQLiteDatabase, createServiceProxy, defineAction, defineDatabase, defineResource, defineService, defineTool, desc, describeResourceReads, ensureResourceIndexes, ensureSQLiteFts, envSource, eq, foreignKey, generateOpenApiSpec, generateSQLiteIndexDdl, generateSQLiteTableDdl, getCompiledCheck, getGlobalRegistry, getTableColumns, getTableName, gt, gte, ilike, inArray, index, integer, is, isBrickServerError, isBrickTransportError, isDatabaseHandle, isNotNull, isNull, isTool, like, listSecrets, lt, lte, ne, not, notInArray, or, overrideSecret, overrideSecrets, prepareResourceReads, prepareResourceWrites, primaryKey, real, relations, resetGlobalRegistry, resetSecrets, resolveSecrets, secret, sql, sqliteTable, syncSchema, text, uniqueIndex, validateWithSchema };
|
|
2030
2141
|
|
|
2031
2142
|
//# sourceMappingURL=index.js.map
|