internal

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package internal implements the ssh-mcp server: a stdio MCP server that exposes SSH command execution, SFTP file transfer and port forwarding for hosts configured in ~/.ssh/config.

Index

Constants

This section is empty.

Variables

View Source
var (
	BackoffBase = 5 * time.Second
	BackoffMax  = 45 * time.Second
)

Backoff escalation for failed dials: 5s -> 15s -> 45s. Rapid retries feed sshd's PerSourcePenalties and turn a diagnosable auth error into an unreadable "handshake EOF" lockout, so we refuse to hammer.

View Source
var CredsFile = filepath.Join(homeDir(), ".ssh", "ssh-mcp.json")

CredsFile stores optional SSH passwords keyed by host alias. It lives in ~/.ssh next to the keys it supplements, mode 0600. The file never leaves this machine and its contents never appear in tool output or logs.

View Source
var MaxOutput = 8 * 1024

MaxOutput bounds how many bytes of command output are returned to the model in a single tool response. The rest stays in memory and can be fetched on demand via the ssh_output tool. Settable via SSH_MCP_MAX_OUTPUT.

View Source
var VLog = log.New(io.Discard, "ssh-mcp: ", log.Ltime)

VLog emits connection diagnostics to stderr. Silent unless SetVerbose(true) is called (the --verbose flag); stderr only, never stdout, so the MCP protocol stream stays clean. Diagnostics contain host names, resolved settings and key fingerprints — never key material.

Functions

func ListHosts

func ListHosts() ([]string, error)

