pi-mcp-extension 1.12.1 → 1.12.4
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/README.md +3 -5
- package/package.json +2 -1
- package/src/resource-bridge.ts +9 -4
- package/src/schema-refs.ts +99 -13
- package/src/tool-bridge.ts +30 -10
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
- **Stable tool names** — Sanitized, collision-avoiding names (`<prefix>_<server>_<tool>`, max 64 chars) that stay constant across reconnects
|
|
28
28
|
- **No tool churn** — Activate/deactivate pattern ensures tools don't flicker during reconnection
|
|
29
29
|
- **Layered config & trust boundary** — Global `<agent dir>/mcp.json`, project `.pi/mcp.json` (loaded only in trusted projects, including Pi 1.3.0 partial overrides), and Pi 1.x extension-registered servers (`pi.registerMcpServer()`)
|
|
30
|
-
- **Rich tool results & annotations** — MCP `text`, `image` (`ImageContent`), `structuredContent`, embedded `resource` text/blobs, and `audio` fallback descriptions, plus `readOnlyHint`, `destructiveHint`, `idempotentHint`, and `openWorldHint` surfaced in tool descriptions
|
|
30
|
+
- **Rich tool results & annotations** — MCP `text`, `image` (`ImageContent`), `structuredContent`, embedded `resource` text/images/binary blobs, `resource_link` hints, and `audio` fallback descriptions, plus `readOnlyHint`, `destructiveHint`, `idempotentHint`, and `openWorldHint` surfaced in tool descriptions
|
|
31
31
|
- **On-demand agent tools & commands** — `mcp_status`, `mcp_connect`, `mcp_list_resources`, and `mcp_read_resource` agent tools plus `/mcp`, `/mcp <name>` (with config provenance and failure hints), `/mcp:start`, `/mcp:stop`, `/mcp:auth`, and `/mcp:resources` slash commands with server-name autocomplete
|
|
32
32
|
- **Structured error handling** — Distinct error codes (`config`, `connection`, `auth`, `timeout`, `protocol`, `tool`) and actionable failure hints
|
|
33
33
|
- **Redacted debug logging** — Opt-in `PI_MCP_DEBUG=1` JSON Lines log (`<agent dir>/logs/pi-mcp.log`, `0600`, ~2 MB rotation) with automatic secret masking and zero TUI `stderr` noise
|
|
@@ -366,7 +366,7 @@ flowchart TD
|
|
|
366
366
|
2. **Eager servers connect** at `session_start` via `ConnectionCoordinator` (offering a sequential `Authorize now` / `Not now` prompt when an eager OAuth server needs authorization in an interactive session); lazy servers wait for `mcp_connect`, `/mcp:start`, or `/mcp:auth`
|
|
367
367
|
3. **Tools are discovered** via paginated `tools/list` calls (cursor-based, up to 100 pages) before a server enters `ready`
|
|
368
368
|
4. **JSON Schema → TypeBox** conversion registers tools with Pi-compatible parameter schemas (preserving nullable union ordering, resolving `$ref`s cycle-safely, and inferring/flattening `allOf` object schemas)
|
|
369
|
-
5. **Pi tools are registered** as `<prefix>_<server>_<tool>` (sanitized `[a-zA-Z0-9_]`, max 64 chars with an 8-char SHA-256 suffix on truncation or collision) and convert MCP `text`, `image` (`ImageContent`), `structuredContent`, `resource`, and `audio` results
|
|
369
|
+
5. **Pi tools are registered** as `<prefix>_<server>_<tool>` (sanitized `[a-zA-Z0-9_]`, max 64 chars with an 8-char SHA-256 suffix on truncation or collision) and convert MCP `text`, `image` (`ImageContent`), `structuredContent`, `resource`, `resource_link`, and `audio` results. Embedded image resources become image blocks, binary resources include MIME type and byte count, and resource links include a `mcp_read_resource` hint.
|
|
370
370
|
6. **Tools activate/deactivate** automatically as servers connect/disconnect
|
|
371
371
|
7. **AbortSignal** is wired through to `notifications/cancelled` for clean cancellation
|
|
372
372
|
8. **Reconnection & recovery** uses a fixed delay schedule with configurable max retries on initial failure, health check failure, or unexpected transport close/error (`client.onclose` / `client.onerror`), while mid-session HTTP `401` / `403 insufficient_scope` invalidates stale tokens and surfaces a re-authorization hint
|
|
@@ -388,15 +388,13 @@ Implements the **MCP 2025-03-26** specification for tool and resource clients:
|
|
|
388
388
|
| Cursor-based `resources/list` and `resources/templates/list` pagination | ✅ |
|
|
389
389
|
| `resources/read` with text, image blobs, and binary size summaries | ✅ |
|
|
390
390
|
| `notifications/resources/list_changed` (catalog invalidation) | ✅ |
|
|
391
|
-
| `tools/call` with parameters, `image` (`ImageContent`), `structuredContent`, and `
|
|
391
|
+
| `tools/call` with parameters, `image` (`ImageContent`), `structuredContent`, `resource`, and `resource_link` conversion | ✅ |
|
|
392
392
|
| `roots/list` capability (workspace root) | ✅ |
|
|
393
393
|
| `notifications/tools/list_changed` (live refresh) | ✅ |
|
|
394
394
|
| `notifications/cancelled` (AbortSignal) | ✅ |
|
|
395
395
|
| `notifications/message` (structured logging) | ✅ |
|
|
396
396
|
| `isError: true` distinction (protocol vs tool errors, non-text filtered on error) | ✅ |
|
|
397
397
|
| Tool annotations (`readOnlyHint`, `destructiveHint`, etc.) | ✅ |
|
|
398
|
-
| Prompts bridge | ⏳ v2 |
|
|
399
|
-
| Sampling | ⏳ v2 |
|
|
400
398
|
|
|
401
399
|
## Contributing / Architecture
|
|
402
400
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-mcp-extension",
|
|
3
|
-
"version": "1.12.
|
|
3
|
+
"version": "1.12.4",
|
|
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": [
|
|
@@ -43,6 +43,7 @@
|
|
|
43
43
|
"typebox": "*"
|
|
44
44
|
},
|
|
45
45
|
"overrides": {
|
|
46
|
+
"minimatch": "^11.0.4",
|
|
46
47
|
"undici": "^8.12.3"
|
|
47
48
|
},
|
|
48
49
|
"devDependencies": {
|
package/src/resource-bridge.ts
CHANGED
|
@@ -261,7 +261,12 @@ export function convertResourceContents(contents: readonly unknown[]): PiResourc
|
|
|
261
261
|
return contents.map(convertResourceContent);
|
|
262
262
|
}
|
|
263
263
|
|
|
264
|
-
function
|
|
264
|
+
function resolveResourceUri(raw: unknown): string {
|
|
265
|
+
const trimmed = typeof raw === "string" ? raw.trim() : "";
|
|
266
|
+
return trimmed.length > 0 ? trimmed : "unknown";
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function convertResourceContent(item: unknown): PiResourceContent {
|
|
265
270
|
if (typeof item !== "object" || item === null) {
|
|
266
271
|
return { type: "text", text: String(item) };
|
|
267
272
|
}
|
|
@@ -274,7 +279,7 @@ function convertResourceContent(item: unknown): PiResourceContent {
|
|
|
274
279
|
if (typeof entry["blob"] === "string") {
|
|
275
280
|
return convertBlobResource(entry);
|
|
276
281
|
}
|
|
277
|
-
return { type: "text", text: `[Resource: ${
|
|
282
|
+
return { type: "text", text: `[Resource: ${resolveResourceUri(entry["uri"])}]` };
|
|
278
283
|
}
|
|
279
284
|
|
|
280
285
|
function resolveBlobMimeType(raw: unknown): string {
|
|
@@ -283,9 +288,9 @@ function resolveBlobMimeType(raw: unknown): string {
|
|
|
283
288
|
}
|
|
284
289
|
|
|
285
290
|
function convertBlobResource(entry: Record<string, unknown>): PiResourceContent {
|
|
286
|
-
const uri =
|
|
291
|
+
const uri = resolveResourceUri(entry["uri"]);
|
|
287
292
|
const mimeType = resolveBlobMimeType(entry["mimeType"]);
|
|
288
|
-
const blob =
|
|
293
|
+
const blob = entry["blob"] as string;
|
|
289
294
|
if (mimeType.startsWith("image/")) {
|
|
290
295
|
return { type: "image", data: blob, mimeType };
|
|
291
296
|
}
|
package/src/schema-refs.ts
CHANGED
|
@@ -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
|
|
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
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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
|
}
|
package/src/tool-bridge.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* - AbortSignal → SDK's built-in cancellation (notifications/cancelled)
|
|
10
10
|
* - Protocol error vs tool execution error distinction
|
|
11
11
|
* - Activate/deactivate pattern (register once, toggle on server state change)
|
|
12
|
-
* -
|
|
12
|
+
* - Images and embedded image resources → ImageContent; resource links → read hints
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
15
|
import { createHash } from "node:crypto";
|
|
@@ -21,6 +21,7 @@ import type { Settings } from "./config.js";
|
|
|
21
21
|
import { isCancellationError, isOAuthUnauthorizedError, isSdkTimeoutError } from "./connect-policy.js";
|
|
22
22
|
import { convertJsonSchemaToTypebox } from "./schema-convert.js";
|
|
23
23
|
import { listAllTools, type McpToolDefinition } from "./tool-discovery.js";
|
|
24
|
+
import { convertResourceContent } from "./resource-bridge.js";
|
|
24
25
|
|
|
25
26
|
// ─── Types ────────────────────────────────────────────────────────────────────
|
|
26
27
|
|
|
@@ -69,13 +70,31 @@ export function buildToolName(
|
|
|
69
70
|
type PiTextContent = { type: "text"; text: string };
|
|
70
71
|
type PiImageContent = { type: "image"; data: string; mimeType: string };
|
|
71
72
|
|
|
72
|
-
function
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
73
|
+
function pickNonEmptyText(value: unknown): string | undefined {
|
|
74
|
+
if (typeof value !== "string") return undefined;
|
|
75
|
+
const trimmed = value.trim();
|
|
76
|
+
return trimmed.length > 0 ? trimmed : undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function convertResourceLink(obj: Record<string, unknown>): PiTextContent {
|
|
80
|
+
const uri = pickNonEmptyText(obj["uri"]) ?? "unknown";
|
|
81
|
+
const title = pickNonEmptyText(obj["title"]) ?? pickNonEmptyText(obj["name"]);
|
|
82
|
+
const label = title ? ` ${JSON.stringify(title)}` : "";
|
|
83
|
+
const details: string[] = [];
|
|
84
|
+
const mimeType = pickNonEmptyText(obj["mimeType"]);
|
|
85
|
+
if (mimeType) {
|
|
86
|
+
details.push(mimeType);
|
|
87
|
+
}
|
|
88
|
+
if (typeof obj["size"] === "number" && Number.isSafeInteger(obj["size"]) && obj["size"] >= 0) {
|
|
89
|
+
details.push(`${obj["size"]} bytes`);
|
|
90
|
+
}
|
|
91
|
+
const metadata = details.length > 0 ? ` (${details.join(", ")})` : "";
|
|
92
|
+
const descriptionText = pickNonEmptyText(obj["description"]);
|
|
93
|
+
const description = descriptionText ? `\n${descriptionText}` : "";
|
|
94
|
+
return {
|
|
95
|
+
type: "text",
|
|
96
|
+
text: `[Resource link: ${uri}${label}${metadata} — Use mcp_read_resource to read]${description}`,
|
|
97
|
+
};
|
|
79
98
|
}
|
|
80
99
|
|
|
81
100
|
function convertNonTextContent(obj: Record<string, unknown>): PiImageContent | PiTextContent | undefined {
|
|
@@ -93,9 +112,10 @@ function convertNonTextContent(obj: Record<string, unknown>): PiImageContent | P
|
|
|
93
112
|
};
|
|
94
113
|
}
|
|
95
114
|
if (obj["type"] === "resource") {
|
|
96
|
-
|
|
115
|
+
const resource = obj["resource"];
|
|
116
|
+
return convertResourceContent(typeof resource === "object" && resource !== null ? resource : {});
|
|
97
117
|
}
|
|
98
|
-
return undefined;
|
|
118
|
+
return obj["type"] === "resource_link" ? convertResourceLink(obj) : undefined;
|
|
99
119
|
}
|
|
100
120
|
|
|
101
121
|
function convertMcpContentItem(item: unknown): PiTextContent | PiImageContent {
|