@google/adk 1.3.0 → 1.5.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 (248) hide show
  1. package/dist/cjs/a2a/agent_to_a2a.js +18 -2
  2. package/dist/cjs/a2a/event_converter_utils.js +8 -4
  3. package/dist/cjs/a2a/metadata_converter_utils.js +1 -1
  4. package/dist/cjs/agents/active_streaming_tool.js +6 -1
  5. package/dist/cjs/agents/base_agent.js +39 -0
  6. package/dist/cjs/agents/context.js +1 -11
  7. package/dist/cjs/agents/functions.js +39 -35
  8. package/dist/cjs/agents/instructions.js +23 -5
  9. package/dist/cjs/agents/invocation_context.js +2 -5
  10. package/dist/cjs/agents/llm_agent.js +10 -7
  11. package/dist/cjs/agents/loop_agent.js +15 -3
  12. package/dist/cjs/agents/processors/code_execution_request_processor.js +0 -3
  13. package/dist/cjs/agents/processors/content_processor_utils.js +18 -4
  14. package/dist/cjs/agents/processors/context_compactor_request_processor.js +4 -2
  15. package/dist/cjs/agents/processors/interactions_request_processor.js +63 -0
  16. package/dist/cjs/agents/routed_agent.js +31 -0
  17. package/dist/cjs/agents/run_config.js +3 -2
  18. package/dist/cjs/apps/app.js +79 -0
  19. package/dist/cjs/apps/resumability_config.js +44 -0
  20. package/dist/cjs/artifacts/file_artifact_service.js +14 -1
  21. package/dist/cjs/artifacts/gcs_artifact_service.js +57 -15
  22. package/dist/cjs/artifacts/in_memory_artifact_service.js +12 -1
  23. package/dist/cjs/artifacts/scoped_artifact_service.js +101 -0
  24. package/dist/cjs/artifacts/session_artifact_service.js +27 -0
  25. package/dist/cjs/auth/oauth2/oauth2_discovery.js +17 -5
  26. package/dist/cjs/auth/oauth2/oauth2_utils.js +12 -1
  27. package/dist/cjs/common.js +76 -0
  28. package/dist/cjs/context/agent_controlled_context_compactor.js +109 -0
  29. package/dist/cjs/context/anchored_context_compactor.js +127 -0
  30. package/dist/cjs/context/compaction_utils.js +52 -0
  31. package/dist/cjs/context/token_based_context_compactor.js +52 -33
  32. package/dist/cjs/context/trajectory_thought_pruning_compactor.js +71 -0
  33. package/dist/cjs/events/compacted_event.js +9 -3
  34. package/dist/cjs/events/event.js +52 -0
  35. package/dist/cjs/index.js +4 -0
  36. package/dist/cjs/integrations/agent_registry/agent_registry_mcp_toolset.js +9 -1
  37. package/dist/cjs/models/google_llm.js +25 -10
  38. package/dist/cjs/models/interactions_utils.js +732 -0
  39. package/dist/cjs/plugins/base_plugin.js +1 -0
  40. package/dist/cjs/plugins/global_instruction_plugin.js +97 -0
  41. package/dist/cjs/runner/in_memory_runner.js +13 -2
  42. package/dist/cjs/runner/runner.js +143 -83
  43. package/dist/cjs/sessions/base_session_service.js +6 -1
  44. package/dist/cjs/sessions/database_session_service.js +24 -7
  45. package/dist/cjs/sessions/vertex_ai_session_service.js +19 -4
  46. package/dist/cjs/skills/loader.js +2 -2
  47. package/dist/cjs/tools/base_toolset.js +1 -1
  48. package/dist/cjs/tools/consolidate_context_tool.js +79 -0
  49. package/dist/cjs/tools/enterprise_web_search_tool.js +81 -0
  50. package/dist/cjs/tools/example_tool.js +68 -0
  51. package/dist/cjs/tools/forwarding_artifact_service.js +16 -28
  52. package/dist/cjs/tools/load_web_page.js +250 -0
  53. package/dist/cjs/tools/mcp/load_mcp_resource_tool.js +140 -0
  54. package/dist/cjs/tools/mcp/mcp_toolset.js +63 -0
  55. package/dist/cjs/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +15 -22
  56. package/dist/cjs/tools/skill/run_skill_inline_script_tool.js +54 -4
  57. package/dist/cjs/tools/skill/skill_toolset.js +4 -2
  58. package/dist/cjs/utils/async_hooks_shim.js +52 -0
  59. package/dist/cjs/utils/branch_trie.js +41 -0
  60. package/dist/cjs/utils/client_labels.js +41 -3
  61. package/dist/cjs/utils/env_aware_utils.js +10 -1
  62. package/dist/cjs/utils/streaming_utils.js +15 -9
  63. package/dist/cjs/utils/task.js +62 -0
  64. package/dist/cjs/version.js +1 -1
  65. package/dist/esm/a2a/agent_to_a2a.js +18 -2
  66. package/dist/esm/a2a/event_converter_utils.js +8 -4
  67. package/dist/esm/a2a/metadata_converter_utils.js +1 -1
  68. package/dist/esm/agents/active_streaming_tool.js +6 -1
  69. package/dist/esm/agents/base_agent.js +39 -0
  70. package/dist/esm/agents/context.js +1 -11
  71. package/dist/esm/agents/functions.js +43 -32
  72. package/dist/esm/agents/instructions.js +23 -5
  73. package/dist/esm/agents/invocation_context.js +2 -5
  74. package/dist/esm/agents/llm_agent.js +12 -9
  75. package/dist/esm/agents/loop_agent.js +15 -3
  76. package/dist/esm/agents/processors/code_execution_request_processor.js +0 -3
  77. package/dist/esm/agents/processors/content_processor_utils.js +16 -3
  78. package/dist/esm/agents/processors/context_compactor_request_processor.js +4 -2
  79. package/dist/esm/agents/processors/interactions_request_processor.js +34 -0
  80. package/dist/esm/agents/routed_agent.js +31 -0
  81. package/dist/esm/agents/run_config.js +3 -2
  82. package/dist/esm/apps/app.js +49 -0
  83. package/dist/esm/apps/resumability_config.js +16 -0
  84. package/dist/esm/artifacts/file_artifact_service.js +14 -1
  85. package/dist/esm/artifacts/gcs_artifact_service.js +57 -15
  86. package/dist/esm/artifacts/in_memory_artifact_service.js +12 -1
  87. package/dist/esm/artifacts/scoped_artifact_service.js +73 -0
  88. package/dist/esm/artifacts/session_artifact_service.js +7 -0
  89. package/dist/esm/auth/oauth2/oauth2_discovery.js +14 -4
  90. package/dist/esm/auth/oauth2/oauth2_utils.js +12 -1
  91. package/dist/esm/common.js +67 -3
  92. package/dist/esm/context/agent_controlled_context_compactor.js +81 -0
  93. package/dist/esm/context/anchored_context_compactor.js +99 -0
  94. package/dist/esm/context/compaction_utils.js +27 -0
  95. package/dist/esm/context/token_based_context_compactor.js +52 -33
  96. package/dist/esm/context/trajectory_thought_pruning_compactor.js +43 -0
  97. package/dist/esm/events/compacted_event.js +7 -2
  98. package/dist/esm/events/event.js +46 -0
  99. package/dist/esm/index.js +6 -1
  100. package/dist/esm/integrations/agent_registry/agent_registry_mcp_toolset.js +9 -1
  101. package/dist/esm/models/google_llm.js +23 -9
  102. package/dist/esm/models/interactions_utils.js +697 -0
  103. package/dist/esm/plugins/base_plugin.js +1 -0
  104. package/dist/esm/plugins/global_instruction_plugin.js +69 -0
  105. package/dist/esm/runner/in_memory_runner.js +13 -2
  106. package/dist/esm/runner/runner.js +141 -84
  107. package/dist/esm/sessions/base_session_service.js +6 -1
  108. package/dist/esm/sessions/database_session_service.js +24 -7
  109. package/dist/esm/sessions/vertex_ai_session_service.js +17 -3
  110. package/dist/esm/skills/loader.js +2 -2
  111. package/dist/esm/tools/base_toolset.js +1 -1
  112. package/dist/esm/tools/consolidate_context_tool.js +51 -0
  113. package/dist/esm/tools/enterprise_web_search_tool.js +55 -0
  114. package/dist/esm/tools/example_tool.js +42 -0
  115. package/dist/esm/tools/forwarding_artifact_service.js +16 -28
  116. package/dist/esm/tools/load_web_page.js +221 -0
  117. package/dist/esm/tools/mcp/load_mcp_resource_tool.js +114 -0
  118. package/dist/esm/tools/mcp/mcp_toolset.js +63 -0
  119. package/dist/esm/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +15 -22
  120. package/dist/esm/tools/skill/run_skill_inline_script_tool.js +53 -4
  121. package/dist/esm/tools/skill/skill_toolset.js +4 -2
  122. package/dist/esm/utils/async_hooks_shim.js +24 -0
  123. package/dist/esm/utils/branch_trie.js +13 -0
  124. package/dist/esm/utils/client_labels.js +38 -2
  125. package/dist/esm/utils/env_aware_utils.js +10 -1
  126. package/dist/esm/utils/streaming_utils.js +11 -5
  127. package/dist/esm/utils/task.js +34 -0
  128. package/dist/esm/version.js +1 -1
  129. package/dist/types/a2a/a2a_remote_agent.d.ts +5 -1
  130. package/dist/types/a2a/agent_to_a2a.d.ts +38 -0
  131. package/dist/types/agents/active_streaming_tool.d.ts +3 -4
  132. package/dist/types/agents/base_agent.d.ts +29 -1
  133. package/dist/types/agents/functions.d.ts +12 -17
  134. package/dist/types/agents/invocation_context.d.ts +7 -3
  135. package/dist/types/agents/llm_agent.d.ts +5 -1
  136. package/dist/types/agents/loop_agent.d.ts +2 -2
  137. package/dist/types/agents/processors/content_processor_utils.d.ts +8 -0
  138. package/dist/types/agents/processors/interactions_request_processor.d.ts +17 -0
  139. package/dist/types/agents/routed_agent.d.ts +24 -1
  140. package/dist/types/agents/run_config.d.ts +1 -1
  141. package/dist/types/apps/app.d.ts +52 -0
  142. package/dist/types/apps/resumability_config.d.ts +27 -0
  143. package/dist/types/artifacts/base_artifact_service.d.ts +6 -36
  144. package/dist/types/artifacts/gcs_artifact_service.d.ts +2 -1
  145. package/dist/types/artifacts/scoped_artifact_service.d.ts +25 -0
  146. package/dist/types/artifacts/session_artifact_service.d.ts +25 -0
  147. package/dist/types/auth/oauth2/oauth2_discovery.d.ts +11 -0
  148. package/dist/types/common.d.ts +29 -5
  149. package/dist/types/context/agent_controlled_context_compactor.d.ts +24 -0
  150. package/dist/types/context/anchored_context_compactor.d.ts +35 -0
  151. package/dist/types/context/base_context_compactor.d.ts +3 -0
  152. package/dist/types/context/compaction_utils.d.ts +15 -0
  153. package/dist/types/context/token_based_context_compactor.d.ts +6 -1
  154. package/dist/types/context/trajectory_thought_pruning_compactor.d.ts +27 -0
  155. package/dist/types/events/compacted_event.d.ts +10 -0
  156. package/dist/types/events/event.d.ts +21 -0
  157. package/dist/types/index.d.ts +3 -2
  158. package/dist/types/models/google_llm.d.ts +15 -1
  159. package/dist/types/models/interactions_utils.d.ts +92 -0
  160. package/dist/types/models/llm_request.d.ts +4 -0
  161. package/dist/types/models/llm_response.d.ts +4 -0
  162. package/dist/types/plugins/base_plugin.d.ts +2 -1
  163. package/dist/types/plugins/global_instruction_plugin.d.ts +38 -0
  164. package/dist/types/runner/in_memory_runner.d.ts +7 -1
  165. package/dist/types/runner/runner.d.ts +44 -10
  166. package/dist/types/sessions/base_session_service.d.ts +3 -16
  167. package/dist/types/sessions/session.d.ts +11 -0
  168. package/dist/types/sessions/vertex_ai_session_service.d.ts +9 -1
  169. package/dist/types/tools/consolidate_context_tool.d.ts +19 -0
  170. package/dist/types/tools/enterprise_web_search_tool.d.ts +26 -0
  171. package/dist/types/tools/example_tool.d.ts +21 -0
  172. package/dist/types/tools/forwarding_artifact_service.d.ts +3 -2
  173. package/dist/types/tools/load_web_page.d.ts +33 -0
  174. package/dist/types/tools/mcp/load_mcp_resource_tool.d.ts +26 -0
  175. package/dist/types/tools/mcp/mcp_toolset.d.ts +27 -0
  176. package/dist/types/tools/skill/run_skill_inline_script_tool.d.ts +25 -0
  177. package/dist/types/tools/skill/skill_toolset.d.ts +9 -0
  178. package/dist/types/utils/async_hooks_shim.d.ts +10 -0
  179. package/dist/types/utils/branch_trie.d.ts +14 -0
  180. package/dist/types/utils/client_labels.d.ts +10 -0
  181. package/dist/types/utils/env_aware_utils.d.ts +3 -3
  182. package/dist/types/utils/task.d.ts +24 -0
  183. package/dist/types/version.d.ts +1 -1
  184. package/dist/web/a2a/agent_to_a2a.js +18 -2
  185. package/dist/web/a2a/event_converter_utils.js +8 -4
  186. package/dist/web/a2a/metadata_converter_utils.js +1 -1
  187. package/dist/web/agents/active_streaming_tool.js +6 -1
  188. package/dist/web/agents/base_agent.js +39 -0
  189. package/dist/web/agents/context.js +1 -11
  190. package/dist/web/agents/functions.js +43 -32
  191. package/dist/web/agents/instructions.js +23 -5
  192. package/dist/web/agents/invocation_context.js +2 -5
  193. package/dist/web/agents/llm_agent.js +12 -9
  194. package/dist/web/agents/loop_agent.js +27 -3
  195. package/dist/web/agents/processors/code_execution_request_processor.js +0 -3
  196. package/dist/web/agents/processors/content_processor_utils.js +16 -3
  197. package/dist/web/agents/processors/context_compactor_request_processor.js +4 -2
  198. package/dist/web/agents/processors/interactions_request_processor.js +52 -0
  199. package/dist/web/agents/routed_agent.js +31 -0
  200. package/dist/web/agents/run_config.js +8 -3
  201. package/dist/web/apps/app.js +49 -0
  202. package/dist/web/apps/resumability_config.js +31 -0
  203. package/dist/web/artifacts/file_artifact_service.js +14 -1
  204. package/dist/web/artifacts/gcs_artifact_service.js +55 -15
  205. package/dist/web/artifacts/in_memory_artifact_service.js +12 -1
  206. package/dist/web/artifacts/scoped_artifact_service.js +86 -0
  207. package/dist/web/artifacts/session_artifact_service.js +7 -0
  208. package/dist/web/auth/oauth2/oauth2_discovery.js +14 -4
  209. package/dist/web/auth/oauth2/oauth2_utils.js +12 -1
  210. package/dist/web/common.js +67 -3
  211. package/dist/web/context/agent_controlled_context_compactor.js +81 -0
  212. package/dist/web/context/anchored_context_compactor.js +117 -0
  213. package/dist/web/context/compaction_utils.js +27 -0
  214. package/dist/web/context/token_based_context_compactor.js +52 -33
  215. package/dist/web/context/trajectory_thought_pruning_compactor.js +43 -0
  216. package/dist/web/events/compacted_event.js +7 -2
  217. package/dist/web/events/event.js +44 -0
  218. package/dist/web/index.js +6 -1
  219. package/dist/web/integrations/agent_registry/agent_registry_mcp_toolset.js +9 -1
  220. package/dist/web/models/google_llm.js +54 -9
  221. package/dist/web/models/interactions_utils.js +728 -0
  222. package/dist/web/plugins/base_plugin.js +1 -0
  223. package/dist/web/plugins/global_instruction_plugin.js +67 -0
  224. package/dist/web/runner/in_memory_runner.js +13 -2
  225. package/dist/web/runner/runner.js +157 -84
  226. package/dist/web/sessions/base_session_service.js +6 -1
  227. package/dist/web/sessions/database_session_service.js +24 -7
  228. package/dist/web/sessions/vertex_ai_session_service.js +17 -3
  229. package/dist/web/skills/loader.js +2 -2
  230. package/dist/web/tools/base_toolset.js +1 -1
  231. package/dist/web/tools/consolidate_context_tool.js +51 -0
  232. package/dist/web/tools/enterprise_web_search_tool.js +55 -0
  233. package/dist/web/tools/example_tool.js +42 -0
  234. package/dist/web/tools/forwarding_artifact_service.js +16 -28
  235. package/dist/web/tools/load_web_page.js +221 -0
  236. package/dist/web/tools/mcp/load_mcp_resource_tool.js +107 -0
  237. package/dist/web/tools/mcp/mcp_toolset.js +63 -0
  238. package/dist/web/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +15 -22
  239. package/dist/web/tools/skill/run_skill_inline_script_tool.js +51 -4
  240. package/dist/web/tools/skill/skill_toolset.js +4 -2
  241. package/dist/web/utils/async_hooks_shim.js +24 -0
  242. package/dist/web/utils/branch_trie.js +13 -0
  243. package/dist/web/utils/client_labels.js +38 -2
  244. package/dist/web/utils/env_aware_utils.js +10 -1
  245. package/dist/web/utils/streaming_utils.js +11 -5
  246. package/dist/web/utils/task.js +34 -0
  247. package/dist/web/version.js +1 -1
  248. package/package.json +2 -2
