forked from nicobailon/pi-mcp-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool-registrar.ts
More file actions
69 lines (63 loc) · 2.18 KB
/
Copy pathtool-registrar.ts
File metadata and controls
69 lines (63 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// tool-registrar.ts - MCP content transformation
// NOTE: Tools are NOT registered with Pi - only the unified `mcp` proxy tool is registered.
// This keeps the LLM context small (1 tool instead of 100s).
import type { McpContent, ContentBlock } from "./types.ts";
/**
* Transform MCP content types to Pi content blocks.
*/
export function transformMcpContent(content: McpContent[]): ContentBlock[] {
return content.map(c => {
if (c.type === "text") {
return { type: "text" as const, text: c.text ?? "" };
}
if (c.type === "image") {
return {
type: "image" as const,
data: c.data ?? "",
mimeType: c.mimeType ?? "image/png",
};
}
if (c.type === "resource") {
const resourceUri = c.resource?.uri ?? "(no URI)";
const resourceContent = c.resource?.text ?? (c.resource ? JSON.stringify(c.resource) : "(no content)");
return {
type: "text" as const,
text: `[Resource: ${resourceUri}]\n${resourceContent}`,
};
}
if (c.type === "resource_link") {
const linkName = c.name ?? c.uri ?? "unknown";
const linkUri = c.uri ?? "(no URI)";
return {
type: "text" as const,
text: `[Resource Link: ${linkName}]\nURI: ${linkUri}`,
};
}
if (c.type === "audio") {
return {
type: "text" as const,
text: `[Audio content: ${c.mimeType ?? "audio/*"}]`,
};
}
return { type: "text" as const, text: JSON.stringify(c) };
});
}
/**
* Resolve a tool result's content blocks, falling back to structuredContent
* when content is empty.
*/
export function resolveMcpResultContent(result: Record<string, unknown>): ContentBlock[] {
const blocks = transformMcpContent((Array.isArray(result.content) ? result.content : []) as McpContent[]);
if (blocks.length > 0) return blocks;
if (result.structuredContent !== undefined && result.structuredContent !== null) {
return [{ type: "text" as const, text: stringifyStructuredContent(result.structuredContent) }];
}
return [];
}
function stringifyStructuredContent(value: unknown): string {
try {
return JSON.stringify(value, null, 2) ?? String(value);
} catch {
return String(value);
}
}