@apollo/client-ai-apps 0.6.2 → 0.6.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/CHANGELOG.md +12 -0
- package/dist/mcp/core/ApolloClient.d.ts.map +1 -1
- package/dist/mcp/core/ApolloClient.js +7 -2
- package/dist/mcp/core/ApolloClient.js.map +1 -1
- package/dist/openai/core/ApolloClient.d.ts.map +1 -1
- package/dist/openai/core/ApolloClient.js +7 -2
- package/dist/openai/core/ApolloClient.js.map +1 -1
- package/dist/openai/core/McpAppManager.d.ts.map +1 -1
- package/dist/openai/core/McpAppManager.js +10 -0
- package/dist/openai/core/McpAppManager.js.map +1 -1
- package/package.json +1 -1
- package/src/mcp/core/ApolloClient.ts +8 -2
- package/src/openai/core/ApolloClient.ts +8 -2
- package/src/openai/core/McpAppManager.ts +11 -0
- package/src/openai/core/__tests__/ApolloClient.test.ts +60 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
## 0.6.3 (2026-03-13)
|
|
2
|
+
|
|
3
|
+
### Fixes
|
|
4
|
+
|
|
5
|
+
#### Fix ChatGPT page reload getting stuck
|
|
6
|
+
|
|
7
|
+
Fixes an issue where reloading the page in ChatGPT would cause the app to get stuck indefinitely. After a reload, ChatGPT does not re-send the `ui/notifications/tool-result` notification, so the app is stuck waiting for an event that would never arrive.
|
|
8
|
+
|
|
9
|
+
#### Fix issue with operation description on executed queries
|
|
10
|
+
|
|
11
|
+
Fixes an issue where a query executed by the client through the Apollo MCP Server `execute` tool maintained operation descriptions on the query.
|
|
12
|
+
|
|
1
13
|
## 0.6.2 (2026-03-12)
|
|
2
14
|
|
|
3
15
|
### Fixes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApolloClient.d.ts","sourceRoot":"","sources":["../../../src/mcp/core/ApolloClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,IAAI,gBAAgB,EAEjC,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mB,CAAC;KAC/B;CACF;AAED,qBAAa,YAAa,SAAQ,gBAAgB;;IAChD,QAAQ,EAAE,mBAAmB,CAAC;IAC9B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAgB;IAE3C,gBAAgB;IAChB,QAAQ,CAAC,CAAC,cAAc,CAAC,QAAQ;gBAIrB,OAAO,EAAE,YAAY,CAAC,OAAO;IA6BzC,IAAI;IAKJ,IAAI,SAAS,wCAEZ;IAED,cAAc;IAId,UAAU,CACR,CAAC,GAAG,GAAG,EACP,UAAU,SAAS,kBAAkB,GAAG,kBAAkB,EAC1D,OAAO,EAAE,iBAAiB,CAAC,UAAU,EAAE,CAAC,CAAC,GAAG,eAAe,CAAC,CAAC,EAAE,UAAU,CAAC;IAyC5E,OAAO;;MAsBJ;CACJ"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ApolloLink, ApolloClient as BaseApolloClient, DocumentTransform, } from "@apollo/client";
|
|
2
2
|
import { removeDirectivesFromDocument } from "@apollo/client/utilities/internal";
|
|
3
|
-
import { parse } from "graphql";
|
|
3
|
+
import { parse, visit } from "graphql";
|
|
4
4
|
import { equal } from "@wry/equality";
|
|
5
5
|
import { __DEV__ } from "@apollo/client/utilities/environment";
|
|
6
6
|
import { ToolCallLink } from "../link/ToolCallLink.js";
|
|
@@ -22,7 +22,12 @@ export class ApolloClient extends BaseApolloClient {
|
|
|
22
22
|
link,
|
|
23
23
|
// Strip out the prefetch/tool directives so they don't get sent with the operation to the server
|
|
24
24
|
documentTransform: new DocumentTransform((document) => {
|
|
25
|
-
|
|
25
|
+
const serverDocument = removeDirectivesFromDocument([{ name: "prefetch" }, { name: "tool" }], document);
|
|
26
|
+
return visit(serverDocument, {
|
|
27
|
+
OperationDefinition(node) {
|
|
28
|
+
return { ...node, description: undefined };
|
|
29
|
+
},
|
|
30
|
+
});
|
|
26
31
|
}).concat(options.documentTransform ?? DocumentTransform.identity()),
|
|
27
32
|
});
|
|
28
33
|
this.manifest = options.manifest;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApolloClient.js","sourceRoot":"","sources":["../../../src/mcp/core/ApolloClient.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,YAAY,IAAI,gBAAgB,EAChC,iBAAiB,GAClB,MAAM,gBAAgB,CAAC;AAMxB,OAAO,EAAE,4BAA4B,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"ApolloClient.js","sourceRoot":"","sources":["../../../src/mcp/core/ApolloClient.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,YAAY,IAAI,gBAAgB,EAChC,iBAAiB,GAClB,MAAM,gBAAgB,CAAC;AAMxB,OAAO,EAAE,4BAA4B,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,sCAAsC,CAAC;AAE/D,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EACL,cAAc,EACd,UAAU,EACV,wBAAwB,EACxB,4BAA4B,EAC5B,qCAAqC,EACrC,sBAAsB,GACvB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AASnD,MAAM,OAAO,YAAa,SAAQ,gBAAgB;IAChD,QAAQ,CAAsB;IACb,UAAU,CAAgB;IAE3C,gBAAgB;IACP,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC;IAEjC,UAAU,CAAsC;IAEhD,YAAY,OAA6B;QACvC,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,IAAI,YAAY,EAAE,CAAC;QAEhD,IAAI,OAAO,EAAE,CAAC;YACZ,uBAAuB,CAAC,IAAI,CAAC,CA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ObservableQuery,\n OperationVariables,\n} from \"@apollo/client\";\nimport { removeDirectivesFromDocument } from \"@apollo/client/utilities/internal\";\nimport { parse, visit } from \"graphql\";\nimport { equal } from \"@wry/equality\";\nimport { __DEV__ } from \"@apollo/client/utilities/environment\";\nimport type { ApplicationManifest } from \"../../types/application-manifest.js\";\nimport { ToolCallLink } from \"../link/ToolCallLink.js\";\nimport {\n aiClientSymbol,\n cacheAsync,\n getToolNamesFromDocument,\n getVariableNamesFromDocument,\n getVariablesForOperationFromToolInput,\n warnOnVariableMismatch,\n} from \"../../utilities/index.js\";\nimport { McpAppManager } from \"./McpAppManager.js\";\n\nexport declare namespace ApolloClient {\n export interface Options extends Omit<BaseApolloClient.Options, \"link\"> {\n link?: BaseApolloClient.Options[\"link\"];\n manifest: ApplicationManifest;\n }\n}\n\nexport class ApolloClient extends BaseApolloClient {\n manifest: ApplicationManifest;\n private readonly appManager: McpAppManager;\n\n /** @internal */\n readonly [aiClientSymbol] = true;\n\n #toolInput: Record<string, unknown> | undefined;\n\n constructor(options: ApolloClient.Options) {\n const link = options.link ?? new ToolCallLink();\n\n if (__DEV__) {\n validateTerminatingLink(link);\n }\n\n super({\n ...options,\n link,\n // Strip out the prefetch/tool directives so they don't get sent with the operation to the server\n documentTransform: new DocumentTransform((document) => {\n const serverDocument = removeDirectivesFromDocument(\n [{ name: \"prefetch\" }, { name: \"tool\" }],\n document\n )!;\n\n return visit(serverDocument, {\n OperationDefinition(node) {\n return { ...node, description: undefined };\n },\n });\n }).concat(options.documentTransform ?? DocumentTransform.identity()),\n });\n\n this.manifest = options.manifest;\n this.appManager = new McpAppManager(this.manifest);\n }\n\n stop() {\n super.stop();\n this.appManager.close().catch(() => {});\n }\n\n get toolInput() {\n return this.#toolInput;\n }\n\n clearToolInput() {\n this.#toolInput = undefined;\n }\n\n watchQuery<\n T = any,\n TVariables extends OperationVariables = OperationVariables,\n >(options: WatchQueryOptions<TVariables, T>): ObservableQuery<T, TVariables> {\n if (__DEV__) {\n const toolInput = this.#toolInput;\n\n if (toolInput) {\n const toolName = this.appManager.toolName;\n const hasMatchingTool =\n !!toolName && getToolNamesFromDocument(options.query).has(toolName);\n\n if (hasMatchingTool) {\n // Clear after first matching comparison so this only fires once and\n // remounting doesn't produce spurious warnings.\n this.#toolInput = undefined;\n\n const variableNames = getVariableNamesFromDocument(options.query);\n\n if (variableNames.size > 0) {\n const { variables } = options;\n const toolInputVariables = Object.entries(toolInput).filter(\n ([key]) => variableNames.has(key)\n );\n\n const hasToolInputMismatch = toolInputVariables.some(\n ([key, value]) => !equal(value, variables?.[key])\n );\n\n if (hasToolInputMismatch) {\n warnOnVariableMismatch(\n options.query,\n Object.fromEntries(toolInputVariables),\n variables\n );\n }\n }\n }\n }\n }\n\n return super.watchQuery(options);\n }\n\n connect = cacheAsync(async () => {\n const { prefetch, result, toolName, args } =\n await this.appManager.connect();\n\n this.#toolInput = args;\n\n this.manifest.operations.forEach((operation) => {\n if (operation.prefetchID && prefetch?.[operation.prefetchID]) {\n this.writeQuery({\n query: parse(operation.body),\n data: prefetch[operation.prefetchID].data,\n });\n }\n\n if (operation.tools.find((tool) => tool.name === toolName)) {\n this.writeQuery({\n query: parse(operation.body),\n data: result.data,\n variables: getVariablesForOperationFromToolInput(operation, args),\n });\n }\n });\n });\n}\n\nfunction validateTerminatingLink(link: ApolloLink) {\n let terminatingLink = link;\n\n while (terminatingLink.right) {\n terminatingLink = terminatingLink.right;\n }\n\n if (\n !isNamedLink(terminatingLink) ||\n terminatingLink.name !== \"ToolCallLink\"\n ) {\n throw new Error(\n \"The terminating link must be a `ToolCallLink`. If you are using a `split` link, ensure the `right` branch uses a `ToolCallLink` as the terminating link.\"\n );\n }\n}\n\nfunction isNamedLink(link: ApolloLink): link is ApolloLink & { name: string } {\n return \"name\" in link;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApolloClient.d.ts","sourceRoot":"","sources":["../../../src/openai/core/ApolloClient.ts"],"names":[],"mapp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,MAAM,CAAC,CAAC;QACxC,QAAQ,EAAE,mBAAmB,CAAC;KAC/B;CACF;AAED,qBAAa,YAAa,SAAQ,gBAAgB;;IAChD,QAAQ,EAAE,mBAAmB,CAAC;IAC9B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAgB;IAE3C,gBAAgB;IAChB,QAAQ,CAAC,CAAC,cAAc,CAAC,QAAQ;gBAIrB,OAAO,EAAE,YAAY,CAAC,OAAO;IA6BzC,IAAI;IAKJ,IAAI,SAAS,wCAEZ;IAED,cAAc;IAId,UAAU,CACR,CAAC,GAAG,GAAG,EACP,UAAU,SAAS,kBAAkB,GAAG,kBAAkB,EAC1D,OAAO,EAAE,iBAAiB,CAAC,UAAU,EAAE,CAAC,CAAC,GAAG,eAAe,CAAC,CAAC,EAAE,UAAU,CAAC;IA0C5E,OAAO;;MAsBJ;CACJ"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ApolloClient as BaseApolloClient } from "@apollo/client";
|
|
2
2
|
import { DocumentTransform } from "@apollo/client";
|
|
3
3
|
import { removeDirectivesFromDocument } from "@apollo/client/utilities/internal";
|
|
4
|
-
import { parse } from "graphql";
|
|
4
|
+
import { parse, visit } from "graphql";
|
|
5
5
|
import { equal } from "@wry/equality";
|
|
6
6
|
import { __DEV__ } from "@apollo/client/utilities/environment";
|
|
7
7
|
import { ToolCallLink } from "../link/ToolCallLink.js";
|
|
@@ -24,7 +24,12 @@ export class ApolloClient extends BaseApolloClient {
|
|
|
24
24
|
link,
|
|
25
25
|
// Strip out the prefetch/tool directives so they don't get sent with the operation to the server
|
|
26
26
|
documentTransform: new DocumentTransform((document) => {
|
|
27
|
-
|
|
27
|
+
const serverDocument = removeDirectivesFromDocument([{ name: "prefetch" }, { name: "tool" }], document);
|
|
28
|
+
return visit(serverDocument, {
|
|
29
|
+
OperationDefinition(node) {
|
|
30
|
+
return { ...node, description: undefined };
|
|
31
|
+
},
|
|
32
|
+
});
|
|
28
33
|
}).concat(options.documentTransform ?? DocumentTransform.identity()),
|
|
29
34
|
});
|
|
30
35
|
this.manifest = options.manifest;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApolloClient.js","sourceRoot":"","sources":["../../../src/openai/core/ApolloClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,IAAI,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAMnD,OAAO,EAAE,4BAA4B,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"ApolloClient.js","sourceRoot":"","sources":["../../../src/openai/core/ApolloClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,IAAI,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAMnD,OAAO,EAAE,4BAA4B,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,sCAAsC,CAAC;AAE/D,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EACL,cAAc,EACd,UAAU,EACV,wBAAwB,EACxB,qCAAqC,EACrC,sBAAsB,GACvB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,4BAA4B,EAAE,MAAM,iDAAiD,CAAC;AAS/F,MAAM,OAAO,YAAa,SAAQ,gBAAgB;IAChD,QAAQ,CAAsB;IACb,UAAU,CAAgB;IAE3C,gBAAgB;IACP,CAAC,cAAc,CAAC,GAAG,IAAI,CAAC;IAEjC,UAAU,CAAsC;IAEhD,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AAgB;IACnC,OAAO,MAAM,IAAI,IAAI,CAAC;AACxB,CAAC","sourcesContent":["import type { ApolloLink } from \"@apollo/client\";\nimport { ApolloClient as BaseApolloClient } from \"@apollo/client\";\nimport { DocumentTransform } from \"@apollo/client\";\nimport type {\n WatchQueryOptions,\n ObservableQuery,\n OperationVariables,\n} from \"@apollo/client\";\nimport { removeDirectivesFromDocument } from \"@apollo/client/utilities/internal\";\nimport { parse, visit } from \"graphql\";\nimport { equal } from \"@wry/equality\";\nimport { __DEV__ } from \"@apollo/client/utilities/environment\";\nimport type { ApplicationManifest } from \"../../types/application-manifest.js\";\nimport { ToolCallLink } from \"../link/ToolCallLink.js\";\nimport {\n aiClientSymbol,\n cacheAsync,\n getToolNamesFromDocument,\n getVariablesForOperationFromToolInput,\n warnOnVariableMismatch,\n} from \"../../utilities/index.js\";\nimport { McpAppManager } from \"./McpAppManager.js\";\nimport { getVariableNamesFromDocument } from \"../../utilities/getVariableNamesFromDocument.js\";\n\nexport declare namespace ApolloClient {\n export interface Options extends Omit<BaseApolloClient.Options, \"link\"> {\n link?: BaseApolloClient.Options[\"link\"];\n manifest: ApplicationManifest;\n }\n}\n\nexport class ApolloClient extends BaseApolloClient {\n manifest: ApplicationManifest;\n private readonly appManager: McpAppManager;\n\n /** @internal */\n readonly [aiClientSymbol] = true;\n\n #toolInput: Record<string, unknown> | undefined;\n\n constructor(options: ApolloClient.Options) {\n const link = options.link ?? new ToolCallLink();\n\n if (__DEV__) {\n validateTerminatingLink(link);\n }\n\n super({\n ...options,\n link,\n // Strip out the prefetch/tool directives so they don't get sent with the operation to the server\n documentTransform: new DocumentTransform((document) => {\n const serverDocument = removeDirectivesFromDocument(\n [{ name: \"prefetch\" }, { name: \"tool\" }],\n document\n )!;\n\n return visit(serverDocument, {\n OperationDefinition(node) {\n return { ...node, description: undefined };\n },\n });\n }).concat(options.documentTransform ?? DocumentTransform.identity()),\n });\n\n this.manifest = options.manifest;\n this.appManager = new McpAppManager(this.manifest);\n }\n\n stop() {\n super.stop();\n this.appManager.close().catch(() => {});\n }\n\n get toolInput() {\n return this.#toolInput;\n }\n\n clearToolInput() {\n this.#toolInput = undefined;\n }\n\n watchQuery<\n T = any,\n TVariables extends OperationVariables = OperationVariables,\n >(options: WatchQueryOptions<TVariables, T>): ObservableQuery<T, TVariables> {\n if (__DEV__) {\n const toolInput = this.#toolInput;\n\n if (toolInput) {\n const toolName = this.appManager.toolName;\n const hasMatchingTool =\n !!toolName && getToolNamesFromDocument(options.query).has(toolName);\n\n if (hasMatchingTool) {\n // Clear after first matching comparison so this only fires once and\n // remounting doesn't produce spurious warnings.\n this.#toolInput = undefined;\n\n const variableNames = getVariableNamesFromDocument(options.query);\n\n if (variableNames.size > 0) {\n const { variables } = options;\n\n const toolInputVariables = Object.entries(toolInput).filter(\n ([key]) => variableNames.has(key)\n );\n\n const hasToolInputMismatch = toolInputVariables.some(\n ([key, value]) => !equal(value, variables?.[key])\n );\n\n if (hasToolInputMismatch) {\n warnOnVariableMismatch(\n options.query,\n Object.fromEntries(toolInputVariables),\n variables\n );\n }\n }\n }\n }\n }\n\n return super.watchQuery(options);\n }\n\n connect = cacheAsync(async () => {\n const { prefetch, result, toolName, args } =\n await this.appManager.connect();\n\n this.#toolInput = args;\n\n this.manifest.operations.forEach((operation) => {\n if (operation.prefetchID && prefetch?.[operation.prefetchID]) {\n this.writeQuery({\n query: parse(operation.body),\n data: prefetch[operation.prefetchID].data,\n });\n }\n\n if (operation.tools.find((tool) => tool.name === toolName)) {\n this.writeQuery({\n query: parse(operation.body),\n data: result.data,\n variables: getVariablesForOperationFromToolInput(operation, args),\n });\n }\n });\n });\n}\n\nfunction validateTerminatingLink(link: ApolloLink) {\n let terminatingLink = link;\n\n while (terminatingLink.right) {\n terminatingLink = terminatingLink.right;\n }\n\n if (\n !isNamedLink(terminatingLink) ||\n terminatingLink.name !== \"ToolCallLink\"\n ) {\n throw new Error(\n \"The terminating link must be a `ToolCallLink`. If you are using a `split` link, ensure the `right` branch uses a `ToolCallLink` as the terminating link.\"\n );\n }\n}\n\nfunction isNamedLink(link: ApolloLink): link is ApolloLink & { name: string } {\n return \"name\" in link;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"McpAppManager.d.ts","sourceRoot":"","sources":["../../../src/openai/core/McpAppManager.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EAEH,KAAK,mCAAmC,EACzC,MAAM,gCAAgC,CAAC;AAExC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AAC5E,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAC;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AASvE,gBAAgB;AAChB,qBAAa,aAAa;;IACxB,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;gBAUN,QAAQ,EAAE,mBAAmB;IAIzC,IAAI,QAAQ,uBAEX;IAED,IAAI,YAAY,mCAEf;IAED,IAAI,SAAS,wCAEZ;IAED,oBAAoB,CAClB,EAAE,EAAE,CAAC,MAAM,EAAE,mCAAmC,CAAC,QAAQ,CAAC,KAAK,IAAI;IAQrE,OAAO;;;;;;;
|
|
1
|
+
{"version":3,"file":"McpAppManager.d.ts","sourceRoot":"","sources":["../../../src/openai/core/McpAppManager.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EAEH,KAAK,mCAAmC,EACzC,MAAM,gCAAgC,CAAC;AAExC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AAC5E,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,SAAS,CAAC;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AASvE,gBAAgB;AAChB,qBAAa,aAAa;;IACxB,QAAQ,CAAC,GAAG,EAAE,GAAG,CAAC;gBAUN,QAAQ,EAAE,mBAAmB;IAIzC,IAAI,QAAQ,uBAEX;IAED,IAAI,YAAY,mCAEf;IAED,IAAI,SAAS,wCAEZ;IAED,oBAAoB,CAClB,EAAE,EAAE,CAAC,MAAM,EAAE,mCAAmC,CAAC,QAAQ,CAAC,KAAK,IAAI;IAQrE,OAAO;;;;;;;MAoDJ;IAEH,KAAK;IAIC,YAAY,CAAC,EACjB,KAAK,EACL,SAAS,GACV,EAAE;QACD,KAAK,EAAE,YAAY,CAAC;QACpB,SAAS,EAAE,kBAAkB,GAAG,SAAS,CAAC;KAC3C;YASa,aAAa;CAW5B"}
|
|
@@ -35,6 +35,16 @@ export class McpAppManager {
|
|
|
35
35
|
this.#hostContextCallbacks.forEach((cb) => cb(params));
|
|
36
36
|
};
|
|
37
37
|
await this.connectToHost();
|
|
38
|
+
// After a page refresh, OpenAI does not re-send `ui/notifications/tool-result`.
|
|
39
|
+
// Instead, the tool result is available immediately via `window.openai.toolOutput`.
|
|
40
|
+
// If it's already set, resolve the promise now rather than waiting for the
|
|
41
|
+
// notification that will never arrive.
|
|
42
|
+
if (window.openai.toolOutput !== null) {
|
|
43
|
+
toolResult.resolve({
|
|
44
|
+
structuredContent: window.openai.toolOutput,
|
|
45
|
+
content: [],
|
|
46
|
+
});
|
|
47
|
+
}
|
|
38
48
|
const { structuredContent } = await toolResult.promise;
|
|
39
49
|
this.#toolName = this.app.getHostContext()?.toolInfo?.tool.name;
|
|
40
50
|
// OpenAI is not consistent about sending `ui/notifications/tool-input`.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"McpAppManager.js","sourceRoot":"","sources":["../../../src/openai/core/McpAppManager.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,oBAAoB,GAErB,MAAM,gCAAgC,CAAC;AAKxC,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAOnE,gBAAgB;AAChB,MAAM,O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C,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;YAEtE,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF","sourcesContent":["import {\n App,\n PostMessageTransport,\n type McpUiHostContextChangedNotification,\n} from \"@modelcontextprotocol/ext-apps\";\nimport type { CallToolResult } from \"@modelcontextprotocol/sdk/types.js\";\nimport type { ApplicationManifest } from \"../../types/application-manifest\";\nimport type { FormattedExecutionResult } from \"graphql\";\nimport type { DocumentNode, OperationVariables } from \"@apollo/client\";\nimport { print } from \"@apollo/client/utilities\";\nimport { cacheAsync, promiseWithResolvers } from \"../../utilities\";\nimport type { ApolloMcpServerApps } from \"../../core/types\";\n\ntype ExecuteQueryCallToolResult = Omit<CallToolResult, \"structuredContent\"> & {\n structuredContent: FormattedExecutionResult;\n};\n\n/** @internal */\nexport class McpAppManager {\n readonly app: App;\n\n #toolName: string | undefined;\n #toolMetadata: Record<string, unknown> | null = null;\n #toolInput: Record<string, unknown> | undefined;\n\n #hostContextCallbacks = new Set<\n (params: McpUiHostContextChangedNotification[\"params\"]) => void\n >();\n\n constructor(manifest: ApplicationManifest) {\n this.app = new App({ name: manifest.name, version: manifest.appVersion });\n }\n\n get toolName() {\n return this.#toolName;\n }\n\n get toolMetadata() {\n return this.#toolMetadata;\n }\n\n get toolInput() {\n return this.#toolInput;\n }\n\n onHostContextChanged(\n cb: (params: McpUiHostContextChangedNotification[\"params\"]) => void\n ) {\n this.#hostContextCallbacks.add(cb);\n return () => {\n this.#hostContextCallbacks.delete(cb);\n };\n }\n\n connect = cacheAsync(async () => {\n let toolResult = promiseWithResolvers<ApolloMcpServerApps.CallToolResult>();\n\n this.app.ontoolresult = (params) => {\n toolResult.resolve(\n params as unknown as ApolloMcpServerApps.CallToolResult\n );\n };\n\n this.app.onhostcontextchanged = (params) => {\n this.#hostContextCallbacks.forEach((cb) => cb(params));\n };\n\n await this.connectToHost();\n\n const { structuredContent } = await toolResult.promise;\n\n this.#toolName = this.app.getHostContext()?.toolInfo?.tool.name;\n\n // OpenAI is not consistent about sending `ui/notifications/tool-input`.\n // Sometimes it doesn't send at all, other times it sends more than once\n // before we get the tool result (which should always happen and at most\n // once according to the spec). Rather than relying on the\n // `ui/notifications/tool-input` notification to set the tool input value,\n // we read from `window.openai.toolInput so that we have the most recent\n // set value.\n //\n // When OpenAI fixes this issue and sends `ui/notifications/tool-input`\n // consistently according to the MCP Apps specification, this can be\n // reverted to use the `app.ontoolinput` callback.\n this.#toolInput = window.openai.toolInput;\n\n // OpenAI doesn't provide access to `_meta`, so we need to use\n // window.openai.toolResponseMetadata directly\n this.#toolMetadata = window.openai.toolResponseMetadata;\n\n return {\n ...structuredContent,\n toolName: this.toolName,\n args: this.toolInput,\n };\n });\n\n close() {\n return this.app.close();\n }\n\n async executeQuery({\n query,\n variables,\n }: {\n query: DocumentNode;\n variables: OperationVariables | undefined;\n }) {\n const result = (await this.app.callServerTool({\n name: \"execute\",\n arguments: { query: print(query), variables },\n })) as ExecuteQueryCallToolResult;\n\n return result.structuredContent;\n }\n\n private async connectToHost() {\n try {\n return await this.app.connect(\n new PostMessageTransport(window.parent, window.parent)\n );\n } catch (e) {\n const error = e instanceof Error ? e : new Error(\"Failed to connect\");\n\n throw error;\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"McpAppManager.js","sourceRoot":"","sources":["../../../src/openai/core/McpAppManager.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,oBAAoB,GAErB,MAAM,gCAAgC,CAAC;AAKxC,OAAO,EAAE,KAAK,EAAE,MAAM,0BAA0B,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAOnE,gBAAgB;AAChB,MAAM,OAAO,aAAa;IACf,GAAG,CAAM;IAElB,SAAS,CAAqB;IAC9B,aAAa,GAAmC,IAAI,CAAC;IACrD,UAAU,CAAsC;IAE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O,CAC3B,IAAI,oBAAoB,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CACvD,CAAC;QACJ,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,MAAM,KAAK,GAAG,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;YAEtE,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF","sourcesContent":["import {\n App,\n PostMessageTransport,\n type McpUiHostContextChangedNotification,\n} from \"@modelcontextprotocol/ext-apps\";\nimport type { CallToolResult } from \"@modelcontextprotocol/sdk/types.js\";\nimport type { ApplicationManifest } from \"../../types/application-manifest\";\nimport type { FormattedExecutionResult } from \"graphql\";\nimport type { DocumentNode, OperationVariables } from \"@apollo/client\";\nimport { print } from \"@apollo/client/utilities\";\nimport { cacheAsync, promiseWithResolvers } from \"../../utilities\";\nimport type { ApolloMcpServerApps } from \"../../core/types\";\n\ntype ExecuteQueryCallToolResult = Omit<CallToolResult, \"structuredContent\"> & {\n structuredContent: FormattedExecutionResult;\n};\n\n/** @internal */\nexport class McpAppManager {\n readonly app: App;\n\n #toolName: string | undefined;\n #toolMetadata: Record<string, unknown> | null = null;\n #toolInput: Record<string, unknown> | undefined;\n\n #hostContextCallbacks = new Set<\n (params: McpUiHostContextChangedNotification[\"params\"]) => void\n >();\n\n constructor(manifest: ApplicationManifest) {\n this.app = new App({ name: manifest.name, version: manifest.appVersion });\n }\n\n get toolName() {\n return this.#toolName;\n }\n\n get toolMetadata() {\n return this.#toolMetadata;\n }\n\n get toolInput() {\n return this.#toolInput;\n }\n\n onHostContextChanged(\n cb: (params: McpUiHostContextChangedNotification[\"params\"]) => void\n ) {\n this.#hostContextCallbacks.add(cb);\n return () => {\n this.#hostContextCallbacks.delete(cb);\n };\n }\n\n connect = cacheAsync(async () => {\n let toolResult = promiseWithResolvers<ApolloMcpServerApps.CallToolResult>();\n\n this.app.ontoolresult = (params) => {\n toolResult.resolve(\n params as unknown as ApolloMcpServerApps.CallToolResult\n );\n };\n\n this.app.onhostcontextchanged = (params) => {\n this.#hostContextCallbacks.forEach((cb) => cb(params));\n };\n\n await this.connectToHost();\n\n // After a page refresh, OpenAI does not re-send `ui/notifications/tool-result`.\n // Instead, the tool result is available immediately via `window.openai.toolOutput`.\n // If it's already set, resolve the promise now rather than waiting for the\n // notification that will never arrive.\n if (window.openai.toolOutput !== null) {\n toolResult.resolve({\n structuredContent: window.openai.toolOutput,\n content: [],\n });\n }\n\n const { structuredContent } = await toolResult.promise;\n\n this.#toolName = this.app.getHostContext()?.toolInfo?.tool.name;\n\n // OpenAI is not consistent about sending `ui/notifications/tool-input`.\n // Sometimes it doesn't send at all, other times it sends more than once\n // before we get the tool result (which should always happen and at most\n // once according to the spec). Rather than relying on the\n // `ui/notifications/tool-input` notification to set the tool input value,\n // we read from `window.openai.toolInput so that we have the most recent\n // set value.\n //\n // When OpenAI fixes this issue and sends `ui/notifications/tool-input`\n // consistently according to the MCP Apps specification, this can be\n // reverted to use the `app.ontoolinput` callback.\n this.#toolInput = window.openai.toolInput;\n\n // OpenAI doesn't provide access to `_meta`, so we need to use\n // window.openai.toolResponseMetadata directly\n this.#toolMetadata = window.openai.toolResponseMetadata;\n\n return {\n ...structuredContent,\n toolName: this.toolName,\n args: this.toolInput,\n };\n });\n\n close() {\n return this.app.close();\n }\n\n async executeQuery({\n query,\n variables,\n }: {\n query: DocumentNode;\n variables: OperationVariables | undefined;\n }) {\n const result = (await this.app.callServerTool({\n name: \"execute\",\n arguments: { query: print(query), variables },\n })) as ExecuteQueryCallToolResult;\n\n return result.structuredContent;\n }\n\n private async connectToHost() {\n try {\n return await this.app.connect(\n new PostMessageTransport(window.parent, window.parent)\n );\n } catch (e) {\n const error = e instanceof Error ? e : new Error(\"Failed to connect\");\n\n throw error;\n }\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@ import type {
|
|
|
9
9
|
OperationVariables,
|
|
10
10
|
} from "@apollo/client";
|
|
11
11
|
import { removeDirectivesFromDocument } from "@apollo/client/utilities/internal";
|
|
12
|
-
import { parse } from "graphql";
|
|
12
|
+
import { parse, visit } from "graphql";
|
|
13
13
|
import { equal } from "@wry/equality";
|
|
14
14
|
import { __DEV__ } from "@apollo/client/utilities/environment";
|
|
15
15
|
import type { ApplicationManifest } from "../../types/application-manifest.js";
|
|
@@ -52,10 +52,16 @@ export class ApolloClient extends BaseApolloClient {
|
|
|
52
52
|
link,
|
|
53
53
|
// Strip out the prefetch/tool directives so they don't get sent with the operation to the server
|
|
54
54
|
documentTransform: new DocumentTransform((document) => {
|
|
55
|
-
|
|
55
|
+
const serverDocument = removeDirectivesFromDocument(
|
|
56
56
|
[{ name: "prefetch" }, { name: "tool" }],
|
|
57
57
|
document
|
|
58
58
|
)!;
|
|
59
|
+
|
|
60
|
+
return visit(serverDocument, {
|
|
61
|
+
OperationDefinition(node) {
|
|
62
|
+
return { ...node, description: undefined };
|
|
63
|
+
},
|
|
64
|
+
});
|
|
59
65
|
}).concat(options.documentTransform ?? DocumentTransform.identity()),
|
|
60
66
|
});
|
|
61
67
|
|
|
@@ -7,7 +7,7 @@ import type {
|
|
|
7
7
|
OperationVariables,
|
|
8
8
|
} from "@apollo/client";
|
|
9
9
|
import { removeDirectivesFromDocument } from "@apollo/client/utilities/internal";
|
|
10
|
-
import { parse } from "graphql";
|
|
10
|
+
import { parse, visit } from "graphql";
|
|
11
11
|
import { equal } from "@wry/equality";
|
|
12
12
|
import { __DEV__ } from "@apollo/client/utilities/environment";
|
|
13
13
|
import type { ApplicationManifest } from "../../types/application-manifest.js";
|
|
@@ -50,10 +50,16 @@ export class ApolloClient extends BaseApolloClient {
|
|
|
50
50
|
link,
|
|
51
51
|
// Strip out the prefetch/tool directives so they don't get sent with the operation to the server
|
|
52
52
|
documentTransform: new DocumentTransform((document) => {
|
|
53
|
-
|
|
53
|
+
const serverDocument = removeDirectivesFromDocument(
|
|
54
54
|
[{ name: "prefetch" }, { name: "tool" }],
|
|
55
55
|
document
|
|
56
56
|
)!;
|
|
57
|
+
|
|
58
|
+
return visit(serverDocument, {
|
|
59
|
+
OperationDefinition(node) {
|
|
60
|
+
return { ...node, description: undefined };
|
|
61
|
+
},
|
|
62
|
+
});
|
|
57
63
|
}).concat(options.documentTransform ?? DocumentTransform.identity()),
|
|
58
64
|
});
|
|
59
65
|
|
|
@@ -67,6 +67,17 @@ export class McpAppManager {
|
|
|
67
67
|
|
|
68
68
|
await this.connectToHost();
|
|
69
69
|
|
|
70
|
+
// After a page refresh, OpenAI does not re-send `ui/notifications/tool-result`.
|
|
71
|
+
// Instead, the tool result is available immediately via `window.openai.toolOutput`.
|
|
72
|
+
// If it's already set, resolve the promise now rather than waiting for the
|
|
73
|
+
// notification that will never arrive.
|
|
74
|
+
if (window.openai.toolOutput !== null) {
|
|
75
|
+
toolResult.resolve({
|
|
76
|
+
structuredContent: window.openai.toolOutput,
|
|
77
|
+
content: [],
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
70
81
|
const { structuredContent } = await toolResult.promise;
|
|
71
82
|
|
|
72
83
|
this.#toolName = this.app.getHostContext()?.toolInfo?.tool.name;
|
|
@@ -400,6 +400,66 @@ describe("prefetchData", () => {
|
|
|
400
400
|
});
|
|
401
401
|
});
|
|
402
402
|
|
|
403
|
+
test("connects using window.openai.toolOutput when tool-result notification is not sent", async () => {
|
|
404
|
+
stubOpenAiGlobals({
|
|
405
|
+
toolOutput: {
|
|
406
|
+
result: {
|
|
407
|
+
data: {
|
|
408
|
+
product: {
|
|
409
|
+
id: "1",
|
|
410
|
+
title: "Pen",
|
|
411
|
+
rating: 5,
|
|
412
|
+
price: 1.0,
|
|
413
|
+
description: "Awesome pen",
|
|
414
|
+
images: [],
|
|
415
|
+
__typename: "Product",
|
|
416
|
+
},
|
|
417
|
+
},
|
|
418
|
+
},
|
|
419
|
+
},
|
|
420
|
+
toolInput: { id: "1" },
|
|
421
|
+
});
|
|
422
|
+
using _ = spyOnConsole("debug");
|
|
423
|
+
const client = new ApolloClient({
|
|
424
|
+
cache: new InMemoryCache(),
|
|
425
|
+
manifest: mockApplicationManifest(),
|
|
426
|
+
});
|
|
427
|
+
using host = await mockMcpHost({
|
|
428
|
+
hostContext: minimalHostContextWithToolName("GetProduct"),
|
|
429
|
+
});
|
|
430
|
+
host.onCleanup(() => client.stop());
|
|
431
|
+
|
|
432
|
+
host.sendToolInput({ arguments: { id: "1" } });
|
|
433
|
+
// No host.sendToolResult() — simulates page reload where ChatGPT does not
|
|
434
|
+
// re-send the tool-result notification
|
|
435
|
+
|
|
436
|
+
await client.connect();
|
|
437
|
+
|
|
438
|
+
// Flush pending setImmediate callbacks (e.g. ResizeObserver in happy-dom)
|
|
439
|
+
// before `using host` disposes and closes the app connection.
|
|
440
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
441
|
+
|
|
442
|
+
expect(client.extract()).toMatchInlineSnapshot(`
|
|
443
|
+
{
|
|
444
|
+
"Product:1": {
|
|
445
|
+
"__typename": "Product",
|
|
446
|
+
"description": "Awesome pen",
|
|
447
|
+
"id": "1",
|
|
448
|
+
"images": [],
|
|
449
|
+
"price": 1,
|
|
450
|
+
"rating": 5,
|
|
451
|
+
"title": "Pen",
|
|
452
|
+
},
|
|
453
|
+
"ROOT_QUERY": {
|
|
454
|
+
"__typename": "Query",
|
|
455
|
+
"product({"id":"1"})": {
|
|
456
|
+
"__ref": "Product:1",
|
|
457
|
+
},
|
|
458
|
+
},
|
|
459
|
+
}
|
|
460
|
+
`);
|
|
461
|
+
});
|
|
462
|
+
|
|
403
463
|
describe("custom links", () => {
|
|
404
464
|
test("allows for custom links provided to the constructor", async () => {
|
|
405
465
|
stubOpenAiGlobals();
|