@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.
Files changed (272) hide show
  1. package/CHANGELOG.md +100 -0
  2. package/CONTRIBUTING.md +195 -0
  3. package/README.md +74 -0
  4. package/dist/core/AbstractApolloClient.d.ts +33 -0
  5. package/dist/core/AbstractApolloClient.d.ts.map +1 -0
  6. package/dist/core/AbstractApolloClient.js +129 -0
  7. package/dist/core/AbstractApolloClient.js.map +1 -0
  8. package/dist/core/ApolloClient.d.ts +3 -7
  9. package/dist/core/ApolloClient.d.ts.map +1 -1
  10. package/dist/core/ApolloClient.js +5 -4
  11. package/dist/core/ApolloClient.js.map +1 -1
  12. package/dist/core/McpAppManager.d.ts +42 -0
  13. package/dist/core/McpAppManager.d.ts.map +1 -0
  14. package/dist/core/McpAppManager.js +56 -0
  15. package/dist/core/McpAppManager.js.map +1 -0
  16. package/dist/core/typeRegistration.d.ts +0 -14
  17. package/dist/core/typeRegistration.d.ts.map +1 -1
  18. package/dist/core/typeRegistration.js.map +1 -1
  19. package/dist/core/types.d.ts +2 -1
  20. package/dist/core/types.d.ts.map +1 -1
  21. package/dist/core/types.js.map +1 -1
  22. package/dist/index.d.ts +1 -1
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js.map +1 -1
  25. package/dist/index.mcp.d.ts +0 -1
  26. package/dist/index.mcp.d.ts.map +1 -1
  27. package/dist/index.mcp.js +0 -1
  28. package/dist/index.mcp.js.map +1 -1
  29. package/dist/index.openai.d.ts +0 -1
  30. package/dist/index.openai.d.ts.map +1 -1
  31. package/dist/index.openai.js +0 -1
  32. package/dist/index.openai.js.map +1 -1
  33. package/dist/link/ToolCallLink.d.ts +6 -1
  34. package/dist/link/ToolCallLink.d.ts.map +1 -1
  35. package/dist/link/ToolCallLink.js +17 -4
  36. package/dist/link/ToolCallLink.js.map +1 -1
  37. package/dist/link/ToolHydrationLink.d.ts +21 -0
  38. package/dist/link/ToolHydrationLink.d.ts.map +1 -0
  39. package/dist/link/ToolHydrationLink.js +57 -0
  40. package/dist/link/ToolHydrationLink.js.map +1 -0
  41. package/dist/mcp/core/ApolloClient.d.ts +3 -20
  42. package/dist/mcp/core/ApolloClient.d.ts.map +1 -1
  43. package/dist/mcp/core/ApolloClient.js +20 -98
  44. package/dist/mcp/core/ApolloClient.js.map +1 -1
  45. package/dist/mcp/index.d.ts +0 -1
  46. package/dist/mcp/index.d.ts.map +1 -1
  47. package/dist/mcp/index.js +0 -1
  48. package/dist/mcp/index.js.map +1 -1
  49. package/dist/openai/core/ApolloClient.d.ts +3 -20
  50. package/dist/openai/core/ApolloClient.d.ts.map +1 -1
  51. package/dist/openai/core/ApolloClient.js +36 -98
  52. package/dist/openai/core/ApolloClient.js.map +1 -1
  53. package/dist/openai/index.d.ts +0 -1
  54. package/dist/openai/index.d.ts.map +1 -1
  55. package/dist/openai/index.js +0 -1
  56. package/dist/openai/index.js.map +1 -1
  57. package/dist/openai/react/index.d.ts +0 -7
  58. package/dist/openai/react/index.d.ts.map +1 -1
  59. package/dist/openai/react/index.js +0 -7
  60. package/dist/openai/react/index.js.map +1 -1
  61. package/dist/react/ApolloProvider.d.ts.map +1 -1
  62. package/dist/react/ApolloProvider.js +1 -1
  63. package/dist/react/ApolloProvider.js.map +1 -1
  64. package/dist/{mcp/react/hooks → react}/createHydrationUtils.d.ts +1 -1
  65. package/dist/react/createHydrationUtils.d.ts.map +1 -0
  66. package/dist/{mcp/react/hooks → react}/createHydrationUtils.js +7 -9
  67. package/dist/react/createHydrationUtils.js.map +1 -0
  68. package/dist/react/hooks/internal/useApolloClient.d.ts +3 -0
  69. package/dist/react/hooks/internal/useApolloClient.d.ts.map +1 -0
  70. package/dist/{mcp/react/hooks → react/hooks/internal}/useApolloClient.js +3 -3
  71. package/dist/react/hooks/internal/useApolloClient.js.map +1 -0
  72. package/dist/react/hooks/useApp.d.ts.map +1 -0
  73. package/dist/react/hooks/useApp.js +5 -0
  74. package/dist/react/hooks/useApp.js.map +1 -0
  75. package/dist/react/hooks/useHostContext.d.ts.map +1 -0
  76. package/dist/{openai/react → react}/hooks/useHostContext.js +1 -1
  77. package/dist/react/hooks/useHostContext.js.map +1 -0
  78. package/dist/react/hooks/useToolInfo.d.ts +3 -0
  79. package/dist/react/hooks/useToolInfo.d.ts.map +1 -0
  80. package/dist/react/hooks/useToolInfo.js +5 -0
  81. package/dist/react/hooks/useToolInfo.js.map +1 -0
  82. package/dist/react/hooks/useToolMetadata.d.ts +2 -0
  83. package/dist/react/hooks/useToolMetadata.d.ts.map +1 -0
  84. package/dist/react/hooks/useToolMetadata.js +5 -0
  85. package/dist/react/hooks/useToolMetadata.js.map +1 -0
  86. package/dist/react/index.d.ts +5 -16
  87. package/dist/react/index.d.ts.map +1 -1
  88. package/dist/react/index.js +5 -19
  89. package/dist/react/index.js.map +1 -1
  90. package/dist/types/application-manifest.d.ts +1 -0
  91. package/dist/types/application-manifest.d.ts.map +1 -1
  92. package/dist/types/application-manifest.js.map +1 -1
  93. package/dist/utilities/connectToHost.d.ts +3 -0
  94. package/dist/utilities/connectToHost.d.ts.map +1 -0
  95. package/dist/utilities/connectToHost.js +11 -0
  96. package/dist/utilities/connectToHost.js.map +1 -0
  97. package/dist/utilities/index.d.ts +1 -0
  98. package/dist/utilities/index.d.ts.map +1 -1
  99. package/dist/utilities/index.js +1 -0
  100. package/dist/utilities/index.js.map +1 -1
  101. package/dist/vite/apolloClientAiApps.d.ts.map +1 -1
  102. package/dist/vite/apolloClientAiApps.js +2 -0
  103. package/dist/vite/apolloClientAiApps.js.map +1 -1
  104. package/package.json +5 -22
  105. package/src/core/AbstractApolloClient.ts +217 -0
  106. package/src/core/ApolloClient.ts +8 -10
  107. package/src/core/McpAppManager.ts +106 -0
  108. package/src/core/typeRegistration.ts +0 -15
  109. package/src/core/types.ts +2 -1
  110. package/src/index.mcp.ts +0 -1
  111. package/src/index.openai.ts +0 -1
  112. package/src/index.ts +1 -6
  113. package/src/link/ToolCallLink.ts +27 -5
  114. package/src/link/ToolHydrationLink.ts +90 -0
  115. package/src/link/__tests__/ToolCallLink.test.ts +99 -0
  116. package/src/mcp/core/ApolloClient.ts +32 -165
  117. package/src/mcp/core/__tests__/ApolloClient.test.ts +571 -71
  118. package/src/mcp/index.ts +0 -1
  119. package/src/openai/core/ApolloClient.ts +48 -161
  120. package/src/openai/core/__tests__/ApolloClient.test.ts +916 -118
  121. package/src/openai/index.ts +0 -1
  122. package/src/openai/react/index.ts +0 -7
  123. package/src/react/ApolloProvider.tsx +1 -6
  124. package/src/react/__tests__/ApolloProvider/mcp.test.tsx +66 -29
  125. package/src/react/__tests__/ApolloProvider/openai.test.tsx +16 -41
  126. package/src/react/__tests__/createHydrationUtils.test.tsx +1260 -0
  127. package/src/{mcp/react/hooks → react}/createHydrationUtils.ts +7 -10
  128. package/src/react/hooks/__tests__/useApp.test.tsx +46 -0
  129. package/src/react/hooks/__tests__/useHostContext.test.tsx +99 -0
  130. package/src/react/hooks/__tests__/useToolInfo.test.tsx +98 -0
  131. package/src/react/hooks/__tests__/useToolMetadata.test.tsx +58 -0
  132. package/src/{mcp/react/hooks → react/hooks/internal}/useApolloClient.ts +3 -3
  133. package/src/{mcp/react → react}/hooks/useApp.ts +1 -1
  134. package/src/{openai/react → react}/hooks/useHostContext.ts +1 -1
  135. package/src/react/hooks/useToolInfo.ts +6 -0
  136. package/src/react/hooks/useToolMetadata.ts +5 -0
  137. package/src/react/index.ts +5 -36
  138. package/src/testing/internal/graphql/parseManifestOperation.ts +87 -0
  139. package/src/testing/internal/index.ts +3 -0
  140. package/src/testing/internal/matchers/index.ts +1 -0
  141. package/src/testing/internal/matchers/toEmitAnything.ts +43 -0
  142. package/src/testing/internal/matchers/types.ts +1 -0
  143. package/src/testing/internal/mcp/mockMcpHost.ts +25 -4
  144. package/src/testing/internal/tests/eachHostEnv.ts +22 -0
  145. package/src/testing/internal/utilities/createHostEnv.ts +117 -0
  146. package/src/types/application-manifest.ts +1 -0
  147. package/src/utilities/connectToHost.ts +13 -0
  148. package/src/utilities/index.ts +1 -0
  149. package/src/vite/__tests__/apolloClientAiApps.test.ts +56 -0
  150. package/src/vite/apolloClientAiApps.ts +5 -0
  151. package/tsconfig.vite.json +1 -1
  152. package/vitest.config.ts +13 -0
  153. package/dist/mcp/core/McpAppManager.d.ts +0 -30
  154. package/dist/mcp/core/McpAppManager.d.ts.map +0 -1
  155. package/dist/mcp/core/McpAppManager.js +0 -82
  156. package/dist/mcp/core/McpAppManager.js.map +0 -1
  157. package/dist/mcp/link/ToolCallLink.d.ts +0 -28
  158. package/dist/mcp/link/ToolCallLink.d.ts.map +0 -1
  159. package/dist/mcp/link/ToolCallLink.js +0 -35
  160. package/dist/mcp/link/ToolCallLink.js.map +0 -1
  161. package/dist/mcp/react/hooks/createHydrationUtils.d.ts.map +0 -1
  162. package/dist/mcp/react/hooks/createHydrationUtils.js.map +0 -1
  163. package/dist/mcp/react/hooks/useApolloClient.d.ts +0 -3
  164. package/dist/mcp/react/hooks/useApolloClient.d.ts.map +0 -1
  165. package/dist/mcp/react/hooks/useApolloClient.js.map +0 -1
  166. package/dist/mcp/react/hooks/useApp.d.ts.map +0 -1
  167. package/dist/mcp/react/hooks/useApp.js +0 -5
  168. package/dist/mcp/react/hooks/useApp.js.map +0 -1
  169. package/dist/mcp/react/hooks/useHostContext.d.ts.map +0 -1
  170. package/dist/mcp/react/hooks/useHostContext.js +0 -7
  171. package/dist/mcp/react/hooks/useHostContext.js.map +0 -1
  172. package/dist/mcp/react/hooks/useToolInfo.d.ts +0 -3
  173. package/dist/mcp/react/hooks/useToolInfo.d.ts.map +0 -1
  174. package/dist/mcp/react/hooks/useToolInfo.js +0 -10
  175. package/dist/mcp/react/hooks/useToolInfo.js.map +0 -1
  176. package/dist/mcp/react/hooks/useToolInput.d.ts +0 -7
  177. package/dist/mcp/react/hooks/useToolInput.d.ts.map +0 -1
  178. package/dist/mcp/react/hooks/useToolInput.js +0 -9
  179. package/dist/mcp/react/hooks/useToolInput.js.map +0 -1
  180. package/dist/mcp/react/hooks/useToolMetadata.d.ts +0 -2
  181. package/dist/mcp/react/hooks/useToolMetadata.d.ts.map +0 -1
  182. package/dist/mcp/react/hooks/useToolMetadata.js +0 -5
  183. package/dist/mcp/react/hooks/useToolMetadata.js.map +0 -1
  184. package/dist/mcp/react/hooks/useToolName.d.ts +0 -7
  185. package/dist/mcp/react/hooks/useToolName.d.ts.map +0 -1
  186. package/dist/mcp/react/hooks/useToolName.js +0 -9
  187. package/dist/mcp/react/hooks/useToolName.js.map +0 -1
  188. package/dist/mcp/react/index.d.ts +0 -8
  189. package/dist/mcp/react/index.d.ts.map +0 -1
  190. package/dist/mcp/react/index.js +0 -8
  191. package/dist/mcp/react/index.js.map +0 -1
  192. package/dist/openai/core/McpAppManager.d.ts +0 -29
  193. package/dist/openai/core/McpAppManager.d.ts.map +0 -1
  194. package/dist/openai/core/McpAppManager.js +0 -91
  195. package/dist/openai/core/McpAppManager.js.map +0 -1
  196. package/dist/openai/link/ToolCallLink.d.ts +0 -28
  197. package/dist/openai/link/ToolCallLink.d.ts.map +0 -1
  198. package/dist/openai/link/ToolCallLink.js +0 -35
  199. package/dist/openai/link/ToolCallLink.js.map +0 -1
  200. package/dist/openai/react/hooks/createHydrationUtils.d.ts +0 -15
  201. package/dist/openai/react/hooks/createHydrationUtils.d.ts.map +0 -1
  202. package/dist/openai/react/hooks/createHydrationUtils.js +0 -113
  203. package/dist/openai/react/hooks/createHydrationUtils.js.map +0 -1
  204. package/dist/openai/react/hooks/useApp.d.ts +0 -2
  205. package/dist/openai/react/hooks/useApp.d.ts.map +0 -1
  206. package/dist/openai/react/hooks/useApp.js +0 -5
  207. package/dist/openai/react/hooks/useApp.js.map +0 -1
  208. package/dist/openai/react/hooks/useHostContext.d.ts +0 -2
  209. package/dist/openai/react/hooks/useHostContext.d.ts.map +0 -1
  210. package/dist/openai/react/hooks/useHostContext.js.map +0 -1
  211. package/dist/openai/react/hooks/useToolInfo.d.ts +0 -3
  212. package/dist/openai/react/hooks/useToolInfo.d.ts.map +0 -1
  213. package/dist/openai/react/hooks/useToolInfo.js +0 -10
  214. package/dist/openai/react/hooks/useToolInfo.js.map +0 -1
  215. package/dist/openai/react/hooks/useToolInput.d.ts +0 -7
  216. package/dist/openai/react/hooks/useToolInput.d.ts.map +0 -1
  217. package/dist/openai/react/hooks/useToolInput.js +0 -9
  218. package/dist/openai/react/hooks/useToolInput.js.map +0 -1
  219. package/dist/openai/react/hooks/useToolMetadata.d.ts +0 -2
  220. package/dist/openai/react/hooks/useToolMetadata.d.ts.map +0 -1
  221. package/dist/openai/react/hooks/useToolMetadata.js +0 -5
  222. package/dist/openai/react/hooks/useToolMetadata.js.map +0 -1
  223. package/dist/openai/react/hooks/useToolName.d.ts +0 -7
  224. package/dist/openai/react/hooks/useToolName.d.ts.map +0 -1
  225. package/dist/openai/react/hooks/useToolName.js +0 -9
  226. package/dist/openai/react/hooks/useToolName.js.map +0 -1
  227. package/dist/react/index.mcp.d.ts +0 -3
  228. package/dist/react/index.mcp.d.ts.map +0 -1
  229. package/dist/react/index.mcp.js +0 -3
  230. package/dist/react/index.mcp.js.map +0 -1
  231. package/dist/react/index.openai.d.ts +0 -3
  232. package/dist/react/index.openai.d.ts.map +0 -1
  233. package/dist/react/index.openai.js +0 -3
  234. package/dist/react/index.openai.js.map +0 -1
  235. package/dist/react/missingHook.d.ts +0 -2
  236. package/dist/react/missingHook.d.ts.map +0 -1
  237. package/dist/react/missingHook.js +0 -6
  238. package/dist/react/missingHook.js.map +0 -1
  239. package/src/mcp/core/McpAppManager.ts +0 -129
  240. package/src/mcp/link/ToolCallLink.ts +0 -40
  241. package/src/mcp/link/__tests__/ToolCallLink.test.ts +0 -62
  242. package/src/mcp/react/hooks/__tests__/createHydrationUtils.test.tsx +0 -1228
  243. package/src/mcp/react/hooks/__tests__/useApp.test.tsx +0 -46
  244. package/src/mcp/react/hooks/__tests__/useHostContext.test.tsx +0 -95
  245. package/src/mcp/react/hooks/__tests__/useToolInfo.test.tsx +0 -53
  246. package/src/mcp/react/hooks/__tests__/useToolInput.test.tsx +0 -50
  247. package/src/mcp/react/hooks/__tests__/useToolMetadata.test.tsx +0 -53
  248. package/src/mcp/react/hooks/__tests__/useToolName.test.tsx +0 -50
  249. package/src/mcp/react/hooks/useHostContext.ts +0 -14
  250. package/src/mcp/react/hooks/useToolInfo.ts +0 -13
  251. package/src/mcp/react/hooks/useToolInput.ts +0 -10
  252. package/src/mcp/react/hooks/useToolMetadata.ts +0 -5
  253. package/src/mcp/react/hooks/useToolName.ts +0 -10
  254. package/src/mcp/react/index.ts +0 -7
  255. package/src/openai/core/McpAppManager.ts +0 -139
  256. package/src/openai/link/ToolCallLink.ts +0 -40
  257. package/src/openai/react/hooks/__tests__/createHydrationUtils.test.tsx +0 -1333
  258. package/src/openai/react/hooks/__tests__/useToolInfo.test.tsx +0 -92
  259. package/src/openai/react/hooks/__tests__/useToolInput.test.tsx +0 -85
  260. package/src/openai/react/hooks/__tests__/useToolMetadata.test.tsx +0 -86
  261. package/src/openai/react/hooks/__tests__/useToolName.test.tsx +0 -50
  262. package/src/openai/react/hooks/createHydrationUtils.ts +0 -182
  263. package/src/openai/react/hooks/useApp.ts +0 -5
  264. package/src/openai/react/hooks/useToolInfo.ts +0 -13
  265. package/src/openai/react/hooks/useToolInput.ts +0 -10
  266. package/src/openai/react/hooks/useToolMetadata.ts +0 -5
  267. package/src/openai/react/hooks/useToolName.ts +0 -10
  268. package/src/react/index.mcp.ts +0 -10
  269. package/src/react/index.openai.ts +0 -10
  270. package/src/react/missingHook.ts +0 -9
  271. /package/dist/{mcp/react → react}/hooks/useApp.d.ts +0 -0
  272. /package/dist/{mcp/react → react}/hooks/useHostContext.d.ts +0 -0
