Skip to content

Commit 7503cc9

Browse files
[Agent Builder] expose tool call context via handler (elastic#267743)
## Summary Add a new `callContext` property to tool handler context to expose the following info: - tool id - tool call id - call source (`user`/`agent`/`mcp`) Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent 90f832a commit 7503cc9

4 files changed

Lines changed: 40 additions & 5 deletions

File tree

x-pack/platform/packages/shared/agent-builder/agent-builder-server/tools/handler.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ import type {
2525
RunContext,
2626
} from '../runner';
2727
import type { IToolFileStore } from '../runner/filestore';
28-
import type { AttachmentStateManager } from '../attachments';
2928
import type { SkillsService } from '../runner/skills_service';
29+
import type { ToolCallSource } from '../runner/runner';
30+
import type { AttachmentStateManager } from '../attachments';
3031

3132
/**
3233
* Tool result as returned by the tool handler.
@@ -74,11 +75,21 @@ export type ToolHandlerFn<
7475
TResult extends ToolResult = ToolResult
7576
> = (args: TParams, context: ToolHandlerContext) => MaybePromise<ToolHandlerReturn<TResult>>;
7677

78+
export interface ToolHandlerCallContext {
79+
toolId: string;
80+
toolCallId: string;
81+
callSource: ToolCallSource;
82+
}
83+
7784
/**
7885
* Scoped context which can be used during tool execution to access
7986
* a panel of built-in services, such as a pre-scoped elasticsearch client.
8087
*/
8188
export interface ToolHandlerContext {
89+
/**
90+
* Information about the tool call
91+
*/
92+
callContext: ToolHandlerCallContext;
8293
/**
8394
* The request that was provided when initiating that tool execution.
8495
* Can be used to create scoped services not directly exposed by this context.

x-pack/platform/packages/shared/agent-builder/agent-builder-server/tools/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export {
2323
type ToolHandlerFn,
2424
type ToolHandlerReturn,
2525
type ToolHandlerContext,
26+
type ToolHandlerCallContext,
2627
type ToolHandlerResult,
2728
type ToolHandlerPromptReturn,
2829
type ToolHandlerStandardReturn,

x-pack/platform/plugins/shared/agent_builder/server/services/execution/runner/run_tool.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ import type {
2929
} from '@kbn/agent-builder-server/runner';
3030
import { generateFakeToolCallId } from '@kbn/agent-builder-genai-utils/langchain';
3131
import { createErrorResult } from '@kbn/agent-builder-server';
32-
import type { InternalToolDefinition } from '@kbn/agent-builder-server/tools';
32+
import type {
33+
InternalToolDefinition,
34+
ToolHandlerCallContext,
35+
} from '@kbn/agent-builder-server/tools';
3336
import { isToolHandlerStandardReturn } from '@kbn/agent-builder-server/tools';
3437
import { getToolResultId } from '@kbn/agent-builder-server/tools';
3538
import { ConfirmationStatus } from '@kbn/agent-builder-common/agents';
@@ -152,9 +155,11 @@ export const runInternalTool = async <TParams = Record<string, unknown>>({
152155
const startTime = Date.now();
153156
const toolHandlerContext = await createToolHandlerContext<TParams>({
154157
toolExecutionParams: {
155-
...toolExecutionParams,
156158
toolId: tool.id,
159+
toolCallId,
160+
source,
157161
toolParams: toolParams as TParams,
162+
onEvent: toolExecutionParams.onEvent ?? (() => undefined),
158163
},
159164
manager,
160165
});
@@ -248,14 +253,19 @@ export const runInternalTool = async <TParams = Record<string, unknown>>({
248253
return runToolReturn;
249254
};
250255

256+
type ToolHandlerExecutionParams<TParams = Record<string, unknown>> = Pick<
257+
Required<ScopedRunnerRunToolsParams<TParams>>,
258+
'onEvent' | 'toolId' | 'toolCallId' | 'toolParams' | 'source'
259+
>;
260+
251261
export const createToolHandlerContext = async <TParams = Record<string, unknown>>({
252262
manager,
253263
toolExecutionParams,
254264
}: {
255-
toolExecutionParams: ScopedRunnerRunToolsParams<TParams>;
265+
toolExecutionParams: ToolHandlerExecutionParams<TParams>;
256266
manager: RunnerManager;
257267
}): Promise<ToolHandlerContext> => {
258-
const { onEvent, toolId, toolCallId, toolParams } = toolExecutionParams;
268+
const { onEvent, toolId, toolCallId, toolParams, source } = toolExecutionParams;
259269
const {
260270
request,
261271
elasticsearch,
@@ -273,7 +283,15 @@ export const createToolHandlerContext = async <TParams = Record<string, unknown>
273283
toolManager,
274284
} = manager.deps;
275285
const spaceId = getCurrentSpaceId({ request, spaces });
286+
287+
const callContext: ToolHandlerCallContext = {
288+
toolId,
289+
toolCallId,
290+
callSource: source,
291+
};
292+
276293
return {
294+
callContext,
277295
request,
278296
spaceId,
279297
logger,

x-pack/platform/plugins/shared/agent_builder/server/test_utils/runner.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ export interface ToolHandlerContextMock extends ToolHandlerContext {
337337

338338
export const createToolHandlerContextMock = (): ToolHandlerContextMock => {
339339
return {
340+
callContext: {
341+
toolId: 'toolId',
342+
toolCallId: 'toolCallId',
343+
callSource: 'agent',
344+
},
340345
request: httpServerMock.createKibanaRequest(),
341346
spaceId: 'default',
342347
esClient: elasticsearchServiceMock.createScopedClusterClient(),

0 commit comments

Comments
 (0)