npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

formagent-sdk

v0.4.0

Published

FormAgent SDK - AI Agent framework with streaming support, tool execution, skill management, and hooks system

Downloads

22

Readme

FormAgent SDK

A powerful AI Agent framework for building intelligent assistants with streaming support, tool execution, skills, hooks, and MCP integration.

npm version License: MIT

Features

  • Session-Based API: Multi-turn conversations with state management
  • Persistent Sessions: File-based storage for session persistence across restarts
  • Streaming Support: Real-time streaming of LLM responses with event-based notifications
  • Built-in Tools: File operations, bash execution, web fetch, and task management
  • Tool System: Flexible tool registration with Zod schema support
  • Skills System: Extend agent capabilities with discoverable skills
  • Hooks System: Intercept and control agent behavior (PreToolUse, PostToolUse)
  • Structured Outputs: JSON Schema validated responses
  • MCP Integration: Model Context Protocol server support
  • Multi-Provider: Support for Anthropic Claude and OpenAI models
  • Cost Tracking: Token usage and cost estimation
  • Type-Safe: Full TypeScript support with strict typing
  • Zero Config: Auto-reads API keys from environment variables

Installation

npm install formagent-sdk
# or
bun add formagent-sdk
# or
yarn add formagent-sdk

Quick Start

One-Shot Prompts

import { prompt } from "formagent-sdk"

const response = await prompt("What is 2+2?")
console.log(response) // "4"

Sessions with Tools

import { createSession, builtinTools } from "formagent-sdk"

const session = await createSession({
  model: "claude-sonnet-4-20250514",
  tools: builtinTools,
})

await session.send("List all TypeScript files in the current directory")

for await (const event of session.receive()) {
  if (event.type === "text") {
    process.stdout.write(event.text)
  } else if (event.type === "tool_use") {
    console.log(`Using tool: ${event.name}`)
  }
}

await session.close()

Environment Variables

The SDK automatically reads configuration from environment variables:

| Variable | Description | |----------|-------------| | ANTHROPIC_API_KEY | Anthropic API key (required for Claude models) | | ANTHROPIC_BASE_URL | Custom Anthropic API endpoint | | OPENAI_API_KEY | OpenAI API key (for GPT models) | | OPENAI_BASE_URL | Custom OpenAI API endpoint |

export ANTHROPIC_API_KEY=your-api-key

Custom Tools

import { tool } from "formagent-sdk"
import { z } from "zod"

const weatherTool = tool({
  name: "get_weather",
  description: "Get the current weather for a location",
  schema: z.object({
    location: z.string().describe("City name"),
    unit: z.enum(["celsius", "fahrenheit"]).optional(),
  }),
  execute: async ({ location, unit = "celsius" }) => {
    return `Weather in ${location}: 22°${unit === "celsius" ? "C" : "F"}`
  },
})

const session = await createSession({
  model: "claude-sonnet-4-20250514",
  tools: [weatherTool],
})

Built-in Tools

| Tool | Description | |------|-------------| | Bash | Execute bash commands with timeout support | | Read | Read file contents with optional line range | | Write | Write content to files, creates directories | | Edit | Find/replace text in files | | Glob | Find files matching glob patterns | | Grep | Search file contents with regex | | WebFetch | Fetch URL content, converts HTML to markdown | | TodoWrite | Manage task lists for progress tracking | | Skill | Discover and invoke specialized skills |

Security defaults (important):

  • File tools (Read/Write/Edit/Glob/Grep) are restricted to process.cwd() by default; configure allowedPaths to widen access.
  • WebFetch blocks localhost/private network targets by default; set allowPrivateNetwork: true to override.
  • Bash blocks a small set of high-risk command patterns by default; set allowDangerous: true to disable the denylist.
import { builtinTools, fileTools, createBuiltinTools } from "formagent-sdk"

// Use all built-in tools
const session = await createSession({
  tools: builtinTools,
})

