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.

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

Constructor

new 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

import {
Agent, AgentSession, tool, RunContext, defineAgent, runApp, JobContext,
} from '@signalwire/sdk/livewire';
interface SessionData {
notes: string[];
}
const addNote = tool<{ note: string }>({
description: 'Add a note to the session.',
parameters: { note: { type: 'string' } },
execute: (params, { ctx }) => {
const data = ctx.userData as SessionData;
data.notes.push(params.note);
return `Note saved. You have ${data.notes.length} note(s).`;
},
});
const agentDef = defineAgent({
entry: async (ctx: JobContext) => {
const agent = new Agent({
instructions: 'You are a note-taking assistant.',
tools: { addNote },
});
const session = new AgentSession<SessionData>({
userData: { notes: [] },
});
await session.start({ agent });
},
});
runApp(agentDef);