@google/adk 2.1.0 → 2.2.1

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 (166) hide show
  1. package/dist/cjs/a2a/agent_card.js +8 -0
  2. package/dist/cjs/a2a/agent_executor.js +8 -3
  3. package/dist/cjs/a2a/event_converter_utils.js +5 -1
  4. package/dist/cjs/a2a/metadata_converter_utils.js +2 -1
  5. package/dist/cjs/agents/llm_agent.js +15 -1
  6. package/dist/cjs/agents/processors/content_processor_utils.js +10 -2
  7. package/dist/cjs/agents/processors/nl_planning_processor.js +114 -0
  8. package/dist/cjs/artifacts/gcs_artifact_service.js +13 -7
  9. package/dist/cjs/auth/auth_credential.js +12 -2
  10. package/dist/cjs/code_executors/code_execution_utils.js +3 -3
  11. package/dist/cjs/common.js +63 -0
  12. package/dist/cjs/events/abort_events.js +135 -0
  13. package/dist/cjs/events/internal_metadata.js +131 -0
  14. package/dist/cjs/examples/example.js +92 -0
  15. package/dist/cjs/examples/example_util.js +28 -4
  16. package/dist/cjs/examples/index.js +39 -0
  17. package/dist/cjs/examples/vertex_ai_example_store.js +39 -0
  18. package/dist/cjs/index.js +46 -0
  19. package/dist/cjs/memory/vertex_ai_memory_bank_service.js +17 -5
  20. package/dist/cjs/models/base_llm.js +1 -6
  21. package/dist/cjs/models/chrome_prompt_llm.js +16 -2
  22. package/dist/cjs/models/chrome_prompt_utils.js +54 -2
  23. package/dist/cjs/models/gemini_continuation.js +232 -0
  24. package/dist/cjs/models/gemini_llm_connection.js +5 -4
  25. package/dist/cjs/planners/base_planner.js +56 -0
  26. package/dist/cjs/planners/built_in_planner.js +85 -0
  27. package/dist/cjs/planners/plan_re_act_planner.js +172 -0
  28. package/dist/cjs/plugins/base_plugin.js +46 -61
  29. package/dist/cjs/plugins/debug_logging_plugin.js +653 -0
  30. package/dist/cjs/plugins/plugin_manager.js +1 -1
  31. package/dist/cjs/plugins/tool_call_integrity_plugin.js +364 -0
  32. package/dist/cjs/runner/runner.js +17 -3
  33. package/dist/cjs/tools/example_tool.js +1 -1
  34. package/dist/cjs/tools/mcp/mcp_session_manager.js +14 -8
  35. package/dist/cjs/tools/openapi_tool/auth/auth_helpers.js +281 -2
  36. package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.js +15 -1
  37. package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/base_auth_credential_exchanger.js +70 -0
  38. package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/oauth2_exchanger.js +120 -0
  39. package/dist/cjs/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.js +10 -10
  40. package/dist/cjs/tools/openapi_tool/common/common.js +139 -0
  41. package/dist/cjs/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.js +11 -6
  42. package/dist/cjs/tools/openapi_tool/openapi_spec_parser/operation_parser.js +73 -13
  43. package/dist/cjs/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +146 -34
  44. package/dist/cjs/tools/openapi_tool/openapi_toolset.js +14 -2
  45. package/dist/cjs/tools/openapi_tool/rest_api_tool.js +90 -28
  46. package/dist/cjs/tools/skill/skill_toolset.js +2 -0
  47. package/dist/cjs/tools/tool_context.js +41 -0
  48. package/dist/cjs/tools/webmcp/index.js +37 -0
  49. package/dist/cjs/tools/webmcp/webmcp_tool.js +156 -0
  50. package/dist/cjs/tools/webmcp/webmcp_toolset.js +116 -0
  51. package/dist/cjs/tools/webmcp/webmcp_types.js +50 -0
  52. package/dist/cjs/utils/case_utils.js +52 -2
  53. package/dist/cjs/utils/client_labels.js +67 -0
  54. package/dist/cjs/utils/gemini_schema_util.js +103 -3
  55. package/dist/cjs/utils/hash_utils.js +73 -0
  56. package/dist/cjs/utils/live_connection_utils.js +10 -9
  57. package/dist/cjs/utils/model_name.js +17 -2
  58. package/dist/cjs/utils/simple_zod_to_json.js +5 -0
  59. package/dist/cjs/version.js +1 -1
  60. package/dist/esm/a2a/agent_card.js +8 -0
  61. package/dist/esm/a2a/agent_executor.js +8 -3
  62. package/dist/esm/a2a/event_converter_utils.js +5 -1
  63. package/dist/esm/a2a/metadata_converter_utils.js +2 -1
  64. package/dist/esm/agents/llm_agent.js +18 -1
  65. package/dist/esm/agents/processors/content_processor_utils.js +10 -2
  66. package/dist/esm/agents/processors/nl_planning_processor.js +87 -0
  67. package/dist/esm/artifacts/gcs_artifact_service.js +13 -7
  68. package/dist/esm/auth/auth_credential.js +11 -1
  69. package/dist/esm/code_executors/code_execution_utils.js +3 -3
  70. package/dist/esm/common.js +58 -2
  71. package/dist/esm/events/abort_events.js +110 -0
  72. package/dist/esm/events/internal_metadata.js +97 -0
  73. package/dist/esm/examples/example.js +85 -0
  74. package/dist/esm/examples/example_util.js +25 -3
  75. package/dist/esm/examples/index.js +11 -0
  76. package/dist/esm/examples/vertex_ai_example_store.js +11 -0
  77. package/dist/esm/index.js +51 -1
  78. package/dist/esm/memory/vertex_ai_memory_bank_service.js +17 -5
  79. package/dist/esm/models/base_llm.js +2 -7
  80. package/dist/esm/models/chrome_prompt_llm.js +20 -3
  81. package/dist/esm/models/chrome_prompt_utils.js +51 -2
  82. package/dist/esm/models/gemini_continuation.js +205 -0
  83. package/dist/esm/models/gemini_llm_connection.js +6 -5
  84. package/dist/esm/planners/base_planner.js +28 -0
  85. package/dist/esm/planners/built_in_planner.js +57 -0
  86. package/dist/esm/planners/plan_re_act_planner.js +145 -0
  87. package/dist/esm/plugins/base_plugin.js +46 -61
  88. package/dist/esm/plugins/debug_logging_plugin.js +615 -0
  89. package/dist/esm/plugins/plugin_manager.js +1 -1
  90. package/dist/esm/plugins/tool_call_integrity_plugin.js +339 -0
  91. package/dist/esm/runner/runner.js +20 -3
  92. package/dist/esm/tools/example_tool.js +1 -1
  93. package/dist/esm/tools/mcp/mcp_session_manager.js +14 -8
  94. package/dist/esm/tools/openapi_tool/auth/auth_helpers.js +272 -1
  95. package/dist/esm/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.js +15 -1
  96. package/dist/esm/tools/openapi_tool/auth/credential_exchangers/base_auth_credential_exchanger.js +43 -0
  97. package/dist/esm/tools/openapi_tool/auth/credential_exchangers/oauth2_exchanger.js +93 -0
  98. package/dist/esm/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.js +10 -12
  99. package/dist/esm/tools/openapi_tool/common/common.js +112 -0
  100. package/dist/esm/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.js +11 -6
  101. package/dist/esm/tools/openapi_tool/openapi_spec_parser/operation_parser.js +80 -13
  102. package/dist/esm/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.js +146 -33
  103. package/dist/esm/tools/openapi_tool/openapi_toolset.js +14 -2
  104. package/dist/esm/tools/openapi_tool/rest_api_tool.js +89 -28
  105. package/dist/esm/tools/skill/skill_toolset.js +2 -0
  106. package/dist/esm/tools/tool_context.js +15 -0
  107. package/dist/esm/tools/webmcp/index.js +10 -0
  108. package/dist/esm/tools/webmcp/webmcp_tool.js +131 -0
  109. package/dist/esm/tools/webmcp/webmcp_toolset.js +89 -0
  110. package/dist/esm/tools/webmcp/webmcp_types.js +22 -0
  111. package/dist/esm/utils/case_utils.js +49 -1
  112. package/dist/esm/utils/client_labels.js +65 -0
  113. package/dist/esm/utils/gemini_schema_util.js +102 -3
  114. package/dist/esm/utils/hash_utils.js +45 -0
  115. package/dist/esm/utils/live_connection_utils.js +11 -10
  116. package/dist/esm/utils/model_name.js +15 -2
  117. package/dist/esm/utils/simple_zod_to_json.js +5 -0
  118. package/dist/esm/version.js +1 -1
  119. package/dist/types/agents/llm_agent.d.ts +12 -1
  120. package/dist/types/agents/processors/nl_planning_processor.d.ts +30 -0
  121. package/dist/types/auth/auth_credential.d.ts +9 -0
  122. package/dist/types/common.d.ts +19 -4
  123. package/dist/types/events/abort_events.d.ts +43 -0
  124. package/dist/types/events/internal_metadata.d.ts +78 -0
  125. package/dist/types/examples/base_example_provider.d.ts +2 -2
  126. package/dist/types/examples/example.d.ts +75 -0
  127. package/dist/types/examples/example_util.d.ts +13 -2
  128. package/dist/types/examples/index.d.ts +18 -0
  129. package/dist/types/examples/vertex_ai_example_store.d.ts +7 -0
  130. package/dist/types/index.d.ts +12 -3
  131. package/dist/types/memory/memory_entry.d.ts +15 -0
  132. package/dist/types/memory/vertex_ai_memory_bank_service.d.ts +9 -0
  133. package/dist/types/models/chrome_prompt_utils.d.ts +29 -0
  134. package/dist/types/models/gemini_continuation.d.ts +59 -0
  135. package/dist/types/planners/base_planner.d.ts +50 -0
  136. package/dist/types/planners/built_in_planner.d.ts +53 -0
  137. package/dist/types/planners/plan_re_act_planner.d.ts +21 -0
  138. package/dist/types/plugins/base_plugin.d.ts +52 -52
  139. package/dist/types/plugins/debug_logging_plugin.d.ts +157 -0
  140. package/dist/types/plugins/tool_call_integrity_plugin.d.ts +116 -0
  141. package/dist/types/skills/skill_registry.d.ts +8 -0
  142. package/dist/types/tools/openapi_tool/auth/auth_helpers.d.ts +110 -1
  143. package/dist/types/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.d.ts +32 -2
  144. package/dist/types/tools/openapi_tool/auth/credential_exchangers/base_auth_credential_exchanger.d.ts +35 -0
  145. package/dist/types/tools/openapi_tool/auth/credential_exchangers/oauth2_exchanger.d.ts +54 -0
  146. package/dist/types/tools/openapi_tool/common/common.d.ts +60 -0
  147. package/dist/types/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.d.ts +7 -0
  148. package/dist/types/tools/openapi_tool/openapi_spec_parser/operation_parser.d.ts +56 -2
  149. package/dist/types/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.d.ts +73 -5
  150. package/dist/types/tools/openapi_tool/openapi_toolset.d.ts +11 -0
  151. package/dist/types/tools/openapi_tool/rest_api_tool.d.ts +42 -8
  152. package/dist/types/tools/tool_context.d.ts +19 -0
  153. package/dist/types/tools/webmcp/index.d.ts +12 -0
  154. package/dist/types/tools/webmcp/webmcp_tool.d.ts +47 -0
  155. package/dist/types/tools/webmcp/webmcp_toolset.d.ts +67 -0
  156. package/dist/types/tools/webmcp/webmcp_types.d.ts +69 -0
  157. package/dist/types/utils/case_utils.d.ts +36 -0
  158. package/dist/types/utils/client_labels.d.ts +33 -0
  159. package/dist/types/utils/gemini_schema_util.d.ts +13 -0
  160. package/dist/types/utils/hash_utils.d.ts +25 -0
  161. package/dist/types/utils/logger.d.ts +10 -2
  162. package/dist/types/utils/model_name.d.ts +30 -2
  163. package/dist/types/version.d.ts +1 -1
  164. package/dist/web/index_web.js +113 -80
  165. package/dist/web/index_web.js.map +4 -4
  166. package/package.json +8 -2
