pi-mcp-extension 1.12.1 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/schema-refs.ts +99 -13
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-mcp-extension",
3
- "version": "1.12.1",
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": [
@@ -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
  }