Help us shape the future of Go! Take the 2026 Go Developer Survey

acp

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package acp is the agent side of the Agent Client Protocol (https://agentclientprotocol.com), protocol version 1: newline-delimited JSON-RPC 2.0 over stdio. It implements the baseline methods (initialize, authenticate, session/new, session/prompt, session/cancel) and the session/update notifications tinyagent sends; the turns themselves run in a Session the caller provides.

Index

Constants

View Source
const (
	CodeParseError     = -32700
	CodeInvalidRequest = -32600
	CodeMethodNotFound = -32601
	CodeInvalidParams  = -32602
	CodeInternalError  = -32603
)

JSON-RPC 2.0 error codes.

View Source
const (
	StopEndTurn         = "end_turn"
	StopMaxTokens       = "max_tokens"
	StopMaxTurnRequests = "max_turn_requests"
	StopCancelled       = "cancelled"
)

Stop reasons of a prompt turn.

View Source
const ProtocolVersion = 1

ProtocolVersion is the ACP version spoken.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContentBlock

type ContentBlock struct {
	Type     string    `json:"type"`
	Text     string    `json:"text,omitempty"`
	MimeType string    `json:"mimeType,omitempty"`
	Data     string    `json:"data,omitempty"`
	URI      string    `json:"uri,omitempty"`
	Name     string    `json:"name,omitempty"`
	Resource *Resource `json:"resource,omitempty"`
}

ContentBlock is an ACP content block. Only the fields tinyagent reads are decoded.

func TextBlock

func TextBlock(text string) ContentBlock

TextBlock is a text content block.

type EnvVariable

type EnvVariable struct {
	Name  string `json:"name"`
	Value string `json:"value"`
}

EnvVariable is one environment variable of a client's MCP server.

type Error

type Error struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

Error is a JSON-RPC error. A Session returns one to pick the code; any other error is reported as an internal error with its message.

func InvalidParams

func InvalidParams(format string, args ...any) *Error

InvalidParams returns an invalid-params error with a formatted message.

func (*Error) Error

func (e *Error) Error() string

type Image

type Image struct {
	MimeType string
	Data     []byte
	Label    string
}

Image is one image block, decoded. MimeType is what the client claimed.

type MCPServer

type MCPServer struct {
	Type    string        `json:"type,omitempty"`
	Name    string        `json:"name"`
	Command string        `json:"command,omitempty"`
	Args    []string      `json:"args,omitempty"`
	Env     []EnvVariable `json:"env,omitempty"`
	URL     string        `json:"url,omitempty"`
}

MCPServer is one MCP server a client sends with session/new. Type is "" or "stdio" for a local command, "http" or "sse" for a remote one.

type NewSessionFunc

type NewSessionFunc func(ctx context.Context, id, cwd string, mcpServers []MCPServer) (Session, error)

NewSessionFunc creates a session for session/new. cwd is absolute; id is the new session's id.

type PlanEntry

type PlanEntry struct {
	Content  string `json:"content"`
	Priority string `json:"priority"`
	Status   string `json:"status"`
}

PlanEntry is one entry of a plan update; Status is pending, in_progress or completed.

type Prompt

type Prompt struct {
	Text   string
	Images []Image
}

Prompt is a session/prompt's content: the task text and the attached images, in order.

func ParsePrompt

func ParsePrompt(blocks []ContentBlock) (Prompt, error)

ParsePrompt turns prompt blocks into task text and images. Text blocks are joined; a text resource becomes quoted context naming its URI; a resource link becomes a reference line (it is not fetched). Images are decoded. Other blocks (audio, binary resources) and a prompt without any text are invalid params.

type Resource

type Resource struct {
	URI      string  `json:"uri"`
	MimeType string  `json:"mimeType,omitempty"`
	Text     *string `json:"text,omitempty"`
	Blob     string  `json:"blob,omitempty"`
}

Resource is the resource of an embedded-context block: text or a base64 blob.

type Server

type Server struct {
	Name, Version string
	NewSession    NewSessionFunc
	// Log receives protocol problems; nil discards them.
	Log io.Writer
	// contains filtered or unexported fields
}

Server answers ACP requests read from a client.

func (*Server) Request added in v0.8.0

func (s *Server) Request(ctx context.Context, method string, params any) (json.RawMessage, error)

Request sends a JSON-RPC request to the client and waits for its response. It is called from a turn goroutine (for session/request_permission); Serve's read loop routes the response back through deliverResponse. A cancelled ctx returns ctx's error.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context, in io.Reader, out io.Writer) error

Serve reads requests from in until it ends or ctx is cancelled, writing responses and notifications to out, one JSON object per line. It then cancels any running turns, waits for them and closes every session. A read error other than EOF is returned.

type Session

type Session interface {
	// Prompt runs one turn, streaming progress through u, and returns its
	// stop reason. When ctx is cancelled (session/cancel) the reply is
	// "cancelled" whatever Prompt returns.
	Prompt(ctx context.Context, p Prompt, u *Updates) (string, error)
	// Close releases the session's resources; it runs after its last turn.
	Close()
}

Session runs one ACP session's prompt turns, one at a time.

type Updates

type Updates struct {
	// contains filtered or unexported fields
}

Updates sends one session's session/update notifications.

func (*Updates) Message

func (u *Updates) Message(text string)

Message sends a chunk of the agent's reply.

func (*Updates) Plan

func (u *Updates) Plan(entries []PlanEntry)

Plan sends the agent's current plan.

func (*Updates) Request added in v0.8.0

func (u *Updates) Request(ctx context.Context, method string, params any) (json.RawMessage, error)

Request sends a request to the client and waits for its response (used for session/request_permission).

func (*Updates) Send

func (u *Updates) Send(update any)

Send sends update as a session/update notification.

func (*Updates) ToolCall

func (u *Updates) ToolCall(id, title, kind string, locations []string, rawInput any)

ToolCall announces a tool call as pending. locations are absolute paths.

func (*Updates) ToolCallStatus

func (u *Updates) ToolCallStatus(id, status, text string)

ToolCallStatus updates a tool call's status, with optional result text.

func (*Updates) Usage

func (u *Updates) Usage(used, size int, costUSD float64)

Usage sends the context usage: used of size tokens, and the session's cost so far in USD when known (> 0).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL