Expand description
§forge-sdk
A peer reference implementation in Rust of a client for Anthropic’s
claude CLI. Spawns the binary as a subprocess and speaks
stream-json over stdio. Wire compatibility with the CLI is the only
hard external invariant; API shape is whatever serves
forge-agent
(and through it the views) best.
§Design
The SDK is a thin wrapper around the claude binary. All agentic
work - tool dispatch, conversation history, session persistence -
happens inside the CLI itself. This crate is responsible for:
- Spawning the subprocess with the right flags.
- Parsing the stream-json output into typed Rust values.
- Serialising user messages into stream-json input.
- Bridging the
can_use_toolcallback (when enabled) across the wire. - Hosting in-process MCP tool servers that the
claudebinary can call.
Client is Clone-able; all commands take &self. Internally
a reader task owns the subprocess after init, decodes lines,
dispatches inbound control_requests on detached tasks, and
routes outbound control_responses to per-request waiters.
§Minimal example
use forge_sdk::{Client, OptionsBuilder};
let options = OptionsBuilder::new().build();
let (client, mut events) = Client::spawn(options).await?;
client.send_user_message("hello").await?;
while let Some(item) = events.recv().await {
let event = item?;
println!("{event:?}");
}
client.disconnect().await?;Re-exports§
pub use crate::mcp::macros::__private;pub use client::Client;pub use client::ClientEvents;pub use client::INIT_HANDSHAKE_TIMEOUT;pub use error::Error;pub use hooks::HookCallback;pub use hooks::HookDecision;pub use hooks::Hooks;pub use hooks::HooksBuilder;pub use options::Options;pub use options::OptionsBuilder;pub use paths::app_support_dir;pub use paths::config_dir_hash;pub use paths::config_dir_store_key;pub use paths::projects_dir_for;pub use permissions::CanUseToolCallback;pub use transport::process::CLOSE_WAIT_TIMEOUT;
Modules§
- argv
- Pure-function argv composition. Takes an
Optionsand produces theVec<String>to pass to theclaudesubprocess. Kept separate fromtransport::processso the transport layer doesn’t import MCP orchestration types. - client 🔒
- Public
Client- the entry point consumers hold. - control
- Control-protocol messages exchanged over the main stream-json stdio channel.
- error 🔒
- Error type for the forge-sdk crate.
- hooks
- Hook callbacks - 10 hook kinds dispatched by opaque
callback_id. - logging 🔒
- Tracing target constants for forge-sdk.
- mcp
- In-process MCP server hosting.
- options 🔒
- Configuration for spawning a
Client. - paths
- Path helpers around the
claudeCLI’s on-disk config layout. - permissions 🔒
- Permission callback trait + tests. Wire-data lives in forge-primitives - consumers import from there directly.
- request_
id - Request ID generator for outbound
control_requestframes. - subagents
- Subagent declarations - re-export shim. Wire-shape data lives in forge-primitives.
- transport
- Subprocess transport for the
claudebinary.
Macros§
- tool
- Declarative tool-trait impl.
Structs§
- Base
Hook Input - Fields shared by every hook input the CLI emits. Every concrete
input type below flattens this struct so callbacks can access
session_id,transcript_path,cwd, and (optionally)permission_modewithout reaching back intosuper::HookContext. - Hook
Context - Context carried alongside every hook invocation.
- Notification
Input - Input payload for
Notification. - Permission
Request Input - Input payload for
PermissionRequest. - Post
Tool UseFailure Input - Input payload for
PostToolUseFailure. - Post
Tool UseInput - Input payload for
PostToolUse. - PreCompact
Input - Input payload for
PreCompact. - PreTool
UseHook Specific Output hookSpecificOutputshape forPreToolUsehook responses.- PreTool
UseInput - Input payload for
PreToolUse. - Stop
Input - Input payload for
Stop. - Subagent
Context - Optional sub-agent attribution present on tool-lifecycle hook
inputs (
PreToolUse,PostToolUse,PostToolUseFailure,PermissionRequest). Fields are populated when the hook fires inside aTask-spawned sub-agent so tool events can be attributed back to the right agent when multiple sub-agents interleave over the same control channel. - Subagent
Start Input - Input payload for
SubagentStart. - Subagent
Stop Input - Input payload for
SubagentStop. - User
Prompt Submit Hook Specific Output hookSpecificOutputshape forUserPromptSubmit.- User
Prompt Submit Input - Input payload for
UserPromptSubmit.
Enums§
- Hook
Kind - Identifies which hook point a callback is registered for. Sixteen event
kinds mirrored from the CLI v0.1.64, plus
Unknownas a fallback for forward-compatibility. - Permission
Mode - Which permission flow the
claudebinary should use for tool invocations. Mirrors the upstream CLI’s six-variant set. - SdkPlugin
Config - Plugin config. Wraps the CLI’s
SdkPluginConfig({"type": "local", "path": str}). - System
Prompt Kind - System-prompt configuration. Wraps the CLI’s discriminated union of
str | SystemPromptPreset | SystemPromptFile.
Type Aliases§
- Result
- Convenient alias for
Result<T, forge_sdk::Error>.