@fitchmultz/pi-mcp-adapter 5.0.0 → 7.0.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.
Files changed (129) hide show
  1. package/CHANGELOG.md +50 -0
  2. package/OAUTH.md +3 -3
  3. package/README.md +160 -88
  4. package/dist/app-bridge.bundle.js +75 -33
  5. package/dist/commands.js +9 -6
  6. package/dist/config.js +41 -16
  7. package/dist/direct-tools.d.ts +2 -4
  8. package/dist/direct-tools.js +21 -319
  9. package/dist/gateway-arguments.d.ts +33 -0
  10. package/dist/gateway-arguments.js +63 -0
  11. package/dist/index.js +181 -276
  12. package/dist/init.js +6 -20
  13. package/dist/lifecycle.d.ts +2 -2
  14. package/dist/lifecycle.js +2 -2
  15. package/dist/mcp-auth-flow.js +2 -2
  16. package/dist/mcp-auth.d.ts +1 -1
  17. package/dist/mcp-auth.js +3 -3
  18. package/dist/mcp-code.d.ts +7 -6
  19. package/dist/mcp-code.js +98 -51
  20. package/dist/mcp-oauth-provider.d.ts +5 -5
  21. package/dist/mcp-oauth-provider.js +5 -5
  22. package/dist/mcp-output-guard.d.ts +57 -2
  23. package/dist/mcp-output-guard.js +129 -24
  24. package/dist/mcp-panel.d.ts +3 -3
  25. package/dist/mcp-panel.js +36 -99
  26. package/dist/mcp-script-worker.mjs +5 -2
  27. package/dist/mcp-setup-panel.d.ts +5 -4
  28. package/dist/mcp-setup-panel.js +15 -7
  29. package/dist/mcp-status.js +2 -1
  30. package/dist/mcp-trace.d.ts +1 -1
  31. package/dist/mcp-trace.js +1 -1
  32. package/dist/metadata-cache.d.ts +1 -1
  33. package/dist/metadata-cache.js +25 -88
  34. package/dist/npx-resolver.js +8 -4
  35. package/dist/prompts.d.ts +1 -1
  36. package/dist/prompts.js +1 -1
  37. package/dist/proxy-modes.d.ts +41 -7
  38. package/dist/proxy-modes.js +275 -490
  39. package/dist/resource-tools.d.ts +15 -0
  40. package/dist/resource-tools.js +26 -1
  41. package/dist/sampling-handler.js +5 -1
  42. package/dist/search-ranking.d.ts +0 -2
  43. package/dist/search-ranking.js +70 -137
  44. package/dist/server-manager.d.ts +2 -2
  45. package/dist/server-manager.js +44 -27
  46. package/dist/session-recovery.d.ts +7 -4
  47. package/dist/session-recovery.js +76 -10
  48. package/dist/state.d.ts +1 -1
  49. package/dist/tool-loader.d.ts +32 -0
  50. package/dist/tool-loader.js +152 -0
  51. package/dist/tool-metadata.d.ts +67 -0
  52. package/dist/tool-metadata.js +23 -17
  53. package/dist/tool-registrar.d.ts +4 -4
  54. package/dist/tool-registrar.js +18 -13
  55. package/dist/tool-result-renderer.d.ts +6 -3
  56. package/dist/tool-result-renderer.js +11 -6
  57. package/dist/types.d.ts +11 -36
  58. package/dist/ui-resource-handler.d.ts +2 -2
  59. package/dist/ui-resource-handler.js +2 -2
  60. package/dist/ui-server.js +4 -3
  61. package/dist/ui-stream-types.d.ts +8 -8
  62. package/dist/unix-socket-transport.d.ts +1 -1
  63. package/dist/unix-socket-transport.js +1 -1
  64. package/dist/utils.d.ts +1 -0
  65. package/dist/utils.js +16 -4
  66. package/package.json +37 -96
  67. package/skills/mcp-scripting/SKILL.md +67 -20
  68. package/abort.ts +0 -34
  69. package/agent-dir.ts +0 -24
  70. package/app-bridge.bundle.js +0 -67
  71. package/banner.png +0 -0
  72. package/checkpoint.ts +0 -55
  73. package/commands.ts +0 -619
  74. package/config.ts +0 -1204
  75. package/consent-manager.ts +0 -25
  76. package/direct-tools.ts +0 -408
  77. package/dist/ts-shape.d.ts +0 -2
  78. package/dist/ts-shape.js +0 -147
  79. package/elicitation-handler.ts +0 -349
  80. package/error-signal.ts +0 -21
  81. package/glimpse-ui.ts +0 -80
  82. package/host-html-template.ts +0 -456
  83. package/index.ts +0 -900
  84. package/init.ts +0 -627
  85. package/legacy-migration.ts +0 -47
  86. package/lifecycle.ts +0 -165
  87. package/logger.ts +0 -127
  88. package/mcp-auth-flow.ts +0 -975
  89. package/mcp-auth.ts +0 -793
  90. package/mcp-callback-server.ts +0 -450
  91. package/mcp-code.ts +0 -386
  92. package/mcp-keyring-helper.cjs +0 -89
  93. package/mcp-oauth-provider.ts +0 -774
  94. package/mcp-output-guard.ts +0 -412
  95. package/mcp-panel.ts +0 -978
  96. package/mcp-script-worker.mjs +0 -128
  97. package/mcp-setup-panel.ts +0 -666
  98. package/mcp-status.ts +0 -109
  99. package/mcp-trace.ts +0 -295
  100. package/metadata-cache.ts +0 -333
  101. package/npx-resolver.ts +0 -488
  102. package/onboarding-state.ts +0 -58
  103. package/panel-keys.ts +0 -37
  104. package/prompts.ts +0 -337
  105. package/proxy-modes.ts +0 -1361
  106. package/resource-tools.ts +0 -17
  107. package/runtime-owner.ts +0 -123
  108. package/sampling-handler.ts +0 -273
  109. package/scripts/build.mjs +0 -131
  110. package/scripts/prepare.mjs +0 -102
  111. package/search-ranking.ts +0 -196
  112. package/server-manager.ts +0 -1320
  113. package/session-recovery.ts +0 -316
  114. package/state.ts +0 -64
  115. package/tool-approval.ts +0 -78
  116. package/tool-metadata.ts +0 -235
  117. package/tool-registrar.ts +0 -69
  118. package/tool-result-renderer.ts +0 -286
  119. package/ts-shape.ts +0 -145
  120. package/tsconfig.build.json +0 -13
  121. package/tsconfig.json +0 -16
  122. package/types.ts +0 -810
  123. package/ui-resource-handler.ts +0 -249
  124. package/ui-server.ts +0 -808
  125. package/ui-session.ts +0 -568
  126. package/ui-stream-types.ts +0 -89
  127. package/ui-tool-visibility.ts +0 -25
  128. package/unix-socket-transport.ts +0 -85
  129. package/utils.ts +0 -309
@@ -1,249 +0,0 @@
1
- import { RESOURCE_MIME_TYPE } from "@modelcontextprotocol/ext-apps/app-bridge";
2
- import { UrlElicitationRequiredError, type ReadResourceResult } from "@modelcontextprotocol/client";
3
- import { logger } from "./logger.ts";
4
- import { SessionRecoveryAuthRequiredError, withSessionRecovery, type SessionRecoveryDeps } from "./session-recovery.ts";
5
- import type { McpServerManager } from "./server-manager.ts";
6
- import { isServerDisabled, type McpConfig, type UiResourceContent, type UiResourceCsp, type UiResourceMeta, type UiResourcePermissions } from "./types.ts";
7
-
8
- interface ResourceContentRecord {
9
- uri?: string;
10
- mimeType?: string;
11
- text?: string;
12
- blob?: string;
13
- _meta?: Record<string, unknown>;
14
- }
15
-
16
- interface ReadUiResourceOptions {
17
- config?: McpConfig;
18
- signal?: AbortSignal;
19
- onNeedsAuth?: SessionRecoveryDeps["onNeedsAuth"];
20
- }
21
-
22
- export class UiResourceHandler {
23
- private log = logger.child({ component: "UiResourceHandler" });
24
-
25
- constructor(private manager: McpServerManager, private config: McpConfig | undefined = undefined) {}
26
-
27
- async readUiResource(serverName: string, uri: string, options: ReadUiResourceOptions = {}): Promise<UiResourceContent> {
28
- const log = this.log.child({ server: serverName, uri });
29
-
30
- if (!uri.startsWith("ui://")) {
31
- throw new Error(`Invalid UI resource "${uri}": URI must start with ui://`);
32
- }
33
-
34
- log.debug("Fetching UI resource");
35
-
36
- let result: ReadResourceResult;
37
- try {
38
- const config = options.config ?? this.config;
39
- if (config && isServerDisabled(config.mcpServers[serverName])) {
40
- throw new Error(`MCP server "${serverName}" is disabled`);
41
- }
42
- if (config) {
43
- this.manager.touch(serverName);
44
- this.manager.incrementInFlight(serverName);
45
- try {
46
- result = await withSessionRecovery(
47
- {
48
- manager: this.manager,
49
- config,
50
- ...(options.signal ? { signal: options.signal } : {}),
51
- ...(options.onNeedsAuth ? { onNeedsAuth: options.onNeedsAuth } : {}),
52
- },
53
- serverName,
54
- (connection) => connection.client.readResource({ uri }, this.manager.getRequestOptions(serverName, options.signal)),
55
- );
56
- } finally {
57
- this.manager.decrementInFlight(serverName);
58
- this.manager.touch(serverName);
59
- }
60
- } else {
61
- result = await this.manager.readResource(serverName, uri, options.signal);
62
- }
63
- } catch (error) {
64
- if (error instanceof UrlElicitationRequiredError || error instanceof SessionRecoveryAuthRequiredError) throw error;
65
- const message = error instanceof Error ? error.message : String(error);
66
- log.error("Failed to read resource", error instanceof Error ? error : undefined);
67
- throw new Error(`Failed to fetch UI resource "${uri}": ${message}`,
68
- error instanceof Error ? { cause: error } : undefined);
69
- }
70
-
71
- const content = selectContent(result, uri);
72
- const mimeType = content.mimeType;
73
-
74
- if (mimeType && !isHtmlMimeType(mimeType)) {
75
- log.warn("Unsupported MIME type", { mimeType });
76
- throw new Error(
77
- `Invalid UI resource "${uri}": unsupported MIME type "${mimeType}" (expected text/html or ${RESOURCE_MIME_TYPE})`
78
- );
79
- }
80
-
81
- const html = toHtml(content);
82
- if (!html.trim()) {
83
- log.warn("Resource content is empty");
84
- throw new Error(`Invalid UI resource "${uri}": content is empty`);
85
- }
86
-
87
- const contentMeta = extractUiMeta(content._meta);
88
- const listMeta = extractUiMeta(this.getListResourceMeta(serverName, uri));
89
-
90
- log.debug("Resource loaded successfully", {
91
- contentLength: html.length,
92
- hasCsp: !!contentMeta.csp || !!listMeta.csp,
93
- });
94
-
95
- return {
96
- uri: content.uri ?? uri,
97
- html,
98
- mimeType: mimeType ?? RESOURCE_MIME_TYPE,
99
- meta: {
100
- ...((contentMeta.csp ?? listMeta.csp) !== undefined ? { csp: contentMeta.csp ?? listMeta.csp } : {}),
101
- ...((contentMeta.permissions ?? listMeta.permissions) !== undefined
102
- ? { permissions: contentMeta.permissions ?? listMeta.permissions }
103
- : {}),
104
- ...((contentMeta.domain ?? listMeta.domain) !== undefined ? { domain: contentMeta.domain ?? listMeta.domain } : {}),
105
- ...((contentMeta.prefersBorder ?? listMeta.prefersBorder) !== undefined
106
- ? { prefersBorder: contentMeta.prefersBorder ?? listMeta.prefersBorder }
107
- : {}),
108
- },
109
- };
110
- }
111
-
112
- private getListResourceMeta(serverName: string, uri: string): Record<string, unknown> | undefined {
113
- const connection = this.manager.getConnection(serverName);
114
- if (!connection?.resources?.length) return undefined;
115
- const resource = connection.resources.find((entry) => entry.uri === uri);
116
- if (!resource || !resource._meta || typeof resource._meta !== "object") return undefined;
117
- return resource._meta;
118
- }
119
- }
120
-
121
- function selectContent(result: ReadResourceResult, preferredUri: string): ResourceContentRecord {
122
- const contents = (result.contents ?? []) as ResourceContentRecord[];
123
- if (contents.length === 0) {
124
- throw new Error(`No contents returned for UI resource: ${preferredUri}`);
125
- }
126
-
127
- const byUri = contents.find((content) => content.uri === preferredUri);
128
- if (byUri) return byUri;
129
-
130
- const byHtmlMime = contents.find(
131
- (content) => content.mimeType && isHtmlMimeType(content.mimeType)
132
- );
133
- if (byHtmlMime) return byHtmlMime;
134
-
135
- const firstContent = contents[0];
136
- if (!firstContent) {
137
- throw new Error(`No contents returned for UI resource: ${preferredUri}`);
138
- }
139
- return firstContent;
140
- }
141
-
142
- function isHtmlMimeType(mimeType: string): boolean {
143
- const normalized = mimeType.toLowerCase();
144
- return normalized.startsWith("text/html") || normalized === RESOURCE_MIME_TYPE.toLowerCase();
145
- }
146
-
147
- function toHtml(content: ResourceContentRecord): string {
148
- if (typeof content.text === "string") {
149
- return content.text;
150
- }
151
-
152
- if (typeof content.blob === "string") {
153
- return Buffer.from(content.blob, "base64").toString("utf-8");
154
- }
155
-
156
- throw new Error(`UI resource ${content.uri ?? "(unknown)"} did not include text or blob content`);
157
- }
158
-
159
- const OPENAI_CSP_FIELD_MAPPINGS = [
160
- ["resource_domains", "resourceDomains"],
161
- ["connect_domains", "connectDomains"],
162
- ["frame_domains", "frameDomains"],
163
- ] as const;
164
-
165
- const UI_CSP_DOMAIN_FIELDS: readonly (keyof UiResourceCsp)[] = [
166
- "resourceDomains",
167
- "connectDomains",
168
- "frameDomains",
169
- "baseUriDomains",
170
- ];
171
-
172
- function extractUiMeta(meta: Record<string, unknown> | undefined): UiResourceMeta {
173
- if (!meta || typeof meta !== "object") return {};
174
-
175
- const ui = isRecord(meta.ui) ? meta.ui : undefined;
176
- const out: UiResourceMeta = {};
177
- const openAiCsp = hasOwnProperty(meta, "openai/widgetCSP")
178
- ? normalizeOpenAiWidgetCsp(meta["openai/widgetCSP"])
179
- : undefined;
180
- const hasStandardCsp = !!ui && hasOwnProperty(ui, "csp");
181
- const standardCspValue = hasStandardCsp ? ui.csp : undefined;
182
-
183
- if (hasStandardCsp && !isRecord(standardCspValue)) {
184
- // A declared canonical container takes precedence even when malformed.
185
- out.csp = {};
186
- } else {
187
- const standardCsp = hasStandardCsp
188
- ? normalizeUiResourceCsp(standardCspValue)
189
- : undefined;
190
- if (openAiCsp || standardCsp) {
191
- out.csp = { ...openAiCsp, ...standardCsp };
192
- if (isRecord(standardCspValue)) {
193
- for (const [, standardField] of OPENAI_CSP_FIELD_MAPPINGS) {
194
- if (hasOwnProperty(standardCspValue, standardField) && !copyStringArray(standardCspValue[standardField])) {
195
- delete out.csp[standardField];
196
- }
197
- }
198
- }
199
- }
200
- }
201
-
202
- if (ui && isRecord(ui.permissions)) {
203
- out.permissions = ui.permissions as UiResourcePermissions;
204
- }
205
- if (ui && typeof ui.domain === "string") {
206
- out.domain = ui.domain;
207
- }
208
- if (ui && typeof ui.prefersBorder === "boolean") {
209
- out.prefersBorder = ui.prefersBorder;
210
- }
211
-
212
- return out;
213
- }
214
-
215
- function normalizeUiResourceCsp(value: unknown): UiResourceCsp {
216
- if (!isRecord(value)) return {};
217
-
218
- const csp: UiResourceCsp = {};
219
- for (const field of UI_CSP_DOMAIN_FIELDS) {
220
- const domains = copyStringArray(value[field]);
221
- if (domains) csp[field] = domains;
222
- }
223
- return csp;
224
- }
225
-
226
- function normalizeOpenAiWidgetCsp(value: unknown): UiResourceCsp {
227
- if (!isRecord(value)) return {};
228
-
229
- const csp: UiResourceCsp = {};
230
- for (const [sourceField, targetField] of OPENAI_CSP_FIELD_MAPPINGS) {
231
- const domains = copyStringArray(value[sourceField]);
232
- if (domains) csp[targetField] = domains;
233
- }
234
- return csp;
235
- }
236
-
237
- function copyStringArray(value: unknown): string[] | undefined {
238
- return Array.isArray(value) && value.every((entry) => typeof entry === "string")
239
- ? [...value]
240
- : undefined;
241
- }
242
-
243
- function hasOwnProperty(record: Record<string, unknown>, property: string): boolean {
244
- return Object.prototype.hasOwnProperty.call(record, property);
245
- }
246
-
247
- function isRecord(value: unknown): value is Record<string, unknown> {
248
- return typeof value === "object" && value !== null && !Array.isArray(value);
249
- }