cli

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: MIT Imports: 32 Imported by: 0

Documentation

Overview

backend.go defines the Backend interface and its implementations for container operations. It provides two backends: DirectBackend (direct Docker access) and HTTPBackend (via HostMCP server). This abstraction allows CLI commands to work both locally and remotely.

backend.goはコンテナ操作のためのBackendインターフェースとその実装を定義します。 2つのバックエンドを提供します:DirectBackend(直接Docker接続)とHTTPBackend(HostMCPサーバー経由)。 この抽象化により、CLIコマンドはローカルとリモートの両方で動作できます。

client.go implements the 'client' parent command for HTTP-based remote access. This command group allows interaction with a HostMCP server from environments without direct Docker socket access, such as DevContainers.

client.goはHTTPベースのリモートアクセス用の'client'親コマンドを実装します。 このコマンドグループにより、DevContainerなどDockerソケットへの直接アクセスがない環境から HostMCPサーバーとの対話が可能になります。

client_blocked.go implements the 'client blocked-paths' subcommand for listing blocked file paths. It queries the HostMCP server to show which file paths are protected from AI access. This is part of the security feature that hides sensitive files like secrets and .env.

client_blocked.goはブロックされたファイルパスを一覧表示する'client blocked-paths'サブコマンドを実装します。 HostMCPサーバーに問い合わせて、AIアクセスから保護されているファイルパスを表示します。 これはsecretsや.envのような機密ファイルを隠すセキュリティ機能の一部です。

client_commands.go implements the 'client commands' subcommand for listing allowed commands. It queries the HostMCP server to show which commands are whitelisted for each container.

client_commands.goは許可されたコマンドを一覧表示する'client commands'サブコマンドを実装します。 HostMCPサーバーに問い合わせて、各コンテナでホワイトリストに登録されているコマンドを表示します。

client_exec.go implements the 'client exec' subcommand for executing commands via HTTP. It uses HTTPBackend to communicate with the HostMCP server instead of direct Docker access. Commands must be whitelisted on the server side to be executed.

client_exec.goはHTTP経由でコマンドを実行する'client exec'サブコマンドを実装します。 直接Docker接続の代わりにHTTPBackendを使用してHostMCPサーバーと通信します。 コマンドは実行するためにサーバー側でホワイトリストに登録されている必要があります。

client_host_exec.go implements the 'client host-exec' subcommand for host command execution via HTTP. It uses HTTPBackend to communicate with the HostMCP server for executing whitelisted host CLI commands.

client_host_exec.goはHTTP経由でホストコマンドを実行する'client host-exec'サブコマンドを実装します。 HTTPBackendを使用してHostMCPサーバーと通信し、ホワイトリストに登録されたホストCLIコマンドを実行します。

client_host_tools.go implements the 'client host-tools' subcommands for host tool operations via HTTP. It uses HTTPBackend to communicate with the HostMCP server for listing, inspecting, and running host tools.

client_host_tools.goはHTTP経由でホストツール操作を行う'client host-tools'サブコマンドを実装します。 HTTPBackendを使用してHostMCPサーバーと通信し、ホストツールの一覧表示、詳細取得、実行を行います。

client_inspect.go implements the 'client inspect' subcommand for retrieving container details via HTTP. It uses HTTPBackend to communicate with the HostMCP server instead of direct Docker access.

client_inspect.goはHTTP経由でコンテナ詳細を取得する'client inspect'サブコマンドを実装します。 直接Docker接続の代わりにHTTPBackendを使用してHostMCPサーバーと通信します。

client_lifecycle.go implements the 'client restart/stop/start' subcommands for container lifecycle management via Docker API.

client_lifecycle.goはDocker API経由のコンテナライフサイクル管理用の 'client restart/stop/start'サブコマンドを実装します。

client_list.go implements the 'client list' subcommand for listing containers via HTTP. It uses HTTPBackend to communicate with the HostMCP server instead of direct Docker access.

client_list.goはHTTP経由でコンテナを一覧表示する'client list'サブコマンドを実装します。 直接Docker接続の代わりにHTTPBackendを使用してHostMCPサーバーと通信します。

client_logs.go implements the 'client logs' subcommand for retrieving container logs via HTTP. It uses HTTPBackend to communicate with the HostMCP server instead of direct Docker access.

client_logs.goはHTTP経由でコンテナログを取得する'client logs'サブコマンドを実装します。 直接Docker接続の代わりにHTTPBackendを使用してHostMCPサーバーと通信します。

client_stats.go implements the 'client stats' subcommand for retrieving container statistics via HTTP. It uses HTTPBackend to communicate with the HostMCP server instead of direct Docker access.

client_stats.goはHTTP経由でコンテナ統計を取得する'client stats'サブコマンドを実装します。 直接Docker接続の代わりにHTTPBackendを使用してHostMCPサーバーと通信します。

colored_handler.go implements a colored slog handler for terminal output. It adds ANSI color codes to log levels for better visibility in terminals.

colored_handler.goはターミナル出力用のカラーslogハンドラーを実装します。 ターミナルでの視認性向上のためにログレベルにANSIカラーコードを追加します。

current.go provides utilities for managing the "current container" state. This feature allows users to set a default container for CLI commands, reducing the need to specify the container name for every command.

current.goは「カレントコンテナ」状態を管理するユーティリティを提供します。 この機能により、ユーザーはCLIコマンドのデフォルトコンテナを設定でき、 毎回コンテナ名を指定する必要がなくなります。

Design philosophy: - AI (MCP/client): Uses explicit parameters, no convenience features needed - Human (direct commands): Uses convenience features for better UX

設計思想: - AI (MCP/client): 明示的なパラメータを使用、利便性機能は不要 - 人 (直接コマンド): より良いUXのために利便性機能を使用

exec.go implements the 'exec' command for executing commands in containers. It enforces security by only allowing whitelisted commands defined in the security policy.

exec.goはコンテナ内でコマンドを実行する'exec'コマンドを実装します。 セキュリティポリシーで定義されたホワイトリストに登録されたコマンドのみを許可してセキュリティを強制します。

inspect.go implements the 'inspect' command for displaying container details. It shows configuration, network settings, and mount information for a container.

inspect.goはコンテナの詳細を表示する'inspect'コマンドを実装します。 設定、ネットワーク設定、マウント情報を表示します。

list.go implements the 'list' command for displaying accessible Docker containers. It uses the DirectBackend to communicate with Docker and applies the security policy.

list.goはアクセス可能なDockerコンテナを表示する'list'コマンドを実装します。 DirectBackendを使用してDockerと通信し、セキュリティポリシーを適用します。

logs.go implements the 'logs' command for retrieving container logs. It uses the DirectBackend to fetch logs from Docker containers.

logs.goはコンテナログを取得する'logs'コマンドを実装します。 DirectBackendを使用してDockerコンテナからログを取得します。

Package cli implements the command-line interface for HostMCP. It provides commands for starting the MCP server, listing containers, viewing logs, executing commands, and client operations for remote access.

cliパッケージはHostMCPのコマンドラインインターフェースを実装します。 MCPサーバーの起動、コンテナ一覧表示、ログ閲覧、コマンド実行、 およびリモートアクセス用のクライアント操作のコマンドを提供します。

serve.go implements the 'serve' command for starting the HostMCP MCP server. This file handles server configuration, logging setup, and graceful shutdown.

serve.goはHostMCP MCPサーバーを起動する'serve'コマンドを実装します。 このファイルはサーバー設定、ログ設定、グレースフルシャットダウンを処理します。

stats.go implements the 'stats' command for displaying container resource statistics. It shows CPU, memory, network, and disk I/O usage for a container.

stats.goはコンテナのリソース統計を表示する'stats'コマンドを実装します。 CPU、メモリ、ネットワーク、ディスクI/Oの使用状況を表示します。

tools.go implements the 'tools' command group for managing host tools. Provides subcommands for syncing tools from staging to approved directory.

tools.goはホストツール管理用の'tools'コマンドグループを実装します。 ステージングから承認済みディレクトリへのツール同期のサブコマンドを提供します。

use.go implements the 'use' command for managing the current container. This command allows users to set, display, or clear the current container for use with other CLI commands like logs, stats, and exec.

use.goはカレントコンテナを管理する'use'コマンドを実装します。 このコマンドにより、ユーザーはlogs、stats、execなどの他のCLIコマンドで使用する カレントコンテナを設定、表示、またはクリアできます。

version.go implements the 'version' command for displaying the HostMCP version. The version is typically set at build time using ldflags.

version.goはHostMCPのバージョンを表示する'version'コマンドを実装します。 バージョンは通常、ldflagsを使用してビルド時に設定されます。

Index

Constants

View Source
const CurrentContainerStateFile = "current"

CurrentContainerStateFile is the default filename for storing current container state. CurrentContainerStateFileはカレントコンテナ状態を保存するデフォルトのファイル名です。

Variables

View Source
var Version = "dev"

Version holds the application version string. This is set at build time using ldflags: go build -ldflags "-X github.com/YujiSuzuki/hostmcp/internal/cli.Version=1.0.0" The default value "dev" indicates a development build.

Versionはアプリケーションのバージョン文字列を保持します。 これはldflagsを使用してビルド時に設定されます: go build -ldflags "-X github.com/YujiSuzuki/hostmcp/internal/cli.Version=1.0.0" デフォルト値"dev"は開発ビルドを示します。

Functions

func ClearCurrentContainer

func ClearCurrentContainer() error

ClearCurrentContainer removes the current container state file. No error is returned if the file doesn't exist.

ClearCurrentContainerはカレントコンテナ状態ファイルを削除します。 ファイルが存在しない場合はエラーを返しません。

func Execute

func Execute()

Execute runs the root command and all its subcommands. This is the main entry point for the CLI application. It handles command parsing, execution, and error reporting. If an error occurs, it prints the error to stderr and exits with code 1.

Executeはルートコマンドとそのすべてのサブコマンドを実行します。 これはCLIアプリケーションのメインエントリーポイントです。 コマンドの解析、実行、エラー報告を処理します。 エラーが発生した場合、stderrにエラーを出力し、終了コード1で終了します。

func GetContainerOrCurrent

func GetContainerOrCurrent(container string) (string, error)

GetContainerOrCurrent returns the specified container or falls back to the current container. If container is not empty, it's returned as-is. If container is empty and current container feature is enabled, the current container is returned. Returns an error if container is empty and no current container is set or feature is disabled.

GetContainerOrCurrentは指定されたコンテナを返すか、カレントコンテナにフォールバックします。 containerが空でない場合はそのまま返します。 containerが空でカレントコンテナ機能が有効な場合はカレントコンテナを返します。 containerが空でカレントコンテナが設定されていないか機能が無効な場合はエラーを返します。

func GetCurrentContainerDir

func GetCurrentContainerDir() (string, error)

GetCurrentContainerDir returns the directory for storing HostMCP state files. Returns ~/.hostmcp by default.

GetCurrentContainerDirはHostMCP状態ファイルを保存するディレクトリを返します。 デフォルトは~/.hostmcpです。

func GetCurrentContainerPath

func GetCurrentContainerPath() (string, error)

GetCurrentContainerPath returns the full path to the current container state file. Returns ~/.hostmcp/current by default.

GetCurrentContainerPathはカレントコンテナ状態ファイルのフルパスを返します。 デフォルトは~/.hostmcp/currentです。

func IsCurrentContainerEnabled

func IsCurrentContainerEnabled() (bool, error)

IsCurrentContainerEnabled checks if the current container feature is enabled in config. Loads the configuration and returns the enabled state.

IsCurrentContainerEnabledは設定でカレントコンテナ機能が有効かどうかをチェックします。 設定を読み込み、有効状態を返します。

func ReadCurrentContainer

func ReadCurrentContainer() (string, error)

ReadCurrentContainer reads the current container from the state file. Returns an empty string if no current container is set or file doesn't exist.

ReadCurrentContainerは状態ファイルからカレントコンテナを読み取ります。 カレントコンテナが設定されていないかファイルが存在しない場合は空文字列を返します。

func WriteCurrentContainer

func WriteCurrentContainer(container string) error

WriteCurrentContainer writes the current container to the state file. Creates the state directory if it doesn't exist.

WriteCurrentContainerはカレントコンテナを状態ファイルに書き込みます。 状態ディレクトリが存在しない場合は作成します。

Types

type Backend

type Backend interface {
	// ListContainers lists all accessible containers.
	// Returns a slice of ContainerInfo for containers matching the security policy.
	//
	// ListContainersはアクセス可能なすべてのコンテナを一覧表示します。
	// セキュリティポリシーに一致するコンテナのContainerInfoスライスを返します。
	ListContainers(ctx context.Context) ([]docker.ContainerInfo, error)

	// GetLogs retrieves logs from a container.
	// The tail parameter specifies the number of lines from the end.
	// The since parameter filters logs to entries after the given timestamp (empty string disables).
	//
	// GetLogsはコンテナからログを取得します。
	// tailパラメータは末尾からの行数を指定します。
	// sinceパラメータは指定タイムスタンプ以降のログにフィルタします(空文字列で無効)。
	GetLogs(ctx context.Context, container string, tail string, since string) (string, error)

	// Exec executes a command in a container.
	// Only whitelisted commands are allowed (enforced by security policy).
	// If dangerously is true, commands from exec_dangerously list are allowed
	// with file path validation against blocked_paths.
	//
	// Execはコンテナ内でコマンドを実行します。
	// ホワイトリストに登録されたコマンドのみが許可されます(セキュリティポリシーで強制)。
	// dangerouslyがtrueの場合、exec_dangerouslyリストのコマンドが
	// blocked_pathsに対するファイルパス検証付きで許可されます。
	Exec(ctx context.Context, container string, command string, dangerously bool) (*docker.ExecResult, error)

	// Close releases any resources held by the backend.
	// Should be called when the backend is no longer needed.
	//
	// Closeはバックエンドが保持するリソースを解放します。
	// バックエンドが不要になったときに呼び出す必要があります。
	Close() error
}

Backend represents a backend for executing container operations. It abstracts the underlying transport mechanism (direct Docker or HTTP/MCP). This interface is implemented by both DirectBackend and HTTPBackend.

Backendはコンテナ操作を実行するバックエンドを表します。 基盤となるトランスポートメカニズム(直接DockerまたはHTTP/MCP)を抽象化します。 このインターフェースはDirectBackendとHTTPBackendの両方で実装されています。

type ColoredHandler

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

ColoredHandler is a slog.Handler that outputs colored log messages to terminals. Colors are only applied when the output is a TTY.

ColoredHandlerはターミナルにカラーログメッセージを出力するslog.Handlerです。 出力がTTYの場合のみ色が適用されます。

func NewColoredHandler

func NewColoredHandler(out io.Writer, level slog.Level) *ColoredHandler

NewColoredHandler creates a new ColoredHandler. If out is a terminal, colors will be enabled.

NewColoredHandlerは新しいColoredHandlerを作成します。 outがターミナルの場合、色が有効になります。

func (*ColoredHandler) Enabled

func (h *ColoredHandler) Enabled(_ context.Context, level slog.Level) bool

Enabled implements slog.Handler.Enabled. EnabledはslogHandler.Enabledを実装します。

func (*ColoredHandler) Handle

func (h *ColoredHandler) Handle(_ context.Context, r slog.Record) error

Handle implements slog.Handler.Handle. It formats and writes the log record with colors.

HandleはslogHandler.Handleを実装します。 ログレコードをフォーマットして色付きで書き込みます。

func (*ColoredHandler) WithAttrs

