mcp-tool-bridge 1.1.0
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/LICENSE +21 -0
- package/README.md +492 -0
- package/dist/chunk-XMYSMKGR.cjs +111 -0
- package/dist/chunk-YGJJEZXI.js +111 -0
- package/dist/index.cjs +1328 -0
- package/dist/index.d.cts +668 -0
- package/dist/index.d.ts +668 -0
- package/dist/index.js +1328 -0
- package/dist/types-tss7dg3j.d.cts +81 -0
- package/dist/types-tss7dg3j.d.ts +81 -0
- package/dist/zod.cjs +51 -0
- package/dist/zod.d.cts +18 -0
- package/dist/zod.d.ts +18 -0
- package/dist/zod.js +51 -0
- package/package.json +99 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var ToolDefinitionError = class extends Error {
|
|
3
|
+
name = "ToolDefinitionError";
|
|
4
|
+
code;
|
|
5
|
+
/** The offending tool, when its name is known. */
|
|
6
|
+
tool;
|
|
7
|
+
constructor(code, message, options) {
|
|
8
|
+
super(message, options?.cause === void 0 ? void 0 : { cause: options.cause });
|
|
9
|
+
this.code = code;
|
|
10
|
+
this.tool = options?.tool;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
var ToolError = class extends Error {
|
|
14
|
+
name = "ToolError";
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// src/json.ts
|
|
18
|
+
var NOT_JSON = /* @__PURE__ */ Symbol("not-json");
|
|
19
|
+
function cloneJson(value) {
|
|
20
|
+
const copy = cloneValue(value, /* @__PURE__ */ new Set());
|
|
21
|
+
return copy === NOT_JSON ? { ok: false } : { ok: true, value: copy };
|
|
22
|
+
}
|
|
23
|
+
function cloneValue(value, ancestors) {
|
|
24
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") return value;
|
|
25
|
+
if (typeof value === "number") return Number.isFinite(value) ? value : NOT_JSON;
|
|
26
|
+
if (typeof value !== "object") return NOT_JSON;
|
|
27
|
+
if (ancestors.has(value)) return NOT_JSON;
|
|
28
|
+
if (Array.isArray(value)) {
|
|
29
|
+
ancestors.add(value);
|
|
30
|
+
const items = [];
|
|
31
|
+
for (const item of value) {
|
|
32
|
+
const copy = cloneValue(item, ancestors);
|
|
33
|
+
if (copy === NOT_JSON) return NOT_JSON;
|
|
34
|
+
items.push(copy);
|
|
35
|
+
}
|
|
36
|
+
ancestors.delete(value);
|
|
37
|
+
return items;
|
|
38
|
+
}
|
|
39
|
+
const proto = Object.getPrototypeOf(value);
|
|
40
|
+
if (proto !== Object.prototype && proto !== null) return NOT_JSON;
|
|
41
|
+
ancestors.add(value);
|
|
42
|
+
const out = {};
|
|
43
|
+
for (const [key, item] of Object.entries(value)) {
|
|
44
|
+
if (item === void 0) continue;
|
|
45
|
+
const copy = cloneValue(item, ancestors);
|
|
46
|
+
if (copy === NOT_JSON) return NOT_JSON;
|
|
47
|
+
out[key] = copy;
|
|
48
|
+
}
|
|
49
|
+
ancestors.delete(value);
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
function isJsonArray(value) {
|
|
53
|
+
return Array.isArray(value);
|
|
54
|
+
}
|
|
55
|
+
function isJsonObject(value) {
|
|
56
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
57
|
+
}
|
|
58
|
+
function deepFreeze(value) {
|
|
59
|
+
if (typeof value === "object" && value !== null) {
|
|
60
|
+
for (const item of Object.values(value)) deepFreeze(item);
|
|
61
|
+
Object.freeze(value);
|
|
62
|
+
}
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
function pointerSegment(segment) {
|
|
66
|
+
return segment.replaceAll("~", "~0").replaceAll("/", "~1");
|
|
67
|
+
}
|
|
68
|
+
function readProperty(source, key) {
|
|
69
|
+
return Reflect.get(source, key);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/schema/types.ts
|
|
73
|
+
var MAX_ISSUES = 20;
|
|
74
|
+
var NOT_JSON_ISSUE = Object.freeze({
|
|
75
|
+
path: "",
|
|
76
|
+
message: "must be plain JSON data"
|
|
77
|
+
});
|
|
78
|
+
function freezeRootSchema(schema, tool) {
|
|
79
|
+
const copy = cloneJson(schema);
|
|
80
|
+
if (!copy.ok || !isJsonObject(copy.value)) {
|
|
81
|
+
throw new ToolDefinitionError("invalid_schema", "an argument schema must be plain JSON data", {
|
|
82
|
+
tool
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
const root = copy.value;
|
|
86
|
+
if (!isRootObjectSchema(root)) {
|
|
87
|
+
throw new ToolDefinitionError(
|
|
88
|
+
"invalid_schema",
|
|
89
|
+
'MCP requires the root of an argument schema to be `type: "object"`',
|
|
90
|
+
{ tool }
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
return deepFreeze(root);
|
|
94
|
+
}
|
|
95
|
+
function isRootObjectSchema(schema) {
|
|
96
|
+
return schema.type === "object";
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export {
|
|
100
|
+
ToolDefinitionError,
|
|
101
|
+
ToolError,
|
|
102
|
+
cloneJson,
|
|
103
|
+
isJsonArray,
|
|
104
|
+
isJsonObject,
|
|
105
|
+
deepFreeze,
|
|
106
|
+
pointerSegment,
|
|
107
|
+
readProperty,
|
|
108
|
+
MAX_ISSUES,
|
|
109
|
+
NOT_JSON_ISSUE,
|
|
110
|
+
freezeRootSchema
|
|
111
|
+
};
|