@apollo/client-ai-apps 0.6.4 → 0.7.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.
- package/CHANGELOG.md +100 -0
- package/CONTRIBUTING.md +195 -0
- package/README.md +74 -0
- package/dist/core/AbstractApolloClient.d.ts +33 -0
- package/dist/core/AbstractApolloClient.d.ts.map +1 -0
- package/dist/core/AbstractApolloClient.js +129 -0
- package/dist/core/AbstractApolloClient.js.map +1 -0
- package/dist/core/ApolloClient.d.ts +3 -7
- package/dist/core/ApolloClient.d.ts.map +1 -1
- package/dist/core/ApolloClient.js +5 -4
- package/dist/core/ApolloClient.js.map +1 -1
- package/dist/core/McpAppManager.d.ts +42 -0
- package/dist/core/McpAppManager.d.ts.map +1 -0
- package/dist/core/McpAppManager.js +56 -0
- package/dist/core/McpAppManager.js.map +1 -0
- package/dist/core/typeRegistration.d.ts +0 -14
- package/dist/core/typeRegistration.d.ts.map +1 -1
- package/dist/core/typeRegistration.js.map +1 -1
- package/dist/core/types.d.ts +2 -1
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mcp.d.ts +0 -1
- package/dist/index.mcp.d.ts.map +1 -1
- package/dist/index.mcp.js +0 -1
- package/dist/index.mcp.js.map +1 -1
- package/dist/index.openai.d.ts +0 -1
- package/dist/index.openai.d.ts.map +1 -1
- package/dist/index.openai.js +0 -1
- package/dist/index.openai.js.map +1 -1
- package/dist/link/ToolCallLink.d.ts +6 -1
- package/dist/link/ToolCallLink.d.ts.map +1 -1
- package/dist/link/ToolCallLink.js +17 -4
- package/dist/link/ToolCallLink.js.map +1 -1
- package/dist/link/ToolHydrationLink.d.ts +21 -0
- package/dist/link/ToolHydrationLink.d.ts.map +1 -0
- package/dist/link/ToolHydrationLink.js +57 -0
- package/dist/link/ToolHydrationLink.js.map +1 -0
- package/dist/mcp/core/ApolloClient.d.ts +3 -20
- package/dist/mcp/core/ApolloClient.d.ts.map +1 -1
- package/dist/mcp/core/ApolloClient.js +20 -98
- package/dist/mcp/core/ApolloClient.js.map +1 -1
- package/dist/mcp/index.d.ts +0 -1
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +0 -1
- package/dist/mcp/index.js.map +1 -1
- package/dist/openai/core/ApolloClient.d.ts +3 -20
- package/dist/openai/core/ApolloClient.d.ts.map +1 -1
- package/dist/openai/core/ApolloClient.js +36 -98
- package/dist/openai/core/ApolloClient.js.map +1 -1
- package/dist/openai/index.d.ts +0 -1
- package/dist/openai/index.d.ts.map +1 -1
- package/dist/openai/index.js +0 -1
- package/dist/openai/index.js.map +1 -1
- package/dist/openai/react/index.d.ts +0 -7
- package/dist/openai/react/index.d.ts.map +1 -1
- package/dist/openai/react/index.js +0 -7
- package/dist/openai/react/index.js.map +1 -1
- package/dist/react/ApolloProvider.d.ts.map +1 -1
- package/dist/react/ApolloProvider.js +1 -1
- package/dist/react/ApolloProvider.js.map +1 -1
- package/dist/{mcp/react/hooks → react}/createHydrationUtils.d.ts +1 -1
- package/dist/react/createHydrationUtils.d.ts.map +1 -0
- package/dist/{mcp/react/hooks → react}/createHydrationUtils.js +7 -9
- package/dist/react/createHydrationUtils.js.map +1 -0
- package/dist/react/hooks/internal/useApolloClient.d.ts +3 -0
- package/dist/react/hooks/internal/useApolloClient.d.ts.map +1 -0
- package/dist/{mcp/react/hooks → react/hooks/internal}/useApolloClient.js +3 -3
- package/dist/react/hooks/internal/useApolloClient.js.map +1 -0
- package/dist/react/hooks/useApp.d.ts.map +1 -0
- package/dist/react/hooks/useApp.js +5 -0
- package/dist/react/hooks/useApp.js.map +1 -0
- package/dist/react/hooks/useHostContext.d.ts.map +1 -0
- package/dist/{openai/react → react}/hooks/useHostContext.js +1 -1
- package/dist/react/hooks/useHostContext.js.map +1 -0
- package/dist/react/hooks/useToolInfo.d.ts +3 -0
- package/dist/react/hooks/useToolInfo.d.ts.map +1 -0
- package/dist/react/hooks/useToolInfo.js +5 -0
- package/dist/react/hooks/useToolInfo.js.map +1 -0
- package/dist/react/hooks/useToolMetadata.d.ts +2 -0
- package/dist/react/hooks/useToolMetadata.d.ts.map +1 -0
- package/dist/react/hooks/useToolMetadata.js +5 -0
- package/dist/react/hooks/useToolMetadata.js.map +1 -0
- package/dist/react/index.d.ts +5 -16
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +5 -19
- package/dist/react/index.js.map +1 -1
- package/dist/types/application-manifest.d.ts +1 -0
- package/dist/types/application-manifest.d.ts.map +1 -1
- package/dist/types/application-manifest.js.map +1 -1
- package/dist/utilities/connectToHost.d.ts +3 -0
- package/dist/utilities/connectToHost.d.ts.map +1 -0
- package/dist/utilities/connectToHost.js +11 -0
- package/dist/utilities/connectToHost.js.map +1 -0
- package/dist/utilities/index.d.ts +1 -0
- package/dist/utilities/index.d.ts.map +1 -1
- package/dist/utilities/index.js +1 -0
- package/dist/utilities/index.js.map +1 -1
- package/dist/vite/apolloClientAiApps.d.ts.map +1 -1
- package/dist/vite/apolloClientAiApps.js +2 -0
- package/dist/vite/apolloClientAiApps.js.map +1 -1
- package/package.json +5 -22
- package/src/core/AbstractApolloClient.ts +217 -0
- package/src/core/ApolloClient.ts +8 -10
- package/src/core/McpAppManager.ts +106 -0
- package/src/core/typeRegistration.ts +0 -15
- package/src/core/types.ts +2 -1
- package/src/index.mcp.ts +0 -1
- package/src/index.openai.ts +0 -1
- package/src/index.ts +1 -6
- package/src/link/ToolCallLink.ts +27 -5
- package/src/link/ToolHydrationLink.ts +90 -0
- package/src/link/__tests__/ToolCallLink.test.ts +99 -0
- package/src/mcp/core/ApolloClient.ts +32 -165
- package/src/mcp/core/__tests__/ApolloClient.test.ts +571 -71
- package/src/mcp/index.ts +0 -1
- package/src/openai/core/ApolloClient.ts +48 -161
- package/src/openai/core/__tests__/ApolloClient.test.ts +916 -118
- package/src/openai/index.ts +0 -1
- package/src/openai/react/index.ts +0 -7
- package/src/react/ApolloProvider.tsx +1 -6
- package/src/react/__tests__/ApolloProvider/mcp.test.tsx +66 -29
- package/src/react/__tests__/ApolloProvider/openai.test.tsx +16 -41
- package/src/react/__tests__/createHydrationUtils.test.tsx +1260 -0
- package/src/{mcp/react/hooks → react}/createHydrationUtils.ts +7 -10
- package/src/react/hooks/__tests__/useApp.test.tsx +46 -0
- package/src/react/hooks/__tests__/useHostContext.test.tsx +99 -0
- package/src/react/hooks/__tests__/useToolInfo.test.tsx +98 -0
- package/src/react/hooks/__tests__/useToolMetadata.test.tsx +58 -0
- package/src/{mcp/react/hooks → react/hooks/internal}/useApolloClient.ts +3 -3
- package/src/{mcp/react → react}/hooks/useApp.ts +1 -1
- package/src/{openai/react → react}/hooks/useHostContext.ts +1 -1
- package/src/react/hooks/useToolInfo.ts +6 -0
- package/src/react/hooks/useToolMetadata.ts +5 -0
- package/src/react/index.ts +5 -36
- package/src/testing/internal/graphql/parseManifestOperation.ts +87 -0
- package/src/testing/internal/index.ts +3 -0
- package/src/testing/internal/matchers/index.ts +1 -0
- package/src/testing/internal/matchers/toEmitAnything.ts +43 -0
- package/src/testing/internal/matchers/types.ts +1 -0
- package/src/testing/internal/mcp/mockMcpHost.ts +25 -4
- package/src/testing/internal/tests/eachHostEnv.ts +22 -0
- package/src/testing/internal/utilities/createHostEnv.ts +117 -0
- package/src/types/application-manifest.ts +1 -0
- package/src/utilities/connectToHost.ts +13 -0
- package/src/utilities/index.ts +1 -0
- package/src/vite/__tests__/apolloClientAiApps.test.ts +56 -0
- package/src/vite/apolloClientAiApps.ts +5 -0
- package/tsconfig.vite.json +1 -1
- package/vitest.config.ts +13 -0
- package/dist/mcp/core/McpAppManager.d.ts +0 -30
- package/dist/mcp/core/McpAppManager.d.ts.map +0 -1
- package/dist/mcp/core/McpAppManager.js +0 -82
- package/dist/mcp/core/McpAppManager.js.map +0 -1
- package/dist/mcp/link/ToolCallLink.d.ts +0 -28
- package/dist/mcp/link/ToolCallLink.d.ts.map +0 -1
- package/dist/mcp/link/ToolCallLink.js +0 -35
- package/dist/mcp/link/ToolCallLink.js.map +0 -1
- package/dist/mcp/react/hooks/createHydrationUtils.d.ts.map +0 -1
- package/dist/mcp/react/hooks/createHydrationUtils.js.map +0 -1
- package/dist/mcp/react/hooks/useApolloClient.d.ts +0 -3
- package/dist/mcp/react/hooks/useApolloClient.d.ts.map +0 -1
- package/dist/mcp/react/hooks/useApolloClient.js.map +0 -1
- package/dist/mcp/react/hooks/useApp.d.ts.map +0 -1
- package/dist/mcp/react/hooks/useApp.js +0 -5
- package/dist/mcp/react/hooks/useApp.js.map +0 -1
- package/dist/mcp/react/hooks/useHostContext.d.ts.map +0 -1
- package/dist/mcp/react/hooks/useHostContext.js +0 -7
- package/dist/mcp/react/hooks/useHostContext.js.map +0 -1
- package/dist/mcp/react/hooks/useToolInfo.d.ts +0 -3
- package/dist/mcp/react/hooks/useToolInfo.d.ts.map +0 -1
- package/dist/mcp/react/hooks/useToolInfo.js +0 -10
- package/dist/mcp/react/hooks/useToolInfo.js.map +0 -1
- package/dist/mcp/react/hooks/useToolInput.d.ts +0 -7
- package/dist/mcp/react/hooks/useToolInput.d.ts.map +0 -1
- package/dist/mcp/react/hooks/useToolInput.js +0 -9
- package/dist/mcp/react/hooks/useToolInput.js.map +0 -1
- package/dist/mcp/react/hooks/useToolMetadata.d.ts +0 -2
- package/dist/mcp/react/hooks/useToolMetadata.d.ts.map +0 -1
- package/dist/mcp/react/hooks/useToolMetadata.js +0 -5
- package/dist/mcp/react/hooks/useToolMetadata.js.map +0 -1
- package/dist/mcp/react/hooks/useToolName.d.ts +0 -7
- package/dist/mcp/react/hooks/useToolName.d.ts.map +0 -1
- package/dist/mcp/react/hooks/useToolName.js +0 -9
- package/dist/mcp/react/hooks/useToolName.js.map +0 -1
- package/dist/mcp/react/index.d.ts +0 -8
- package/dist/mcp/react/index.d.ts.map +0 -1
- package/dist/mcp/react/index.js +0 -8
- package/dist/mcp/react/index.js.map +0 -1
- package/dist/openai/core/McpAppManager.d.ts +0 -29
- package/dist/openai/core/McpAppManager.d.ts.map +0 -1
- package/dist/openai/core/McpAppManager.js +0 -91
- package/dist/openai/core/McpAppManager.js.map +0 -1
- package/dist/openai/link/ToolCallLink.d.ts +0 -28
- package/dist/openai/link/ToolCallLink.d.ts.map +0 -1
- package/dist/openai/link/ToolCallLink.js +0 -35
- package/dist/openai/link/ToolCallLink.js.map +0 -1
- package/dist/openai/react/hooks/createHydrationUtils.d.ts +0 -15
- package/dist/openai/react/hooks/createHydrationUtils.d.ts.map +0 -1
- package/dist/openai/react/hooks/createHydrationUtils.js +0 -113
- package/dist/openai/react/hooks/createHydrationUtils.js.map +0 -1
- package/dist/openai/react/hooks/useApp.d.ts +0 -2
- package/dist/openai/react/hooks/useApp.d.ts.map +0 -1
- package/dist/openai/react/hooks/useApp.js +0 -5
- package/dist/openai/react/hooks/useApp.js.map +0 -1
- package/dist/openai/react/hooks/useHostContext.d.ts +0 -2
- package/dist/openai/react/hooks/useHostContext.d.ts.map +0 -1
- package/dist/openai/react/hooks/useHostContext.js.map +0 -1
- package/dist/openai/react/hooks/useToolInfo.d.ts +0 -3
- package/dist/openai/react/hooks/useToolInfo.d.ts.map +0 -1
- package/dist/openai/react/hooks/useToolInfo.js +0 -10
- package/dist/openai/react/hooks/useToolInfo.js.map +0 -1
- package/dist/openai/react/hooks/useToolInput.d.ts +0 -7
- package/dist/openai/react/hooks/useToolInput.d.ts.map +0 -1
- package/dist/openai/react/hooks/useToolInput.js +0 -9
- package/dist/openai/react/hooks/useToolInput.js.map +0 -1
- package/dist/openai/react/hooks/useToolMetadata.d.ts +0 -2
- package/dist/openai/react/hooks/useToolMetadata.d.ts.map +0 -1
- package/dist/openai/react/hooks/useToolMetadata.js +0 -5
- package/dist/openai/react/hooks/useToolMetadata.js.map +0 -1
- package/dist/openai/react/hooks/useToolName.d.ts +0 -7
- package/dist/openai/react/hooks/useToolName.d.ts.map +0 -1
- package/dist/openai/react/hooks/useToolName.js +0 -9
- package/dist/openai/react/hooks/useToolName.js.map +0 -1
- package/dist/react/index.mcp.d.ts +0 -3
- package/dist/react/index.mcp.d.ts.map +0 -1
- package/dist/react/index.mcp.js +0 -3
- package/dist/react/index.mcp.js.map +0 -1
- package/dist/react/index.openai.d.ts +0 -3
- package/dist/react/index.openai.d.ts.map +0 -1
- package/dist/react/index.openai.js +0 -3
- package/dist/react/index.openai.js.map +0 -1
- package/dist/react/missingHook.d.ts +0 -2
- package/dist/react/missingHook.d.ts.map +0 -1
- package/dist/react/missingHook.js +0 -6
- package/dist/react/missingHook.js.map +0 -1
- package/src/mcp/core/McpAppManager.ts +0 -129
- package/src/mcp/link/ToolCallLink.ts +0 -40
- package/src/mcp/link/__tests__/ToolCallLink.test.ts +0 -62
- package/src/mcp/react/hooks/__tests__/createHydrationUtils.test.tsx +0 -1228
- package/src/mcp/react/hooks/__tests__/useApp.test.tsx +0 -46
- package/src/mcp/react/hooks/__tests__/useHostContext.test.tsx +0 -95
- package/src/mcp/react/hooks/__tests__/useToolInfo.test.tsx +0 -53
- package/src/mcp/react/hooks/__tests__/useToolInput.test.tsx +0 -50
- package/src/mcp/react/hooks/__tests__/useToolMetadata.test.tsx +0 -53
- package/src/mcp/react/hooks/__tests__/useToolName.test.tsx +0 -50
- package/src/mcp/react/hooks/useHostContext.ts +0 -14
- package/src/mcp/react/hooks/useToolInfo.ts +0 -13
- package/src/mcp/react/hooks/useToolInput.ts +0 -10
- package/src/mcp/react/hooks/useToolMetadata.ts +0 -5
- package/src/mcp/react/hooks/useToolName.ts +0 -10
- package/src/mcp/react/index.ts +0 -7
- package/src/openai/core/McpAppManager.ts +0 -139
- package/src/openai/link/ToolCallLink.ts +0 -40
- package/src/openai/react/hooks/__tests__/createHydrationUtils.test.tsx +0 -1333
- package/src/openai/react/hooks/__tests__/useToolInfo.test.tsx +0 -92
- package/src/openai/react/hooks/__tests__/useToolInput.test.tsx +0 -85
- package/src/openai/react/hooks/__tests__/useToolMetadata.test.tsx +0 -86
- package/src/openai/react/hooks/__tests__/useToolName.test.tsx +0 -50
- package/src/openai/react/hooks/createHydrationUtils.ts +0 -182
- package/src/openai/react/hooks/useApp.ts +0 -5
- package/src/openai/react/hooks/useToolInfo.ts +0 -13
- package/src/openai/react/hooks/useToolInput.ts +0 -10
- package/src/openai/react/hooks/useToolMetadata.ts +0 -5
- package/src/openai/react/hooks/useToolName.ts +0 -10
- package/src/react/index.mcp.ts +0 -10
- package/src/react/index.openai.ts +0 -10
- package/src/react/missingHook.ts +0 -9
- /package/dist/{mcp/react → react}/hooks/useApp.d.ts +0 -0
- /package/dist/{mcp/react → react}/hooks/useHostContext.d.ts +0 -0
package/src/mcp/index.ts
CHANGED
|
@@ -1,172 +1,59 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import type {
|
|
5
|
-
WatchQueryOptions,
|
|
6
|
-
ObservableQuery,
|
|
7
|
-
OperationVariables,
|
|
8
|
-
} from "@apollo/client";
|
|
9
|
-
import { removeDirectivesFromDocument } from "@apollo/client/utilities/internal";
|
|
10
|
-
import { parse, visit } from "graphql";
|
|
11
|
-
import { equal } from "@wry/equality";
|
|
12
|
-
import { __DEV__ } from "@apollo/client/utilities/environment";
|
|
13
|
-
import type { ApplicationManifest } from "../../types/application-manifest.js";
|
|
14
|
-
import { ToolCallLink } from "../link/ToolCallLink.js";
|
|
15
|
-
import {
|
|
16
|
-
aiClientSymbol,
|
|
17
|
-
cacheAsync,
|
|
18
|
-
getToolNamesFromDocument,
|
|
19
|
-
getVariablesForOperationFromToolInput,
|
|
20
|
-
warnOnVariableMismatch,
|
|
21
|
-
} from "../../utilities/index.js";
|
|
22
|
-
import { McpAppManager } from "./McpAppManager.js";
|
|
23
|
-
import { getVariableNamesFromDocument } from "../../utilities/getVariableNamesFromDocument.js";
|
|
1
|
+
import { AbstractApolloClient } from "../../core/AbstractApolloClient.js";
|
|
2
|
+
import { connectToHost, promiseWithResolvers } from "../../utilities/index.js";
|
|
3
|
+
import type { ApolloMcpServerApps } from "../../core/types.js";
|
|
24
4
|
|
|
25
5
|
export declare namespace ApolloClient {
|
|
26
|
-
export interface Options extends
|
|
27
|
-
link?: BaseApolloClient.Options["link"];
|
|
28
|
-
manifest: ApplicationManifest;
|
|
29
|
-
}
|
|
6
|
+
export interface Options extends AbstractApolloClient.Options {}
|
|
30
7
|
}
|
|
31
8
|
|
|
32
|
-
export class ApolloClient extends
|
|
33
|
-
manifest: ApplicationManifest;
|
|
34
|
-
private readonly appManager: McpAppManager;
|
|
35
|
-
|
|
36
|
-
/** @internal */
|
|
37
|
-
readonly [aiClientSymbol] = true;
|
|
38
|
-
|
|
39
|
-
#toolInput: Record<string, unknown> | undefined;
|
|
40
|
-
|
|
9
|
+
export class ApolloClient extends AbstractApolloClient {
|
|
41
10
|
constructor(options: ApolloClient.Options) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
});
|
|
63
|
-
}).concat(options.documentTransform ?? DocumentTransform.identity()),
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
this.manifest = options.manifest;
|
|
67
|
-
this.appManager = new McpAppManager(this.manifest);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
stop() {
|
|
71
|
-
super.stop();
|
|
72
|
-
this.appManager.close().catch(() => {});
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
get toolInput() {
|
|
76
|
-
return this.#toolInput;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
clearToolInput() {
|
|
80
|
-
this.#toolInput = undefined;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
watchQuery<
|
|
84
|
-
T = any,
|
|
85
|
-
TVariables extends OperationVariables = OperationVariables,
|
|
86
|
-
>(options: WatchQueryOptions<TVariables, T>): ObservableQuery<T, TVariables> {
|
|
87
|
-
if (__DEV__) {
|
|
88
|
-
const toolInput = this.#toolInput;
|
|
89
|
-
|
|
90
|
-
if (toolInput) {
|
|
91
|
-
const toolName = this.appManager.toolName;
|
|
92
|
-
const hasMatchingTool =
|
|
93
|
-
!!toolName && getToolNamesFromDocument(options.query).has(toolName);
|
|
94
|
-
|
|
95
|
-
if (hasMatchingTool) {
|
|
96
|
-
// Clear after first matching comparison so this only fires once and
|
|
97
|
-
// remounting doesn't produce spurious warnings.
|
|
98
|
-
this.#toolInput = undefined;
|
|
99
|
-
|
|
100
|
-
const variableNames = getVariableNamesFromDocument(options.query);
|
|
101
|
-
|
|
102
|
-
if (variableNames.size > 0) {
|
|
103
|
-
const { variables } = options;
|
|
104
|
-
|
|
105
|
-
const toolInputVariables = Object.entries(toolInput).filter(
|
|
106
|
-
([key]) => variableNames.has(key)
|
|
107
|
-
);
|
|
108
|
-
|
|
109
|
-
const hasToolInputMismatch = toolInputVariables.some(
|
|
110
|
-
([key, value]) => !equal(value, variables?.[key])
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
if (hasToolInputMismatch) {
|
|
114
|
-
warnOnVariableMismatch(
|
|
115
|
-
options.query,
|
|
116
|
-
Object.fromEntries(toolInputVariables),
|
|
117
|
-
variables
|
|
118
|
-
);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return super.watchQuery(options);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
connect = cacheAsync(async () => {
|
|
129
|
-
const { prefetch, result, toolName, args } =
|
|
130
|
-
await this.appManager.connect();
|
|
131
|
-
|
|
132
|
-
this.#toolInput = args;
|
|
133
|
-
|
|
134
|
-
this.manifest.operations.forEach((operation) => {
|
|
135
|
-
if (operation.prefetchID && prefetch?.[operation.prefetchID]) {
|
|
136
|
-
this.writeQuery({
|
|
137
|
-
query: parse(operation.body),
|
|
138
|
-
data: prefetch[operation.prefetchID].data,
|
|
11
|
+
super(options, async (app) => {
|
|
12
|
+
const toolResult =
|
|
13
|
+
promiseWithResolvers<ApolloMcpServerApps.CallToolResult>();
|
|
14
|
+
|
|
15
|
+
app.ontoolresult = (params) => {
|
|
16
|
+
toolResult.resolve(
|
|
17
|
+
params as unknown as ApolloMcpServerApps.CallToolResult
|
|
18
|
+
);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
await connectToHost(app);
|
|
22
|
+
|
|
23
|
+
// After a page refresh, OpenAI does not re-send `ui/notifications/tool-result`.
|
|
24
|
+
// Instead, the tool result is available immediately via `window.openai.toolOutput`.
|
|
25
|
+
// If it's already set, resolve the promise now rather than waiting for the
|
|
26
|
+
// notification that will never arrive.
|
|
27
|
+
if (window.openai.toolOutput !== null) {
|
|
28
|
+
toolResult.resolve({
|
|
29
|
+
structuredContent: window.openai.toolOutput,
|
|
30
|
+
content: [],
|
|
139
31
|
});
|
|
140
32
|
}
|
|
141
33
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
34
|
+
const { structuredContent } = await toolResult.promise;
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
structuredContent,
|
|
38
|
+
// OpenAI is not consistent about sending `ui/notifications/tool-input`.
|
|
39
|
+
// Sometimes it doesn't send at all, other times it sends more than once
|
|
40
|
+
// before we get the tool result (which should always happen and at most
|
|
41
|
+
// once according to the spec). Rather than relying on the
|
|
42
|
+
// `ui/notifications/tool-input` notification to set the tool input value,
|
|
43
|
+
// we read from `window.openai.toolInput so that we have the most recent
|
|
44
|
+
// set value.
|
|
45
|
+
//
|
|
46
|
+
// When OpenAI fixes this issue and sends `ui/notifications/tool-input`
|
|
47
|
+
// consistently according to the MCP Apps specification, this can be
|
|
48
|
+
// reverted to use the `app.ontoolinput` callback.
|
|
49
|
+
toolInput: window.openai.toolInput,
|
|
50
|
+
|
|
51
|
+
// OpenAI doesn't provide access to `_meta` from ui/notifications/tool-result,
|
|
52
|
+
// so we need to use window.openai.toolResponseMetadata directly
|
|
53
|
+
_meta: window.openai.toolResponseMetadata as
|
|
54
|
+
| ApolloMcpServerApps.Meta
|
|
55
|
+
| undefined,
|
|
56
|
+
};
|
|
149
57
|
});
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function validateTerminatingLink(link: ApolloLink) {
|
|
154
|
-
let terminatingLink = link;
|
|
155
|
-
|
|
156
|
-
while (terminatingLink.right) {
|
|
157
|
-
terminatingLink = terminatingLink.right;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (
|
|
161
|
-
!isNamedLink(terminatingLink) ||
|
|
162
|
-
terminatingLink.name !== "ToolCallLink"
|
|
163
|
-
) {
|
|
164
|
-
throw new Error(
|
|
165
|
-
"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."
|
|
166
|
-
);
|
|
167
58
|
}
|
|
168
59
|
}
|
|
169
|
-
|
|
170
|
-
function isNamedLink(link: ApolloLink): link is ApolloLink & { name: string } {
|
|
171
|
-
return "name" in link;
|
|
172
|
-
}
|