ListHosts returns host aliases defined in ~/.ssh/config (including Include'd files), skipping wildcard and negated patterns.

func ListPasswordHosts added in v0.1.1

func ListPasswordHosts() ([]string, error)

ListPasswordHosts returns the aliases with stored passwords, sorted.

func LoadPasswords added in v0.1.1

func LoadPasswords() (map[string]string, error)

LoadPasswords reads the credentials file. A missing file is not an error.

func LookupPassword added in v0.1.1

func LookupPassword(alias, hostName string) (string, bool)

LookupPassword returns the password for a host, trying the alias first and then the resolved hostname.

func Redact

func Redact(s string) string

Redact scrubs private key material from arbitrary text before it reaches the model. Applied to every tool response.

func SavePassword added in v0.1.1

func SavePassword(alias, password string) error

SavePassword writes one host's password, creating the file with 0600.

func SetVerbose added in v0.1.3

func SetVerbose(v bool)

SetVerbose enables diagnostic logging.

func Text

func Text(s string) (*mcp.CallToolResult, any, error)

Text wraps a plain-text payload as the only content of a tool result. Responses are plain text, never JSON, to keep them cheap in model context.

func ToolError

func ToolError(err error) (*mcp.CallToolResult, any, error)

func Truncate

func Truncate(b []byte, max int, id string) string

Truncate returns printable output for the model: redacted, and cropped to head+tail when over max. The inline note names the id and byte offset for fetching the omitted middle with ssh_output.

Types

type App

type App struct {
	Pool      *Pool
	Store     *OutputStore
	Forwards  *Forwarder
	LocalUser string
}

App bundles the shared state behind all tool handlers.

func NewApp

func NewApp(insecure bool) *App

func (*App) Exec

func (a *App) Exec(ctx context.Context, req *mcp.CallToolRequest, in ExecIn) (*mcp.CallToolResult, any, error)

Exec runs a command on the remote host. Output is redacted and truncated; the full output is cached under an id retrievable via ssh_output.

func (*App) Forward

func (a *App) Forward(ctx context.Context, req *mcp.CallToolRequest, in ForwardIn) (*mcp.CallToolResult, any, error)

Forward manages local port forwards of the form 127.0.0.1:<auto> -> <remote_host>:<remote_port> through the SSH server.

func (*App) Get

func (a *App) Get(ctx context.Context, req *mcp.CallToolRequest, in GetIn) (*mcp.CallToolResult, any, error)

Get downloads a file over SFTP.

func (*App) LS

func (a *App) LS(ctx context.Context, req *mcp.CallToolRequest, in LSIn) (*mcp.CallToolResult, any, error)

LS lists a remote directory, one entry per line: mode size name.

func (*App) Output

func (a *App) Output(ctx context.Context, req *mcp.CallToolRequest, in OutputIn) (*mcp.CallToolResult, any, error)

Output pages through a cached exec result that was truncated earlier.

func (*App) Put

func (a *App) Put(ctx context.Context, req *mcp.CallToolRequest, in PutIn) (*mcp.CallToolResult, any, error)

Put uploads a file over SFTP.

type ExecIn

type ExecIn struct {
	Host       string `json:"host" jsonschema:"host alias from ~/.ssh/config, or a raw hostname"`
	Command    string `json:"command" jsonschema:"shell command to run on the remote host"`
	TimeoutSec int    `json:"timeout_sec,omitempty" jsonschema:"timeout in seconds (default 60)"`
	MaxBytes   int    `json:"max_bytes,omitempty" jsonschema:"max output bytes returned (default 8192)"`
}

type ForwardIn

type ForwardIn struct {
	Action     string `json:"action" jsonschema:"\"start\" or \"stop\""`
	Host       string `json:"host,omitempty" jsonschema:"host alias to tunnel through (required to start)"`
	RemoteHost string `json:"remote_host,omitempty" jsonschema:"target host as seen from the SSH server (default 127.0.0.1)"`
	RemotePort int    `json:"remote_port,omitempty" jsonschema:"target port on remote_host (required to start)"`
	ID         string `json:"id,omitempty" jsonschema:"forward id returned by start (required to stop)"`
}

type Forwarder

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

func NewForwarder

func NewForwarder() *Forwarder

func (*Forwarder) Start

func (f *Forwarder) Start(pool *Pool, alias, remoteHost string, remotePort int) (string, string, error)

func (*Forwarder) Stop

func (f *Forwarder) Stop(id string) error

type GetIn

type GetIn struct {
	Host       string `json:"host" jsonschema:"host alias from ~/.ssh/config, or a raw hostname"`
	RemotePath string `json:"remote_path" jsonschema:"file to download from the remote host"`
	LocalPath  string `json:"local_path" jsonschema:"destination file path on this machine"`
}

type HostConfig

type HostConfig struct {
	Alias         string
	HostName      string
	User          string
	Port          int
	IdentityFiles []string
	ProxyJump     string
}

HostConfig is the resolved connection info for one alias in ~/.ssh/config.

func ResolveHost

func ResolveHost(alias string) (*HostConfig, error)

ResolveHost resolves an alias (or a raw hostname) into connection settings the same way ssh(1) would: ~/.ssh/config first, then defaults.

type LSIn

type LSIn struct {
	Host string `json:"host" jsonschema:"host alias from ~/.ssh/config, or a raw hostname"`
	Path string `json:"path" jsonschema:"absolute remote directory path"`
}

type OutputIn

type OutputIn struct {
	ID     string `json:"id" jsonschema:"output id from a previous truncated ssh_exec result"`
	Offset int    `json:"offset,omitempty" jsonschema:"byte offset to start from"`
	Length int    `json:"length,omitempty" jsonschema:"number of bytes to return (0 = to the end)"`
}

type OutputStore

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

OutputStore keeps the full, untruncated output of each exec so the model can page through it with ssh_output when it actually needs more.

func NewOutputStore

func NewOutputStore() *OutputStore

func (*OutputStore) Page

func (s *OutputStore) Page(id string, offset, length int) (string, bool)

Page returns the [offset, offset+length) window of stored output. length <= 0 means "to the end".

func (*OutputStore) Save

func (s *OutputStore) Save(b []byte) string

type Pool

type Pool struct {
	Insecure bool
	// contains filtered or unexported fields
}

Pool holds one persistent SSH connection (plus an SFTP client) per host alias, reconnected on demand and evicted when the keepalive fails.

func NewPool

func NewPool(insecure bool) *Pool

func (*Pool) Client

func (p *Pool) Client(alias string) (*ssh.Client, error)

func (*Pool) SFTP

func (p *Pool) SFTP(alias string) (*sftp.Client, error)

SFTP returns (and caches) an SFTP client over the pooled connection.

type PutIn

type PutIn struct {
	Host       string `json:"host" jsonschema:"host alias from ~/.ssh/config, or a raw hostname"`
	LocalPath  string `json:"local_path" jsonschema:"file on this machine to upload"`
	RemotePath string `json:"remote_path" jsonschema:"destination file path on the remote host"`
}

Jump to

Keyboard shortcuts

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