@@ -0,0 +1,364 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2026 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ "use strict";
8
+ var __defProp = Object.defineProperty;
9
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
10
+ var __getOwnPropNames = Object.getOwnPropertyNames;
11
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
12
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
13
+ var __export = (target, all) => {
14
+ for (var name in all)
15
+ __defProp(target, name, { get: all[name], enumerable: true });
16
+ };
17
+ var __copyProps = (to, from, except, desc) => {
18
+ if (from && typeof from === "object" || typeof from === "function") {
19
+ for (let key of __getOwnPropNames(from))
20
+ if (!__hasOwnProp.call(to, key) && key !== except)
21
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
22
+ }
23
+ return to;
24
+ };
25
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
26
+ var tool_call_integrity_plugin_exports = {};
27
+ __export(tool_call_integrity_plugin_exports, {
28
+ TOOL_CALL_HMAC_METADATA_KEY: () => TOOL_CALL_HMAC_METADATA_KEY,
29
+ ToolCallIntegrityError: () => ToolCallIntegrityError,
30
+ ToolCallIntegrityPlugin: () => ToolCallIntegrityPlugin
31
+ });
32
+ module.exports = __toCommonJS(tool_call_integrity_plugin_exports);
33
+ var import_event = require("../events/event.js");
34
+ var import_internal_metadata = require("../events/internal_metadata.js");
35
+ var import_logger = require("../utils/logger.js");
36
+ var import_base_plugin = require("./base_plugin.js");
37
+ /**
38
+ * @license
39
+ * Copyright 2026 Google LLC
40
+ * SPDX-License-Identifier: Apache-2.0
41
+ */
42
+ const TOOL_CALL_HMAC_METADATA_KEY = `${import_internal_metadata.INTERNAL_METADATA_PREFIX}fc_hmac`;
43
+ const STAMP_PREFIX = "v1:";
44
+ const _ToolCallIntegrityError = class _ToolCallIntegrityError extends Error {
45
+ constructor(message) {
46
+ super(message);
47
+ this.name = "ToolCallIntegrityError";
48
+ }
49
+ };
50
+ __name(_ToolCallIntegrityError, "ToolCallIntegrityError");
51
+ let ToolCallIntegrityError = _ToolCallIntegrityError;
52
+ function jsonForm(value) {
53
+ const json = JSON.stringify(value);
54
+ return json === void 0 ? null : JSON.parse(json);
55
+ }
56
+ __name(jsonForm, "jsonForm");
57
+ function canonicalJson(value) {
58
+ if (Array.isArray(value)) {
59
+ return `[${value.map((item) => canonicalJson(item)).join(",")}]`;
60
+ }
61
+ if (value !== null && typeof value === "object") {
62
+ const object = value;
63
+ const entries = Object.keys(object).sort().map((key) => `${JSON.stringify(key)}:${canonicalJson(object[key])}`);
64
+ return `{${entries.join(",")}}`;
65
+ }
66
+ return JSON.stringify(value);
67
+ }
68
+ __name(canonicalJson, "canonicalJson");
69
+ function jsonArgs(args) {
70
+ const json = jsonForm(args != null ? args : {});
71
+ return json != null ? json : {};
72
+ }
73
+ __name(jsonArgs, "jsonArgs");
74
+ function canonicalPayload(session, event, functionCall) {
75
+ var _a, _b, _c, _d, _e;
76
+ return new TextEncoder().encode(
77
+ canonicalJson({
78
+ app_name: session.appName,
79
+ user_id: session.userId,
80
+ session_id: session.id,
81
+ invocation_id: (_a = event.invocationId) != null ? _a : null,
82
+ branch: (_b = event.branch) != null ? _b : null,
83
+ author: (_c = event.author) != null ? _c : null,
84
+ name: (_d = functionCall.name) != null ? _d : null,
85
+ id: (_e = functionCall.id) != null ? _e : null,
86
+ args: jsonArgs(functionCall.args)
87
+ })
88
+ );
89
+ }
90
+ __name(canonicalPayload, "canonicalPayload");
91
+ function isStampMap(value) {
92
+ return value !== null && typeof value === "object" && !Array.isArray(value) && Object.values(value).every(
93
+ // eslint-disable-next-line no-control-regex
94
+ (stamp) => typeof stamp === "string" && /^[\x00-\x7f]*$/.test(stamp)
95
+ );
96
+ }
97
+ __name(isStampMap, "isStampMap");
98
+ function isRestored(event) {
99
+ var _a;
100
+ return Boolean((_a = event.customMetadata) == null ? void 0 : _a[import_internal_metadata.RESTORED_EVENT_KEY]);
101
+ }
102
+ __name(isRestored, "isRestored");
103
+ function getStamps(event) {
104
+ var _a;
105
+ const stamps = (_a = event.customMetadata) == null ? void 0 : _a[TOOL_CALL_HMAC_METADATA_KEY];
106
+ if (stamps === void 0 || stamps === null) {
107
+ return {};
108
+ }
109
+ if (!isStampMap(stamps)) {
110
+ throw new ToolCallIntegrityError(
111
+ `Event '${event.id}' has a malformed integrity stamp.`
112
+ );
113
+ }
114
+ return stamps;
115
+ }
116
+ __name(getStamps, "getStamps");
117
+ function hasStamp(stamps, callId) {
118
+ return Object.hasOwn(stamps, callId);
119
+ }
120
+ __name(hasStamp, "hasStamp");
121
+ function toHex(bytes) {
122
+ return Array.from(
123
+ new Uint8Array(bytes),
124
+ (b) => b.toString(16).padStart(2, "0")
125
+ ).join("");
126
+ }
127
+ __name(toHex, "toHex");
128
+ function fromHex(hex) {
129
+ if (!/^[0-9a-f]{64}$/.test(hex)) {
130
+ return void 0;
131
+ }
132
+ const bytes = new Uint8Array(hex.length / 2);
133
+ for (let i = 0; i < bytes.length; i++) {
134
+ bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
135
+ }
136
+ return bytes;
137
+ }
138
+ __name(fromHex, "fromHex");
139
+ function getSubtleCrypto() {
140
+ var _a;
141
+ const subtle = (_a = globalThis.crypto) == null ? void 0 : _a.subtle;
142
+ if (!subtle) {
143
+ throw new Error(
144
+ "ToolCallIntegrityPlugin requires the Web Crypto API (globalThis.crypto.subtle), which is not available in this environment."
145
+ );
146
+ }
147
+ return subtle;
148
+ }
149
+ __name(getSubtleCrypto, "getSubtleCrypto");
150
+ const _ToolCallIntegrityPlugin = class _ToolCallIntegrityPlugin extends import_base_plugin.BasePlugin {
151
+ /**
152
+ * @throws {Error} If `secretKey` is empty or contains a key that is not
153
+ * non-empty bytes.
154
+ */
155
+ constructor(options) {
156
+ var _a, _b;
157
+ super((_a = options.name) != null ? _a : "tool_call_integrity");
158
+ const secretKey = options.secretKey;
159
+ const rawKeys = secretKey instanceof Uint8Array ? [secretKey] : Array.isArray(secretKey) ? [...secretKey] : [];
160
+ if (rawKeys.length === 0 || !rawKeys.every((key) => key instanceof Uint8Array && key.length > 0)) {
161
+ throw new Error(
162
+ "secretKey must be non-empty bytes or a non-empty list of them."
163
+ );
164
+ }
165
+ this.keys = rawKeys.map((key) => new Uint8Array(key));
166
+ this.allowUnstampedCalls = (_b = options.allowUnstampedCalls) != null ? _b : false;
167
+ }
168
+ /**
169
+ * Returns a copy with internal metadata removed and marked as restored.
170
+ *
171
+ * Use it on events that your code copies into another session, so that the
172
+ * history passes the integrity check. The copy only keeps the history: its
173
+ * function calls are never executed.
174
+ *
175
+ * @param event The event to copy. It is not modified.
176
+ * @returns The marked copy, ready to append to the new session.
177
+ */
178
+ static prepareRestoredEvent(event) {
179
+ return (0, import_internal_metadata.markRestored)((0, import_event.createEvent)(structuredClone(event)));
180
+ }
181
+ getCryptoKeys() {
182
+ if (!this.cryptoKeys) {
183
+ const subtle = getSubtleCrypto();
184
+ this.cryptoKeys = Promise.all(
185
+ this.keys.map(
186
+ (key) => subtle.importKey("raw", key, { name: "HMAC", hash: "SHA-256" }, false, [
187
+ "sign",
188
+ "verify"
189
+ ])
190
+ )
191
+ );
192
+ }
193
+ return this.cryptoKeys;
194
+ }
195
+ async sign(payload) {
196
+ const [primary] = await this.getCryptoKeys();
197
+ const digest = await getSubtleCrypto().sign("HMAC", primary, payload);
198
+ return STAMP_PREFIX + toHex(digest);
199
+ }
200
+ /** `subtle.verify` compares in constant time. */
201
+ async verify(payload, stamp) {
202
+ if (!stamp.startsWith(STAMP_PREFIX)) {
203
+ return false;
204
+ }
205
+ const signature = fromHex(stamp.slice(STAMP_PREFIX.length));
206
+ if (!signature) {
207
+ return false;
208
+ }
209
+ const subtle = getSubtleCrypto();
210
+ for (const key of await this.getCryptoKeys()) {
211
+ if (await subtle.verify("HMAC", key, signature, payload)) {
212
+ return true;
213
+ }
214
+ }
215
+ return false;
216
+ }
217
+ unstamped(name, callId) {
218
+ const message = `${name} call '${callId}' has no integrity stamp.`;
219
+ if (!this.allowUnstampedCalls) {
220
+ throw new ToolCallIntegrityError(message);
221
+ }
222
+ import_logger.logger.warn(message);
223
+ }
224
+ async onEventCallback({
225
+ invocationContext,
226
+ event
227
+ }) {
228
+ var _a;
229
+ const session = invocationContext.session;
230
+ const calls = (0, import_event.getFunctionCalls)(event).filter((fc) => fc.id);
231
+ if (!event.partial) {
232
+ const seen = /* @__PURE__ */ new Set();
233
+ for (const fc of calls) {
234
+ if (seen.has(fc.id)) {
235
+ throw new ToolCallIntegrityError(
236
+ `Event '${event.id}' repeats function call ID '${fc.id}'.`
237
+ );
238
+ }
239
+ seen.add(fc.id);
240
+ }
241
+ }
242
+ const stamps = {};
243
+ for (const fc of calls) {
244
+ stamps[fc.id] = await this.sign(canonicalPayload(session, event, fc));
245
+ }
246
+ if (Object.keys(stamps).length > 0) {
247
+ event.customMetadata = {
248
+ ...(_a = event.customMetadata) != null ? _a : {},
249
+ [TOOL_CALL_HMAC_METADATA_KEY]: stamps
250
+ };
251
+ }
252
+ return void 0;
253
+ }
254
+ async beforeRunCallback({
255
+ invocationContext
256
+ }) {
257
+ const session = invocationContext.session;
258
+ for (const event of session.events) {
259
+ const calls = (0, import_event.getFunctionCalls)(event);
260
+ if (calls.length === 0 || isRestored(event)) {
261
+ continue;
262
+ }
263
+ const stamps = getStamps(event);
264
+ const verified = /* @__PURE__ */ new Set();
265
+ for (const fc of calls) {
266
+ if (!fc.id) {
267
+ continue;
268
+ }
269
+ if (!hasStamp(stamps, fc.id)) {
270
+ this.unstamped(fc.name, fc.id);
271
+ continue;
272
+ }
273
+ if (verified.has(fc.id)) {
274
+ throw new ToolCallIntegrityError(
275
+ `Event '${event.id}' has duplicate function call ID '${fc.id}'.`
276
+ );
277
+ }
278
+ const payload = canonicalPayload(session, event, fc);
279
+ if (!await this.verify(payload, stamps[fc.id])) {
280
+ throw new ToolCallIntegrityError(
281
+ `${fc.name} call '${fc.id}' does not match its integrity stamp.`
282
+ );
283
+ }
284
+ verified.add(fc.id);
285
+ }
286
+ const unmatched = Object.keys(stamps).filter((id) => !verified.has(id)).sort();
287
+ if (unmatched.length > 0) {
288
+ throw new ToolCallIntegrityError(
289
+ `Event '${event.id}' has integrity stamps with no matching call: ${JSON.stringify(unmatched)}.`
290
+ );
291
+ }
292
+ }
293
+ return void 0;
294
+ }
295
+ async beforeToolCallback({
296
+ tool,
297
+ toolArgs,
298
+ toolContext
299
+ }) {
300
+ const callId = toolContext.functionCallId;
301
+ if (!callId) {
302
+ throw new ToolCallIntegrityError(
303
+ `${tool.name} call has no ID, so its integrity cannot be checked.`
304
+ );
305
+ }
306
+ const session = toolContext.invocationContext.session;
307
+ const stored = [];
308
+ let restored = false;
309
+ for (const event of session.events) {
310
+ for (const fc of (0, import_event.getFunctionCalls)(event)) {
311
+ if (fc.id !== callId) {
312
+ continue;
313
+ }
314
+ if (isRestored(event)) {
315
+ restored = true;
316
+ } else {
317
+ stored.push([event, fc]);
318
+ }
319
+ }
320
+ }
321
+ if (stored.length === 0) {
322
+ if (restored) {
323
+ throw new ToolCallIntegrityError(
324
+ `${tool.name} call '${callId}' comes from restored history and cannot be executed.`
325
+ );
326
+ }
327
+ this.unstamped(tool.name, callId);
328
+ return void 0;
329
+ }
330
+ const trusted = [];
331
+ for (const [event, fc] of stored) {
332
+ const stamps = getStamps(event);
333
+ if (!hasStamp(stamps, callId)) {
334
+ this.unstamped(tool.name, callId);
335
+ } else if (!await this.verify(
336
+ canonicalPayload(session, event, fc),
337
+ stamps[callId]
338
+ )) {
339
+ throw new ToolCallIntegrityError(
340
+ `${tool.name} call '${callId}' does not match its integrity stamp.`
341
+ );
342
+ }
343
+ trusted.push(fc);
344
+ }
345
+ const executed = canonicalJson(jsonArgs(toolArgs));
346
+ const matches = trusted.some(
347
+ (fc) => fc.name === tool.name && canonicalJson(jsonArgs(fc.args)) === executed
348
+ );
349
+ if (!matches) {
350
+ throw new ToolCallIntegrityError(
351
+ `${tool.name} call '${callId}' arguments do not match its stored call.`
352
+ );
353
+ }
354
+ return void 0;
355
+ }
356
+ };
357
+ __name(_ToolCallIntegrityPlugin, "ToolCallIntegrityPlugin");
358
+ let ToolCallIntegrityPlugin = _ToolCallIntegrityPlugin;
359
+ // Annotate the CommonJS export names for ESM import in node:
360
+ 0 && (module.exports = {
361
+ TOOL_CALL_HMAC_METADATA_KEY,
362
+ ToolCallIntegrityError,
363
+ ToolCallIntegrityPlugin
364
+ });
@@ -45,6 +45,7 @@ var import_session_artifact_service = require("../artifacts/session_artifact_ser
45
45
  var import_built_in_code_executor = require("../code_executors/built_in_code_executor.js");
46
46
  var import_event = require("../events/event.js");
47
47
  var import_event_actions = require("../events/event_actions.js");
48
+ var import_internal_metadata = require("../events/internal_metadata.js");
48
49
  var import_plugin_manager = require("../plugins/plugin_manager.js");
49
50
  var import_tracing = require("../telemetry/tracing.js");
50
51
  var import_base_toolset = require("../tools/base_toolset.js");
@@ -230,7 +231,8 @@ const _Runner = class _Runner {
230
231
  author: "user",
231
232
  actions: stateDelta ? (0, import_event_actions.createEventActions)({ stateDelta }) : void 0,
232
233
  content: newMessage,
233
- customMetadata: params.customMetadata
234
+ // Callers cannot set ADK-internal keys.
235
+ customMetadata: (0, import_internal_metadata.withoutInternalMetadata)(params.customMetadata)
234
236
  })
