miller/pkg/bifs/system.go
John Kerl 41f5188bd0
Add mlr mcp MCP server + agent playbook, with a --no-shell gate (#2098 PR7) (#2133)
* Plan: flesh out PR7 (MCP server + Agent Skill) design

stdio transport (no HTTP port), mlr mcp terminal in the main binary,
SDK-vs-handroll decision, tool list, in-process vs subprocess split,
run-tool safety (--no-shell prerequisite), single-sourced skill, tests.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Add mlr mcp: MCP server + agent playbook; --no-shell gate (#2098 PR7)

New terminal `mlr mcp` runs a Model Context Protocol server over stdio
(spawned by MCP clients; no network port), exposing five tools --
list_capabilities, which, validate_dsl, describe_data, run -- plus an
agent playbook as MCP prompt/resource. Catalog tools are served
in-process from the help registries; the rest subprocess this same
binary with MLR_ERRORS_JSON=1, a timeout, and an output cap.

Prerequisite: a new --no-shell flag / MLR_NO_SHELL env var (one-way
gate) disables the DSL system/exec functions, piped redirects, and
--prepipe/--prepipex; the MCP server sets it on the commands it runs
unless started with --allow-shell.

Adds the github.com/modelcontextprotocol/go-sdk dependency.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Force LF checkout for the embedded SKILL.md (Windows CI fix)

go:embed embeds checkout bytes, so a CRLF checkout on Windows made the
embedded playbook differ per platform and failed
TestPlaybookHasFrontmatter. Pin the file to eol=lf in .gitattributes.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Move no-shell test DSL into per-case mlr files (Windows CI fix)

Inline single-quoted DSL in cmd files is mangled by the Windows shell
(single quotes are not quote characters there); the harness's
put -f ${CASEDIR}/mlr pattern avoids shell quoting entirely.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-03 19:41:49 -04:00

132 lines
3.1 KiB
Go

package bifs
import (
"os"
"os/exec"
"runtime"
"strings"
"github.com/johnkerl/miller/v6/pkg/lib"
"github.com/johnkerl/miller/v6/pkg/mlrval"
"github.com/johnkerl/miller/v6/pkg/platform"
"github.com/johnkerl/miller/v6/pkg/version"
)
func BIF_version() *mlrval.Mlrval {
return mlrval.FromString(version.STRING)
}
func BIF_os() *mlrval.Mlrval {
return mlrval.FromString(runtime.GOOS)
}
func BIF_hostname() *mlrval.Mlrval {
hostname, err := os.Hostname()
if err != nil {
return mlrval.FromErrorString("could not retrieve system hostname")
}
return mlrval.FromString(hostname)
}
func BIF_system(input1 *mlrval.Mlrval) *mlrval.Mlrval {
if !lib.ShellOutEnabled() {
return mlrval.FromErrorString("system: disabled by --no-shell / MLR_NO_SHELL")
}
if !input1.IsStringOrVoid() {
return mlrval.FromNotStringError("system", input1)
}
commandString := input1.AcquireStringValue()
shellRunArray := platform.GetShellRunArray(commandString)
outputBytes, err := exec.Command(shellRunArray[0], shellRunArray[1:]...).Output()
if err != nil {
return mlrval.FromError(err)
}
outputString := strings.TrimRight(string(outputBytes), "\n")
return mlrval.FromString(outputString)
}
func BIF_exec(mlrvals []*mlrval.Mlrval) *mlrval.Mlrval {
if !lib.ShellOutEnabled() {
return mlrval.FromErrorString("exec: disabled by --no-shell / MLR_NO_SHELL")
}
if len(mlrvals) == 0 {
return mlrval.FromErrorString("exec: zero-length input given")
}
cmd := exec.Command(mlrvals[0].String())
combinedOutput := false
args := []string{mlrvals[0].String()}
if len(mlrvals) > 1 {
for _, val := range mlrvals[1].GetArray()[0:] {
args = append(args, val.String())
}
}
cmd.Args = args
if len(mlrvals) > 2 {
for pe := mlrvals[2].AcquireMapValue().Head; pe != nil; pe = pe.Next {
if pe.Key == "env" {
env := []string{}
for _, val := range pe.Value.GetArray()[0:] {
env = append(env, val.String())
}
cmd.Env = env
}
if pe.Key == "dir" {
cmd.Dir = pe.Value.String()
}
if pe.Key == "combined_output" {
combinedOutput = pe.Value.AcquireBoolValue()
}
if pe.Key == "stdin_string" {
cmd.Stdin = strings.NewReader(pe.Value.String())
}
}
}
outputBytes := []byte(nil)
err := error(nil)
if combinedOutput {
outputBytes, err = cmd.CombinedOutput()
} else {
outputBytes, err = cmd.Output()
}
if err != nil {
return mlrval.FromError(err)
}
outputString := strings.TrimRight(string(outputBytes), "\n")
return mlrval.FromString(outputString)
}
func BIF_stat(input1 *mlrval.Mlrval) *mlrval.Mlrval {
if !input1.IsStringOrVoid() {
return mlrval.FromNotStringError("system", input1)
}
path := input1.AcquireStringValue()
fileInfo, err := os.Stat(path)
if err != nil {
return mlrval.FromError(err)
}
output := mlrval.NewMlrmap()
output.PutReference("name", mlrval.FromString(fileInfo.Name()))
output.PutReference("size", mlrval.FromInt(fileInfo.Size()))
output.PutReference("mode", mlrval.FromIntShowingOctal(int64(fileInfo.Mode())))
output.PutReference("modtime", mlrval.FromInt(fileInfo.ModTime().UTC().Unix()))
output.PutReference("isdir", mlrval.FromBool(fileInfo.IsDir()))
return mlrval.FromMap(output)
}