pi-mcp-extension 1.12.0 → 1.12.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mcp-extension",
3
- "version": "1.12.0",
3
+ "version": "1.12.3",
4
4
  "description": "Full-featured, lightweight MCP extension for the Pi coding agent: stdio, SSE & Streamable HTTP transports, OAuth 2.1 (PKCE + DCR), lazy/eager lifecycles — minimal dependencies, zero bloat.",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -371,16 +371,65 @@ function convertMultiTypeSchema(
371
371
  return Type.Union(orderUnionMembers(members), opts);
372
372
  }
373
373
 
374
+ /**
375
+ * Convert one tuple rest schema (an object schema or a boolean) to a TypeBox schema.
376
+ * Any other value (`undefined`, `null`, a number, a string, an array) is not a schema
377
+ * keyword: later positions stay unconstrained instead of inheriting the closed default.
378
+ */
379
+ function convertTupleRestSchema(rest: unknown, ctx: ConversionContext): TSchema | boolean {
380
+ if (typeof rest === "boolean") {
381
+ return rest;
382
+ }
383
+ if (isPlainObject(rest)) {
384
+ return convertJsonSchemaToTypebox(rest, ctx.rootDefs, ctx.effectiveRoot, ctx.seenRefs);
385
+ }
386
+ return true;
387
+ }
388
+
389
+ /**
390
+ * Add a tuple's rest schema to its TypeBox options as `additionalItems`.
391
+ * WHY: `Type.Tuple` forces `additionalItems: false`, so a variadic tuple such as
392
+ * `{ prefixItems: [{ type: "string" }], items: { type: "integer" } }` (`[string, ...integer[]]`)
393
+ * rejects every rest element with `schema is false` in Pi's `validateToolArguments`.
394
+ * TypeBox types `TTuple.additionalItems` as the literal `false` and `Type.TupleOptions()`
395
+ * drops the key, so this override is runtime-only: any `Type.Instantiate`-style rebuild of a
396
+ * tuple loses it and has to re-apply it. The rest schema is also validated but never
397
+ * `Value.Convert`-coerced — TypeBox's tuple converter only visits the declared positions —
398
+ * so `["ls", "1"]` stays a string and is rejected where a plain array would coerce it.
399
+ */
400
+ function withTupleRestSchema(
401
+ rest: unknown,
402
+ opts: Record<string, unknown>,
403
+ ctx: ConversionContext,
404
+ ): Record<string, unknown> {
405
+ return { ...opts, additionalItems: convertTupleRestSchema(rest, ctx) };
406
+ }
407
+
408
+ /**
409
+ * The rest keyword of a tuple: 2020-12 `items` after `prefixItems` (falling back to a legacy
410
+ * sibling `additionalItems`), or draft-07 `additionalItems` after an array `items`.
411
+ * WHY: both drafts leave later positions unconstrained when that keyword is absent, while
412
+ * `Type.Tuple` defaults to `additionalItems: false` — which would reject every rest element
413
+ * with the same `schema is false` failure this conversion exists to prevent. A tuple is closed
414
+ * only when the source schema says so (`items: false` / `additionalItems: false`).
415
+ */
416
+ function tupleRestKeyword(hasPrefixItems: boolean, schema: Record<string, unknown>): unknown {
417
+ if (!hasPrefixItems) {
418
+ return schema["additionalItems"];
419
+ }
420
+ return schema["items"] ?? schema["additionalItems"];
421
+ }
422
+
374
423
  function convertArraySchema(
375
424
  schema: Record<string, unknown>,
376
425
  opts: Record<string, unknown>,
377
426
  ctx: ConversionContext,
378
427
  ): TSchema {
379
- const tupleItems = Array.isArray(schema["prefixItems"])
428
+ const prefixItems = Array.isArray(schema["prefixItems"])
380
429
  ? (schema["prefixItems"] as unknown[])
381
- : Array.isArray(schema["items"])
382
- ? (schema["items"] as unknown[])
383
- : undefined;
430
+ : undefined;
431
+ const tupleItems =
432
+ prefixItems ?? (Array.isArray(schema["items"]) ? (schema["items"] as unknown[]) : undefined);
384
433
  if (tupleItems) {
385
434
  return Type.Tuple(
386
435
  tupleItems.map((item) =>
@@ -391,7 +440,7 @@ function convertArraySchema(
391
440
  ctx.seenRefs,
392
441
  ),
393
442
  ),
394
- opts,
443
+ withTupleRestSchema(tupleRestKeyword(prefixItems !== undefined, schema), opts, ctx),
395
444
  );
396
445
  }
397
446
  const items = schema["items"];
@@ -655,7 +704,9 @@ function resolveNonTypedSchema(
655
704
  * Convert a JSON Schema object to a TypeBox schema.
656
705
  * Handles the common subset used by real-world MCP servers:
657
706
  * - Primitives (string, number, integer, boolean, null)
658
- * - Arrays, tuples (prefixItems / array items), objects (with required/optional/additionalProperties)
707
+ * - Arrays, tuples (prefixItems / array items, with a variadic `items` after
708
+ * `prefixItems` or `additionalItems` after a draft-07 tuple), objects
709
+ * (with required/optional/additionalProperties)
659
710
  * - Enums and const (string, numeric, boolean, and nullable enums → Union of Literals)
660
711
  * - Nullable and multi-type arrays ("type": ["string", "null"]) with Type.Null() ordered first
661
712
  * - Cycle-safe $ref (#, #/$defs/, #/definitions/, and arbitrary RFC 6901 JSON Pointers)
@@ -4,9 +4,24 @@
4
4
 
5
5
  // ─── JSON Pointer Resolution ──────────────────────────────────────────────────
6
6
 
7
+ function decodeFragmentSegment(segment: string): string {
8
+ // WHY: RFC 6901 §6 encodes a "#/..." reference as a URI fragment, so each token arrives
9
+ // percent-encoded ("#/$defs/Response%5BItem%5D"). Decode before §4 unescaping: an encoded
10
+ // "~" ("%7E1") denotes the escape sequence "~1" ("/"), not a literal "~1" key.
11
+ try {
12
+ return decodeURIComponent(segment);
13
+ } catch {
14
+ // WHY: a malformed escape ("%ZZ") must not throw, unlike decodeURIComponent. The raw token is
15
+ // then resolved literally (a definition named "Bad%ZZName" still matches), and otherwise the
16
+ // $ref degrades like any other unresolvable one instead of failing the whole tools/list
17
+ // conversion on a server's arbitrary schema.
18
+ return segment;
19
+ }
20
+ }
21
+
7
22
  function unescapeJsonPointer(segment: string): string {
8
23
  // WHY: RFC 6901 §4 requires unescaping "~1" to "/" before "~0" to "~" so "~01" becomes "~1".
9
- return segment.replace(/~1/g, "/").replace(/~0/g, "~");
24
+ return decodeFragmentSegment(segment).replace(/~1/g, "/").replace(/~0/g, "~");
10
25
  }
11
26
 
12
27
  function stepArrayPointer(curr: unknown[], part: string): unknown {
@@ -66,12 +81,89 @@ function resolveFromRootDefs(
66
81
  return walkJsonPointer(rootDefs[defName], parts.slice(2));
67
82
  }
68
83
 
84
+ function unescapeLiteralPointer(segment: string): string {
85
+ // WHY: §6 requires a literal "%" to be encoded as "%25", but a server that emits it raw
86
+ // would stop resolving once segments are decoded. Unescaping only §4 escapes keeps the
87
+ // undecoded token available as a fallback key.
88
+ return segment.replace(/~1/g, "/").replace(/~0/g, "~");
89
+ }
90
+
91
+ function resolvePointerParts(
92
+ parts: readonly string[],
93
+ rootDefs: Record<string, Record<string, unknown>> | undefined,
94
+ rootSchema: Record<string, unknown>,
95
+ ): Record<string, unknown> | undefined {
96
+ if (isDefsPointer(parts)) {
97
+ const defTarget = resolveFromRootDefs(parts, rootDefs);
98
+ if (defTarget) {
99
+ return defTarget;
100
+ }
101
+ }
102
+ return walkJsonPointer(rootSchema, parts);
103
+ }
104
+
105
+ /**
106
+ * Token-wise reading: split the fragment, then decode each token (ajv's order).
107
+ * WHY: splitting first keeps "%2F" inside one token, which is what a key containing "/"
108
+ * means — §6 spells such a key "~1", so an encoded separator is the unusual reading.
109
+ */
110
+ function resolveDecodedTokens(
111
+ ref: string,
112
+ rootDefs: Record<string, Record<string, unknown>> | undefined,
113
+ rootSchema: Record<string, unknown>,
114
+ ): Record<string, unknown> | undefined {
115
+ const parts = ref
116
+ .slice(2)
117
+ .split("/")
118
+ .map(unescapeJsonPointer);
119
+ return resolvePointerParts(parts, rootDefs, rootSchema);
120
+ }
121
+
122
+ /**
123
+ * Literal reading: same tokens without percent-decoding.
124
+ * WHY: §6 requires a literal "%" to be encoded as "%25", but a server that emits it raw only
125
+ * resolves this way, and decoding would have turned its token into a different key.
126
+ */
127
+ function resolveLiteralTokens(
128
+ ref: string,
129
+ rootDefs: Record<string, Record<string, unknown>> | undefined,
130
+ rootSchema: Record<string, unknown>,
131
+ ): Record<string, unknown> | undefined {
132
+ const parts = ref
133
+ .slice(2)
134
+ .split("/")
135
+ .map(unescapeLiteralPointer);
136
+ return resolvePointerParts(parts, rootDefs, rootSchema);
137
+ }
138
+
139
+ /**
140
+ * Whole-fragment reading: decode the entire fragment, then split it, so "%2F" acts as a
141
+ * separator. WHY: a resolver that decodes before parsing reaches subtrees the token-wise
142
+ * reading cannot; trying it last only adds resolutions, never changes an earlier hit.
143
+ */
144
+ function resolveWholeFragment(
145
+ ref: string,
146
+ rootDefs: Record<string, Record<string, unknown>> | undefined,
147
+ rootSchema: Record<string, unknown>,
148
+ ): Record<string, unknown> | undefined {
149
+ let fragment: string;
150
+ try {
151
+ fragment = decodeURIComponent(ref.slice(2));
152
+ } catch {
153
+ return undefined; // WHY: already tried literally; a malformed escape cannot decode either way.
154
+ }
155
+ return resolvePointerParts(fragment.split("/").map(unescapeLiteralPointer), rootDefs, rootSchema);
156
+ }
157
+
69
158
  /**
70
159
  * Resolve a JSON Schema `$ref` against the root schema's `$defs`/`definitions`
71
160
  * and the root schema itself.
72
161
  *
73
162
  * Supports `#`, `#/$defs/<name>`, `#/definitions/<name>`, and arbitrary RFC 6901
74
- * JSON Pointers into either. Returns undefined when the reference cannot be resolved.
163
+ * JSON Pointers into either, in every fragment encoding a server may emit: decoded
164
+ * tokens (RFC 6901 §6, ajv's order), raw tokens (an unencoded literal "%"), and a
165
+ * whole-fragment decode ("%2F" as a separator). Returns undefined when the reference
166
+ * cannot be resolved.
75
167
  */
76
168
  export function resolveRefTarget(
77
169
  ref: string,
@@ -84,15 +176,9 @@ export function resolveRefTarget(
84
176
  if (!ref.startsWith("#/")) {
85
177
  return undefined;
86
178
  }
87
-
88
- const parts = ref.slice(2).split("/").map(unescapeJsonPointer);
89
-
90
- if (isDefsPointer(parts)) {
91
- const defTarget = resolveFromRootDefs(parts, rootDefs);
92
- if (defTarget) {
93
- return defTarget;
94
- }
95
- }
96
-
97
- return walkJsonPointer(rootSchema, parts);
179
+ return (
180
+ resolveDecodedTokens(ref, rootDefs, rootSchema) ??
181
+ resolveLiteralTokens(ref, rootDefs, rootSchema) ??
182
+ resolveWholeFragment(ref, rootDefs, rootSchema)
183
+ );
98
184
  }