Documentation
¶
Overview ¶
Package transport implements HTTP and SSE transport for MCP servers.
Index ¶
- Constants
- func BuildMcpCommand(baseCmd []string, options *types.ClaudeAgentOptions) ([]string, error)
- func CheckCLIVersion(cliPath string) error
- func FindCLI() (string, error)
- type HTTPTransport
- func (t *HTTPTransport) Close(ctx context.Context) error
- func (t *HTTPTransport) Connect(ctx context.Context) error
- func (t *HTTPTransport) GetError() error
- func (t *HTTPTransport) IsReady() bool
- func (t *HTTPTransport) OnError(err error)
- func (t *HTTPTransport) ReadMessages(ctx context.Context) <-chan types.Message
- func (t *HTTPTransport) Write(ctx context.Context, data string) error
- type JSONLineReader
- type JSONLineWriter
- type MCPServerTransport
- func (t *MCPServerTransport) Close(ctx context.Context) error
- func (t *MCPServerTransport) Connect(ctx context.Context) error
- func (t *MCPServerTransport) GetError() error
- func (t *MCPServerTransport) GetSDKMCPServer(name string) (*mcp.SdkMCPServer, bool)
- func (t *MCPServerTransport) IsReady() bool
- func (t *MCPServerTransport) OnError(err error)
- func (t *MCPServerTransport) ReadMessages(ctx context.Context) <-chan types.Message
- func (t *MCPServerTransport) RegisterSDKMCPServer(name string, server *mcp.SdkMCPServer)
- func (t *MCPServerTransport) RouteToolUse(toolName string) (bool, string, string, error)
- func (t *MCPServerTransport) Write(ctx context.Context, data string) error
- type SemanticVersion
- type SubprocessCLITransport
- func (t *SubprocessCLITransport) Close(ctx context.Context) error
- func (t *SubprocessCLITransport) Connect(ctx context.Context) error
- func (t *SubprocessCLITransport) GetError() error
- func (t *SubprocessCLITransport) IsReady() bool
- func (t *SubprocessCLITransport) OnError(err error)
- func (t *SubprocessCLITransport) ReadMessages(ctx context.Context) <-chan types.Message
- func (t *SubprocessCLITransport) Write(ctx context.Context, data string) error
- type Transport
Constants ¶
const ( // MinimumCLIVersion is the minimum required CLI version (2.0.0) MinimumCLIVersion = "2.0.0" // MinimumCLIMajor is the minimum major version MinimumCLIMajor = 2 // MinimumCLIMinor is the minimum minor version MinimumCLIMinor = 0 // MinimumCLIPatch is the minimum patch version MinimumCLIPatch = 0 )
const (
// DefaultMaxBufferSize is the default maximum size for JSON line buffer (1MB)
DefaultMaxBufferSize = 1024 * 1024
)
const (
// SDKVersion is the version identifier for this SDK
SDKVersion = "0.1.19"
)
Variables ¶
This section is empty.
Functions ¶
func BuildMcpCommand ¶
func BuildMcpCommand(baseCmd []string, options *types.ClaudeAgentOptions) ([]string, error)
BuildMcpCommand augments the CLI command with MCP configuration.
func CheckCLIVersion ¶
CheckCLIVersion verifies that the CLI version meets minimum requirements Returns nil if version is acceptable, or an error if not
func FindCLI ¶
FindCLI searches for Claude Code CLI binary in standard locations. It checks in this order:
- PATH via exec.LookPath("claude")
- Default Claude installation location (new in CLI 2.0+): - ~/.claude/local/claude
- Common npm/yarn global install locations: - ~/.npm-global/bin/claude - /usr/local/bin/claude - ~/.local/bin/claude - ~/node_modules/.bin/claude - ~/.yarn/bin/claude
After finding the CLI, it checks the version to ensure it meets minimum requirements (unless CLAUDE_AGENT_SDK_SKIP_VERSION_CHECK is set).
Returns the path to the CLI binary or a CLINotFoundError if not found.
Types ¶
type HTTPTransport ¶
type HTTPTransport struct {
// contains filtered or unexported fields
}
HTTPTransport implements HTTP-based MCP transport
func NewHTTPTransport ¶
NewHTTPTransport creates a new HTTP transport
func NewHTTPTransportFromConfig ¶
func NewHTTPTransportFromConfig(config types.McpHTTPServerConfig, logger *log.Logger) *HTTPTransport
NewHTTPTransportFromConfig creates an HTTP transport from a config
func NewSSETransportFromConfig ¶
func NewSSETransportFromConfig(config types.McpSSEServerConfig, logger *log.Logger) *HTTPTransport
NewSSETransportFromConfig creates an SSE transport from a config
func (*HTTPTransport) Close ¶
func (t *HTTPTransport) Close(ctx context.Context) error
Close closes the transport
func (*HTTPTransport) Connect ¶
func (t *HTTPTransport) Connect(ctx context.Context) error
Connect establishes connection to the MCP server
func (*HTTPTransport) GetError ¶
func (t *HTTPTransport) GetError() error
GetError returns any error
func (*HTTPTransport) IsReady ¶
func (t *HTTPTransport) IsReady() bool
IsReady returns true if transport is ready
func (*HTTPTransport) ReadMessages ¶
func (t *HTTPTransport) ReadMessages(ctx context.Context) <-chan types.Message
ReadMessages returns a channel of incoming JSON-RPC responses
type JSONLineReader ¶
type JSONLineReader struct {
// contains filtered or unexported fields
}
JSONLineReader reads JSON lines from an input stream with buffering. Each call to ReadLine returns the next complete JSON line (without newline).
func NewJSONLineReader ¶
func NewJSONLineReader(r io.Reader) *JSONLineReader
NewJSONLineReader creates a new JSONLineReader with the default buffer size.
func NewJSONLineReaderWithSize ¶
func NewJSONLineReaderWithSize(r io.Reader, maxSize int) *JSONLineReader
NewJSONLineReaderWithSize creates a new JSONLineReader with a custom max buffer size.
func (*JSONLineReader) ReadLine ¶
func (r *JSONLineReader) ReadLine() ([]byte, error)
ReadLine reads the next JSON line from the stream. Returns the raw JSON bytes (without newline) or an error. Returns io.EOF when the stream ends.
type JSONLineWriter ¶
type JSONLineWriter struct {
// contains filtered or unexported fields
}
JSONLineWriter writes JSON lines to an output stream with buffering. Each call to WriteLine writes the data followed by a newline and flushes.
func NewJSONLineWriter ¶
func NewJSONLineWriter(w io.Writer) *JSONLineWriter
NewJSONLineWriter creates a new JSONLineWriter with default buffer size.
func (*JSONLineWriter) Flush ¶
func (w *JSONLineWriter) Flush() error
Flush flushes any buffered data to the underlying writer.
func (*JSONLineWriter) WriteLine ¶
func (w *JSONLineWriter) WriteLine(data string) error
WriteLine writes a JSON line to the stream with a trailing newline. The data is written to the buffer and then immediately flushed.
type MCPServerTransport ¶
type MCPServerTransport struct {
// contains filtered or unexported fields
}
MCPServerTransport wraps a transport with MCP server support. It handles routing between SDK MCP servers (in-process) and external transports.
func NewMCPServerTransport ¶
func NewMCPServerTransport(transport Transport, options *types.ClaudeAgentOptions) *MCPServerTransport
NewMCPServerTransport creates a new MCP server transport wrapper.
func (*MCPServerTransport) Close ¶
func (t *MCPServerTransport) Close(ctx context.Context) error
Close closes the transport and cleans up MCP resources.
func (*MCPServerTransport) Connect ¶
func (t *MCPServerTransport) Connect(ctx context.Context) error
Connect establishes connection and initializes MCP servers.
func (*MCPServerTransport) GetError ¶
func (t *MCPServerTransport) GetError() error
GetError returns any error from the transport.
func (*MCPServerTransport) GetSDKMCPServer ¶
func (t *MCPServerTransport) GetSDKMCPServer(name string) (*mcp.SdkMCPServer, bool)
GetSDKMCPServer retrieves an SDK MCP server by name.
func (*MCPServerTransport) IsReady ¶
func (t *MCPServerTransport) IsReady() bool
IsReady checks if the transport is ready.
func (*MCPServerTransport) OnError ¶
func (t *MCPServerTransport) OnError(err error)
OnError handles errors from the transport.
func (*MCPServerTransport) ReadMessages ¶
func (t *MCPServerTransport) ReadMessages(ctx context.Context) <-chan types.Message
ReadMessages returns a channel of incoming messages.
func (*MCPServerTransport) RegisterSDKMCPServer ¶
func (t *MCPServerTransport) RegisterSDKMCPServer(name string, server *mcp.SdkMCPServer)
RegisterSDKMCPServer registers an SDK MCP server with the transport.
func (*MCPServerTransport) RouteToolUse ¶
RouteToolUse routes a tool use request to the appropriate server. Returns: isMcp, serverName, toolName, error
type SemanticVersion ¶
SemanticVersion represents a semantic version number (major.minor.patch)
func GetCLIVersion ¶
func GetCLIVersion(cliPath string) (SemanticVersion, error)
GetCLIVersion retrieves the version of the Claude CLI binary
func ParseSemanticVersion ¶
func ParseSemanticVersion(versionStr string) (SemanticVersion, error)
ParseSemanticVersion parses a semantic version string (e.g., "2.1.0")
func (SemanticVersion) IsAtLeast ¶
func (v SemanticVersion) IsAtLeast(required SemanticVersion) bool
IsAtLeast checks if this version is at least the specified version
func (SemanticVersion) String ¶
func (v SemanticVersion) String() string
String returns the string representation of the version
type SubprocessCLITransport ¶
type SubprocessCLITransport struct {
// contains filtered or unexported fields
}
SubprocessCLITransport implements Transport using a Claude Code CLI subprocess. It manages the subprocess lifecycle, stdin/stdout/stderr pipes, and message streaming.
func NewSubprocessCLITransport ¶
func NewSubprocessCLITransport(cliPath, cwd string, env map[string]string, logger *log.Logger, resumeSessionID string, options *types.ClaudeAgentOptions) *SubprocessCLITransport
NewSubprocessCLITransport creates a new transport instance. The cliPath should point to the claude binary. The cwd is the working directory for the subprocess (empty string uses current directory). The env map contains additional environment variables to set for the subprocess. The logger is used for debug/diagnostic output. The resumeSessionID is an optional session ID to resume a previous conversation. The options contains configuration for the CLI.
func (*SubprocessCLITransport) Close ¶
func (t *SubprocessCLITransport) Close(ctx context.Context) error
Close terminates the subprocess and cleans up all resources. It attempts to gracefully shut down the subprocess with a timeout.
func (*SubprocessCLITransport) Connect ¶
func (t *SubprocessCLITransport) Connect(ctx context.Context) error
Connect starts the Claude Code CLI subprocess and establishes communication pipes. It launches the subprocess with "agent --stdio" arguments and sets up the environment.
func (*SubprocessCLITransport) GetError ¶
func (t *SubprocessCLITransport) GetError() error
GetError returns any error that occurred during transport operation. This is useful for checking if an error occurred in the reading loop.
func (*SubprocessCLITransport) IsReady ¶
func (t *SubprocessCLITransport) IsReady() bool
IsReady returns true if the transport is ready for communication.
func (*SubprocessCLITransport) OnError ¶
func (t *SubprocessCLITransport) OnError(err error)
OnError stores an error that occurred during transport operation. This allows errors from the reading loop to be retrieved later.
func (*SubprocessCLITransport) ReadMessages ¶
func (t *SubprocessCLITransport) ReadMessages(ctx context.Context) <-chan types.Message
ReadMessages returns a channel of incoming messages from the subprocess. The channel is closed when the subprocess exits or an error occurs.
type Transport ¶
type Transport interface {
// Connect establishes connection to Claude Code CLI subprocess.
// For subprocess transports, this starts the process and prepares stdin/stdout/stderr pipes.
Connect(ctx context.Context) error
// Close terminates subprocess and cleans up resources.
// This should gracefully shut down the subprocess and clean up all goroutines.
Close(ctx context.Context) error
// Write sends a JSON message to the subprocess stdin.
// The data should be a complete JSON line (without the trailing newline - it will be added).
Write(ctx context.Context, data string) error
// ReadMessages returns a channel of incoming messages from subprocess stdout.
// The channel is closed when the subprocess exits or an error occurs.
// Messages are parsed from JSON lines and returned as Message interface types.
ReadMessages(ctx context.Context) <-chan types.Message
// OnError is called when an error occurs in the reading loop.
// Implementations can use this to store errors for later retrieval.
OnError(err error)
// IsReady checks if the transport is ready for communication.
// Returns true if the subprocess is running and ready to send/receive messages.
IsReady() bool
// GetError returns any error that occurred during transport operation.
// This is useful for checking if an error occurred in async operations (like stderr parsing).
GetError() error
}
Transport defines the interface for communicating with Claude Code CLI subprocess. This is a low-level transport interface that handles raw I/O with the Claude process. The Query class builds on top of this to implement the control protocol and message routing.