@@ -0,0 +1,42 @@
1
+ import {createRequire as topLevelCreateRequire} from 'module';
2
+ const require = topLevelCreateRequire(import.meta.url);
3
+ /**
4
+ * @license
5
+ * Copyright 2026 Google LLC
6
+ * SPDX-License-Identifier: Apache-2.0
7
+ */
8
+ import { buildExampleSi } from "../examples/example_util.js";
9
+ import { appendInstructions } from "../models/llm_request.js";
10
+ import {
11
+ BaseTool
12
+ } from "./base_tool.js";
13
+ class ExampleTool extends BaseTool {
14
+ constructor(examples) {
15
+ super({
16
+ // Name and description are not used because this tool only changes
17
+ // llmRequest.
18
+ name: "example_tool",
19
+ description: "example tool"
20
+ });
21
+ this.examples = examples;
22
+ }
23
+ async runAsync(_request) {
24
+ throw new Error("ExampleTool should not be called by model");
25
+ }
26
+ async processLlmRequest({
27
+ toolContext,
28
+ llmRequest
29
+ }) {
30
+ var _a, _b;
31
+ const parts = (_a = toolContext.userContent) == null ? void 0 : _a.parts;
32
+ if (!parts || !((_b = parts[0]) == null ? void 0 : _b.text)) {
33
+ return;
34
+ }
35
+ appendInstructions(llmRequest, [
36
+ buildExampleSi(this.examples, parts[0].text, llmRequest.model)
37
+ ]);
38
+ }
39
+ }
40
+ export {
41
+ ExampleTool
42
+ };
@@ -10,48 +10,36 @@ class ForwardingArtifactService {
10
10
  this.toolContext = toolContext;
11
11
  this.invocationContext = toolContext.invocationContext;
12
12
  }