func (h *ColoredHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs implements slog.Handler.WithAttrs. WithAttrsはslog.Handler.WithAttrsを実装します。

func (*ColoredHandler) WithGroup

func (h *ColoredHandler) WithGroup(name string) slog.Handler

WithGroup implements slog.Handler.WithGroup. WithGroupはslog.Handler.WithGroupを実装します。

type DirectBackend

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

DirectBackend implements Backend using direct Docker access. It communicates directly with the Docker daemon via the Docker socket. This is used by the main CLI commands (list, logs, exec) when running on the host.

DirectBackendは直接Dockerアクセスを使用してBackendを実装します。 Dockerソケット経由でDockerデーモンと直接通信します。 ホストで実行される際のメインCLIコマンド(list, logs, exec)で使用されます。

func NewDirectBackend

func NewDirectBackend() (*DirectBackend, error)

NewDirectBackend creates a new DirectBackend with the loaded configuration. It initializes the security policy and Docker client. Returns an error if configuration loading or Docker client creation fails.

NewDirectBackendは読み込まれた設定で新しいDirectBackendを作成します。 セキュリティポリシーとDockerクライアントを初期化します。 設定の読み込みまたはDockerクライアント作成が失敗した場合はエラーを返します。

func (*DirectBackend) Close

func (b *DirectBackend) Close() error

Close closes the Docker client and releases associated resources.

CloseはDockerクライアントを閉じて関連リソースを解放します。

func (*DirectBackend) Exec

func (b *DirectBackend) Exec(ctx context.Context, container string, command string, dangerously bool) (*docker.ExecResult, error)

Exec executes a command in the specified container. The command must be whitelisted in the security policy. If dangerously is true, commands from exec_dangerously list are allowed.

Execは指定されたコンテナでコマンドを実行します。 コマンドはセキュリティポリシーでホワイトリストに登録されている必要があります。 dangerouslyがtrueの場合、exec_dangerouslyリストのコマンドが許可されます。

func (*DirectBackend) GetLogs

func (b *DirectBackend) GetLogs(ctx context.Context, container string, tail string, since string) (string, error)

GetLogs retrieves logs from the specified container. The tail parameter controls how many lines from the end to retrieve. The since parameter filters logs to entries after the given timestamp.

GetLogsは指定されたコンテナからログを取得します。 tailパラメータは末尾から取得する行数を制御します。 sinceパラメータは指定タイムスタンプ以降のログにフィルタします。

func (*DirectBackend) ListContainers

func (b *DirectBackend) ListContainers(ctx context.Context) ([]docker.ContainerInfo, error)

ListContainers returns all containers accessible according to the security policy. It delegates to the underlying Docker client.

ListContainersはセキュリティポリシーに従ってアクセス可能なすべてのコンテナを返します。 基盤となるDockerクライアントに委譲します。

type HTTPBackend

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

HTTPBackend implements Backend using HostMCP HTTP server. It connects to a running HostMCP server via HTTP/SSE and uses MCP. This is used by 'client' subcommands for remote access (e.g., from DevContainers).

HTTPBackendはHostMCP HTTPサーバーを使用してBackendを実装します。 HTTP/SSE経由で実行中のHostMCPサーバーに接続し、MCPプロトコルを使用します。 リモートアクセス用の'client'サブコマンドで使用されます(例:DevContainerから)。

func NewHTTPBackend

func NewHTTPBackend(url string) (*HTTPBackend, error)

NewHTTPBackend creates a new HTTPBackend connected to the specified server URL. It performs a health check and establishes an SSE connection before returning. Returns an error if health check fails or connection cannot be established.

NewHTTPBackendは指定されたサーバーURLに接続する新しいHTTPBackendを作成します。 返す前にヘルスチェックを実行し、SSE接続を確立します。 ヘルスチェックが失敗するか接続が確立できない場合はエラーを返します。

func NewHTTPBackendWithSuffix

func NewHTTPBackendWithSuffix(url string, suffix string) (*HTTPBackend, error)

NewHTTPBackendWithSuffix creates a new HTTPBackend with an optional client suffix. The suffix is appended to the client name for identification purposes.

NewHTTPBackendWithSuffixはオプションのクライアントサフィックス付きで新しいHTTPBackendを作成します。 サフィックスは識別目的でクライアント名に追加されます。

func (*HTTPBackend) Close

func (b *HTTPBackend) Close() error

Close closes the HTTP client connection.

CloseはHTTPクライアント接続を閉じます。

func (*HTTPBackend) Exec

func (b *HTTPBackend) Exec(ctx context.Context, container string, command string, dangerously bool) (*docker.ExecResult, error)

Exec executes a command via the MCP 'exec_command' tool. The command must be whitelisted on the server side. If dangerously is true, commands from exec_dangerously list are allowed. Returns the execution result including exit code and output.

ExecはMCPの'exec_command'ツール経由でコマンドを実行します。 コマンドはサーバー側でホワイトリストに登録されている必要があります。 dangerouslyがtrueの場合、exec_dangerouslyリストのコマンドが許可されます。 終了コードと出力を含む実行結果を返します。

func (*HTTPBackend) ExecHostCommand

func (b *HTTPBackend) ExecHostCommand(ctx context.Context, command string, dangerously bool) (string, error)

ExecHostCommand executes a host CLI command via the MCP 'exec_host_command' tool. Returns the raw text response from the server.

ExecHostCommandはMCPの'exec_host_command'ツール経由でホストCLIコマンドを実行します。 サーバーからのテキストレスポンスを返します。

func (*HTTPBackend) GetHostToolInfo

func (b *HTTPBackend) GetHostToolInfo(ctx context.Context, name string) (string, error)

GetHostToolInfo retrieves detailed info about a host tool via the MCP 'get_host_tool_info' tool. Returns the raw text response from the server.

GetHostToolInfoはMCPの'get_host_tool_info'ツール経由でホストツールの詳細情報を取得します。 サーバーからのテキストレスポンスを返します。

func (*HTTPBackend) GetLogs

func (b *HTTPBackend) GetLogs(ctx context.Context, container string, tail string, since string) (string, error)

GetLogs retrieves container logs via the MCP 'get_logs' tool. The container name, tail count, and since timestamp are passed as arguments.

GetLogsはMCPの'get_logs'ツール経由でコンテナログを取得します。 コンテナ名、tailカウント、sinceタイムスタンプは引数として渡されます。

func (*HTTPBackend) GetStats

func (b *HTTPBackend) GetStats(ctx context.Context, container string) (string, error)

GetStats retrieves container statistics via the MCP 'get_stats' tool. Returns the raw JSON response from the server.

GetStatsはMCPの'get_stats'ツール経由でコンテナ統計を取得します。 サーバーからの生のJSONレスポンスを返します。

func (*HTTPBackend) InspectContainer

func (b *HTTPBackend) InspectContainer(ctx context.Context, container string) (string, error)

InspectContainer retrieves container details via the MCP 'inspect_container' tool. Returns the raw JSON response from the server.

InspectContainerはMCPの'inspect_container'ツール経由でコンテナ詳細を取得します。 サーバーからの生のJSONレスポンスを返します。

func (*HTTPBackend) ListContainers

func (b *HTTPBackend) ListContainers(ctx context.Context) ([]docker.ContainerInfo, error)

ListContainers retrieves the container list via the MCP 'list_containers' tool. It parses the JSON response and returns the container information.

ListContainersはMCPの'list_containers'ツール経由でコンテナリストを取得します。 JSONレスポンスを解析してコンテナ情報を返します。

func (*HTTPBackend) ListHostTools

func (b *HTTPBackend) ListHostTools(ctx context.Context) (string, error)

ListHostTools lists available host tools via the MCP 'list_host_tools' tool. Returns the raw text response from the server.

ListHostToolsはMCPの'list_host_tools'ツール経由で利用可能なホストツールを一覧表示します。 サーバーからのテキストレスポンスを返します。

func (*HTTPBackend) RestartContainer

func (b *HTTPBackend) RestartContainer(ctx context.Context, container string, timeout *int) (string, error)

RestartContainer restarts a container via the MCP 'restart_container' tool.

RestartContainerはMCPの'restart_container'ツール経由でコンテナを再起動します。

func (*HTTPBackend) RunHostTool

func (b *HTTPBackend) RunHostTool(ctx context.Context, name string, args []string) (string, error)

RunHostTool executes a host tool via the MCP 'run_host_tool' tool. Returns the raw text response from the server.

RunHostToolはMCPの'run_host_tool'ツール経由でホストツールを実行します。 サーバーからのテキストレスポンスを返します。

func (*HTTPBackend) StartContainer

func (b *HTTPBackend) StartContainer(ctx context.Context, container string) (string, error)

StartContainer starts a container via the MCP 'start_container' tool.

StartContainerはMCPの'start_container'ツール経由でコンテナを起動します。

func (*HTTPBackend) StopContainer

func (b *HTTPBackend) StopContainer(ctx context.Context, container string, timeout *int) (string, error)

StopContainer stops a container via the MCP 'stop_container' tool.

StopContainerはMCPの'stop_container'ツール経由でコンテナを停止します。

Jump to

Keyboard shortcuts

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