Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions core/src/tools/tool_context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @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`.
*
* 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';
109 changes: 109 additions & 0 deletions core/test/tools/tool_context_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import {
CallbackContext,
Context,
createEventActions,
createSession,
FunctionTool,
InvocationContext,
PluginManager,
State,
ToolContext,
} from '@google/adk';
import {describe, expect, it} from 'vitest';
import {z} from 'zod/v4';
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('exports exactly the ported names', () => {
expect(Object.keys(toolContextModule).sort()).toEqual([
'CallbackContext',
'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'});
});
});
Loading