13
- // TODO - b/425992518: Remove unnecessary parameters. We should rethink the
14
- // abstraction layer to make it more clear.
15
13
  async saveArtifact(request) {
16
14
  return this.toolContext.saveArtifact(request.filename, request.artifact);
17
15
  }
18
16
  async loadArtifact(request) {
19
17
  return this.toolContext.loadArtifact(request.filename, request.version);
20
18
  }
21
- async listArtifactKeys() {
19
+ async listArtifactKeys(_request) {
22
20
  return this.toolContext.listArtifacts();
23
21
  }
24
- async deleteArtifact(request) {
25
- if (!this.toolContext.invocationContext.artifactService) {
22
+ getArtifactService() {
23
+ const service = this.invocationContext.artifactService;
24
+ if (!service) {
26
25
  throw new Error("Artifact service is not initialized.");
27
26
  }
28
- return this.toolContext.invocationContext.artifactService.deleteArtifact(
29
- request
30
- );
27
+ return service;
28
+ }
29
+ async deleteArtifact(request) {
30
+ return this.getArtifactService().deleteArtifact(request.filename);
31
31
  }
32
32
  async listVersions(request) {
33
- if (!this.toolContext.invocationContext.artifactService) {
34
- throw new Error("Artifact service is not initialized.");
35
- }
36
- return this.toolContext.invocationContext.artifactService.listVersions(
37
- request
38
- );
33
+ return this.getArtifactService().listVersions(request.filename);
39
34
  }
40
- listArtifactVersions(request) {
41
- if (!this.toolContext.invocationContext.artifactService) {
42
- throw new Error("Artifact service is not initialized.");
43
- }
44
- return this.toolContext.invocationContext.artifactService.listArtifactVersions(
45
- request
46
- );
35
+ async listArtifactVersions(request) {
36
+ return this.getArtifactService().listArtifactVersions(request.filename);
47
37
  }
48
- getArtifactVersion(request) {
49
- if (!this.toolContext.invocationContext.artifactService) {
50
- throw new Error("Artifact service is not initialized.");
51
- }
52
- return this.toolContext.invocationContext.artifactService.getArtifactVersion(
53
- request
54
- );
38
+ async getArtifactVersion(request) {
39
+ return this.getArtifactService().getArtifactVersion({
40
+ filename: request.filename,
41
+ version: request.version
42
+ });
55
43
  }
56
44
  }
57
45
  export {
@@ -0,0 +1,221 @@
1
+ import {createRequire as topLevelCreateRequire} from 'module';
2
+ const require = topLevelCreateRequire(import.meta.url);
3
+ /**
4
+ * @license
5
+ * Copyright 2026 Google LLC
6
+ * SPDX-License-Identifier: Apache-2.0
7
+ */
8
+ import { lookup } from "node:dns/promises";
9
+ import { isIP } from "node:net";
10
+ import { z } from "zod";
11
+ import { FunctionTool } from "./function_tool.js";
12
+ const ALLOWED_SCHEMES = /* @__PURE__ */ new Set(["http:", "https:"]);
13
+ const DEFAULT_TIMEOUT_MS = 3e4;
14
+ const BLOCKED_IPV4_CIDRS = [
15
+ "0.0.0.0/8",
16
+ // "this host on this network"
17
+ "10.0.0.0/8",
18
+ // private
19
+ "100.64.0.0/10",
20
+ // shared address space / CGNAT
21
+ "127.0.0.0/8",
22
+ // loopback
23
+ "169.254.0.0/16",
24
+ // link-local (includes GCP metadata 169.254.169.254)
25
+ "172.16.0.0/12",
26
+ // private
27
+ "192.0.0.0/24",
28
+ // IETF protocol assignments
29
+ "192.0.2.0/24",
30
+ // TEST-NET-1 (documentation)
31
+ "192.88.99.0/24",
32
+ // 6to4 relay anycast (deprecated)
33
+ "192.168.0.0/16",
34
+ // private
35
+ "198.18.0.0/15",
36
+ // benchmarking
37
+ "198.51.100.0/24",
38
+ // TEST-NET-2 (documentation)
39
+ "203.0.113.0/24",
40
+ // TEST-NET-3 (documentation)
41
+ "224.0.0.0/4",
42
+ // multicast
43
+ "240.0.0.0/4"
44
+ // reserved / future use (includes 255.255.255.255)
45
+ ].map(parseIpv4Cidr);
46
+ const BLOCKED_IPV6_CIDRS = [
47
+ "::/128",
48
+ // unspecified
49
+ "::1/128",
50
+ // loopback
51
+ "64:ff9b:1::/48",
52
+ // local NAT64
53
+ "100::/64",
54
+ // discard-only
55
+ "2001:db8::/32",
56
+ // documentation
57
+ "fc00::/7",
58
+ // unique-local (ULA, private)
59
+ "fe80::/10",
60
+ // link-local
61
+ "ff00::/8"
62
+ // multicast
63
+ ].map(parseIpv6Cidr);
64
+ function failedToFetchMessage(url) {
65
+ return `Failed to fetch url: ${url}`;
66
+ }
67
+ function isBlockedHostname(hostname) {
68
+ const normalized = hostname.replace(/\.+$/, "").toLowerCase();
69
+ return normalized === "localhost" || normalized.endsWith(".localhost");
70
+ }
71
+ function normalizeHost(hostname) {
72
+ return hostname.startsWith("[") ? hostname.slice(1, -1) : hostname;
73
+ }
74
+ function parseIpv4(address) {
75
+ const match = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(address);
76
+ if (!match) {
77
+ return null;
78
+ }
79
+ const octets = match.slice(1).map(Number);
80
+ if (octets.some((octet) => octet > 255)) {
81
+ return null;
82
+ }
83
+ return octets;
84
+ }
85
+ function parseIpv6(address) {
86
+ if (isIP(address) !== 6) {
87
+ return null;
88
+ }
89
+ const [head, tail] = address.split("::");
90
+ const highGroups = head ? expandHextets(head) : [];
91
+ const lowGroups = tail ? expandHextets(tail) : [];
92
+ const compressed = address.includes("::") ? new Array(8 - highGroups.length - lowGroups.length).fill(0) : [];
93
+ return [...highGroups, ...compressed, ...lowGroups];
94
+ }
95
+ function expandHextets(fragment) {
96
+ const hextets = [];
97
+ for (const group of fragment.split(":")) {
98
+ if (group.includes(".")) {
99
+ const octets = parseIpv4(group);
100
+ hextets.push(octets[0] << 8 | octets[1], octets[2] << 8 | octets[3]);
101
+ } else {
102
+ hextets.push(parseInt(group, 16));
103
+ }
104
+ }
105
+ return hextets;
106
+ }
107
+ function ipv4ToInt(octets) {
108
+ return (octets[0] << 24 | octets[1] << 16 | octets[2] << 8 | octets[3]) >>> 0;
109
+ }
110
+ function hextetsToBigInt(hextets) {
111
+ let value = 0n;
112
+ for (const hextet of hextets) {
113
+ value = value << 16n | BigInt(hextet);
114
+ }
115
+ return value;
116
+ }
117
+ function parseIpv4Cidr(cidr) {
118
+ const [address, prefix] = cidr.split("/");
119
+ const mask = 4294967295 << 32 - Number(prefix) >>> 0;
120
+ return { base: (ipv4ToInt(parseIpv4(address)) & mask) >>> 0, mask };
121
+ }
122
+ function parseIpv6Cidr(cidr) {
123
+ const [address, prefix] = cidr.split("/");
124
+ return { base: hextetsToBigInt(parseIpv6(address)), prefix: Number(prefix) };
125
+ }
126
+ function isBlockedIpv4(octets) {
127
+ const value = ipv4ToInt(octets);
128
+ return BLOCKED_IPV4_CIDRS.some(
129
+ ({ base, mask }) => (value & mask) >>> 0 === base
130
+ );
131
+ }
132
+ function isBlockedIpv6(hextets) {
133
+ const value = hextetsToBigInt(hextets);
134
+ if (value >> 32n === 0xffffn) {
135
+ return isBlockedIpv4([
136
+ Number(value >> 24n & 0xffn),
137
+ Number(value >> 16n & 0xffn),
138
+ Number(value >> 8n & 0xffn),
139
+ Number(value & 0xffn)
140
+ ]);
141
+ }
142
+ return BLOCKED_IPV6_CIDRS.some(
143
+ ({ base, prefix }) => value >> BigInt(128 - prefix) === base >> BigInt(128 - prefix)
144
+ );
145
+ }
146
+ function isBlockedAddress(address) {
147
+ const octets = parseIpv4(address);
148
+ if (octets) {
149
+ return isBlockedIpv4(octets);
150
+ }
151
+ const hextets = parseIpv6(address);
152
+ if (hextets) {
153
+ return isBlockedIpv6(hextets);
154
+ }
155
+ return true;
156
+ }
157
+ async function resolveHostAddresses(hostname) {
158
+ if (isIP(hostname) !== 0) {
159
+ return [hostname];
160
+ }
161
+ const records = await lookup(hostname, { all: true });
162
+ const addresses = [...new Set(records.map((record) => record.address))];
163
+ if (addresses.length === 0) {
164
+ throw new Error(`Unable to resolve host: ${hostname}`);
165
+ }
166
+ return addresses;
167
+ }
168
+ function assertUrlAllowed(url) {
169
+ const parsed = new URL(url);
170
+ if (!ALLOWED_SCHEMES.has(parsed.protocol)) {
171
+ throw new Error(`Unsupported url scheme: ${url}`);
172
+ }
173
+ if (isBlockedHostname(parsed.hostname)) {
174
+ throw new Error(`Blocked host: ${parsed.hostname}`);
175
+ }
176
+ return parsed;
177
+ }
178
+ async function validateResolvedAddresses(hostname) {
179
+ const addresses = await resolveHostAddresses(hostname);
180
+ if (addresses.some(isBlockedAddress)) {
181
+ throw new Error(`Blocked host: ${hostname}`);
182
+ }
183
+ }
184
+ function decodeHtmlEntities(text) {
185
+ return text.replace(/&nbsp;/g, " ").replace(/&#39;/g, "'").replace(/&#x27;/g, "'").replace(/&quot;/g, '"').replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&");
186
+ }
187
+ function htmlToText(html) {
188
+ const withoutCode = html.replace(/<script[\s\S]*?<\/script>/gi, " ").replace(/<style[\s\S]*?<\/style>/gi, " ").replace(/<!--[\s\S]*?-->/g, " ");
189
+ const text = decodeHtmlEntities(withoutCode.replace(/<[^>]+>/g, "\n"));
190
+ return text.split("\n").map((line) => line.trim()).filter((line) => line.split(/\s+/).filter(Boolean).length > 3).join("\n");
191
+ }
192
+ async function loadWebPage(url, options) {
193
+ var _a;
194
+ const timeoutMs = (_a = options == null ? void 0 : options.timeoutMs) != null ? _a : DEFAULT_TIMEOUT_MS;
195
+ try {
196
+ const parsed = assertUrlAllowed(url);
197
+ await validateResolvedAddresses(normalizeHost(parsed.hostname));
198
+ const response = await fetch(url, {
199
+ redirect: "manual",
200
+ signal: AbortSignal.timeout(timeoutMs)
201
+ });
202
+ if (response.status !== 200) {
203
+ return failedToFetchMessage(url);
204
+ }
205
+ return htmlToText(await response.text());
206
+ } catch {
207
+ return failedToFetchMessage(url);
208
+ }
209
+ }
210
+ const LOAD_WEB_PAGE = new FunctionTool({
211
+ name: "load_web_page",
212
+ description: "Fetches the content at the given URL and returns the readable text extracted from the page.",
213
+ parameters: z.object({
214
+ url: z.string().describe("The URL to fetch and extract text from.")
215
+ }),
216
+ execute: ({ url }) => loadWebPage(url)
217
+ });
218
+ export {
219
+ LOAD_WEB_PAGE,
220
+ loadWebPage
221
+ };
@@ -0,0 +1,114 @@
1
+ import {createRequire as topLevelCreateRequire} from 'module';
2
+ const require = topLevelCreateRequire(import.meta.url);
3
+ /**
4
+ * @license
5
+ * Copyright 2026 Google LLC
6
+ * SPDX-License-Identifier: Apache-2.0
7
+ */
8
+ import { Type } from "@google/genai";
9
+ import { appendInstructions } from "../../models/llm_request.js";
10
+ import { logger } from "../../utils/logger.js";
11
+ import {
12
+ BaseTool
13
+ } from "../base_tool.js";
14
+ class LoadMcpResourceTool extends BaseTool {
15
+ constructor(mcpToolset) {
16
+ super({
17
+ name: "load_mcp_resource",
18
+ description: `Loads resources from the MCP server.
19
+
20
+ NOTE: Call when you need access to resources.`
21
+ });
22
+ this.mcpToolset = mcpToolset;
23
+ }
24
+ _getDeclaration() {
25
+ return {
26
+ name: this.name,
27
+ description: this.description,
28
+ parameters: {
29
+ type: Type.OBJECT,
30
+ properties: {
31
+ resource_names: {
32
+ type: Type.ARRAY,
33
+ items: {
34
+ type: Type.STRING
35
+ },
36
+ description: "The names of the MCP resources to load."
37
+ }
38
+ }
39
+ }
40
+ };
41
+ }
42
+ async runAsync({ args }) {
43
+ const resourceNames = args["resource_names"] || [];
44
+ return {
45
+ resource_names: resourceNames,
46
+ status: "resource contents temporarily inserted and removed. to access these resources, call load_mcp_resource tool again."
47
+ };
48
+ }
49
+ async processLlmRequest(request) {
50
+ await super.processLlmRequest(request);
51
+ await this.appendResourcesToLlmRequest(request.llmRequest);
52
+ }
53
+ async appendResourcesToLlmRequest(llmRequest) {
54
+ var _a, _b;
55
+ try {
56
+ const availableResourceNames = await this.mcpToolset.listResources();
57
+ if (availableResourceNames.length > 0) {
58
+ appendInstructions(llmRequest, [
59
+ `You have a list of MCP resources:
60
+ ${JSON.stringify(
61
+ availableResourceNames
62
+ )}
63
+
64
+ When the user asks questions about any of the resources, you should call the
65
+ \`load_mcp_resource\` function to load the resource. Always call load_mcp_resource
66
+ before answering questions related to the resources.`
67
+ ]);
68
+ }
69
+ } catch (e) {
70
+ logger.warn(`Failed to list MCP resources: ${e}`);
71
+ }
72
+ const lastContent = llmRequest.contents.at(-1);
73
+ const functionResponse = (_b = (_a = lastContent == null ? void 0 : lastContent.parts) == null ? void 0 : _a[0]) == null ? void 0 : _b.functionResponse;
74
+ if (!functionResponse || functionResponse.name !== this.name) {
75
+ return;
76
+ }
77
+ const response = functionResponse.response || {};
78
+ const requestedResourceNames = response["resource_names"] || [];
79
+ for (const resourceName of requestedResourceNames) {
80
+ try {
81
+ const resourceContents = await this.mcpToolset.readResource(resourceName);
82
+ for (const content of resourceContents) {
83
+ llmRequest.contents.push({
84
+ role: "user",
85
+ parts: [
86
+ { text: `Resource ${resourceName} is:` },
87
+ this.mcpContentToPart(content, resourceName)
88
+ ]
89
+ });
90
+ }
91
+ } catch (e) {
92
+ logger.warn(`Failed to read MCP resource '${resourceName}': ${e}`);
93
+ }
94
+ }
95
+ }
96
+ mcpContentToPart(content, resourceName) {
97
+ var _a;
98
+ if ("text" in content) {
99
+ return { text: content.text };
100
+ }
101
+ if ("blob" in content) {
102
+ return {
103
+ inlineData: {
104
+ data: content.blob,
105
+ mimeType: (_a = content.mimeType) != null ? _a : "application/octet-stream"
106
+ }
107
+ };
108
+ }
109
+ return { text: `[Unknown content type for ${resourceName}]` };
110
+ }
111
+ }
112
+ export {
113
+ LoadMcpResourceTool
114
+ };
@@ -48,6 +48,69 @@ class MCPToolset extends BaseToolset {
48
48
  );
49
49
  return tools;
50
50
  }
51
+ /**
52
+ * Lists the names of the resources advertised by the MCP server.
53
+ *
54
+ * @return The resource names available on the server.
55
+ */
56
+ async listResources() {
57
+ const session = await this.mcpSessionManager.createSession();
58
+ try {
59
+ const result = await session.listResources();
60
+ return result.resources.map((resource) => resource.name);
61
+ } finally {
62
+ await this.mcpSessionManager.closeSession(session);
63
+ }
64
+ }
65
+ /**
66
+ * Returns metadata for the resource whose name matches `name`.
67
+ *
68
+ * @param name The advertised name of the resource.
69
+ * @return The matching MCP `Resource`.
70
+ * @throws If no resource with the given name is advertised by the server.
71
+ */
72
+ async getResourceInfo(name) {
73
+ const session = await this.mcpSessionManager.createSession();
74
+ let result;
75
+ try {
76
+ result = await session.listResources();
77
+ } finally {
78
+ await this.mcpSessionManager.closeSession(session);
79
+ }
80
+ const resource = result.resources.find(
81
+ (candidate) => candidate.name === name
82
+ );
83
+ if (!resource) {
84
+ throw new Error(`Resource with name '${name}' not found.`);
85
+ }
86
+ return resource;
87
+ }
88
+ /**
89
+ * Reads the contents of the named resource from the MCP server.
90
+ *
91
+ * The resource name is resolved to a URI via {@link getResourceInfo} before
92
+ * reading. Binary contents are returned base64-encoded, exactly as provided
93
+ * by the server (never decoded and re-encoded).
94
+ *
95
+ * @param name The advertised name of the resource to read.
96
+ * @return The resource contents (text and/or base64-encoded binary).
97
+ * @throws If the resource is unknown or has no URI.
98
+ */
99
+ async readResource(name) {
100
+ const resourceInfo = await this.getResourceInfo(name);
101
+ if (!resourceInfo.uri) {
102
+ throw new Error(`Resource '${name}' has no URI.`);
103
+ }
104
+ const session = await this.mcpSessionManager.createSession();
105
+ try {
106
+ const result = await session.readResource({
107
+ uri: resourceInfo.uri
108
+ });
109
+ return result.contents;
110
+ } finally {
111
+ await this.mcpSessionManager.closeSession(session);
112
+ }
113
+ }
51
114
  async close() {
52
115
  const sessions = this.mcpSessionManager.getActiveSessions();
53
116
  await Promise.allSettled(
@@ -27,20 +27,10 @@ class ToolContextCredentialStore {
27
27
  }
28
28
  getCredential(authScheme) {
29
29
  const key = this.getCredentialKey(authScheme);
30
- const state = this.context.state;
31
- if (state) {
32
- const serialized = state[key];
33
- if (serialized) {
34
- return serialized;
35
- }
36
- }
37
- return void 0;
30
+ return this.context.state.get(key);
38
31
  }
39
32
  storeCredential(key, credential) {
40
- const state = this.context.state;
41
- if (state) {
42
- state[key] = credential;
43
- }
33
+ this.context.state.set(key, credential);
44
34
  }
45
35
  }
46
36
  let ToolAuthHandler = class {
@@ -72,19 +62,22 @@ let ToolAuthHandler = class {
72
62
  rawAuthCredential: this.authCredential,
73
63
  credentialKey: this.credentialKey || "default_openapi_key"
74
64
  };
75
- const credential = this.context.getAuthResponse(authConfig);
76
- if (credential) {
77
- const exchanger = new AutoAuthCredentialExchanger();
78
- const result = await exchanger.exchange({
79
- authScheme: this.authScheme,
80
- authCredential: credential
81
- });
65
+ const authResponseCredential = this.context.getAuthResponse(authConfig);
66
+ const credential = authResponseCredential != null ? authResponseCredential : this.authCredential;
67
+ if (!credential) {
68
+ this.context.requestCredential(authConfig);
69
+ return { state: "pending" };
70
+ }
71
+ const exchanger = new AutoAuthCredentialExchanger();
72
+ const result = await exchanger.exchange({
73
+ authScheme: this.authScheme,
74
+ authCredential: credential
75
+ });
76
+ if (authResponseCredential || result.wasExchanged) {
82
77
  const key = store.getCredentialKey(this.authScheme);
83
78
  store.storeCredential(key, result.credential);
84
- return { state: "done", authCredential: result.credential };
85
79
  }
86
- this.context.requestCredential(authConfig);
87
- return { state: "pending" };
80
+ return { state: "done", authCredential: result.credential };
88
81
  }
89
82
  };
90
83
  __decorateClass([
@@ -21,6 +21,15 @@ import { CodeExecutionLanguage } from "../../code_executors/code_execution_utils
21
21
  import { experimental } from "../../utils/experimental.js";
22
22
  import { materializeFiles } from "../../utils/file_utils.js";
23
23
  import { BaseTool } from "../base_tool.js";
24
+ var RunSkillInlineScriptErrorCode = /* @__PURE__ */ ((RunSkillInlineScriptErrorCode2) => {
25
+ RunSkillInlineScriptErrorCode2["MISSING_SCRIPT_CONTENT"] = "MISSING_SCRIPT_CONTENT";
26
+ RunSkillInlineScriptErrorCode2["MISSING_LANGUAGE"] = "MISSING_LANGUAGE";
27
+ RunSkillInlineScriptErrorCode2["NO_CODE_EXECUTOR"] = "NO_CODE_EXECUTOR";
28
+ RunSkillInlineScriptErrorCode2["EXECUTION_ERROR"] = "EXECUTION_ERROR";
29
+ RunSkillInlineScriptErrorCode2["CONFIRMATION_REJECTED"] = "CONFIRMATION_REJECTED";
30
+ return RunSkillInlineScriptErrorCode2;
31
+ })(RunSkillInlineScriptErrorCode || {});
32
+ const REQUIRE_CONFIRMATION_MESSAGE = "This tool call needs external confirmation before completion.";
24
33
  let RunSkillInlineScriptTool = class extends BaseTool {
25
34
  constructor(toolset) {
26
35
  super({
@@ -70,13 +79,13 @@ let RunSkillInlineScriptTool = class extends BaseTool {
70
79
  if (!inlineScriptContent) {
71
80
  return {
72
81
  error: "Script content is required.",
73
- errorCode: "MISSING_SCRIPT_CONTENT"
82
+ errorCode: "MISSING_SCRIPT_CONTENT" /* MISSING_SCRIPT_CONTENT */
74
83
  };
75
84
  }
76
85
  if (!language) {
77
86
  return {
78
87
  error: "Language is required.",
79
- errorCode: "MISSING_LANGUAGE"
88
+ errorCode: "MISSING_LANGUAGE" /* MISSING_LANGUAGE */
80
89
  };
81
90
  }
82
91
  let codeExecutor = (_a = this.toolset) == null ? void 0 : _a.codeExecutor;
@@ -89,9 +98,17 @@ let RunSkillInlineScriptTool = class extends BaseTool {
89
98
  if (!codeExecutor) {
90
99
  return {
91
100
  error: "No code executor configured.",
92
- errorCode: "NO_CODE_EXECUTOR"
101
+ errorCode: "NO_CODE_EXECUTOR" /* NO_CODE_EXECUTOR */
93
102
  };
94
103
  }
104
+ const confirmationResult = this.enforceConfirmation(
105
+ toolContext,
106
+ inlineScriptContent,
107
+ language
108
+ );
109
+ if (confirmationResult) {
110
+ return confirmationResult;
111
+ }
95
112
  try {
96
113
  const result = await codeExecutor.executeCode({
97
114
  invocationContext: toolContext.invocationContext,
@@ -107,14 +124,46 @@ let RunSkillInlineScriptTool = class extends BaseTool {
107
124
  } catch (e) {
108
125
  return {
109
126
  error: `Failed to execute inline script: ${e.message}`,
110
- errorCode: "EXECUTION_ERROR"
127
+ errorCode: "EXECUTION_ERROR" /* EXECUTION_ERROR */
128
+ };
129
+ }
130
+ }
131
+ /**
132
+ * Enforces a human-in-the-loop confirmation gate before executing an inline
133
+ * script, using the repo's standard tool-confirmation mechanism.
134
+ *
135
+ * - When no confirmation has been provided yet, a confirmation is requested
136
+ * (surfacing the language and script that would run) and an intermediate
137
+ * result is returned so the tool call pauses until the client responds.
138
+ * - When a confirmation exists but was not confirmed, a rejection error is
139
+ * returned and execution is refused.
140
+ * - When the confirmation is confirmed, `undefined` is returned to allow the
141
+ * caller to proceed with execution.
142
+ */
143
+ enforceConfirmation(toolContext, scriptContent, language) {
144
+ const confirmation = toolContext.toolConfirmation;
145
+ if (!confirmation) {
146
+ toolContext.requestConfirmation({
147
+ hint: `Confirmation is required before executing an inline skill script. The agent requested to run the following ${language} script in the code executor. Only approve if you trust its contents:
148
+
149
+ ` + scriptContent,
150
+ payload: { language, scriptContent }
151
+ });
152
+ return { partial: REQUIRE_CONFIRMATION_MESSAGE };
153
+ }
154
+ if (!confirmation.confirmed) {
155
+ return {
156
+ error: "Inline script execution was not confirmed and was rejected.",
157
+ errorCode: "CONFIRMATION_REJECTED" /* CONFIRMATION_REJECTED */
111
158
  };
112
159
  }
160
+ return void 0;
113
161
  }
114
162
  };
115
163
  RunSkillInlineScriptTool = __decorateClass([
116
164
  experimental
117
165
  ], RunSkillInlineScriptTool);
118
166
  export {
167
+ RunSkillInlineScriptErrorCode,
119
168
  RunSkillInlineScriptTool
120
169
  };
@@ -55,9 +55,11 @@ let SkillToolset = class extends BaseToolset {
55
55
  new ListSkillsTool(this),
56
56
  new LoadSkillTool(this),
57
57
  new LoadSkillResourceTool(this),
58
- new RunSkillScriptTool(this),
59
- new RunSkillInlineScriptTool(this)
58
+ new RunSkillScriptTool(this)
60
59
  ];
60
+ if (options.allowInlineScripts) {
61
+ this.tools.push(new RunSkillInlineScriptTool(this));
62
+ }
61
63
  if (this.registry) {
62
64
  this.tools.push(new SearchSkillsTool(this));
63
65
  }