mcpserver

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 27 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 mcpserver exposes the pool to MCP clients over stdio.

It is the rewrite's core change: every tool call goes through the shared connection pool instead of performing a full login/resolve/connect/ teardown cycle (DESIGN.md「总体架构」).

Index

Constants

View Source
const (
	ToolAuditList     = "jms_audit_list"
	ToolSessionList   = "jms_session_list"
	ToolSessionShow   = "jms_session_show"
	ToolCommandList   = "jms_command_list"
	ToolNodeTree      = "jms_node_tree"
	ToolPlatformList  = "jms_platform_list"
	ToolAccountList   = "jms_account_list"
	ToolIdentityList  = "jms_identity_list"
	ToolIdentityShow  = "jms_identity_show"
	ToolJobList       = "jms_job_list"
	ToolJobExecutions = "jms_job_execution_list"
	ToolTicketList    = "jms_ticket_list"
	ToolTicketShow    = "jms_ticket_show"
	ToolLocalAudit    = "jms_local_audit"
)

Tool names for the management read surface (MIFA-73, 方案 A 单轨).

There is intentionally no generic "call any path" tool here, not even a read-only one: the caller of an MCP tool is a model, so a `path` argument would put "read anything the token can see" one sentence away, and the boundary that matters (which fields are secrets, whose session log this is) cannot be expressed as a path prefix. Each of these tools is a verb with a fixed shape instead.

View Source
const (
	// EnvToolset set to "core" registers only the pre-MIFA-73 tools.
	EnvToolset = "JMS_MCP_TOOLSET"
	// EnvWrite set to "limited" registers the write tools at all.
	EnvWrite = "JMS_MCP_WRITE"
	// EnvWriteDomains is a comma list narrowing EnvWrite: "node,account".
	EnvWriteDomains = "JMS_MCP_WRITE_DOMAINS"
)

Environment switches for the management surface.

All of them are read at tool-registration time, so a restart is enough to change the surface and no configuration file has to change. That matters for rollback: unsetting a variable restores the previous surface without touching anything on disk.

View Source
const (
	ToolNodeWrite     = "jms_node_write"
	ToolAccountSecret = "jms_account_secret"
)

Tool names for the gated write surface.

Two operations, not fifteen. Node create/update/delete keeps the asset tree maintainable between console visits; account secret rotation is the one routine operation that is both frequent and easy to get wrong by hand. Everything else (user changes, session kill, job execution, ticket approval) waits for a real need — a write tool is a production change that no longer needs a console.

View Source
const (
	DomainNode    = "node"
	DomainAccount = "account"
)

Write domains, matching EnvWriteDomains values.

View Source
const (
	ToolAssetCreate = "jms_asset_create"
	ToolAssetList   = "jms_asset_list"
	ToolAssetShow   = "jms_asset_show"
	ToolAssetUpdate = "jms_asset_update"
	ToolAssetDelete = "jms_asset_delete"
)

Tool names for the asset-management tools.

View Source
const (
	ToolAssetGrant       = "jms_asset_grant"
	ToolAssetPermissions = "jms_asset_permissions"
	ToolAssetRevoke      = "jms_asset_revoke"
)

Tool names for the permission tools.

View Source
const (
	ToolLS           = "jms_ls"
	ToolResolveAsset = "jms_resolve_asset"
	ToolExec         = "jms_exec"
	ToolSFTPUpload   = "jms_sftp_upload"
	ToolSFTPDownload = "jms_sftp_download"
	ToolSFTPRelay    = "jms_sftp_relay"
	ToolConfigList   = "jms_config_list"
	ToolPoolStatus   = "jms_pool_status"
)

Tool names, kept identical to the Python implementation so existing prompts and skills keep working (DESIGN.md「总体架构」).

