@google/adk 0.2.4 → 0.3.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 (191) hide show
  1. package/dist/cjs/agents/base_agent.js +52 -24
  2. package/dist/cjs/agents/callback_context.js +4 -1
  3. package/dist/cjs/agents/content_processor_utils.js +15 -7
  4. package/dist/cjs/agents/functions.js +79 -29
  5. package/dist/cjs/agents/invocation_context.js +3 -1
  6. package/dist/cjs/agents/llm_agent.js +188 -108
  7. package/dist/cjs/agents/loop_agent.js +18 -6
  8. package/dist/cjs/agents/parallel_agent.js +20 -7
  9. package/dist/cjs/agents/readonly_context.js +4 -1
  10. package/dist/cjs/agents/sequential_agent.js +34 -12
  11. package/dist/cjs/artifacts/gcs_artifact_service.js +28 -20
  12. package/dist/cjs/artifacts/in_memory_artifact_service.js +18 -4
  13. package/dist/cjs/auth/auth_handler.js +3 -1
  14. package/dist/cjs/code_executors/base_code_executor.js +14 -2
  15. package/dist/cjs/code_executors/built_in_code_executor.js +21 -5
  16. package/dist/cjs/code_executors/code_executor_context.js +5 -5
  17. package/dist/cjs/common.js +45 -0
  18. package/dist/cjs/events/event.js +1 -3
  19. package/dist/cjs/examples/base_example_provider.js +18 -2
  20. package/dist/cjs/examples/example_util.js +1 -1
  21. package/dist/cjs/index.js +19 -19
  22. package/dist/cjs/index.js.map +4 -4
  23. package/dist/cjs/memory/in_memory_memory_service.js +3 -1
  24. package/dist/cjs/models/base_llm.js +8 -4
  25. package/dist/cjs/models/gemini_llm_connection.js +1 -0
  26. package/dist/cjs/models/google_llm.js +3 -3
  27. package/dist/cjs/plugins/base_plugin.js +71 -49
  28. package/dist/cjs/plugins/logging_plugin.js +50 -13
  29. package/dist/cjs/plugins/plugin_manager.js +56 -24
  30. package/dist/cjs/plugins/security_plugin.js +5 -2
  31. package/dist/cjs/runner/runner.js +126 -101
  32. package/dist/cjs/sessions/in_memory_session_service.js +38 -14
  33. package/dist/cjs/telemetry/google_cloud.js +7 -9
  34. package/dist/cjs/telemetry/setup.js +15 -7
  35. package/dist/cjs/telemetry/tracing.js +37 -15
  36. package/dist/cjs/tools/agent_tool.js +26 -13
  37. package/dist/cjs/tools/base_tool.js +19 -7
  38. package/dist/cjs/tools/forwarding_artifact_service.js +1 -1
  39. package/dist/cjs/tools/function_tool.js +15 -7
  40. package/dist/cjs/tools/google_search_tool.js +8 -4
  41. package/dist/cjs/tools/mcp/mcp_session_manager.js +16 -10
  42. package/dist/cjs/tools/mcp/mcp_tool.js +1 -3
  43. package/dist/cjs/tools/mcp/mcp_toolset.js +1 -1
  44. package/dist/cjs/tools/tool_context.js +4 -9
  45. package/dist/cjs/utils/env_aware_utils.js +1 -1
  46. package/dist/cjs/utils/gemini_schema_util.js +10 -4
  47. package/dist/cjs/utils/logger.js +47 -3
  48. package/dist/cjs/utils/simple_zod_to_json.js +100 -141
  49. package/dist/cjs/utils/variant_utils.js +1 -1
  50. package/dist/cjs/version.js +1 -1
  51. package/dist/esm/agents/base_agent.js +57 -25
  52. package/dist/esm/agents/callback_context.js +4 -1
  53. package/dist/esm/agents/content_processor_utils.js +25 -9
  54. package/dist/esm/agents/functions.js +83 -29
  55. package/dist/esm/agents/invocation_context.js +3 -1
  56. package/dist/esm/agents/llm_agent.js +228 -116
  57. package/dist/esm/agents/loop_agent.js +16 -5
  58. package/dist/esm/agents/parallel_agent.js +18 -6
  59. package/dist/esm/agents/readonly_context.js +4 -1
  60. package/dist/esm/agents/sequential_agent.js +33 -12
  61. package/dist/esm/artifacts/gcs_artifact_service.js +28 -20
  62. package/dist/esm/artifacts/in_memory_artifact_service.js +18 -4
  63. package/dist/esm/auth/auth_handler.js +3 -1
  64. package/dist/esm/code_executors/base_code_executor.js +12 -1
  65. package/dist/esm/code_executors/built_in_code_executor.js +19 -4
  66. package/dist/esm/code_executors/code_executor_context.js +5 -5
  67. package/dist/esm/common.js +56 -11
  68. package/dist/esm/events/event.js +1 -3
  69. package/dist/esm/examples/base_example_provider.js +16 -1
  70. package/dist/esm/examples/example_util.js +4 -2
  71. package/dist/esm/index.js +19 -19
  72. package/dist/esm/index.js.map +4 -4
  73. package/dist/esm/memory/in_memory_memory_service.js +3 -1
  74. package/dist/esm/models/base_llm.js +8 -4
  75. package/dist/esm/models/gemini_llm_connection.js +1 -0
  76. package/dist/esm/models/google_llm.js +8 -4
  77. package/dist/esm/plugins/base_plugin.js +71 -49
  78. package/dist/esm/plugins/logging_plugin.js +55 -14
  79. package/dist/esm/plugins/plugin_manager.js +56 -24
  80. package/dist/esm/plugins/security_plugin.js +5 -2
  81. package/dist/esm/runner/runner.js +139 -105
  82. package/dist/esm/sessions/in_memory_session_service.js +41 -15
  83. package/dist/esm/telemetry/google_cloud.js +7 -9
  84. package/dist/esm/telemetry/setup.js +23 -9
  85. package/dist/esm/telemetry/tracing.js +37 -15
  86. package/dist/esm/tools/agent_tool.js +25 -13
  87. package/dist/esm/tools/base_tool.js +17 -6
  88. package/dist/esm/tools/forwarding_artifact_service.js +1 -1
  89. package/dist/esm/tools/function_tool.js +13 -8
  90. package/dist/esm/tools/google_search_tool.js +6 -3
  91. package/dist/esm/tools/long_running_tool.js +3 -1
  92. package/dist/esm/tools/mcp/mcp_session_manager.js +22 -12
  93. package/dist/esm/tools/mcp/mcp_tool.js +1 -3
  94. package/dist/esm/tools/mcp/mcp_toolset.js +1 -1
  95. package/dist/esm/tools/tool_context.js +4 -9
  96. package/dist/esm/utils/env_aware_utils.js +1 -1
  97. package/dist/esm/utils/gemini_schema_util.js +10 -4
  98. package/dist/esm/utils/logger.js +43 -2
  99. package/dist/esm/utils/simple_zod_to_json.js +102 -141
  100. package/dist/esm/utils/variant_utils.js +1 -1
  101. package/dist/esm/version.js +1 -1
  102. package/dist/types/agents/base_agent.d.ts +16 -4
  103. package/dist/types/agents/callback_context.d.ts +1 -1
  104. package/dist/types/agents/invocation_context.d.ts +4 -2
  105. package/dist/types/agents/llm_agent.d.ts +43 -31
  106. package/dist/types/agents/loop_agent.d.ts +17 -1
  107. package/dist/types/agents/parallel_agent.d.ts +17 -1
  108. package/dist/types/agents/sequential_agent.d.ts +17 -1
  109. package/dist/types/artifacts/in_memory_artifact_service.d.ts +3 -3
  110. package/dist/types/auth/auth_schemes.d.ts +5 -2
  111. package/dist/types/code_executors/base_code_executor.d.ts +14 -0
  112. package/dist/types/code_executors/built_in_code_executor.d.ts +20 -1
  113. package/dist/types/code_executors/code_executor_context.d.ts +2 -4
  114. package/dist/types/common.d.ts +40 -15
  115. package/dist/types/examples/base_example_provider.d.ts +16 -0
  116. package/dist/types/index.d.ts +3 -3
  117. package/dist/types/models/base_llm_connection.d.ts +1 -1
  118. package/dist/types/models/llm_response.d.ts +1 -1
  119. package/dist/types/plugins/base_plugin.d.ts +50 -40
  120. package/dist/types/plugins/logging_plugin.d.ts +12 -12
  121. package/dist/types/plugins/plugin_manager.d.ts +12 -12
  122. package/dist/types/plugins/security_plugin.d.ts +1 -1
  123. package/dist/types/runner/runner.d.ts +18 -10
  124. package/dist/types/sessions/in_memory_session_service.d.ts +5 -5
  125. package/dist/types/telemetry/setup.d.ts +1 -1
  126. package/dist/types/telemetry/tracing.d.ts +7 -6
  127. package/dist/types/tools/agent_tool.d.ts +15 -1
  128. package/dist/types/tools/base_tool.d.ts +15 -1
  129. package/dist/types/tools/base_toolset.d.ts +2 -1
  130. package/dist/types/tools/forwarding_artifact_service.d.ts +2 -2
  131. package/dist/types/tools/function_tool.d.ts +18 -4
  132. package/dist/types/tools/google_search_tool.d.ts +5 -6
  133. package/dist/types/tools/mcp/mcp_session_manager.d.ts +10 -3
  134. package/dist/types/tools/mcp/mcp_toolset.d.ts +1 -2
  135. package/dist/types/tools/tool_context.d.ts +1 -1
  136. package/dist/types/utils/gemini_schema_util.d.ts +4 -12
  137. package/dist/types/utils/logger.d.ts +11 -10
  138. package/dist/types/utils/simple_zod_to_json.d.ts +5 -4
  139. package/dist/types/version.d.ts +1 -1
  140. package/dist/web/agents/base_agent.js +103 -36
  141. package/dist/web/agents/callback_context.js +4 -1
  142. package/dist/web/agents/content_processor_utils.js +25 -9
  143. package/dist/web/agents/functions.js +83 -29
  144. package/dist/web/agents/invocation_context.js +3 -1
  145. package/dist/web/agents/llm_agent.js +282 -137
  146. package/dist/web/agents/loop_agent.js +16 -5
  147. package/dist/web/agents/parallel_agent.js +18 -6
  148. package/dist/web/agents/readonly_context.js +4 -1
  149. package/dist/web/agents/sequential_agent.js +33 -12
  150. package/dist/web/artifacts/gcs_artifact_service.js +25 -17
  151. package/dist/web/artifacts/in_memory_artifact_service.js +18 -4
  152. package/dist/web/auth/auth_handler.js +3 -1
  153. package/dist/web/code_executors/base_code_executor.js +12 -1
  154. package/dist/web/code_executors/built_in_code_executor.js +19 -4
  155. package/dist/web/code_executors/code_executor_context.js +5 -5
  156. package/dist/web/common.js +56 -11
  157. package/dist/web/events/event.js +1 -3
  158. package/dist/web/examples/base_example_provider.js +16 -1
  159. package/dist/web/examples/example_util.js +4 -2
  160. package/dist/web/index.js +1 -1
  161. package/dist/web/index.js.map +4 -4
  162. package/dist/web/memory/in_memory_memory_service.js +3 -1
  163. package/dist/web/models/base_llm.js +8 -4
  164. package/dist/web/models/gemini_llm_connection.js +1 -0
  165. package/dist/web/models/google_llm.js +8 -4
  166. package/dist/web/plugins/base_plugin.js +71 -49
  167. package/dist/web/plugins/logging_plugin.js +55 -14
  168. package/dist/web/plugins/plugin_manager.js +56 -24
  169. package/dist/web/plugins/security_plugin.js +5 -2
  170. package/dist/web/runner/runner.js +186 -119
  171. package/dist/web/sessions/in_memory_session_service.js +41 -15
  172. package/dist/web/telemetry/google_cloud.js +7 -9
  173. package/dist/web/telemetry/setup.js +23 -9
  174. package/dist/web/telemetry/tracing.js +37 -15
  175. package/dist/web/tools/agent_tool.js +25 -13
  176. package/dist/web/tools/base_tool.js +17 -6
  177. package/dist/web/tools/forwarding_artifact_service.js +1 -1
  178. package/dist/web/tools/function_tool.js +13 -8
  179. package/dist/web/tools/google_search_tool.js +6 -3
  180. package/dist/web/tools/long_running_tool.js +3 -1
  181. package/dist/web/tools/mcp/mcp_session_manager.js +22 -12
  182. package/dist/web/tools/mcp/mcp_tool.js +1 -3
  183. package/dist/web/tools/mcp/mcp_toolset.js +1 -1
  184. package/dist/web/tools/tool_context.js +4 -9
  185. package/dist/web/utils/env_aware_utils.js +1 -1
  186. package/dist/web/utils/gemini_schema_util.js +10 -4
  187. package/dist/web/utils/logger.js +43 -2
  188. package/dist/web/utils/simple_zod_to_json.js +102 -155
  189. package/dist/web/utils/variant_utils.js +1 -1
  190. package/dist/web/version.js +1 -1
  191. package/package.json +5 -3
