@tokenlabai/mcp-server 0.6.14 → 0.6.17

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.
@@ -0,0 +1,217 @@
1
+ const DRAFT_07_SCHEMA = "http://json-schema.org/draft-07/schema#";
2
+ const PORTABLE_PROPERTY_DEPTH = 3;
3
+ const MAX_DESCRIPTION_LENGTH = 500;
4
+ const MAX_STRICT_DESCRIPTION_LENGTH = 160;
5
+
6
+ export const TOOL_SCHEMA_MODES = ["exact", "portable", "strict"];
7
+
8
+ function compactDescription(value, maxLength = MAX_DESCRIPTION_LENGTH) {
9
+ if (typeof value !== "string") return undefined;
10
+ return value.length <= maxLength
11
+ ? value
12
+ : `${value.slice(0, maxLength - 1)}…`;
13
+ }
14
+
15
+ function portableScalarSchema(schema) {
16
+ const projected = {};
17
+ for (const key of [
18
+ "type",
19
+ "format",
20
+ "enum",
21
+ "const",
22
+ "minimum",
23
+ "maximum",
24
+ "exclusiveMinimum",
25
+ "exclusiveMaximum",
26
+ "multipleOf",
27
+ "minLength",
28
+ "maxLength",
29
+ "pattern"
30
+ ]) {
31
+ if (Object.hasOwn(schema, key)) projected[key] = structuredClone(schema[key]);
32
+ }
33
+ const description = compactDescription(schema.description);
34
+ if (description) projected.description = description;
35
+ return Object.keys(projected).length > 0 ? projected : {};
36
+ }
37
+
38
+ function portableSchemaNode(schema, propertyDepth) {
39
+ if (Array.isArray(schema)) {
40
+ return schema.map((entry) => portableSchemaNode(entry, propertyDepth));
41
+ }
42
+ if (!schema || typeof schema !== "object") return schema;
43
+
44
+ const description = compactDescription(schema.description);
45
+ if (propertyDepth >= PORTABLE_PROPERTY_DEPTH) {
46
+ const types = Array.isArray(schema.type)
47
+ ? schema.type
48
+ : schema.type ? [schema.type] : [];
49
+ if (types.includes("object") || schema.properties || schema.additionalProperties) {
50
+ return {
51
+ type: "object",
52
+ ...(description ? { description } : {})
53
+ };
54
+ }
55
+ if (types.includes("array") || schema.items) {
56
+ return {
57
+ type: "array",
58
+ items: {},
59
+ ...(description ? { description } : {})
60
+ };
61
+ }
62
+ return portableScalarSchema(schema);
63
+ }
64
+
65
+ const projected = {};
66
+ for (const [key, value] of Object.entries(schema)) {
67
+ if (key === "$schema") {
68
+ continue;
69
+ }
70
+ if (key === "description") {
71
+ if (description) projected.description = description;
72
+ continue;
73
+ }
74
+ if (key === "properties") {
75
+ projected.properties = Object.fromEntries(
76
+ Object.entries(value).map(([name, property]) => [
77
+ name,
78
+ portableSchemaNode(property, propertyDepth + 1)
79
+ ])
80
+ );
81
+ continue;
82
+ }
83
+ if (key === "items") {
84
+ projected.items = portableSchemaNode(value, propertyDepth + 1);
85
+ continue;
86
+ }
87
+ if (["oneOf", "anyOf", "allOf"].includes(key)) {
88
+ projected[key] = value.map((entry) => portableSchemaNode(entry, propertyDepth + 1));
89
+ continue;
90
+ }
91
+ projected[key] = structuredClone(value);
92
+ }
93
+ return projected;
94
+ }
95
+
96
+ function isSimpleScalarSchema(schema) {
97
+ return Boolean(
98
+ schema
99
+ && typeof schema === "object"
100
+ && !Array.isArray(schema)
101
+ && ["string", "number", "integer", "boolean"].includes(schema.type)
102
+ && !schema.oneOf
103
+ && !schema.anyOf
104
+ && !schema.allOf
105
+ );
106
+ }
107
+
108
+ function strictScalarSchema(schema) {
109
+ const projected = portableScalarSchema(schema);
110
+ const description = compactDescription(schema.description, MAX_STRICT_DESCRIPTION_LENGTH);
111
+ if (description) projected.description = description;
112
+ else delete projected.description;
113
+ delete projected.default;
114
+ return projected;
115
+ }
116
+
117
+ function strictPropertySchema(schema, optional) {
118
+ const description = compactDescription(schema?.description, MAX_STRICT_DESCRIPTION_LENGTH);
119
+ const valueSchema = isSimpleScalarSchema(schema)
120
+ ? strictScalarSchema(schema)
121
+ : {
122
+ type: "string",
123
+ description: [
124
+ "JSON-encoded value.",
125
+ description
126
+ ].filter(Boolean).join(" ")
127
+ };
128
+ if (!optional) return valueSchema;
129
+ return {
130
+ anyOf: [
131
+ valueSchema,
132
+ { type: "null" }
133
+ ]
134
+ };
135
+ }
136
+
137
+ function strictToolSchema(schema) {
138
+ const properties = schema?.properties || {};
139
+ const originallyRequired = new Set(schema?.required || []);
140
+ return {
141
+ type: "object",
142
+ properties: Object.fromEntries(
143
+ Object.entries(properties).map(([name, property]) => [
144
+ name,
145
+ strictPropertySchema(property, !originallyRequired.has(name))
146
+ ])
147
+ ),
148
+ required: Object.keys(properties),
149
+ additionalProperties: false
150
+ };
151
+ }
152
+
153
+ export function withDraft07(schema) {
154
+ return {
155
+ $schema: DRAFT_07_SCHEMA,
156
+ ...schema
157
+ };
158
+ }
159
+
160
+ export function projectToolInputSchema(schema, mode) {
161
+ if (!TOOL_SCHEMA_MODES.includes(mode)) {
162
+ throw new Error(`Unknown MCP tool schema mode '${mode}'. Expected ${TOOL_SCHEMA_MODES.join(" or ")}.`);
163
+ }
164
+ if (mode === "exact") return structuredClone(schema);
165
+ if (mode === "strict") return strictToolSchema(schema);
166
+ return portableSchemaNode(schema, 0);
167
+ }
168
+
169
+ export function normalizeToolInputForSchemaMode(input, schema, mode) {
170
+ if (mode !== "strict") return { ...input };
171
+ const normalized = { ...input };
172
+ const required = new Set(schema.required || []);
173
+ for (const [name, property] of Object.entries(schema.properties || {})) {
174
+ if (!Object.hasOwn(normalized, name)) continue;
175
+ if (normalized[name] === null && !required.has(name)) {
176
+ delete normalized[name];
177
+ continue;
178
+ }
179
+ if (isSimpleScalarSchema(property) || typeof normalized[name] !== "string") continue;
180
+ try {
181
+ normalized[name] = JSON.parse(normalized[name]);
182
+ } catch {
183
+ // A canonical union may accept a plain string as-is.
184
+ }
185
+ }
186
+ return normalized;
187
+ }
188
+
189
+ export function schemaDepth(value, depth = 0) {
190
+ if (!value || typeof value !== "object") return depth;
191
+ return Object.values(value).reduce(
192
+ (maximum, entry) => Math.max(maximum, schemaDepth(entry, depth + 1)),
193
+ depth
194
+ );
195
+ }
196
+
197
+ export function schemaBytes(schema) {
198
+ return Buffer.byteLength(JSON.stringify(schema));
199
+ }
200
+
201
+ export function findProviderIncompatibleLiterals(schema, path = "$", findings = []) {
202
+ if (!schema || typeof schema !== "object") return findings;
203
+ if (Object.hasOwn(schema, "const") && typeof schema.const !== "string") {
204
+ findings.push({ path, keyword: "const", value: schema.const });
205
+ }
206
+ if (Array.isArray(schema.enum) && schema.enum.some((entry) => typeof entry !== "string")) {
207
+ findings.push({ path, keyword: "enum", value: schema.enum });
208
+ }
209
+ for (const [key, value] of Object.entries(schema)) {
210
+ if (Array.isArray(value)) {
211
+ value.forEach((entry, index) => findProviderIncompatibleLiterals(entry, `${path}.${key}[${index}]`, findings));
212
+ } else {
213
+ findProviderIncompatibleLiterals(value, `${path}.${key}`, findings);
214
+ }
215
+ }
216
+ return findings;
217
+ }