
Go SDK for Claude Code——构建包含工具使用、MCP集成和流媒体的AI代理
概览
一个纯粹的 Go SDK 来实现 Claude 的代理能力。 claude-agent-sdk-go 通过将 CLI 管理为子进程,为 Claude Code 提供了原生的 Go 接口。它通过行分隔的 JSON 通过 stdin/stdout 通信,让你访问 Claude 的工具使用、扩展思考、会话管理和钩子系统。 该仓库通过 v0.3.296 的补期工作跟踪官方 TypeScript Agent SDK 表面,使用了 API 形态不同的 Go 成语。 当你调用 Query() 或 Stream() 时,SDK 会生成 CLI,发送提示,并在消息到达时发送。对于 MCP 工具,流程包含双向控制消息: 需求:- 使用 1.23+(用于 iter.Seq)- Claude Code CLI: npm install -g @anthropic-ai/claude-code - 在你的环境中ANTHROPIC_API_KEY或 CLAUDE_CODE_OAUTH_TOKEN 设置可以作为设置文件路径或内联 JSON 形式提供: 定义Claude在对话中可以使用的工具: 请参见MCP工具,了解输入式响应、二进制服务器等。
概述(AI 翻译)
一个纯粹的 Go SDK 来实现 Claude 的代理能力。
概述
Claude-agent-SDK-Go 通过管理 Claude Code 提供了原生的 Go 接口 CLI作为子进程。它通过行分隔的JSON通信 stdin/stdout,能让你接触到Claude的工具使用,扩展思维, 会话管理和挂钩系统。
该仓库通过 v0.3.296 的补足工作,使用 Go 惯用法,API 形态不同。
当你调用 Query() 或 Stream() 时,SDK 会生成 CLI,发送你的
提示,并在消息到达时自动生成。对于MCP工具,流程包括
双向控制消息:
安装
要求:
- Go 1.23+(代表
iter.Seq) - Claude 代码 CLI:
npm install -g @anthropic-ai/claude-code ANTHROPIC_API_KEY或CLAUDE_CODE_OAUTH_TOKEN在你的环境中
快速启动
流媒体
对于多回合对话:
配置
设置可以作为设置文件路径或内联 JSON 形式提供:
自定义工具(MCP)
定义Claude在对话中可以使用的工具:
请参见MCP工具,了解输入式响应、二进制服务器等。
拉尔夫·维格姆环路
本页由 AI 翻译,技术细节请以原文为准。
README
以下为原文 README 节选,完整内容请查看原文。
claude-agent-sdk-go
A pure Go SDK for Claude’s agentic capabilities.
Overview
claude-agent-sdk-go provides a native Go interface to Claude Code by managing the CLI as a subprocess. It communicates via line-delimited JSON over stdin/stdout, giving you access to Claude’s tool use, extended thinking, session management, and hook system.
This repository tracks the official TypeScript Agent SDK surface through the v0.3.316 catchup work, using Go idioms where the API shape differs.
flowchart TB
subgraph SDK[Go SDK]
Client --> Protocol --> Transport
MCP[SDK MCP] |tool calls| Protocol
end
Transport |stream-json| CLI[Claude CLI]
CLI API[Claude API]
When you call Query() or Stream(), the SDK spawns the CLI, sends your
prompt, and yields messages as they arrive. For MCP tools, the flow includes
bidirectional control messages:
sequenceDiagram
participant App as Go App
participant CLI as Claude CLI
participant API as Claude API
App->>CLI: prompt (stdin)
CLI->>API: API request
API-->>CLI: response stream
CLI-->>App: messages (stdout)
CLI->>App: mcp_message (tool call)
App-->>CLI: mcp_response
CLI-->>App: result
Installation
go get github.com/roasbeef/claude-agent-sdk-go
Requirements:
- Go 1.23+ (for
iter.Seq) - Claude Code CLI:
npm install -g @anthropic-ai/claude-code ANTHROPIC_API_KEYorCLAUDE_CODE_OAUTH_TOKENin your environment
Quick Start
package main
import (
"context"
"fmt"
"log"
"github.com/roasbeef/claude-agent-sdk-go"
)
func main() {
client, err := claudeagent.NewClient(
claudeagent.WithSystemPrompt("You are a helpful assistant."),
)
if err != nil {
log.Fatal(err)
}
defer client.Close()
ctx := context.Background()
for msg := range client.Query(ctx, "What is the capital of France?") {
switch m := msg.(type) {
case claudeagent.AssistantMessage:
fmt.Println(m.ContentText())
case claudeagent.ResultMessage:
fmt.Printf("Cost: $%.4f\n", m.TotalCostUSD)
}
}
}
Streaming
For multi-turn conversations:
stream, _ := client.Stream(ctx)
defer stream.Close()
stream.Send(ctx, "Let's plan a trip to Japan.")
for msg := range stream.Messages() {
if m, ok := msg.(claudeagent.AssistantMessage); ok {
fmt.Println(m.ContentText())
}
}
// Continue the conversation
stream.Send(ctx, "What about Kyoto?")
Configuration
client, _ := claudeagent.NewClient(
claudeagent.WithModel("claude-sonnet-4-5-20250929"),
claudeagent.WithSystemPrompt("You are an expert Go developer."),
claudeagent.WithPermissionMode(claudeagent.PermissionModeAcceptEdits),
claudeagent.WithMaxTurns(20),
)
安装
This server does not publish a one-line install command.
Open the repository installation guide 新手?先看 mcpskill.org 教程 →