@@ -18,7 +18,7 @@ var __objRest = (source, exclude) => {
18
18
  * Copyright 2025 Google LLC
19
19
  * SPDX-License-Identifier: Apache-2.0
20
20
  */
21
- import { trace, context } from "@opentelemetry/api";
21
+ import { context, trace } from "@opentelemetry/api";
22
22
  import { version } from "../version.js";
23
23
  const GEN_AI_AGENT_DESCRIPTION = "gen_ai.agent.description";
24
24
  const GEN_AI_AGENT_NAME = "gen_ai.agent.name";
@@ -28,14 +28,11 @@ const GEN_AI_TOOL_CALL_ID = "gen_ai.tool.call.id";
28
28
  const GEN_AI_TOOL_DESCRIPTION = "gen_ai.tool.description";
29
29
  const GEN_AI_TOOL_NAME = "gen_ai.tool.name";
30
30
  const GEN_AI_TOOL_TYPE = "gen_ai.tool.type";
31
- const tracer = trace.getTracer(
32
- "gcp.vertex.agent",
33
- version
34
- );
31
+ const tracer = trace.getTracer("gcp.vertex.agent", version);
35
32
  function safeJsonSerialize(obj) {
36
33
  try {
37
34
  return JSON.stringify(obj);
38
- } catch (error) {
35
+ } catch (_e) {
39
36
  return "<not serializable>";
40
37
  }
41
38
  }
@@ -112,7 +109,10 @@ function traceMergedToolCalls({
112
109
  "gcp.vertex.agent.llm_request": "{}",
113
110
  "gcp.vertex.agent.llm_response": "{}"
114
111
  });
115
- span.setAttribute("gcp.vertex.agent.tool_response", shouldAddRequestResponseToSpans() ? safeJsonSerialize(functionResponseEvent) : "{}");
112
+ span.setAttribute(
113
+ "gcp.vertex.agent.tool_response",
114
+ shouldAddRequestResponseToSpans() ? safeJsonSerialize(functionResponseEvent) : "{}"
115
+ );
116
116
  }