View Source
const (
	// EnvBackend forces the transport backend: ssh, ws or auto.
	EnvBackend = "JMS_MCP_BACKEND"
	// EnvEndpoint forces an address kind: internal or external.
	EnvEndpoint = "JMS_MCP_ENDPOINT"
)

EnvBackend and EnvEndpoint override, for the whole MCP process, what the CLI exposes as --backend and --endpoint.

The MCP surface has no flags: an AI client launches one long-lived process and cannot add arguments per call. Without these variables the only way to steer the transport from the MCP side was to edit configuration and restart the host, which is exactly the trap this fixes — a host that had pinned itself to an endpoint/backend pair the failover policy had since abandoned could only be recovered by restarting the client (DESIGN.md「端点故障转移」第 4 点、第 7 点).

View Source
const DefaultExecTimeout = 5 * time.Minute

DefaultExecTimeout bounds one jms_exec call when the client omits timeout.

Five minutes, not the Python tool's 30s: the MCP surface is driven by an agent that cannot see how long a command will take, and 30s cut off builds, deploys and package installs that a human would have waited out. The value mirrors ipc.execTimeout, which bounds an operator command the same way.

The ceiling still exists on purpose — DESIGN.md「KoKo 协议」notes that a command ending in '#' or '\', or with unbalanced quotes, swallows the status marker and hangs, and the timeout is the only defence. EnvExecTimeout raises or lowers it without a rebuild.

View Source
const EnvDebug = "JMS_MCP_DEBUG"

EnvDebug enables the debug-only jms_pool_status tool.

View Source
const EnvExecTimeout = "JMS_MCP_EXEC_TIMEOUT"

EnvExecTimeout overrides DefaultExecTimeout for the whole MCP process.

Like EnvBackend and EnvEndpoint, it exists because an MCP client launches one long-lived process and cannot pass a flag per call: without it, tuning the ceiling meant editing the build or teaching every caller to pass `timeout` explicitly. The value is a Go duration ("90s", "10m") or a bare number of seconds.

View Source
const ServerName = "jms"

ServerName is the MCP implementation name.

View Source
const ServerVersion = "0.1.0"

ServerVersion is the version reported to MCP clients when Options.Version is empty. It is a fallback only: the CLI passes its own version so a client sees the same string `jms version` prints.

Variables

This section is empty.

Functions

func DebugEnabled

func DebugEnabled() bool

DebugEnabled reports whether JMS_MCP_DEBUG asks for the debug tool.

Only an explicit "1"/"true" enables it: a stray value must not widen the tool surface the AI can see.

func PrintConfig

func PrintConfig() string

PrintConfig renders the client configuration snippet for `jms mcp --print-config`.

The snippet carries no credential: the MCP entry is just the command, and credentials live in the OS credential store (DESIGN.md「MCP 客户端集成」). That is what makes the entry safe to sync through a client manager.

Types

type ExecFunc

type ExecFunc func(ctx context.Context, srv *config.ServerConfig, sess *auth.Session,
	assetName, command string, opts connpool.ExecOptions) (transport.Result, error)

ExecFunc runs one command on an asset through the shared terminal pool.

It matches connpool.TerminalPool.Exec so the production default is that method itself.

type Options

