Agent
Defined in: src/agent/agent.ts:125
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.
Implements
Section titled “Implements”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new Agent(config?): Agent;Defined in: src/agent/agent.ts:165
Creates an instance of the Agent.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
config? | AgentConfig | The configuration for the agent. |
Returns
Section titled “Returns”Agent
Properties
Section titled “Properties”messages
Section titled “messages”readonly messages: Message[];Defined in: src/agent/agent.ts:129
The conversation history of messages between user and assistant.
Implementation of
Section titled “Implementation of”readonly state: AgentState;Defined in: src/agent/agent.ts:134
Agent state storage accessible to tools and application logic. State is not passed to the model during inference.
Implementation of
Section titled “Implementation of”conversationManager
Section titled “conversationManager”readonly conversationManager: HookProvider;Defined in: src/agent/agent.ts:138
Conversation manager for handling message history and context overflow.
readonly hooks: HookRegistry;Defined in: src/agent/agent.ts:143
Hook registry for managing event callbacks. Hooks enable observing and extending agent behavior.
model: Model;Defined in: src/agent/agent.ts:148
The model provider used by the agent for inference.
systemPrompt?
Section titled “systemPrompt?”optional systemPrompt: SystemPrompt;Defined in: src/agent/agent.ts:153
The system prompt to pass to the model provider.
Accessors
Section titled “Accessors”Get Signature
Section titled “Get Signature”get tools(): Tool[];Defined in: src/agent/agent.ts:238
The tools this agent can use.
Returns
Section titled “Returns”Tool[]
toolRegistry
Section titled “toolRegistry”Get Signature
Section titled “Get Signature”get toolRegistry(): ToolRegistry;Defined in: src/agent/agent.ts:245
The tool registry for managing the agent’s tools.
Returns
Section titled “Returns”ToolRegistry
Methods
Section titled “Methods”initialize()
Section titled “initialize()”initialize(): Promise<void>;Defined in: src/agent/agent.ts:199
Returns
Section titled “Returns”Promise<void>
invoke()
Section titled “invoke()”invoke(args): Promise<AgentResult>;Defined in: src/agent/agent.ts:266
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.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
args | InvokeArgs | Arguments for invoking the agent |
Returns
Section titled “Returns”Promise<AgentResult>
Promise that resolves to the final AgentResult
Example
Section titled “Example”const agent = new Agent({ model, tools })const result = await agent.invoke('What is 2 + 2?')console.log(result.lastMessage) // Agent's responsestream()
Section titled “stream()”stream(args): AsyncGenerator<AgentStreamEvent, AgentResult, undefined>;Defined in: src/agent/agent.ts:304
Streams the agent execution, yielding events and returning the final result.
The agent loop manages the conversation flow by:
- Streaming model responses and yielding all events
- Executing tools when the model requests them
- 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
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
args | InvokeArgs | Arguments for invoking the agent |
Returns
Section titled “Returns”AsyncGenerator<AgentStreamEvent, AgentResult, undefined>
Async generator that yields AgentStreamEvent objects and returns AgentResult
Example
Section titled “Example”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