Skip to content

Agent

Defined in: src/agent/agent.ts:170

Orchestrates the interaction between a model, a set of tools, and MCP clients. The Agent is responsible for managing the lifecycle of tools and clients and invoking the core decision-making loop.

  • InvokableAgent
new Agent(config?): Agent;

Defined in: src/agent/agent.ts:227

Creates an instance of the Agent.

ParameterTypeDescription
config?AgentConfigThe configuration for the agent.

Agent

messages: Message[];

Defined in: src/agent/agent.ts:177

The conversation history of messages between user and assistant.

LocalAgent.messages

readonly appState: StateStore;

Defined in: src/agent/agent.ts:182

App state storage accessible to tools and application logic. State is not passed to the model during inference.

LocalAgent.appState

model: Model;

Defined in: src/agent/agent.ts:188

The model provider used by the agent for inference.


optional systemPrompt?: SystemPrompt;

Defined in: src/agent/agent.ts:193

The system prompt to pass to the model provider.

LocalAgent.systemPrompt

readonly name: string;

Defined in: src/agent/agent.ts:198

The name of the agent.

InvokableAgent.name

readonly id: string;

Defined in: src/agent/agent.ts:203

The unique identifier of the agent instance.

LocalAgent.id

readonly optional description?: string;

Defined in: src/agent/agent.ts:208

Optional description of what the agent does.

InvokableAgent.description
get tools(): Tool[];

Defined in: src/agent/agent.ts:343

The tools this agent can use.

Tool[]


get toolRegistry(): ToolRegistry;

Defined in: src/agent/agent.ts:350

The tool registry for managing the agent’s tools.

ToolRegistry

LocalAgent.toolRegistry
addHook<T>(eventType, callback): HookCleanup;

Defined in: src/agent/agent.ts:297

Register a hook callback for a specific event type.

Type Parameter
T extends HookableEvent
ParameterTypeDescription
eventTypeHookableEventConstructor<T>The event class constructor to register the callback for
callbackHookCallback<T>The callback function to invoke when the event occurs

HookCleanup

Cleanup function that removes the callback when invoked

const agent = new Agent({ model })
const cleanup = agent.addHook(BeforeInvocationEvent, (event) => {
console.log('Invocation started')
})
// Later, to remove the hook:
cleanup()
LocalAgent.addHook

initialize(): Promise<void>;

Defined in: src/agent/agent.ts:301

Promise<void>


invoke(args, options?): Promise<AgentResult>;

Defined in: src/agent/agent.ts:372

Invokes the agent and returns the final result.

This is a convenience method that consumes the stream() method and returns only the final AgentResult. Use stream() if you need access to intermediate streaming events.

ParameterTypeDescription
argsInvokeArgsArguments for invoking the agent
options?InvokeOptionsOptional per-invocation options

Promise<AgentResult>

Promise that resolves to the final AgentResult

const agent = new Agent({ model, tools })
const result = await agent.invoke('What is 2 + 2?')
console.log(result.lastMessage) // Agent's response
InvokableAgent.invoke

stream(args, options?): AsyncGenerator<AgentStreamEvent, AgentResult, undefined>;

Defined in: src/agent/agent.ts:411

Streams the agent execution, yielding events and returning the final result.

The agent loop manages the conversation flow by:

  1. Streaming model responses and yielding all events
  2. Executing tools when the model requests them
  3. Continuing the loop until the model completes without tool use

Use this method when you need access to intermediate streaming events. For simple request/response without streaming, use invoke() instead.

An explicit goal of this method is to always leave the message array in a way that the agent can be reinvoked with a user prompt after this method completes. To that end assistant messages containing tool uses are only added after tool execution succeeds with valid toolResponses

ParameterTypeDescription
argsInvokeArgsArguments for invoking the agent
options?InvokeOptionsOptional per-invocation options

AsyncGenerator<AgentStreamEvent, AgentResult, undefined>

Async generator that yields AgentStreamEvent objects and returns AgentResult

const agent = new Agent({ model, tools })
for await (const event of agent.stream('Hello')) {
console.log('Event:', event.type)
}
// Messages array is mutated in place and contains the full conversation
InvokableAgent.stream