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 ¶
- Variables
- func ListHosts() ([]string, error)
- func ListPasswordHosts() ([]string, error)
- func LoadPasswords() (map[string]string, error)
- func LookupPassword(alias, hostName string) (string, bool)
- func Redact(s string) string
- func SavePassword(alias, password string) error
- func SetVerbose(v bool)
- func Text(s string) (*mcp.CallToolResult, any, error)
- func ToolError(err error) (*mcp.CallToolResult, any, error)
- func Truncate(b []byte, max int, id string) string
- type App
- func (a *App) Exec(ctx context.Context, req *mcp.CallToolRequest, in ExecIn) (*mcp.CallToolResult, any, error)
- func (a *App) Forward(ctx context.Context, req *mcp.CallToolRequest, in ForwardIn) (*mcp.CallToolResult, any, error)
- func (a *App) Get(ctx context.Context, req *mcp.CallToolRequest, in GetIn) (*mcp.CallToolResult, any, error)
- func (a *App) LS(ctx context.Context, req *mcp.CallToolRequest, in LSIn) (*mcp.CallToolResult, any, error)
- func (a *App) Output(ctx context.Context, req *mcp.CallToolRequest, in OutputIn) (*mcp.CallToolResult, any, error)
- func (a *App) Put(ctx context.Context, req *mcp.CallToolRequest, in PutIn) (*mcp.CallToolResult, any, error)
- type ExecIn
- type ForwardIn
- type Forwarder
- type GetIn
- type HostConfig
- type LSIn
- type OutputIn
- type OutputStore
- type Pool
- type PutIn
Constants ¶
This section is empty.
Variables ¶
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.
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.
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.
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 ¶
ListHosts returns host aliases defined in ~/.ssh/config (including Include'd files), skipping wildcard and negated patterns.
func ListPasswordHosts ¶ added in v0.1.1
ListPasswordHosts returns the aliases with stored passwords, sorted.
func LoadPasswords ¶ added in v0.1.1
LoadPasswords reads the credentials file. A missing file is not an error.
func LookupPassword ¶ added in v0.1.1
LookupPassword returns the password for a host, trying the alias first and then the resolved hostname.
func Redact ¶
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
SavePassword writes one host's password, creating the file with 0600.
Types ¶
type App ¶
type App struct {
Pool *Pool
Store *OutputStore
Forwards *Forwarder
LocalUser string
}
App bundles the shared state behind all tool handlers.
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.
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
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 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