package host import ( "context" "crypto/sha256" "encoding/hex" "fmt" "os/exec" ) // Command returns the command that runs a verified stdio MCP server with args, // in its own user and network namespace. // // An extension is confined only when its package names egress hosts; a stdio // MCP server always is, because there is no contract behind it to hold it: the // namespace and the allow-list are what limit it, and an acceptance that names // no host leaves it able to reach nothing. // // env is added to the server's environment as given, for the settings and // credentials its acceptance resolves. The returned release ends the server's // egress gate and closes the verified binary. func (h *Host) Command(ctx context.Context, spec Spec, args, env []string) (*exec.Cmd, func(), error) { sum, err := hex.DecodeString(spec.SHA256) if err != nil || len(sum) != sha256.Size { return nil, nil, fmt.Errorf("mcp server %q: sha256 must be a hex digest", spec.Name) } binary, err := openVerified(spec.Path, sum) if err != nil { return nil, nil, fmt.Errorf("mcp server %q: %w", spec.Name, err) } release := func() { _ = binary.Close() } g, err := openGate(ctx, spec.Egress) if err != nil { release() return nil, nil, fmt.Errorf("mcp server %q: egress: %w", spec.Name, err) } // The server's lifetime is its session's, not the caller's deadline: the // accept path and the supervisor both connect under a timeout. lifetime, stop := context.WithCancel(context.WithoutCancel(ctx)) cmd, err := confinedCommand(lifetime, binary, args, g.socket) if err != nil { stop() g.close() release() return nil, nil, fmt.Errorf("mcp server %q: %w", spec.Name, err) } cmd.Env = append(passEnv(spec.Env), env...) return cmd, func() { stop(); g.close(); release() }, nil }