@@ -1,3 +0,0 @@
1
- export * from "./index.js";
2
- export { useApp, useHostContext, useToolInput, useToolInfo, createHydrationUtils, useToolMetadata, useToolName, } from "../openai/react/index.js";
3
- //# sourceMappingURL=index.openai.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.openai.js","sourceRoot":"","sources":["../../src/react/index.openai.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,OAAO,EACL,MAAM,EACN,cAAc,EACd,YAAY,EACZ,WAAW,EACX,oBAAoB,EACpB,eAAe,EACf,WAAW,GACZ,MAAM,0BAA0B,CAAC","sourcesContent":["export * from \"./index.js\";\nexport {\n useApp,\n useHostContext,\n useToolInput,\n useToolInfo,\n createHydrationUtils,\n useToolMetadata,\n useToolName,\n} from \"../openai/react/index.js\";\n"]}
@@ -1,2 +0,0 @@
1
- export declare function missingHook<HookFn extends (...args: any[]) => any>(name: string): HookFn;
2
- //# sourceMappingURL=missingHook.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"missingHook.d.ts","sourceRoot":"","sources":["../../src/react/missingHook.ts"],"names":[],"mappings":"AAAA,wBAAgB,WAAW,CAAC,MAAM,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,EAChE,IAAI,EAAE,MAAM,GAMK,MAAM,CACxB"}
@@ -1,6 +0,0 @@
1
- export function missingHook(name) {
2
- return (() => {
3
- throw new Error(`Cannot use the '${name}' hook without export conditions. Please set export conditions or import from the \`/openai\` or \`/mcp\` subpath directly.`);
4
- });
5
- }
6
- //# sourceMappingURL=missingHook.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"missingHook.js","sourceRoot":"","sources":["../../src/react/missingHook.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,WAAW,CACzB,IAAY;IAEZ,OAAO,CAAC,GAAG,EAAE;QACX,MAAM,IAAI,KAAK,CACb,mBAAmB,IAAI,6HAA6H,CACrJ,CAAC;IACJ,CAAC,CAAsB,CAAC;AAC1B,CAAC","sourcesContent":["export function missingHook<HookFn extends (...args: any[]) => any>(\n name: string\n) {\n return (() => {\n throw new Error(\n `Cannot use the '${name}' hook without export conditions. Please set export conditions or import from the \\`/openai\\` or \\`/mcp\\` subpath directly.`\n );\n }) as unknown as HookFn;\n}\n"]}
@@ -1,129 +0,0 @@
1
- import {
2
- App,
3
- PostMessageTransport,
4
- type McpUiHostContextChangedNotification,
5
- } from "@modelcontextprotocol/ext-apps";
6
- import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
7
- import type { ApplicationManifest } from "../../types/application-manifest";
8
- import type { FormattedExecutionResult } from "graphql";
9
- import type { DocumentNode, OperationVariables } from "@apollo/client";
10
- import { print } from "@apollo/client/utilities";
11
- import { cacheAsync, promiseWithResolvers } from "../../utilities";
12
- import type { ApolloMcpServerApps } from "../../core/types";
13
-
14
- type ExecuteQueryCallToolResult = Omit<CallToolResult, "structuredContent"> & {
15
- structuredContent: FormattedExecutionResult;
16
- };
17
-
18
- /** @internal */
19
- export class McpAppManager {
20
- readonly app: App;
21
-
22
- #toolName: string | undefined;
23
- #toolMetadata: ApolloMcpServerApps.CallToolResult["_meta"] | undefined;
24
- #toolInput: Record<string, unknown> | undefined;
25
-
26
- #hostContextCallbacks = new Set<
27
- (params: McpUiHostContextChangedNotification["params"]) => void
28
- >();
29
-
30
- constructor(manifest: ApplicationManifest) {
31
- this.app = new App({ name: manifest.name, version: manifest.appVersion });
32
- }
33
-
34
- get toolName() {
35
- return this.#toolName;
36
- }
37
-
38
- get toolMetadata() {
39
- return this.#toolMetadata;
40
- }
41
-
42
- get toolInput() {
43
- return this.#toolInput;
44
- }
45
-
46
- onHostContextChanged(
47
- cb: (params: McpUiHostContextChangedNotification["params"]) => void
48
- ) {
49
- this.#hostContextCallbacks.add(cb);
50
-
51
- return () => {
52
- this.#hostContextCallbacks.delete(cb);
53
- };
54
- }
55
-
56
- connect = cacheAsync(async () => {
57
- let toolResult = promiseWithResolvers<ApolloMcpServerApps.CallToolResult>();
58
- let toolInput = promiseWithResolvers<Parameters<App["ontoolinput"]>[0]>();
59
-
60
- this.app.ontoolresult = (params) => {
61
- toolResult.resolve(
62
- params as unknown as ApolloMcpServerApps.CallToolResult
63
- );
64
- };
65
-
66
- this.app.ontoolinput = (params) => {
67
- toolInput.resolve(params);
68
- };
69
-
70
- this.app.onhostcontextchanged = (params) => {
71
- this.#hostContextCallbacks.forEach((cb) => cb(params));
72
- };
73
-
74
- await this.connectToHost();
75
-
76
- const { structuredContent, _meta } = await toolResult.promise;
77
- const { arguments: args } = await toolInput.promise;
78
-
79
- // Some hosts do not provide toolInfo in the ui/initialize response, so we
80
- // fallback to `_meta.toolName` provided by Apollo MCP server if the value
81
- // is not available.
82
- this.#toolName =
83
- this.app.getHostContext()?.toolInfo?.tool.name ??
84
- _meta?.toolName ??
85
- // Some hosts do not forward `_meta` nor do they provide `toolInfo`. Our
86
- // MCP server provides `toolName` in `structuredContent` as a workaround
87
- // that we can use if all else fails
88
- structuredContent.toolName;
89
- this.#toolMetadata = _meta;
90
- this.#toolInput = args;
91
-
92
- return {
93
- ...structuredContent,
94
- toolName: this.toolName,
95
- args,
96
- };
97
- });
98
-
99
- close() {
100
- return this.app.close();
101
- }
102
-
103
- async executeQuery({
104
- query,
105
- variables,
106
- }: {
107
- query: DocumentNode;
108
- variables: OperationVariables | undefined;
109
- }) {
110
- const result = (await this.app.callServerTool({
111
- name: "execute",
112
- arguments: { query: print(query), variables },
113
- })) as ExecuteQueryCallToolResult;
114
-
115
- return result.structuredContent;
116
- }
117
-
118
- private async connectToHost() {
119
- try {
120
- return await this.app.connect(
121
- new PostMessageTransport(window.parent, window.parent)
122
- );
123
- } catch (e) {
124
- const error = e instanceof Error ? e : new Error("Failed to connect");
125
-
126
- throw error;
127
- }
128
- }
129
- }
@@ -1,40 +0,0 @@
1
- import { ApolloLink, Observable } from "@apollo/client";
2
- import { from } from "rxjs";
3
- import type { ApolloClient as McpApolloClient } from "../core/ApolloClient";
4
-
5
- /**
6
- * A terminating link that sends a GraphQL request through an agent tool call.
7
- * When providing a custom link chain to `ApolloClient`, `ApolloClient` will
8
- * validate that the terminating link is an instance of this link.
9
- *
10
- * @example Providing a custom link chain
11
- *
12
- * ```ts
13
- * import { ApolloLink } from "@apollo/client";
14
- * import { ApolloClient, ToolCallLink } from "@apollo/client-ai-apps";
15
- *
16
- * const link = ApolloLink.from([
17
- * ...otherLinks,
18
- * new ToolCallLink()
19
- * ]);
20
- *
21
- * const client = new ApolloClient({
22
- * link,
23
- * // ...
24
- * });
25
- * ```
26
- */
27
- export class ToolCallLink extends ApolloLink {
28
- readonly name = "ToolCallLink";
29
-
30
- request(operation: ApolloLink.Operation): Observable<ApolloLink.Result> {
31
- const client = operation.client as McpApolloClient;
32
-
33
- return from(
34
- client["appManager"].executeQuery({
35
- query: operation.query,
36
- variables: operation.variables,
37
- })
38
- );
39
- }
40
- }
@@ -1,62 +0,0 @@
1
- import { expect, test } from "vitest";
2
- import { gql, InMemoryCache } from "@apollo/client";
3
- import { execute } from "@apollo/client/link";
4
- import { ApolloClient } from "../../core/ApolloClient.js";
5
- import {
6
- minimalHostContextWithToolName,
7
- mockApplicationManifest,
8
- mockMcpHost,
9
- ObservableStream,
10
- spyOnConsole,
11
- } from "../../../testing/internal/index.js";
12
- import { ToolCallLink } from "../ToolCallLink.js";
13
-
14
- test("delegates query execution to MCP host", async () => {
15
- using _ = spyOnConsole("debug");
16
- const query = gql`
17
- query GreetingQuery {
18
- greeting
19
- }
20
- `;
21
-
22
- const client = new ApolloClient({
23
- cache: new InMemoryCache(),
24
- manifest: mockApplicationManifest(),
25
- });
26
-
27
- using host = await mockMcpHost({
28
- hostContext: minimalHostContextWithToolName("GetProduct"),
29
- });
30
- host.onCleanup(() => client.stop());
31
-
32
- host.sendToolInput({ arguments: {} });
33
- host.sendToolResult({
34
- _meta: { toolName: "GetProduct" },
35
- content: [],
36
- structuredContent: {
37
- result: {
38
- data: {
39
- product: null,
40
- },
41
- },
42
- },
43
- });
44
-
45
- host.mockToolCall("execute", () => ({
46
- content: [],
47
- structuredContent: {
48
- data: { greeting: "Hello, world" },
49
- },
50
- }));
51
-
52
- await client.connect();
53
-
54
- const observable = execute(new ToolCallLink(), { query }, { client });
55
- const stream = new ObservableStream(observable);
56
-
57
- await expect(stream).toEmitValue({
58
- data: { greeting: "Hello, world" },
59
- });
60
-
61
- await expect(stream).toComplete();
62
- });