235
237
  });
236
238
  if ((_e = params.abortSignal) == null ? void 0 : _e.aborted) {
@@ -274,7 +276,7 @@ const _Runner = class _Runner {
274
276
  event
275
277
  });
276
278
  const outputEvent = modifiedEvent ? {
277
- ...modifiedEvent,
279
+ ...withInternalMetadataOf(event, modifiedEvent),
278
280
  id: event.id,
279
281
  invocationId: event.invocationId,
280
282
  timestamp: event.timestamp,
@@ -526,7 +528,7 @@ const _Runner = class _Runner {
526
528
  if ((_d2 = params.abortSignal) == null ? void 0 : _d2.aborted) {
527
529
  return;
528
530
  }
529
- const eventToProcess = modifiedEvent != null ? modifiedEvent : event;
531
+ const eventToProcess = modifiedEvent ? withInternalMetadataOf(event, modifiedEvent) : event;
530
532
  if (!eventToProcess.partial && !isLiveModelMediaEventWithInlineData(eventToProcess)) {
531
533
  await this.sessionService.appendEvent({
532
534
  session,
@@ -545,6 +547,18 @@ const _Runner = class _Runner {
545
547
  };
546
548
  __name(_Runner, "Runner");
547
549
  let Runner = _Runner;
550
+ function withInternalMetadataOf(original, replacement) {
551
+ var _a2;
552
+ const internal = (0, import_internal_metadata.internalMetadata)(original.customMetadata);
553
+ if (Object.keys(internal).length === 0) {
554
+ return replacement;
555
+ }
556
+ return {
557
+ ...replacement,
558
+ customMetadata: { ...(_a2 = replacement.customMetadata) != null ? _a2 : {}, ...internal }
559
+ };
560
+ }
561
+ __name(withInternalMetadataOf, "withInternalMetadataOf");
548
562
  function isLiveModelMediaEventWithInlineData(event) {
549
563
  var _a2;
550
564
  const parts = (_a2 = event.content) == null ? void 0 : _a2.parts;
@@ -59,7 +59,7 @@ const _ExampleTool = class _ExampleTool extends import_base_tool.BaseTool {
59
59
  return;
60
60
  }
61
61
  (0, import_llm_request.appendInstructions)(llmRequest, [
62
- (0, import_example_util.buildExampleSi)(this.examples, parts[0].text, llmRequest.model)
62
+ await (0, import_example_util.buildExampleSi)(this.examples, parts[0].text, llmRequest.model)
63
63
  ]);
64
64
  }
65
65
  };
@@ -38,6 +38,7 @@ __export(mcp_session_manager_exports, {
38
38
  MCPSessionManager: () => MCPSessionManager
39
39
  });
40
40
  module.exports = __toCommonJS(mcp_session_manager_exports);
41
+ var import_client_labels = require("../../utils/client_labels.js");
41
42
  var import_error_utils = require("../../utils/error_utils.js");
42
43
  var import_logger = require("../../utils/logger.js");
43
44
  var import_optional_peer = require("../../utils/optional_peer.js");
@@ -54,13 +55,24 @@ function logTransportError(err) {
54
55
  import_logger.logger.error("MCP transport error: " + (0, import_error_utils.formatError)(err));
55
56
  }
56
57
  __name(logTransportError, "logTransportError");
58
+ function buildTransportOptions(params) {
59
+ const { transportOptions, header } = params;
60
+ const callerHeaders = (transportOptions == null ? void 0 : transportOptions.requestInit) === void 0 ? header : transportOptions.requestInit.headers;
61
+ return {
62
+ ...transportOptions,
63
+ requestInit: {
64
+ ...transportOptions == null ? void 0 : transportOptions.requestInit,
65
+ headers: (0, import_client_labels.mergeTrackingHeaders)(callerHeaders)
66
+ }
67
+ };
68
+ }
69
+ __name(buildTransportOptions, "buildTransportOptions");
57
70
  const _MCPSessionManager = class _MCPSessionManager {
58
71
  constructor(connectionParams) {
59
72
  this.activeSessions = /* @__PURE__ */ new Set();
60
73
  this.connectionParams = connectionParams;
61
74
  }
62
75
  async createSession() {
63
- var _a;
64
76
  const { Client } = await (0, import_optional_peer.loadOptionalPeer)(
65
77
  MCP_SDK,
66
78
  () => Promise.resolve().then(() => __toESM(require("@modelcontextprotocol/sdk/client/index.js"), 1))
@@ -81,19 +93,13 @@ const _MCPSessionManager = class _MCPSessionManager {
81
93
  break;
82
94
  }
83
95
  case "StreamableHTTPConnectionParams": {
84
- const options = (_a = this.connectionParams.transportOptions) != null ? _a : {};
85
- if (!options.requestInit && this.connectionParams.header !== void 0) {
86
- options.requestInit = {
87
- headers: this.connectionParams.header
88
- };
89
- }
90
96
  const { StreamableHTTPClientTransport } = await (0, import_optional_peer.loadOptionalPeer)(
91
97
  MCP_SDK,
92
98
  () => Promise.resolve().then(() => __toESM(require("@modelcontextprotocol/sdk/client/streamableHttp.js"), 1))
93
99
  );
94
100
  const transport = new StreamableHTTPClientTransport(
95
101
  new URL(this.connectionParams.url),
96
- options
102
+ buildTransportOptions(this.connectionParams)
97
103
  );
98
104
  transport.onerror = logTransportError;
99
105
  await client.connect(transport);