This directory contains examples demonstrating how to use the formagent-sdk SDK.
Set up your API keys as environment variables:
export ANTHROPIC_API_KEY="your-anthropic-api-key"
export OPENAI_API_KEY="your-openai-api-key" # Optional, for OpenAI examplesNote: The SDK automatically reads API keys from environment variables. No need to configure providers manually in your code!
# Using bun
bun run examples/01-basic-session.ts
# Using ts-node
npx ts-node examples/01-basic-session.tsFundamental session-based API for conversations:
- Creating sessions
- Send/receive pattern
- Multi-turn conversations
- Session resume and fork
- Automatic cleanup with
await using
Simple single-turn interactions:
- Basic prompts
- Streaming responses
- Custom models and parameters
- System prompt configuration
- Full result with usage info
Streaming responses and event handling:
- Session streaming
- Query function streaming
- Handling all event types
- Aborting streams
- Collecting responses while streaming
Tool definition and usage:
- Defining tools with schemas
- Tool execution
- ToolManager for tool registration
- Tool filtering (allowedTools)
- Tool events
MCP (Model Context Protocol) integration:
- Creating MCP servers from tools
- Tool namespacing (
mcp__{server}__{tool}) - MCPServerWrapper and MCPServerManager
- Integrating MCP with ToolManager
System prompt customization:
- Built-in presets (claude_code, default, minimal)
- Prepend/append content
- Context-based prompts
- Custom presets
- CLAUDE.md loading
Token usage and cost calculation:
- Basic cost tracking
- Message deduplication
- Usage by model
- Session-specific tracking
- Cache token tracking
- Custom pricing configuration
Multi-provider support:
- Anthropic provider
- OpenAI provider
- Provider resolution
- Custom patterns
- OpenAI-compatible endpoints
Skill discovery and activation:
- Registering skills
- Skill discovery from SKILL.md files
- Searching skills
- Skill activation triggers
- System prompt integration
Complete agent implementation:
- Combining all features
- Tool execution flow
- Skill activation
- Cost tracking
- Event handling
Skill tool that Claude can invoke to discover and use skills:
- Path-based skill sources (
settingSourcesconfig) - Skill tool for Claude to discover/invoke skills
- Direct skill loading and searching
- Session auto-adds Skill tool when
settingSourcesis configured - Skill activation based on triggers
Claude Agent SDK compatible built-in tools:
- Using
builtinToolscollection (Bash, Read, Write, Edit, Glob, Grep, WebFetch, TodoWrite) - File-only tools with
fileToolscollection - Custom configuration with
createBuiltinTools()andcreateFileTools() - Individual tool usage and registration
- Todo management with callbacks (
getTodos,clearTodos,setTodoChangeCallback) - Custom timeout and working directory options
Interactive command-line agent similar to Claude Code:
- Multi-turn conversations with streaming
- All built-in tools (Bash, Read, Write, Edit, Glob, Grep, WebFetch, TodoWrite)
- Tool call visualization with colored output
- Slash commands:
/help,/clear,/tools,/todos,/usage,/exit - Token usage tracking and cost estimation
- ANSI colored terminal output
# Run the CLI agent
bun run examples/13-cli-agent.ts
# Or make it executable
chmod +x examples/13-cli-agent.ts
./examples/13-cli-agent.tsGet validated JSON responses from agent workflows:
- JSON Schema output format configuration
- Zod schema support for type-safe outputs
- Result event with
structured_outputfield - Code analysis with structured results
- TODO extraction with schema validation
- Error handling for invalid outputs
Intercept and control agent behavior with hooks:
- PreToolUse hooks (block, allow, modify inputs)
- PostToolUse hooks (logging, context injection)
- Permission decisions (allow/deny/ask)
- Protect sensitive files (.env protection)
- Block dangerous shell commands
- Audit logging for compliance
- Sandbox file operations
- Auto-approve read-only tools
- Chain multiple hooks
- Regex-based tool matchers
import { createSession, resumeSession, forkSession } from "formagent-sdk"
// Create a session
const session = await createSession({
model: "claude-sonnet-4-20250514",
tools: [...],
systemPrompt: { preset: "claude_code" },
})
// Send message
await session.send("Hello!")
// Receive response (streaming)
for await (const event of session.receive()) {
if (event.type === "text") {
console.log(event.text)
}
}
// Close session
await session.close()import { prompt } from "formagent-sdk"
// Simple prompt
const response = await prompt("What is 2+2?")
// With options
const response = await prompt("Write a poem", {
model: "claude-sonnet-4-20250514",
stream: true,
onText: (text) => console.log(text),
})import { tool, ToolManager } from "formagent-sdk"
// Define a tool
const myTool = tool({
name: "my_tool",
description: "Does something useful",
schema: {
type: "object",
properties: {
input: { type: "string" },
},
},
execute: async (input) => {
return { content: `Result: ${input}` }
},
})
// Use with ToolManager
const manager = new ToolManager()
manager.register(myTool)import { AnthropicProvider, OpenAIProvider, setDefaultProvider } from "formagent-sdk"
// Set up provider
const provider = new AnthropicProvider({
apiKey: process.env.ANTHROPIC_API_KEY!,
})
setDefaultProvider(provider)import {
builtinTools,
fileTools,
createBuiltinTools,
getTodos,
clearTodos,
setTodoChangeCallback,
} from "formagent-sdk"
// Use all built-in tools
const session = await createSession({
model: "claude-sonnet-4-20250514",
tools: builtinTools,
})
// Or use custom configured tools
const customTools = createBuiltinTools({
cwd: "/my/project",
defaultTimeout: 60000,
})
// Track todos
setTodoChangeCallback((todos) => {
console.log("Todos updated:", todos)
})
const currentTodos = getTodos()
clearTodos()import { createSession } from "formagent-sdk"
// Define output schema
const session = await createSession({
model: "claude-sonnet-4-20250514",
outputFormat: {
type: "json_schema",
schema: {
type: "object",
properties: {
summary: { type: "string" },
score: { type: "number" },
},
required: ["summary"],
},
},
})
// Receive structured output
for await (const event of session.receive()) {
if (event.type === "result" && event.structured_output) {
console.log(event.structured_output)
}
}import {
createSession,
builtinTools,
type HookCallback,
type PreToolUseHookInput,
} from "formagent-sdk"
// Define a hook
const protectFiles: HookCallback = async (input, toolUseId, context) => {
const preInput = input as PreToolUseHookInput
const filePath = preInput.tool_input?.file_path as string
if (filePath?.endsWith(".env")) {
return {
hookSpecificOutput: {
hookEventName: "PreToolUse",
permissionDecision: "deny",
permissionDecisionReason: "Cannot modify .env files",
},
}
}
return {}
}
// Use hooks in session
const session = await createSession({
model: "claude-sonnet-4-20250514",
tools: builtinTools,
hooks: {
PreToolUse: [
{ matcher: "Write|Edit", hooks: [protectFiles] },
],
},
})