miller/pkg/terminals/terminals.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

93 lines
2.5 KiB
Go

// Support for Miller regression testing. Originally bash scripts; ported to Go
// for ease of Windows-native testing.
package terminals
import (
"fmt"
"os"
"runtime"
"github.com/johnkerl/miller/v6/pkg/terminals/completion"
"github.com/johnkerl/miller/v6/pkg/terminals/help"
"github.com/johnkerl/miller/v6/pkg/terminals/mcp"
"github.com/johnkerl/miller/v6/pkg/terminals/registry"
"github.com/johnkerl/miller/v6/pkg/terminals/regtest"
"github.com/johnkerl/miller/v6/pkg/terminals/repl"
"github.com/johnkerl/miller/v6/pkg/terminals/script"
"github.com/johnkerl/miller/v6/pkg/version"
)
// tTerminalMain is a function-pointer type for the entrypoint handler for a given terminal,
// such as 'help' or 'regtest'.
type tTerminalMain func(args []string) int
type tTerminalLookupEntry struct {
name string
main tTerminalMain
}
// _TERMINAL_LOOKUP_TABLE is the lookup table for terminals. We get a Golang
// "initialization loop" if this is defined statically. So, we use a "package
// init" function.
var _TERMINAL_LOOKUP_TABLE = []tTerminalLookupEntry{}
func init() {
_TERMINAL_LOOKUP_TABLE = []tTerminalLookupEntry{
{registry.TerminalList, terminalListMain},
{registry.Completion, completion.CompletionMain},
{registry.Help, help.HelpMain},
{registry.Mcp, mcp.McpMain},
{registry.Regtest, regtest.RegTestMain},
{registry.Repl, repl.ReplMain},
{registry.Script, script.ScriptMain},
{registry.Version, showVersion},
{registry.Which, help.WhichMain},
}
}
func Dispatchable(arg string) bool {
for _, entry := range _TERMINAL_LOOKUP_TABLE {
if arg == entry.name {
return true
}
}
return false
}
func Dispatch(args []string) {
if len(args) < 1 {
return
}
terminal := args[0]
for _, entry := range _TERMINAL_LOOKUP_TABLE {
if terminal == entry.name {
os.Exit(entry.main(args))
}
}
fmt.Fprintf(os.Stderr, "mlr: terminal \"%s\" not found.\n", terminal)
os.Exit(1)
}
// terminalListMain is the handler for 'mlr terminal-list'.
func terminalListMain(args []string) int {
ShowTerminalEntries(os.Stdout)
return 0
}
// ShowTerminalEntries is a symbol is exported for 'mlr --help'.
func ShowTerminalEntries(o *os.File) {
fmt.Fprintf(o, "Available subcommands:\n")
for _, entry := range _TERMINAL_LOOKUP_TABLE {
fmt.Fprintf(o, " %s\n", entry.name)
}
fmt.Fprintf(o, "For more information, please invoke mlr {subcommand} --help.\n")
}
func showVersion(args []string) int {
fmt.Printf("mlr version %s for %s/%s/%s\n",
version.STRING, runtime.GOOS, runtime.GOARCH, runtime.Version())
return 0
}