type Options struct {
	// ConfigPath overrides the configuration file path.
	ConfigPath string
	// Version is reported to MCP clients in the initialize handshake. Empty
	// means ServerVersion, the package default. The caller injects it so the
	// version a client sees matches `jms version` rather than drifting from
	// it.
	Version string
	// Actor names this client in the audit stream (e.g. "mcp:pi").
	Actor string
	// Bus receives audit events; nil disables publication.
	Bus *obs.Bus
	// AuditDir is the directory the audit JSONL files live in, so the
	// management surface can expose the local stream (jms_local_audit)
	// without a second configuration path.
	AuditDir string
	// Sessions and Terminals are the pools to serve from.
	Sessions  *connpool.SessionPool
	Terminals *connpool.TerminalPool
	// Debug registers jms_pool_status.
	Debug bool
	// In and Out are the stdio transport; nil means os.Stdin/os.Stdout.
	In  interface{ Read([]byte) (int, error) }
	Out interface{ Write([]byte) (int, error) }

	// Creds reads the stored password and TOTP secret. Nil means the
	// environment-then-OS-store chain.
	Creds config.CredentialStore
	// OTPPrompt asks for a one-time code when the server demands MFA and no
	// secret is stored. It is nil by default because an MCP client has no
	// terminal; the session then fails with an actionable message.
	OTPPrompt func() (string, error)

	// Session overrides how a server alias becomes a session. Nil means the
	// Sessions pool with credentials from Creds.
	Session SessionFunc
	// Probe checks address reachability during endpoint selection. Nil means
	// endpoint.ProbeWith, which honours the server's configured proxy policy
	// (DESIGN.md「代理策略」); a test can inject a fake to avoid real dials.
	Probe endpoint.ProbeFunc

	// Resolve overrides asset resolution. Nil means assets.Resolve on the
	// session's API client.
	Resolve ResolveFunc
	// Exec overrides command execution. Nil means Terminals.Exec.
	Exec ExecFunc
	// Transfer overrides file transfer. Nil means a fresh xfer engine.
	Transfer TransferFunc
	// Publish overrides audit publication. Nil means Bus.Publish.
	Publish PublishFunc

	// Stderr receives diagnostics. Nil means os.Stderr.
	//
	// It exists because stdio is the protocol channel: a log line on stdout
	// would corrupt the JSON-RPC stream (DESIGN.md「技术栈」).
	Stderr io.Writer
	// contains filtered or unexported fields
}

Options configures the server.

type PublishFunc

type PublishFunc func(obs.Event)

PublishFunc records one audit event.

type RelayRequest

type RelayRequest struct {
	SrcSession *auth.Session
	SrcAsset   string
	SrcAccount string
	SrcPath    string
	DstSession *auth.Session
	DstAsset   string
	DstAccount string
	DstPath    string
}

RelayRequest is one remote-to-remote transfer.

type ResolveFunc

type ResolveFunc func(ctx context.Context, sess *auth.Session, name, account, protocol string) (assets.Info, error)

ResolveFunc resolves an asset to its connection info.

It matches connpool.TerminalPool.Resolve so the production default is the same call the pool makes.

type Server

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

Server is a stdio MCP server.

func New

func New(opts Options) *Server

New returns a server for opts.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context) error

Serve runs the stdio protocol until the client disconnects or ctx is cancelled.

type SessionFunc

type SessionFunc func(ctx context.Context, serverName string) (*auth.Session, *config.ServerConfig, error)

SessionFunc returns an authenticated session for one server alias, together with the server entry it belongs to.

It is the seam that keeps a tool call off the network in tests: the real implementation goes through the SessionPool, a test answers with a pre-built session.

type TransferFunc

type TransferFunc func(ctx context.Context, req TransferRequest) (xfer.Result, error)

TransferFunc performs one file transfer, one-sided or relayed.

type TransferRequest

type TransferRequest struct {
	// Session and Asset describe the remote side of a one-sided transfer.
	Session *auth.Session
	Asset   string
	Account string
	// Local and Remote are the paths of a one-sided transfer.
	Local  string
	Remote string
	// Direction is upload or download for a one-sided transfer.
	Direction xfer.Direction
	// Relay describes a remote-to-remote transfer; nil for one-sided.
	Relay *RelayRequest
	// Verify asks for an md5 comparison after the transfer.
	Verify bool
	// Chunk and Parallel override the xfer defaults when non-zero.
	Chunk    int
	Parallel int
}

TransferRequest is one file transfer as the MCP tools describe it.

A relay has both Remote sides set and no Local; a one-sided transfer sets Remote and Local and leaves Relay nil.

Jump to

Keyboard shortcuts

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