// Or use just file tools (Read, Write, Edit, Glob, Grep)
const session = await createSession({
  tools: fileTools,
})

// Or configure access boundaries explicitly
const tools = createBuiltinTools({
  cwd: process.cwd(),
  allowedPaths: [process.cwd()],
  allowPrivateNetwork: false,
  allowDangerous: false,
})

Skills System

Load skills from directories to extend agent capabilities:

import { createSession, DEFAULT_USER_SKILLS_PATH } from "formagent-sdk"

const session = await createSession({
  model: "claude-sonnet-4-20250514",
  settingSources: [
    DEFAULT_USER_SKILLS_PATH, // ~/.claude/skills
    "/path/to/project/skills",
  ],
})

// Claude can now use the Skill tool to discover and invoke skills
await session.send("What skills are available?")

Hooks System

Intercept and control tool execution:

import { createSession, builtinTools, type HookCallback } from "formagent-sdk"

const protectEnvFiles: HookCallback = async (input, toolUseId, context) => {
  const filePath = input.tool_input?.file_path as string

  if (filePath?.endsWith(".env")) {
    return {
      hookSpecificOutput: {
        hookEventName: "PreToolUse",
        permissionDecision: "deny",
        permissionDecisionReason: "Cannot modify .env files",
      },
    }
  }
  return {}
}

const session = await createSession({
  model: "claude-sonnet-4-20250514",
  tools: builtinTools,
  hooks: {
    PreToolUse: [
      { matcher: "Write|Edit", hooks: [protectEnvFiles] },
    ],
  },
})

Structured Outputs

Get validated JSON responses:

import { createSession } from "formagent-sdk"

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"],
    },
  },
})

for await (const event of session.receive()) {
  if (event.type === "result" && event.structured_output) {
    console.log(event.structured_output)
  }
}

Session Management

Default Behavior: Sessions use in-memory storage by default. Conversation history is lost when the process exits. Use FileSessionStorage for persistence across restarts.

Persistent Sessions

Enable session persistence with FileSessionStorage:

import { createSession, FileSessionStorage, builtinTools } from "formagent-sdk"

// Create persistent storage
const storage = new FileSessionStorage("./sessions")

// Create session with persistence
const session = await createSession({
  model: "claude-sonnet-4-20250514",
  tools: builtinTools,
  sessionStorage: storage,
})

console.log(`Session ID: ${session.id}`)  // Save this for later
await session.close()

Resume Sessions

Resume a previous session with full conversation context:

import { createSession, FileSessionStorage } from "formagent-sdk"

const storage = new FileSessionStorage("./sessions")

// Resume from saved session ID
const session = await createSession({
  sessionStorage: storage,
  resume: "previous-session-id",
})

await session.send("Continue where we left off")

Global Storage Configuration

Set a default storage for all sessions:

import { setDefaultStorage, FileSessionStorage, createSession } from "formagent-sdk"

// Set once at startup
setDefaultStorage(new FileSessionStorage("./sessions"))

// All sessions now persist automatically
const session = await createSession({ model: "claude-sonnet-4-20250514" })

Fork Sessions

Create a branch from an existing session:

import { forkSession } from "formagent-sdk"

// Fork creates a new session with copied conversation history
const forked = await forkSession("original-session-id")

Event Types

The receive() method yields different event types:

| Type | Properties | Description | |------|------------|-------------| | text | text: string | Text content chunk | | tool_use | id, name, input | Tool invocation | | tool_result | tool_use_id, content, is_error | Tool execution result | | message | message: SDKMessage | Complete message | | result | structured_output | Structured output (when configured) | | stop | stop_reason, usage | Generation complete | | error | error: Error | Error occurred |

Examples

See the examples directory for complete examples:

  • Basic sessions and prompts
  • Streaming responses
  • Custom tools and MCP servers
  • Skills and hooks
  • Structured outputs
  • CLI agent implementation

Documentation

License

MIT