117
117
  function traceCallLlm({
118
118
  invocationContext,
@@ -136,14 +136,26 @@ function traceCallLlm({
136
136
  span.setAttribute("gen_ai.request.top_p", llmRequest.config.topP);
137
137
  }
138
138
  if (((_b = llmRequest.config) == null ? void 0 : _b.maxOutputTokens) !== void 0) {
139
- span.setAttribute("gen_ai.request.max_tokens", llmRequest.config.maxOutputTokens);
139
+ span.setAttribute(
140
+ "gen_ai.request.max_tokens",
141
+ llmRequest.config.maxOutputTokens
142
+ );
140
143
  }
141
- span.setAttribute("gcp.vertex.agent.llm_response", shouldAddRequestResponseToSpans() ? safeJsonSerialize(llmResponse) : "{}");
144
+ span.setAttribute(
145
+ "gcp.vertex.agent.llm_response",
146
+ shouldAddRequestResponseToSpans() ? safeJsonSerialize(llmResponse) : "{}"
147
+ );
142
148
  if (llmResponse.usageMetadata) {
143
- span.setAttribute("gen_ai.usage.input_tokens", llmResponse.usageMetadata.promptTokenCount || 0);
149
+ span.setAttribute(
150
+ "gen_ai.usage.input_tokens",
151
+ llmResponse.usageMetadata.promptTokenCount || 0
152
+ );
144
153
  }
145
154
  if ((_c = llmResponse.usageMetadata) == null ? void 0 : _c.candidatesTokenCount) {
146
- span.setAttribute("gen_ai.usage.output_tokens", llmResponse.usageMetadata.candidatesTokenCount);
155
+ span.setAttribute(
156
+ "gen_ai.usage.output_tokens",
157
+ llmResponse.usageMetadata.candidatesTokenCount
158
+ );
147
159
  }
148
160
  if (llmResponse.finishReason) {
149
161
  const finishReasonValue = typeof llmResponse.finishReason === "string" ? llmResponse.finishReason.toLowerCase() : String(llmResponse.finishReason).toLowerCase();
@@ -161,7 +173,10 @@ function traceSendData({
161
173
  "gcp.vertex.agent.invocation_id": invocationContext.invocationId,
162
174
  "gcp.vertex.agent.event_id": eventId
163
175
  });
164
- span.setAttribute("gcp.vertex.agent.data", shouldAddRequestResponseToSpans() ? safeJsonSerialize(data) : "{}");
176
+ span.setAttribute(
177
+ "gcp.vertex.agent.data",
178
+ shouldAddRequestResponseToSpans() ? safeJsonSerialize(data) : "{}"
179
+ );
165
180
  }
166
181
  function buildLlmRequestForTrace(llmRequest) {
167
182
  const result = {
@@ -181,7 +196,7 @@ function buildLlmRequestForTrace(llmRequest) {
181
196
  });
182
197
  return result;
183
198
  }
184
- function bindAsyncGenerator(ctx, generator) {
199
+ function bindOtelContextToAsyncGenerator(ctx, generator) {
185
200
  return {
186
201
  // Bind the next() method to execute within the provided context
187
202
  next: context.bind(ctx, generator.next.bind(generator)),
@@ -191,16 +206,23 @@ function bindAsyncGenerator(ctx, generator) {
191
206
  throw: context.bind(ctx, generator.throw.bind(generator)),
192
207
  // Ensure the async iterator symbol also returns a context-bound generator
193
208
  [Symbol.asyncIterator]() {
194
- return bindAsyncGenerator(ctx, generator[Symbol.asyncIterator]());
209
+ return bindOtelContextToAsyncGenerator(
210
+ ctx,
211
+ generator[Symbol.asyncIterator]()
212
+ );
195
213
  }
196
214
  };
197
215
  }
216
+ function runAsyncGeneratorWithOtelContext(otelContext, generatorFnContext, generatorFn) {
217
+ const generator = generatorFn.call(generatorFnContext);
218
+ return bindOtelContextToAsyncGenerator(otelContext, generator);
219
+ }
198
220
  function shouldAddRequestResponseToSpans() {
199
221
  const envValue = process.env.ADK_CAPTURE_MESSAGE_CONTENT_IN_SPANS || "true";
200
222
  return envValue === "true" || envValue === "1";
201
223
  }
202
224
  export {
203
- bindAsyncGenerator,
225
+ runAsyncGeneratorWithOtelContext,
204
226
  traceAgentInvocation,
205
227
  traceCallLlm,
206
228
  traceMergedToolCalls,
@@ -5,25 +5,33 @@ var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")])
5
5
  * Copyright 2025 Google LLC
6
6
  * SPDX-License-Identifier: Apache-2.0
7
7
  */
8
+ var _a, _b;
8
9
  import { Type } from "@google/genai";
9
- import { LlmAgent } from "../agents/llm_agent.js";
10
+ import { isLlmAgent } from "../agents/llm_agent.js";
10
11
  import { InMemoryMemoryService } from "../memory/in_memory_memory_service.js";
11
12
  import { Runner } from "../runner/runner.js";
12
13
  import { InMemorySessionService } from "../sessions/in_memory_session_service.js";
13
14
  import { GoogleLLMVariant } from "../utils/variant_utils.js";
14
15
  import { BaseTool } from "./base_tool.js";
15
16
  import { ForwardingArtifactService } from "./forwarding_artifact_service.js";
16
- class AgentTool extends BaseTool {
17
+ const AGENT_TOOL_SIGNATURE_SYMBOL = Symbol.for("google.adk.agentTool");
18
+ function isAgentTool(obj) {
19
+ return typeof obj === "object" && obj !== null && AGENT_TOOL_SIGNATURE_SYMBOL in obj && obj[AGENT_TOOL_SIGNATURE_SYMBOL] === true;
20
+ }
21
+ class AgentTool extends (_b = BaseTool, _a = AGENT_TOOL_SIGNATURE_SYMBOL, _b) {
17
22
  constructor(config) {
18
- super(
19
- { name: config.agent.name, description: config.agent.description || "" }
20
- );
23
+ super({
24
+ name: config.agent.name,
25
+ description: config.agent.description || ""
26
+ });
27
+ /** A unique symbol to identify ADK agent tool class. */
28
+ this[_a] = true;
21
29
  this.agent = config.agent;
22
30
  this.skipSummarization = config.skipSummarization || false;
23
31
  }
24
32
  _getDeclaration() {
25
33
  let declaration;
26
- if (this.agent instanceof LlmAgent && this.agent.inputSchema) {
34
+ if (isLlmAgent(this.agent) && this.agent.inputSchema) {
27
35
  declaration = {
28
36
  name: this.name,
29
37
  description: this.description,
@@ -48,17 +56,20 @@ class AgentTool extends BaseTool {
48
56
  };
49
57
  }
50
58
  if (this.apiVariant !== GoogleLLMVariant.GEMINI_API) {
51
- const hasOutputSchema = this.agent instanceof LlmAgent && this.agent.outputSchema;
59
+ const hasOutputSchema = isLlmAgent(this.agent) && this.agent.outputSchema;
52
60
  declaration.response = hasOutputSchema ? { type: Type.OBJECT } : { type: Type.STRING };
53
61
  }
54
62
  return declaration;
55
63
  }
56
- async runAsync({ args, toolContext }) {
57
- var _a, _b;
64
+ async runAsync({
65
+ args,
66
+ toolContext
67
+ }) {
68
+ var _a2, _b2;
58
69
  if (this.skipSummarization) {
59
70
  toolContext.actions.skipSummarization = true;
60
71
  }
61
- const hasInputSchema = this.agent instanceof LlmAgent && this.agent.inputSchema;
72
+ const hasInputSchema = isLlmAgent(this.agent) && this.agent.inputSchema;
62
73
  const content = {
63
74
  role: "user",
64
75
  parts: [
@@ -105,14 +116,15 @@ class AgentTool extends BaseTool {
105
116
  throw error[0];
106
117
  }
107
118
  }
108
- if (!((_b = (_a = lastEvent == null ? void 0 : lastEvent.content) == null ? void 0 : _a.parts) == null ? void 0 : _b.length)) {
119
+ if (!((_b2 = (_a2 = lastEvent == null ? void 0 : lastEvent.content) == null ? void 0 : _a2.parts) == null ? void 0 : _b2.length)) {
109
120
  return "";
110
121
  }
111
- const hasOutputSchema = this.agent instanceof LlmAgent && this.agent.outputSchema;
122
+ const hasOutputSchema = isLlmAgent(this.agent) && this.agent.outputSchema;
112
123
  const mergedText = lastEvent.content.parts.map((part) => part.text).filter((text) => text).join("\n");
113
124
  return hasOutputSchema ? JSON.parse(mergedText) : mergedText;
114
125
  }
115
126
  }
116
127
  export {
117
- AgentTool
128
+ AgentTool,
129
+ isAgentTool
118
130
  };
@@ -3,7 +3,13 @@
3
3
  * Copyright 2025 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
+ var _a;
6
7
  import { getGoogleLlmVariant } from "../utils/variant_utils.js";
8
+ const BASE_TOOL_SIGNATURE_SYMBOL = Symbol.for("google.adk.baseTool");
9
+ function isBaseTool(obj) {
10
+ return typeof obj === "object" && obj !== null && BASE_TOOL_SIGNATURE_SYMBOL in obj && obj[BASE_TOOL_SIGNATURE_SYMBOL] === true;
11
+ }
12
+ _a = BASE_TOOL_SIGNATURE_SYMBOL;
7
13
  class BaseTool {
8
14
  /**
9
15
  * Base constructor for a tool.
@@ -11,10 +17,12 @@ class BaseTool {
11
17
  * @param params The parameters for `BaseTool`.
12
18
  */
13
19
  constructor(params) {
14
- var _a;
20
+ /** A unique symbol to identify ADK base tool class. */
21
+ this[_a] = true;
22
+ var _a2;
15
23
  this.name = params.name;
16
24
  this.description = params.description;
17
- this.isLongRunning = (_a = params.isLongRunning) != null ? _a : false;
25
+ this.isLongRunning = (_a2 = params.isLongRunning) != null ? _a2 : false;
18
26
  }
19
27
  /**
20
28
  * Gets the OpenAPI specification of this tool in the form of a
@@ -41,7 +49,7 @@ class BaseTool {
41
49
  *
42
50
  * @param request The request to process the LLM request.
43
51
  */
44
- async processLlmRequest({ toolContext, llmRequest }) {
52
+ async processLlmRequest({ llmRequest }) {
45
53
  const functionDeclaration = this._getDeclaration();
46
54
  if (!functionDeclaration) {
47
55
  return;
@@ -69,9 +77,12 @@ class BaseTool {
69
77
  }
70
78
  }
71
79
  function findToolWithFunctionDeclarations(llmRequest) {
72
- var _a;
73
- return (((_a = llmRequest.config) == null ? void 0 : _a.tools) || []).find((tool) => "functionDeclarations" in tool);
80
+ var _a2;
81
+ return (((_a2 = llmRequest.config) == null ? void 0 : _a2.tools) || []).find(
82
+ (tool) => "functionDeclarations" in tool
83
+ );
74
84
  }
75
85
  export {
76
- BaseTool
86
+ BaseTool,
87
+ isBaseTool
77
88
  };
@@ -16,7 +16,7 @@ class ForwardingArtifactService {
16
16
  async loadArtifact(request) {
17
17
  return this.toolContext.loadArtifact(request.filename, request.version);
18
18
  }
19
- async listArtifactKeys(request) {
19
+ async listArtifactKeys() {
20
20
  return this.toolContext.listArtifacts();
21
21
  }
22
22
  async deleteArtifact(request) {
@@ -3,10 +3,8 @@
3
3
  * Copyright 2025 Google LLC
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
+ var _a, _b;
6
7
  import { Type } from "@google/genai";
7
- import {
8
- ZodObject
9
- } from "zod";
10
8
  import { isZodObject, zodObjectToSchema } from "../utils/simple_zod_to_json.js";
11
9
  import { BaseTool } from "./base_tool.js";
12
10
  function toSchema(parameters) {
@@ -18,14 +16,18 @@ function toSchema(parameters) {
18
16
  }
19
17
  return parameters;
20
18
  }
21
- class FunctionTool extends BaseTool {
19
+ const FUNCTION_TOOL_SIGNATURE_SYMBOL = Symbol.for("google.adk.functionTool");
20
+ function isFunctionTool(obj) {
21
+ return typeof obj === "object" && obj !== null && FUNCTION_TOOL_SIGNATURE_SYMBOL in obj && obj[FUNCTION_TOOL_SIGNATURE_SYMBOL] === true;
22
+ }
23
+ class FunctionTool extends (_b = BaseTool, _a = FUNCTION_TOOL_SIGNATURE_SYMBOL, _b) {
22
24
  /**
23
25
  * The constructor acts as the user-friendly factory.
24
26
  * @param options The configuration for the tool.
25
27
  */
26
28
  constructor(options) {
27
- var _a;
28
- const name = (_a = options.name) != null ? _a : options.execute.name;
29
+ var _a2;
30
+ const name = (_a2 = options.name) != null ? _a2 : options.execute.name;
29
31
  if (!name) {
30
32
  throw new Error(
31
33
  "Tool name cannot be empty. Either name the `execute` function or provide a `name`."
@@ -36,6 +38,8 @@ class FunctionTool extends BaseTool {
36
38
  description: options.description,
37
39
  isLongRunning: options.isLongRunning
38
40
  });
41
+ /** A unique symbol to identify ADK function tool class. */
42
+ this[_a] = true;
39
43
  this.execute = options.execute;
40
44
  this.parameters = options.parameters;
41
45
  }
@@ -55,7 +59,7 @@ class FunctionTool extends BaseTool {
55
59
  async runAsync(req) {
56
60
  try {
57
61
  let validatedArgs = req.args;
58
- if (this.parameters instanceof ZodObject) {
62
+ if (isZodObject(this.parameters)) {
59
63
  validatedArgs = this.parameters.parse(req.args);
60
64
  }
61
65
  return await this.execute(
@@ -69,5 +73,6 @@ class FunctionTool extends BaseTool {
69
73
  }
70
74
  }
71
75
  export {
72
- FunctionTool
76
+ FunctionTool,
77
+ isFunctionTool
73
78
  };
@@ -9,10 +9,12 @@ class GoogleSearchTool extends BaseTool {
9
9
  constructor() {
10
10
  super({ name: "google_search", description: "Google Search Tool" });
11
11
  }
12
- runAsync(request) {
12
+ runAsync() {
13
13
  return Promise.resolve();
14
14
  }
15
- async processLlmRequest({ toolContext, llmRequest }) {
15
+ async processLlmRequest({
16
+ llmRequest
17
+ }) {
16
18
  if (!llmRequest.model) {
17
19
  return;
18
20
  }
@@ -42,5 +44,6 @@ class GoogleSearchTool extends BaseTool {
42
44
  }
43
45
  const GOOGLE_SEARCH = new GoogleSearchTool();
44
46
  export {
45
- GOOGLE_SEARCH
47
+ GOOGLE_SEARCH,
48
+ GoogleSearchTool
46
49
  };
@@ -22,7 +22,9 @@ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
22
22
  * Copyright 2025 Google LLC
23
23
  * SPDX-License-Identifier: Apache-2.0
24
24
  */
25
- import { FunctionTool } from "./function_tool.js";
25
+ import {
26
+ FunctionTool
27
+ } from "./function_tool.js";
26
28
  const LONG_RUNNING_INSTRUCTION = "\n\nNOTE: This is a long-running operation. Do not call this tool again if it has already returned some intermediate or pending status.";
27
29
  class LongRunningFunctionTool extends FunctionTool {
28
30
  /**
@@ -4,13 +4,18 @@
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
6
  import { Client } from "@modelcontextprotocol/sdk/client/index.js";
7
- import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
8
- import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
7
+ import {
8
+ StdioClientTransport
9
+ } from "@modelcontextprotocol/sdk/client/stdio.js";
10
+ import {
11
+ StreamableHTTPClientTransport
12
+ } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
9
13
  class MCPSessionManager {
10
14
  constructor(connectionParams) {
11
15
  this.connectionParams = connectionParams;
12
16
  }
13
17
  async createSession() {
18
+ var _a;
14
19
  const client = new Client({ name: "MCPClient", version: "1.0.0" });
15
20
  switch (this.connectionParams.type) {
16
21
  case "StdioConnectionParams":
@@ -18,20 +23,25 @@ class MCPSessionManager {
18
23
  new StdioClientTransport(this.connectionParams.serverParams)
19
24
  );
20
25
  break;
21
- case "StreamableHTTPConnectionParams":
22
- const transportOptions = this.connectionParams.header ? {
23
- requestInit: {
26
+ case "StreamableHTTPConnectionParams": {
27
+ const options = (_a = this.connectionParams.transportOptions) != null ? _a : {};
28
+ if (!options.requestInit && this.connectionParams.header !== void 0) {
29
+ options.requestInit = {
24
30
  headers: this.connectionParams.header
25
- }
26
- } : void 0;
27
- await client.connect(new StreamableHTTPClientTransport(
28
- new URL(this.connectionParams.url),
29
- transportOptions
30
- ));
31
+ };
32
+ }
33
+ await client.connect(
34
+ new StreamableHTTPClientTransport(
35
+ new URL(this.connectionParams.url),
36
+ options
37
+ )
38
+ );
31
39
  break;
32
- default:
40
+ }
41
+ default: {
33
42
  const _exhaustiveCheck = this.connectionParams;
34
43
  break;
44
+ }
35
45
  }
36
46
  return client;
37
47
  }
@@ -12,8 +12,7 @@ class MCPTool extends BaseTool {
12
12
  this.mcpSessionManager = mcpSessionManager;
13
13
  }
14
14
  _getDeclaration() {
15
- let declaration;
16
- declaration = {
15
+ return {
17
16
  name: this.mcpTool.name,
18
17
  description: this.mcpTool.description,
19
18
  parameters: toGeminiSchema(this.mcpTool.inputSchema),
@@ -21,7 +20,6 @@ class MCPTool extends BaseTool {
21
20
  // https://modelcontextprotocol.io/specification/2025-06-18/server/tools#tool-result
22
21
  response: toGeminiSchema(this.mcpTool.outputSchema)
23
22
  };
24
- return declaration;
25
23
  }
26
24
  async runAsync(request) {
27
25
  const session = await this.mcpSessionManager.createSession();
@@ -12,7 +12,7 @@ class MCPToolset extends BaseToolset {
12
12
  super(toolFilter);
13
13
  this.mcpSessionManager = new MCPSessionManager(connectionParams);
14
14
  }
15
- async getTools(context) {
15
+ async getTools() {
16
16
  const session = await this.mcpSessionManager.createSession();
17
17
  const listResult = await session.listTools();
18
18
  logger.debug("number of tools: ".concat(listResult.tools.length));
@@ -18,15 +18,10 @@ class ToolContext extends CallbackContext {
18
18
  * @param params.toolConfirmation The tool confirmation of the current tool
19
19
  * call.
20
20
  */
21
- constructor({
22
- invocationContext,
23
- eventActions,
24
- functionCallId,
25
- toolConfirmation
26
- }) {
27
- super({ invocationContext, eventActions });
28
- this.functionCallId = functionCallId;
29
- this.toolConfirmation = toolConfirmation;
21
+ constructor(params) {
22
+ super(params);
23
+ this.functionCallId = params.functionCallId;
24
+ this.toolConfirmation = params.toolConfirmation;
30
25
  }
31
26
  get actions() {
32
27
  return this.eventActions;
@@ -36,7 +36,7 @@ function base64Decode(data) {
36
36
  function isBase64Encoded(data) {
37
37
  try {
38
38
  return base64Encode(base64Decode(data)) === data;
39
- } catch (e) {
39
+ } catch (_e) {
40
40
  return false;
41
41
  }
42
42
  }
@@ -5,12 +5,13 @@
5
5
  */
6
6
  import { Type } from "@google/genai";
7
7
  import { z } from "zod";
8
- const MCPToolSchema = z.object({
8
+ const MCPToolSchemaObject = z.object({
9
9
  type: z.literal("object"),
10
- properties: z.record(z.unknown()).optional(),
10
+ properties: z.record(z.string(), z.unknown()).optional(),
11
11
  required: z.string().array().optional()
12
12
  });
13
13
  function toGeminiType(mcpType) {
14
+ if (!mcpType) return Type.TYPE_UNSPECIFIED;
14
15
  switch (mcpType.toLowerCase()) {
15
16
  case "text":
16
17
  case "string":
@@ -51,12 +52,17 @@ function toGeminiSchema(mcpSchema) {
51
52
  }
52
53
  }
53
54
  const geminiType = toGeminiType(mcp.type);
54
- const geminiSchema = { type: geminiType, description: mcp.description };
55
+ const geminiSchema = {
56
+ type: geminiType,
57
+ description: mcp.description
58
+ };
55
59
  if (geminiType === Type.OBJECT) {
56
60
  geminiSchema.properties = {};
57
61
  if (mcp.properties) {
58
62
  for (const name in mcp.properties) {
59
- geminiSchema.properties[name] = recursiveConvert(mcp.properties[name]);
63
+ geminiSchema.properties[name] = recursiveConvert(
64
+ mcp.properties[name]
65
+ );
60
66
  }
61
67
  }
62
68
  geminiSchema.required = mcp.required;
@@ -61,6 +61,18 @@ class SimpleLogger {
61
61
  console.error(getColoredPrefix(3 /* ERROR */), ...args);
62
62
  }
63
63
  }
64
+ class NoOpLogger {
65
+ log(_level, ..._args) {
66
+ }
67
+ debug(..._args) {
68
+ }
69
+ info(..._args) {
70
+ }
71
+ warn(..._args) {
72
+ }
73
+ error(..._args) {
74
+ }
75
+ }
64
76
  const LOG_LEVEL_STR = {
65
77
  [0 /* DEBUG */]: "DEBUG",
66
78
  [1 /* INFO */]: "INFO",
@@ -81,9 +93,38 @@ const RESET_COLOR = "\x1B[0m";
81
93
  function getColoredPrefix(level) {
82
94
  return "".concat(CONSOLE_COLOR_MAP[level], "[ADK ").concat(LOG_LEVEL_STR[level], "]:").concat(RESET_COLOR);
83
95
  }
84
- const logger = new SimpleLogger();
96
+ let currentLogger = new SimpleLogger();
97
+ function setLogger(customLogger) {
98
+ currentLogger = customLogger != null ? customLogger : new NoOpLogger();
99
+ }
100
+ function getLogger() {
101
+ return currentLogger;
102
+ }
103
+ function resetLogger() {
104
+ currentLogger = new SimpleLogger();
105
+ }
106
+ const logger = {
107
+ log(level, ...args) {
108
+ currentLogger.log(level, ...args);
109
+ },
110
+ debug(...args) {
111
+ currentLogger.debug(...args);
112
+ },
113
+ info(...args) {
114
+ currentLogger.info(...args);
115
+ },
116
+ warn(...args) {
117
+ currentLogger.warn(...args);
118
+ },
119
+ error(...args) {
120
+ currentLogger.error(...args);
121
+ }
122
+ };
85
123
  export {
86
124
  LogLevel,
125
+ getLogger,
87
126
  logger,
88
- setLogLevel
127
+ resetLogger,
128
+ setLogLevel,
129
+ setLogger
89
130
  };