From f81b0653fc7d184f73f3f92961a1ea4905e580f3 Mon Sep 17 00:00:00 2001 From: adk-foundry-bot Date: Thu, 24 Sep 2026 14:16:18 -0700 Subject: [PATCH 1/3] feat(tools): add tool_context module aliasing ToolContext to Context adk-python exposes ToolContext and CallbackContext from google.adk.tools.tool_context, where both names bind to the unified Context class. A TypeScript user porting a Python agent writes import {ToolContext} from '@google/adk' and gets a compile error. Bind both names to Context as plain re-exports, so a Context the framework builds satisfies a ToolContext-typed parameter. Re-export the three auth names the Python module re-exports from the same path. --- core/src/common.ts | 1 + core/src/tools/tool_context.ts | 23 +++++++ core/test/tools/tool_context_test.ts | 99 ++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 core/src/tools/tool_context.ts create mode 100644 core/test/tools/tool_context_test.ts diff --git a/core/src/common.ts b/core/src/common.ts index 5a86d491a7..7ac4cd4629 100644 --- a/core/src/common.ts +++ b/core/src/common.ts @@ -361,6 +361,7 @@ export { isIntentMismatchError, } from './tools/tool_confirmation.js'; export type {IntentMismatchReason} from './tools/tool_confirmation.js'; +export {CallbackContext, ToolContext} from './tools/tool_context.js'; export {URL_CONTEXT, UrlContextTool} from './tools/url_context_tool.js'; export {VertexAiSearchTool} from './tools/vertex_ai_search_tool.js'; export type { diff --git a/core/src/tools/tool_context.ts b/core/src/tools/tool_context.ts new file mode 100644 index 0000000000..a7becc1ca5 --- /dev/null +++ b/core/src/tools/tool_context.ts @@ -0,0 +1,23 @@ +/** + * @license + * Copyright 2026 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * The tool-facing names for the unified agent context. + * + * `ToolContext` and `CallbackContext` are aliases of {@link Context}, not + * subclasses, so a context the framework builds satisfies either name. This + * mirrors `adk-python`'s `google.adk.tools.tool_context`, which binds + * `ToolContext = Context` and re-exports `CallbackContext` and the three auth + * names from the same module. + */ + +export { + Context as CallbackContext, + Context as ToolContext, +} from '../agents/context.js'; +export type {AuthCredential} from '../auth/auth_credential.js'; +export {AuthHandler} from '../auth/auth_handler.js'; +export type {AuthConfig} from '../auth/auth_tool.js'; diff --git a/core/test/tools/tool_context_test.ts b/core/test/tools/tool_context_test.ts new file mode 100644 index 0000000000..2288900cff --- /dev/null +++ b/core/test/tools/tool_context_test.ts @@ -0,0 +1,99 @@ +/** + * @license + * Copyright 2026 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { + AuthCredentialTypes, + AuthHandler, + CallbackContext, + Context, + createEventActions, + createSession, + InvocationContext, + PluginManager, + State, + ToolContext, +} from '@google/adk'; +import {describe, expect, it} from 'vitest'; +import type {AuthConfig, AuthCredential} from '../../src/tools/tool_context.js'; +import * as toolContextModule from '../../src/tools/tool_context.js'; + +function newInvocationContext() { + return new InvocationContext({ + invocationId: 'test-invocation', + session: createSession({ + id: 'test-session', + appName: 'tool-context-test', + userId: 'test-user', + }), + pluginManager: new PluginManager([]), + }); +} + +function takesToolContext(context: ToolContext): string | undefined { + return context.functionCallId; +} + +describe('tool_context', () => { + it('binds ToolContext to the Context class itself', () => { + expect(ToolContext).toBe(Context); + }); + + it('binds CallbackContext to the same class as ToolContext', () => { + expect(CallbackContext).toBe(Context); + expect(ToolContext).toBe(CallbackContext); + }); + + it('builds a working context through the ToolContext name', () => { + const eventActions = createEventActions(); + const context = new ToolContext({ + invocationContext: newInvocationContext(), + eventActions, + functionCallId: 'fc-1', + }); + + expect(context).toBeInstanceOf(Context); + expect(context.actions).toBe(eventActions); + expect(context.state).toBeInstanceOf(State); + + context.state.set('greeting', 'hello'); + + expect(context.state.get('greeting')).toBe('hello'); + expect(eventActions.stateDelta).toEqual({greeting: 'hello'}); + }); + + it('accepts a framework-built Context where a ToolContext is required', () => { + const context = new Context({ + invocationContext: newInvocationContext(), + functionCallId: 'fc-2', + }); + + expect(takesToolContext(context)).toBe('fc-2'); + }); + + it('re-exports the auth names the Python module re-exports', () => { + expect(toolContextModule.AuthHandler).toBe(AuthHandler); + + const credential: AuthCredential = { + authType: AuthCredentialTypes.API_KEY, + apiKey: 'placeholder-key', + }; + const config: AuthConfig = { + authScheme: {type: 'apiKey', in: 'header', name: 'X-Api-Key'}, + rawAuthCredential: credential, + credentialKey: 'test-credential', + }; + + expect(config.rawAuthCredential).toBe(credential); + }); + + it('exports exactly the ported names', () => { + expect(Object.keys(toolContextModule).sort()).toEqual([ + 'AuthHandler', + 'CallbackContext', + 'ToolContext', + ]); + }); +}); From 355c21234543f192bdf04e6fd6c3e92c32ce6187 Mon Sep 17 00:00:00 2001 From: adk-foundry-bot Date: Thu, 24 Sep 2026 14:17:28 -0700 Subject: [PATCH 2/3] docs(tools): add the ToolContext developer guide adk-python documents its guides under docs/guides///. adk-js had no such directory, so a reader porting a Python agent has nowhere to learn which adk-js name matches ToolContext. Add the guide and the index it needs. The unit test covers the sample. --- core/test/tools/tool_context_test.ts | 30 ++++++ docs/guides/README.md | 2 + docs/guides/tools/tool_context/index.md | 117 ++++++++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 docs/guides/tools/tool_context/index.md diff --git a/core/test/tools/tool_context_test.ts b/core/test/tools/tool_context_test.ts index 2288900cff..b775c510a8 100644 --- a/core/test/tools/tool_context_test.ts +++ b/core/test/tools/tool_context_test.ts @@ -11,12 +11,14 @@ import { Context, createEventActions, createSession, + FunctionTool, InvocationContext, PluginManager, State, ToolContext, } from '@google/adk'; import {describe, expect, it} from 'vitest'; +import {z} from 'zod/v4'; import type {AuthConfig, AuthCredential} from '../../src/tools/tool_context.js'; import * as toolContextModule from '../../src/tools/tool_context.js'; @@ -96,4 +98,32 @@ describe('tool_context', () => { 'ToolContext', ]); }); + + it('runs a tool whose execute declares a ToolContext parameter', async () => { + const rememberTool = new FunctionTool({ + name: 'remember', + description: 'Stores a value in session state.', + parameters: z.object({key: z.string(), value: z.string()}), + execute: async (input, toolContext?: ToolContext) => { + toolContext?.state.set(input.key, input.value); + return `stored ${input.key}`; + }, + }); + + const eventActions = createEventActions(); + const context = new ToolContext({ + invocationContext: newInvocationContext(), + eventActions, + functionCallId: 'fc-3', + }); + + const result = await rememberTool.runAsync({ + args: {key: 'city', value: 'Paris'}, + toolContext: context, + }); + + expect(result).toBe('stored city'); + expect(context.state.get('city')).toBe('Paris'); + expect(eventActions.stateDelta).toEqual({city: 'Paris'}); + }); }); diff --git a/docs/guides/README.md b/docs/guides/README.md index 468d533dcb..ff38caa8ed 100644 --- a/docs/guides/README.md +++ b/docs/guides/README.md @@ -10,6 +10,8 @@ This index is the only table of contents. A guide that is not listed here is unr ### Tools +- [ToolContext](tools/tool_context/index.md) - The tool-facing name for the unified agent context, and the auth names re-exported alongside it. + #### Retrieval Client-side retrieval tools. The agent calls a one-argument search function, your code answers it, and you choose the store, the chunking and the ranking. Start with `BaseRetrievalTool` if you are deciding between these and server-side retrieval. diff --git a/docs/guides/tools/tool_context/index.md b/docs/guides/tools/tool_context/index.md new file mode 100644 index 0000000000..4655a98b40 --- /dev/null +++ b/docs/guides/tools/tool_context/index.md @@ -0,0 +1,117 @@ +# ToolContext + +`ToolContext` is the name a tool uses for the context object the framework +hands it. It is an alias of `Context`, so the two names refer to one class. +Reach for it when you write a tool, or when you port an agent from the Python +SDK and want the import to line up. + +## Introduction + +adk-js has one context class, `Context`. It carries the invocation, the +delta-aware `State`, the `EventActions` the current call will emit, and the +artifact and memory helpers a tool calls. Earlier ADK versions split this role +across a tool-facing type and a callback-facing type. adk-js unified them, and +`google.adk.tools.tool_context` in the Python SDK made the same decision: it +binds `ToolContext = Context`. + +This module gives adk-js the same two names. `ToolContext` and +`CallbackContext` are plain re-exports of `Context`, not subclasses. That +matters for assignability in both directions. A `Context` the framework builds +satisfies a parameter typed `ToolContext`, and a `ToolContext` you construct in +a test is accepted everywhere `Context` is. A subclass would give you only one +of those directions. + +Use whichever name reads best at the call site. `Context` is the name the rest +of adk-js uses, so new TypeScript code can keep using it. `ToolContext` is what +the Python documentation and Python examples say, so it is the name to use when +you follow them. + +The module also re-exports `AuthHandler`, and the `AuthConfig` and +`AuthCredential` types, because Python re-exports them from the same path. A +tool that declares its credentials can take the context type and the auth types +from one import. + +## Get started + +This example writes to session state through a `ToolContext`, and reads the +pending delta back. It needs no model and no credentials. + +```ts +import { + Context, + createEventActions, + createSession, + FunctionTool, + InvocationContext, + PluginManager, + ToolContext, +} from '@google/adk'; +import {z} from 'zod/v4'; + +const rememberTool = new FunctionTool({ + name: 'remember', + description: 'Stores a value in session state.', + parameters: z.object({key: z.string(), value: z.string()}), + execute: async (input, toolContext?: ToolContext) => { + toolContext?.state.set(input.key, input.value); + return `stored ${input.key}`; + }, +}); + +const eventActions = createEventActions(); +const toolContext = new ToolContext({ + invocationContext: new InvocationContext({ + invocationId: 'invocation-1', + session: createSession({id: 'session-1', appName: 'notes'}), + pluginManager: new PluginManager([]), + }), + eventActions, + functionCallId: 'fc-1', +}); + +await rememberTool.runAsync({args: {key: 'city', value: 'Paris'}, toolContext}); + +toolContext.state.get('city'); // 'Paris' +eventActions.stateDelta; // {city: 'Paris'} +toolContext instanceof Context; // true +``` + +The tool receives the context adk-js built. Nothing converts or wraps it, +because `ToolContext` and `Context` are the same class. + +## What the module exports + +| Name | Kind | Binds to | +| ----------------- | ----- | ---------------- | +| `ToolContext` | class | `Context` | +| `CallbackContext` | class | `Context` | +| `AuthHandler` | class | `AuthHandler` | +| `AuthConfig` | type | `AuthConfig` | +| `AuthCredential` | type | `AuthCredential` | + +`ToolContext` and `CallbackContext` are also exported from the package root, so +`import {ToolContext} from '@google/adk'` works. The auth names already had a +root export of their own. + +The three identities hold at runtime, which is what makes the alias useful +rather than decorative: + +```ts +import {CallbackContext, Context, ToolContext} from '@google/adk'; + +ToolContext === Context; // true +CallbackContext === Context; // true +ToolContext === CallbackContext; // true +``` + +## Differences from the Python module + +Python resolves `AuthHandler`, `AuthConfig` and `AuthCredential` lazily through +a module-level `__getattr__`, which defers the import cost of the auth package. +TypeScript has no equivalent hook, and a dynamic `import()` would make the names +asynchronous and break the alias. The TypeScript module re-exports them +statically. There is no import cycle to avoid: `agents/context.ts` already +imports all three auth modules. + +Python's `__getattr__` also raises `AttributeError` for any other name. In +TypeScript that guarantee is static — an unlisted name is a compile error. From 1d4d3f32ddb85c798dbce4c260c7cb770c43044a Mon Sep 17 00:00:00 2001 From: adk-foundry-bot Date: Thu, 24 Sep 2026 14:17:47 -0700 Subject: [PATCH 3/3] refactor(tools): keep only the reachable part of the tool_context port The three auth re-exports could not be reached. The core package exports map is a closed allowlist with no entry for this path, and it publishes only dist, so no consumer can import them from here. All three already ship from the package root. Drop the guide tree with them. adk-js routes its documentation to adk.dev, and declaring a docs/guides convention is a package-layout decision, not part of this port. --- core/src/tools/tool_context.ts | 11 ++- core/test/tools/tool_context_test.ts | 20 ---- docs/guides/README.md | 2 - docs/guides/tools/tool_context/index.md | 117 ------------------------ 4 files changed, 6 insertions(+), 144 deletions(-) delete mode 100644 docs/guides/tools/tool_context/index.md diff --git a/core/src/tools/tool_context.ts b/core/src/tools/tool_context.ts index a7becc1ca5..4e14da8af4 100644 --- a/core/src/tools/tool_context.ts +++ b/core/src/tools/tool_context.ts @@ -10,14 +10,15 @@ * `ToolContext` and `CallbackContext` are aliases of {@link Context}, not * subclasses, so a context the framework builds satisfies either name. This * mirrors `adk-python`'s `google.adk.tools.tool_context`, which binds - * `ToolContext = Context` and re-exports `CallbackContext` and the three auth - * names from the same module. + * `ToolContext = Context`. + * + * Python also re-exports `AuthHandler`, `AuthConfig` and `AuthCredential` from + * this module. adk-js does not: the package `exports` map has no entry for this + * path, so no consumer can import them from here, and all three already ship + * from the package root. */ export { Context as CallbackContext, Context as ToolContext, } from '../agents/context.js'; -export type {AuthCredential} from '../auth/auth_credential.js'; -export {AuthHandler} from '../auth/auth_handler.js'; -export type {AuthConfig} from '../auth/auth_tool.js'; diff --git a/core/test/tools/tool_context_test.ts b/core/test/tools/tool_context_test.ts index b775c510a8..e11f9f6e59 100644 --- a/core/test/tools/tool_context_test.ts +++ b/core/test/tools/tool_context_test.ts @@ -5,8 +5,6 @@ */ import { - AuthCredentialTypes, - AuthHandler, CallbackContext, Context, createEventActions, @@ -19,7 +17,6 @@ import { } from '@google/adk'; import {describe, expect, it} from 'vitest'; import {z} from 'zod/v4'; -import type {AuthConfig, AuthCredential} from '../../src/tools/tool_context.js'; import * as toolContextModule from '../../src/tools/tool_context.js'; function newInvocationContext() { @@ -75,25 +72,8 @@ describe('tool_context', () => { expect(takesToolContext(context)).toBe('fc-2'); }); - it('re-exports the auth names the Python module re-exports', () => { - expect(toolContextModule.AuthHandler).toBe(AuthHandler); - - const credential: AuthCredential = { - authType: AuthCredentialTypes.API_KEY, - apiKey: 'placeholder-key', - }; - const config: AuthConfig = { - authScheme: {type: 'apiKey', in: 'header', name: 'X-Api-Key'}, - rawAuthCredential: credential, - credentialKey: 'test-credential', - }; - - expect(config.rawAuthCredential).toBe(credential); - }); - it('exports exactly the ported names', () => { expect(Object.keys(toolContextModule).sort()).toEqual([ - 'AuthHandler', 'CallbackContext', 'ToolContext', ]); diff --git a/docs/guides/README.md b/docs/guides/README.md index ff38caa8ed..468d533dcb 100644 --- a/docs/guides/README.md +++ b/docs/guides/README.md @@ -10,8 +10,6 @@ This index is the only table of contents. A guide that is not listed here is unr ### Tools -- [ToolContext](tools/tool_context/index.md) - The tool-facing name for the unified agent context, and the auth names re-exported alongside it. - #### Retrieval Client-side retrieval tools. The agent calls a one-argument search function, your code answers it, and you choose the store, the chunking and the ranking. Start with `BaseRetrievalTool` if you are deciding between these and server-side retrieval. diff --git a/docs/guides/tools/tool_context/index.md b/docs/guides/tools/tool_context/index.md deleted file mode 100644 index 4655a98b40..0000000000 --- a/docs/guides/tools/tool_context/index.md +++ /dev/null @@ -1,117 +0,0 @@ -# ToolContext - -`ToolContext` is the name a tool uses for the context object the framework -hands it. It is an alias of `Context`, so the two names refer to one class. -Reach for it when you write a tool, or when you port an agent from the Python -SDK and want the import to line up. - -## Introduction - -adk-js has one context class, `Context`. It carries the invocation, the -delta-aware `State`, the `EventActions` the current call will emit, and the -artifact and memory helpers a tool calls. Earlier ADK versions split this role -across a tool-facing type and a callback-facing type. adk-js unified them, and -`google.adk.tools.tool_context` in the Python SDK made the same decision: it -binds `ToolContext = Context`. - -This module gives adk-js the same two names. `ToolContext` and -`CallbackContext` are plain re-exports of `Context`, not subclasses. That -matters for assignability in both directions. A `Context` the framework builds -satisfies a parameter typed `ToolContext`, and a `ToolContext` you construct in -a test is accepted everywhere `Context` is. A subclass would give you only one -of those directions. - -Use whichever name reads best at the call site. `Context` is the name the rest -of adk-js uses, so new TypeScript code can keep using it. `ToolContext` is what -the Python documentation and Python examples say, so it is the name to use when -you follow them. - -The module also re-exports `AuthHandler`, and the `AuthConfig` and -`AuthCredential` types, because Python re-exports them from the same path. A -tool that declares its credentials can take the context type and the auth types -from one import. - -## Get started - -This example writes to session state through a `ToolContext`, and reads the -pending delta back. It needs no model and no credentials. - -```ts -import { - Context, - createEventActions, - createSession, - FunctionTool, - InvocationContext, - PluginManager, - ToolContext, -} from '@google/adk'; -import {z} from 'zod/v4'; - -const rememberTool = new FunctionTool({ - name: 'remember', - description: 'Stores a value in session state.', - parameters: z.object({key: z.string(), value: z.string()}), - execute: async (input, toolContext?: ToolContext) => { - toolContext?.state.set(input.key, input.value); - return `stored ${input.key}`; - }, -}); - -const eventActions = createEventActions(); -const toolContext = new ToolContext({ - invocationContext: new InvocationContext({ - invocationId: 'invocation-1', - session: createSession({id: 'session-1', appName: 'notes'}), - pluginManager: new PluginManager([]), - }), - eventActions, - functionCallId: 'fc-1', -}); - -await rememberTool.runAsync({args: {key: 'city', value: 'Paris'}, toolContext}); - -toolContext.state.get('city'); // 'Paris' -eventActions.stateDelta; // {city: 'Paris'} -toolContext instanceof Context; // true -``` - -The tool receives the context adk-js built. Nothing converts or wraps it, -because `ToolContext` and `Context` are the same class. - -## What the module exports - -| Name | Kind | Binds to | -| ----------------- | ----- | ---------------- | -| `ToolContext` | class | `Context` | -| `CallbackContext` | class | `Context` | -| `AuthHandler` | class | `AuthHandler` | -| `AuthConfig` | type | `AuthConfig` | -| `AuthCredential` | type | `AuthCredential` | - -`ToolContext` and `CallbackContext` are also exported from the package root, so -`import {ToolContext} from '@google/adk'` works. The auth names already had a -root export of their own. - -The three identities hold at runtime, which is what makes the alias useful -rather than decorative: - -```ts -import {CallbackContext, Context, ToolContext} from '@google/adk'; - -ToolContext === Context; // true -CallbackContext === Context; // true -ToolContext === CallbackContext; // true -``` - -## Differences from the Python module - -Python resolves `AuthHandler`, `AuthConfig` and `AuthCredential` lazily through -a module-level `__getattr__`, which defers the import cost of the auth package. -TypeScript has no equivalent hook, and a dynamic `import()` would make the names -asynchronous and break the alias. The TypeScript module re-exports them -statically. There is no import cycle to avoid: `agents/context.ts` already -imports all three auth modules. - -Python's `__getattr__` also raises `AttributeError` for any other name. In -TypeScript that guarantee is static — an unlisted name is a compile error.