RunContext

View as MarkdownOpen in Claude

RunContext<UserData> is passed to tool handler functions via the context parameter’s ctx property. It provides access to the current AgentSession and its user data, letting tools read and modify session state.

1import { tool, RunContext } from '@signalwire/sdk/livewire';
2
3const savePreference = tool<{ color: string }>({
4 description: 'Save the user\'s favorite color.',
5 parameters: { color: { type: 'string' } },
6 execute: (params, { ctx }) => {
7 const data = ctx.userData as Record<string, string>;
8 data.favoriteColor = params.color;
9 return `Got it, your favorite color is ${params.color}.`;
10 },
11});

Constructor

1new RunContext<UserData>(session: AgentSession<UserData>)

The RunContext is constructed internally by AgentSession when dispatching tool calls. You do not need to create it manually.

Properties

session
AgentSession<UserData>

The AgentSession that owns this context.

userData
UserData

Shortcut for session.userData. Returns the user data attached to the session.

Example

1import {
2 Agent, AgentSession, tool, RunContext, defineAgent, runApp, JobContext,
3} from '@signalwire/sdk/livewire';
4
5interface SessionData {
6 notes: string[];
7}
8
9const addNote = tool<{ note: string }>({
10 description: 'Add a note to the session.',
11 parameters: { note: { type: 'string' } },
12 execute: (params, { ctx }) => {
13 const data = ctx.userData as SessionData;
14 data.notes.push(params.note);
15 return `Note saved. You have ${data.notes.length} note(s).`;
16 },
17});
18
19const agentDef = defineAgent({
20 entry: async (ctx: JobContext) => {
21 const agent = new Agent({
22 instructions: 'You are a note-taking assistant.',
23 tools: { addNote },
24 });
25
26 const session = new AgentSession<SessionData>({
27 userData: { notes: [] },
28 });
29
30 await session.start({ agent });
31 },
32});
33
34runApp(agentDef);