@prunellejs/mcp 0.0.0-stage → 0.6.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/llms.txt +52 -0
- package/package.json +24 -4
- package/src/footprint.ts +129 -0
- package/src/format.ts +259 -0
- package/src/index.ts +589 -0
- package/src/rpc.ts +327 -0
- package/src/schema.ts +406 -0
- package/README.md +0 -3
package/src/schema.ts
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
// JSON Schema extraction and compaction for MCP tools/list: strips boilerplate, form-coercion unions and no-op bounds.
|
|
2
|
+
|
|
3
|
+
import type { StandardSchemaV1 } from 'prunelle/server'
|
|
4
|
+
|
|
5
|
+
const DEFAULT_FOLD_HOMOGENEOUS_MIN = 4
|
|
6
|
+
const SAFE_INT_MIN = Number.MIN_SAFE_INTEGER
|
|
7
|
+
const SAFE_INT_MAX = Number.MAX_SAFE_INTEGER
|
|
8
|
+
const BOOLEAN_ENUM_VALUES = new Set(['true', 'false', '0', '1', 'yes', 'no', 'on', 'off'])
|
|
9
|
+
|
|
10
|
+
export interface JsonSchemaObject {
|
|
11
|
+
type?: string | undefined
|
|
12
|
+
properties?: Record<string, unknown> | undefined
|
|
13
|
+
required?: string[] | undefined
|
|
14
|
+
additionalProperties?: boolean | Record<string, unknown> | undefined
|
|
15
|
+
[key: string]: unknown
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface CompactSchemaOptions {
|
|
19
|
+
/** Collapse form-coercion string branches in `anyOf`/`oneOf` for boolean/number/integer fields (default: true). */
|
|
20
|
+
stripFormCoercion?: boolean | undefined
|
|
21
|
+
/** Strip `x-*` vendor extension keys (default: true). */
|
|
22
|
+
stripExtensions?: boolean | undefined
|
|
23
|
+
/** Strip no-op bounds (`minLength: 0`, `minItems: 0`, `minProperties: 0`, `±MAX_SAFE_INTEGER`) and optional app-wide default caps (default: true). */
|
|
24
|
+
stripDefaultBounds?: boolean | { maxLength?: number | undefined, maxItems?: number | undefined } | undefined
|
|
25
|
+
/** Fold optional-only objects with >= N identical-schema properties into `additionalProperties` + a compact `Keys: ...` description (default: 4; `false` disables). */
|
|
26
|
+
foldHomogeneousObjects?: number | false | undefined
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface StandardJsonSchemaCapable {
|
|
30
|
+
'~standard'?: {
|
|
31
|
+
jsonSchema?: {
|
|
32
|
+
input?: (opts: { target: string }) => Record<string, unknown>
|
|
33
|
+
} | undefined
|
|
34
|
+
} | undefined
|
|
35
|
+
toJSONSchema?: (() => Record<string, unknown>) | undefined
|
|
36
|
+
jsonSchema?: Record<string, unknown> | undefined
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface ResolvedCompactConfig {
|
|
40
|
+
enabled: boolean
|
|
41
|
+
stripFormCoercion: boolean
|
|
42
|
+
stripExtensions: boolean
|
|
43
|
+
stripDefaultBounds: boolean
|
|
44
|
+
defaultMaxLength: number | undefined
|
|
45
|
+
defaultMaxItems: number | undefined
|
|
46
|
+
foldHomogeneousMin: number | false
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const DEFAULT_INPUT_SCHEMA: JsonSchemaObject = {
|
|
50
|
+
type: 'object',
|
|
51
|
+
properties: {},
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const STRIPPED_SCHEMA_KEYS = new Set(['$schema', '$id', '$comment'])
|
|
55
|
+
|
|
56
|
+
function resolveCompactConfig(opt: boolean | CompactSchemaOptions | undefined): ResolvedCompactConfig {
|
|
57
|
+
if (opt === false) {
|
|
58
|
+
return {
|
|
59
|
+
enabled: false,
|
|
60
|
+
stripFormCoercion: false,
|
|
61
|
+
stripExtensions: false,
|
|
62
|
+
stripDefaultBounds: false,
|
|
63
|
+
defaultMaxLength: undefined,
|
|
64
|
+
defaultMaxItems: undefined,
|
|
65
|
+
foldHomogeneousMin: false,
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const cfg = opt === true || opt === undefined ? {} : opt
|
|
69
|
+
const bounds = cfg.stripDefaultBounds
|
|
70
|
+
return {
|
|
71
|
+
enabled: true,
|
|
72
|
+
stripFormCoercion: cfg.stripFormCoercion ?? true,
|
|
73
|
+
stripExtensions: cfg.stripExtensions ?? true,
|
|
74
|
+
stripDefaultBounds: bounds !== false,
|
|
75
|
+
defaultMaxLength: typeof bounds === 'object' ? bounds.maxLength : undefined,
|
|
76
|
+
defaultMaxItems: typeof bounds === 'object' ? bounds.maxItems : undefined,
|
|
77
|
+
foldHomogeneousMin: cfg.foldHomogeneousObjects !== undefined ? cfg.foldHomogeneousObjects : DEFAULT_FOLD_HOMOGENEOUS_MIN,
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function isStandardSchema(value: unknown): value is StandardSchemaV1 {
|
|
82
|
+
return typeof value === 'object'
|
|
83
|
+
&& value !== null
|
|
84
|
+
&& '~standard' in value
|
|
85
|
+
&& typeof (value as { '~standard'?: { validate?: unknown } })['~standard']?.validate === 'function'
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function isPlainRecord(value: unknown): value is Record<string, unknown> {
|
|
89
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value))
|
|
90
|
+
return false
|
|
91
|
+
const proto = Object.getPrototypeOf(value)
|
|
92
|
+
return proto === Object.prototype || proto === null
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function isBooleanCoercionBranch(branch: unknown): boolean {
|
|
96
|
+
if (!isPlainRecord(branch) || branch.type !== 'string')
|
|
97
|
+
return false
|
|
98
|
+
if (branch.format === 'boolean')
|
|
99
|
+
return true
|
|
100
|
+
if (Array.isArray(branch.enum) && branch.enum.length > 0 && branch.enum.every(v => typeof v === 'string' && BOOLEAN_ENUM_VALUES.has(v.toLowerCase())))
|
|
101
|
+
return true
|
|
102
|
+
return false
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function isNumericCoercionBranch(branch: unknown): boolean {
|
|
106
|
+
if (!isPlainRecord(branch) || branch.type !== 'string')
|
|
107
|
+
return false
|
|
108
|
+
return branch.format === 'numeric' || branch.format === 'number' || branch.format === 'integer'
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function collapseCoercionVariants(variants: unknown[]): { variants: unknown[], liftedDefault?: unknown, liftedDescription?: string | undefined } {
|
|
112
|
+
let hasBoolean = false
|
|
113
|
+
let hasNumber = false
|
|
114
|
+
for (const v of variants) {
|
|
115
|
+
if (!isPlainRecord(v))
|
|
116
|
+
continue
|
|
117
|
+
if (v.type === 'boolean')
|
|
118
|
+
hasBoolean = true
|
|
119
|
+
if (v.type === 'number' || v.type === 'integer')
|
|
120
|
+
hasNumber = true
|
|
121
|
+
}
|
|
122
|
+
if (!hasBoolean && !hasNumber)
|
|
123
|
+
return { variants }
|
|
124
|
+
|
|
125
|
+
let liftedDefault: unknown
|
|
126
|
+
let liftedDescription: string | undefined
|
|
127
|
+
const kept: unknown[] = []
|
|
128
|
+
|
|
129
|
+
for (const v of variants) {
|
|
130
|
+
if (hasBoolean && isBooleanCoercionBranch(v)) {
|
|
131
|
+
const rec = v as Record<string, unknown>
|
|
132
|
+
if (liftedDefault === undefined && typeof rec.default === 'boolean')
|
|
133
|
+
liftedDefault = rec.default
|
|
134
|
+
if (liftedDescription === undefined && typeof rec.description === 'string' && rec.description.trim().length > 0)
|
|
135
|
+
liftedDescription = rec.description
|
|
136
|
+
continue
|
|
137
|
+
}
|
|
138
|
+
if (hasNumber && isNumericCoercionBranch(v)) {
|
|
139
|
+
const rec = v as Record<string, unknown>
|
|
140
|
+
if (liftedDefault === undefined && typeof rec.default === 'number')
|
|
141
|
+
liftedDefault = rec.default
|
|
142
|
+
if (liftedDescription === undefined && typeof rec.description === 'string' && rec.description.trim().length > 0)
|
|
143
|
+
liftedDescription = rec.description
|
|
144
|
+
continue
|
|
145
|
+
}
|
|
146
|
+
kept.push(v)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return { variants: kept, liftedDefault, liftedDescription }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function tryFoldHomogeneousObject(obj: Record<string, unknown>, minKeys: number): void {
|
|
153
|
+
const props = obj.properties
|
|
154
|
+
if (!isPlainRecord(props))
|
|
155
|
+
return
|
|
156
|
+
if (Array.isArray(obj.required) && obj.required.length > 0)
|
|
157
|
+
return
|
|
158
|
+
if (obj.additionalProperties !== undefined && obj.additionalProperties !== false)
|
|
159
|
+
return
|
|
160
|
+
|
|
161
|
+
const entries = Object.entries(props)
|
|
162
|
+
if (entries.length < minKeys)
|
|
163
|
+
return
|
|
164
|
+
|
|
165
|
+
let signature: string | undefined
|
|
166
|
+
let sharedSchema: Record<string, unknown> | undefined
|
|
167
|
+
|
|
168
|
+
for (const [, val] of entries) {
|
|
169
|
+
if (!isPlainRecord(val))
|
|
170
|
+
return
|
|
171
|
+
if (typeof val.description === 'string' && val.description.length > 0)
|
|
172
|
+
return
|
|
173
|
+
const serialized = JSON.stringify(val)
|
|
174
|
+
if (signature === undefined) {
|
|
175
|
+
signature = serialized
|
|
176
|
+
sharedSchema = val
|
|
177
|
+
}
|
|
178
|
+
else if (serialized !== signature) {
|
|
179
|
+
return
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (sharedSchema === undefined)
|
|
184
|
+
return
|
|
185
|
+
|
|
186
|
+
const keysHint = `Keys: ${entries.map(([k]) => k).join(', ')}`
|
|
187
|
+
const existingDesc = typeof obj.description === 'string' && obj.description.trim().length > 0
|
|
188
|
+
? obj.description.trim()
|
|
189
|
+
: undefined
|
|
190
|
+
obj.description = existingDesc ? `${existingDesc} (${keysHint})` : keysHint
|
|
191
|
+
obj.additionalProperties = sharedSchema
|
|
192
|
+
delete obj.properties
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function collectSchemaRefs(node: unknown, out: Set<string>): void {
|
|
196
|
+
if (Array.isArray(node)) {
|
|
197
|
+
for (const item of node)
|
|
198
|
+
collectSchemaRefs(item, out)
|
|
199
|
+
return
|
|
200
|
+
}
|
|
201
|
+
if (node === null || typeof node !== 'object')
|
|
202
|
+
return
|
|
203
|
+
for (const [k, v] of Object.entries(node as Record<string, unknown>)) {
|
|
204
|
+
if (k === '$ref' && typeof v === 'string')
|
|
205
|
+
out.add(v)
|
|
206
|
+
else
|
|
207
|
+
collectSchemaRefs(v, out)
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function pruneUnusedDefs(root: JsonSchemaObject): void {
|
|
212
|
+
for (const defKey of ['$defs', 'definitions'] as const) {
|
|
213
|
+
const defs = root[defKey]
|
|
214
|
+
if (defs === null || typeof defs !== 'object' || Array.isArray(defs))
|
|
215
|
+
continue
|
|
216
|
+
|
|
217
|
+
const defsRecord = defs as Record<string, unknown>
|
|
218
|
+
const prefix = `#/${defKey}/`
|
|
219
|
+
const referenced = new Set<string>()
|
|
220
|
+
const queue: unknown[] = []
|
|
221
|
+
|
|
222
|
+
for (const [k, v] of Object.entries(root)) {
|
|
223
|
+
if (k !== defKey)
|
|
224
|
+
queue.push(v)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const visitedDefs = new Set<string>()
|
|
228
|
+
while (queue.length > 0) {
|
|
229
|
+
const next = queue.pop()
|
|
230
|
+
const found = new Set<string>()
|
|
231
|
+
collectSchemaRefs(next, found)
|
|
232
|
+
for (const ref of found) {
|
|
233
|
+
if (!ref.startsWith(prefix))
|
|
234
|
+
continue
|
|
235
|
+
const rawSegment = ref.slice(prefix.length).split('/')[0] ?? ''
|
|
236
|
+
const name = rawSegment.replace(/~1/g, '/').replace(/~0/g, '~')
|
|
237
|
+
if (!visitedDefs.has(name) && Object.prototype.hasOwnProperty.call(defsRecord, name)) {
|
|
238
|
+
visitedDefs.add(name)
|
|
239
|
+
referenced.add(name)
|
|
240
|
+
queue.push(defsRecord[name])
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const kept: Record<string, unknown> = {}
|
|
246
|
+
for (const [name, def] of Object.entries(defsRecord)) {
|
|
247
|
+
if (referenced.has(name))
|
|
248
|
+
kept[name] = def
|
|
249
|
+
}
|
|
250
|
+
if (Object.keys(kept).length === 0)
|
|
251
|
+
delete root[defKey]
|
|
252
|
+
else
|
|
253
|
+
root[defKey] = kept
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function compactSchemaNode(node: unknown, cfg: ResolvedCompactConfig, propName?: string, isRoot = false): unknown {
|
|
258
|
+
if (Array.isArray(node))
|
|
259
|
+
return node.map(item => compactSchemaNode(item, cfg))
|
|
260
|
+
if (node === null || typeof node !== 'object')
|
|
261
|
+
return node
|
|
262
|
+
|
|
263
|
+
const src = node as Record<string, unknown>
|
|
264
|
+
const out: Record<string, unknown> = {}
|
|
265
|
+
|
|
266
|
+
for (const [key, value] of Object.entries(src)) {
|
|
267
|
+
if (value === undefined || STRIPPED_SCHEMA_KEYS.has(key))
|
|
268
|
+
continue
|
|
269
|
+
if (cfg.stripExtensions && key.startsWith('x-'))
|
|
270
|
+
continue
|
|
271
|
+
if (key === 'additionalProperties' && value === true)
|
|
272
|
+
continue
|
|
273
|
+
if (key === 'required' && Array.isArray(value) && value.length === 0)
|
|
274
|
+
continue
|
|
275
|
+
if (key === 'description' && typeof value === 'string' && value.trim().length === 0)
|
|
276
|
+
continue
|
|
277
|
+
if (key === 'title' && typeof value === 'string') {
|
|
278
|
+
const trimmed = value.trim()
|
|
279
|
+
if (trimmed.length === 0 || (propName !== undefined && trimmed.toLowerCase() === propName.toLowerCase()))
|
|
280
|
+
continue
|
|
281
|
+
}
|
|
282
|
+
if (cfg.stripDefaultBounds) {
|
|
283
|
+
if ((key === 'minLength' || key === 'minItems' || key === 'minProperties') && value === 0)
|
|
284
|
+
continue
|
|
285
|
+
if (key === 'minimum' && value === SAFE_INT_MIN)
|
|
286
|
+
continue
|
|
287
|
+
if (key === 'maximum' && value === SAFE_INT_MAX)
|
|
288
|
+
continue
|
|
289
|
+
if (key === 'maxLength' && cfg.defaultMaxLength !== undefined && value === cfg.defaultMaxLength)
|
|
290
|
+
continue
|
|
291
|
+
if (key === 'maxItems' && cfg.defaultMaxItems !== undefined && value === cfg.defaultMaxItems)
|
|
292
|
+
continue
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (key === 'properties' && value !== null && typeof value === 'object' && !Array.isArray(value)) {
|
|
296
|
+
const propsOut: Record<string, unknown> = {}
|
|
297
|
+
for (const [pKey, pVal] of Object.entries(value as Record<string, unknown>))
|
|
298
|
+
propsOut[pKey] = compactSchemaNode(pVal, cfg, pKey, false)
|
|
299
|
+
out[key] = propsOut
|
|
300
|
+
continue
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
out[key] = compactSchemaNode(value, cfg)
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (cfg.stripFormCoercion) {
|
|
307
|
+
for (const unionKey of ['anyOf', 'oneOf'] as const) {
|
|
308
|
+
const rawVariants = out[unionKey]
|
|
309
|
+
if (!Array.isArray(rawVariants) || rawVariants.length < 2)
|
|
310
|
+
continue
|
|
311
|
+
const { variants, liftedDefault, liftedDescription } = collapseCoercionVariants(rawVariants)
|
|
312
|
+
if (variants.length === rawVariants.length)
|
|
313
|
+
continue
|
|
314
|
+
if (variants.length === 1 && isPlainRecord(variants[0])) {
|
|
315
|
+
delete out[unionKey]
|
|
316
|
+
const single = variants[0]
|
|
317
|
+
for (const [k, v] of Object.entries(single)) {
|
|
318
|
+
if (out[k] === undefined)
|
|
319
|
+
out[k] = v
|
|
320
|
+
}
|
|
321
|
+
if (liftedDefault !== undefined && out.default === undefined)
|
|
322
|
+
out.default = liftedDefault
|
|
323
|
+
if (liftedDescription !== undefined && out.description === undefined)
|
|
324
|
+
out.description = liftedDescription
|
|
325
|
+
}
|
|
326
|
+
else if (variants.length > 0) {
|
|
327
|
+
out[unionKey] = variants
|
|
328
|
+
if (liftedDefault !== undefined && out.default === undefined)
|
|
329
|
+
out.default = liftedDefault
|
|
330
|
+
if (liftedDescription !== undefined && out.description === undefined)
|
|
331
|
+
out.description = liftedDescription
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (!isRoot && cfg.foldHomogeneousMin !== false && cfg.foldHomogeneousMin > 0)
|
|
337
|
+
tryFoldHomogeneousObject(out, cfg.foldHomogeneousMin)
|
|
338
|
+
|
|
339
|
+
return out
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** Strips JSON Schema boilerplate, form-coercion unions, `x-*` extensions, no-op bounds, and homogeneous records. */
|
|
343
|
+
export function compactSchema(raw: Record<string, unknown>, options?: CompactSchemaOptions): JsonSchemaObject {
|
|
344
|
+
const cfg = resolveCompactConfig(options ?? true)
|
|
345
|
+
const compacted = compactSchemaNode(raw, cfg, undefined, true) as JsonSchemaObject
|
|
346
|
+
pruneUnusedDefs(compacted)
|
|
347
|
+
if (compacted.type === undefined)
|
|
348
|
+
compacted.type = 'object'
|
|
349
|
+
if (compacted.type === 'object' && compacted.properties === undefined && compacted.additionalProperties === undefined)
|
|
350
|
+
compacted.properties = {}
|
|
351
|
+
return compacted
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
export function normalizeObjectSchema(raw: Record<string, unknown> | undefined, compact: boolean | CompactSchemaOptions): JsonSchemaObject {
|
|
355
|
+
if (!raw || typeof raw !== 'object')
|
|
356
|
+
return { ...DEFAULT_INPUT_SCHEMA }
|
|
357
|
+
if (compact !== false)
|
|
358
|
+
return compactSchema(raw, compact === true ? undefined : compact)
|
|
359
|
+
const copy: JsonSchemaObject = { ...raw }
|
|
360
|
+
if (copy.type === undefined)
|
|
361
|
+
copy.type = 'object'
|
|
362
|
+
if (copy.type === 'object' && copy.properties === undefined)
|
|
363
|
+
copy.properties = {}
|
|
364
|
+
return copy
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** Extracts an MCP-compatible JSON Schema object from a Standard Schema (Zod 4, Valibot, ArkType, etc.). */
|
|
368
|
+
export function schemaToJsonSchema(
|
|
369
|
+
schema: StandardSchemaV1 | undefined,
|
|
370
|
+
options?: { compact?: boolean | CompactSchemaOptions | undefined },
|
|
371
|
+
): JsonSchemaObject {
|
|
372
|
+
const compact = options?.compact ?? true
|
|
373
|
+
if (schema === undefined)
|
|
374
|
+
return { ...DEFAULT_INPUT_SCHEMA }
|
|
375
|
+
|
|
376
|
+
const candidate = schema as unknown as StandardJsonSchemaCapable
|
|
377
|
+
const stdJson = candidate['~standard']?.jsonSchema
|
|
378
|
+
if (stdJson && typeof stdJson.input === 'function') {
|
|
379
|
+
for (const target of ['draft-2020-12', 'draft-07']) {
|
|
380
|
+
try {
|
|
381
|
+
const out = stdJson.input({ target })
|
|
382
|
+
if (out && typeof out === 'object')
|
|
383
|
+
return normalizeObjectSchema(out, compact)
|
|
384
|
+
}
|
|
385
|
+
catch {
|
|
386
|
+
// Try next JSON Schema draft target
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
if (typeof candidate.toJSONSchema === 'function') {
|
|
392
|
+
try {
|
|
393
|
+
const out = candidate.toJSONSchema()
|
|
394
|
+
if (out && typeof out === 'object')
|
|
395
|
+
return normalizeObjectSchema(out, compact)
|
|
396
|
+
}
|
|
397
|
+
catch {
|
|
398
|
+
// Fall through to default object schema
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if (candidate.jsonSchema && typeof candidate.jsonSchema === 'object')
|
|
403
|
+
return normalizeObjectSchema(candidate.jsonSchema, compact)
|
|
404
|
+
|
|
405
|
+
return { ...DEFAULT_INPUT_SCHEMA }
|
|
406
|
+
}
|
package/README.md
DELETED