mirror of
https://github.com/johnkerl/miller.git
synced 2026-07-17 16:38:54 +00:00
* 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>
134 lines
5.5 KiB
Go
134 lines
5.5 KiB
Go
// Server construction for `mlr mcp`: tool, prompt, and resource registration.
|
|
|
|
package mcp
|
|
|
|
import (
|
|
"context"
|
|
_ "embed"
|
|
|
|
"github.com/johnkerl/miller/v6/pkg/version"
|
|
|
|
mcpsdk "github.com/modelcontextprotocol/go-sdk/mcp"
|
|
)
|
|
|
|
//go:embed SKILL.md
|
|
var playbookText string
|
|
|
|
const playbookPromptName = "miller-playbook"
|
|
const playbookResourceURI = "miller://playbook"
|
|
|
|
const serverInstructions = `Miller (mlr) processes CSV/TSV/JSON/etc. from the command line.
|
|
Work the loop: describe_data to learn the input's fields and values; which or
|
|
list_capabilities (index first, then one full entry) to pick a verb;
|
|
validate_dsl before running any put/filter expression; then run. On a run
|
|
error, branch on the structured error's kind/hint/did_you_mean rather than
|
|
re-guessing. Get the full playbook from the miller-playbook prompt or the
|
|
miller://playbook resource. Never invent flag or function names: everything
|
|
valid is in list_capabilities.`
|
|
|
|
func boolPtr(b bool) *bool { return &b }
|
|
|
|
// newServer assembles the MCP server: five tools, the playbook prompt, and
|
|
// the playbook resource.
|
|
func newServer(config *serverConfig) *mcpsdk.Server {
|
|
server := mcpsdk.NewServer(
|
|
&mcpsdk.Implementation{
|
|
Name: "miller",
|
|
Title: "Miller (mlr)",
|
|
Version: version.STRING,
|
|
},
|
|
&mcpsdk.ServerOptions{
|
|
Instructions: serverInstructions,
|
|
},
|
|
)
|
|
|
|
mcpsdk.AddTool(server, &mcpsdk.Tool{
|
|
Name: "list_capabilities",
|
|
Description: "List Miller's verbs, DSL functions, main flags, and DSL keywords " +
|
|
"as structured JSON (the `mlr help --as-json` catalog). " +
|
|
"With index=true, returns only {kind, name, summary} triples across the whole surface -- " +
|
|
"the cheap first call. With kind (and optionally names), returns full entries for " +
|
|
"just those items. With no arguments, returns the entire catalog (large). " +
|
|
"Results are fully cacheable against (mlr_version, catalog_schema_version).",
|
|
Annotations: &mcpsdk.ToolAnnotations{ReadOnlyHint: true, IdempotentHint: true, OpenWorldHint: boolPtr(false)},
|
|
}, listCapabilitiesHandler)
|
|
|
|
mcpsdk.AddTool(server, &mcpsdk.Tool{
|
|
Name: "which",
|
|
Description: "Route a natural-language intent (e.g. \"join two files on a key\") to ranked " +
|
|
"Miller capabilities: verbs, functions, flags, keywords. Returns {confident, results}; " +
|
|
"when confident is true the top result's name matched a query word. " +
|
|
"Use before drilling into list_capabilities.",
|
|
Annotations: &mcpsdk.ToolAnnotations{ReadOnlyHint: true, IdempotentHint: true, OpenWorldHint: boolPtr(false)},
|
|
}, whichHandler)
|
|
|
|
mcpsdk.AddTool(server, &mcpsdk.Tool{
|
|
Name: "validate_dsl",
|
|
Description: "Parse and type-check a Miller put/filter DSL expression without reading any " +
|
|
"input data (mlr put --explain). Returns {valid} on success, or {valid: false, error} " +
|
|
"with a structured error document: kind, hint, did_you_mean. " +
|
|
"Always validate before using an expression in run.",
|
|
Annotations: &mcpsdk.ToolAnnotations{ReadOnlyHint: true, IdempotentHint: true, OpenWorldHint: boolPtr(false)},
|
|
}, validateDSLHandler(config))
|
|
|
|
mcpsdk.AddTool(server, &mcpsdk.Tool{
|
|
Name: "describe_data",
|
|
Description: "Learn the shape of input data before constructing a command " +
|
|
"(mlr describe): per-field name, types seen with counts, occurrence count, null count, " +
|
|
"cardinality, min/max, and -- for low-cardinality fields -- every distinct value. " +
|
|
"Copy field names and values from here instead of guessing them.",
|
|
Annotations: &mcpsdk.ToolAnnotations{ReadOnlyHint: true, IdempotentHint: true},
|
|
}, describeDataHandler(config))
|
|
|
|
mcpsdk.AddTool(server, &mcpsdk.Tool{
|
|
Name: "run",
|
|
Description: "Run an mlr command line. Pass argv after the leading \"mlr\", one element per " +
|
|
"shell word, e.g. [\"--icsv\", \"--ojson\", \"cat\", \"data.csv\"]; optional inline input " +
|
|
"via stdin_text. Returns {exit_code, stdout, stderr} plus a parsed structured error " +
|
|
"when the command fails. Output is truncated at the server's --max-output-bytes; " +
|
|
"execution is time-limited. External-command execution (the DSL system/exec functions, " +
|
|
"piped redirects, --prepipe) is disabled unless the server was started with --allow-shell. " +
|
|
"Commands can still write files via tee/split/--from-less redirection outputs.",
|
|
Annotations: &mcpsdk.ToolAnnotations{DestructiveHint: boolPtr(true)},
|
|
}, runHandler(config))
|
|
|
|
server.AddPrompt(&mcpsdk.Prompt{
|
|
Name: playbookPromptName,
|
|
Title: "Miller agent playbook",
|
|
Description: "How to drive Miller effectively as an agent: the discover -> constrain -> validate -> run loop.",
|
|
}, playbookPromptHandler)
|
|
|
|
server.AddResource(&mcpsdk.Resource{
|
|
URI: playbookResourceURI,
|
|
Name: playbookPromptName,
|
|
Title: "Miller agent playbook",
|
|
Description: "How to drive Miller effectively as an agent: the discover -> constrain -> validate -> run loop.",
|
|
MIMEType: "text/markdown",
|
|
}, playbookResourceHandler)
|
|
|
|
return server
|
|
}
|
|
|
|
func playbookPromptHandler(_ context.Context, _ *mcpsdk.GetPromptRequest) (*mcpsdk.GetPromptResult, error) {
|
|
return &mcpsdk.GetPromptResult{
|
|
Description: "Miller agent playbook",
|
|
Messages: []*mcpsdk.PromptMessage{
|
|
{
|
|
Role: "user",
|
|
Content: &mcpsdk.TextContent{Text: playbookText},
|
|
},
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func playbookResourceHandler(_ context.Context, _ *mcpsdk.ReadResourceRequest) (*mcpsdk.ReadResourceResult, error) {
|
|
return &mcpsdk.ReadResourceResult{
|
|
Contents: []*mcpsdk.ResourceContents{
|
|
{
|
|
URI: playbookResourceURI,
|
|
MIMEType: "text/markdown",
|
|
Text: playbookText,
|
|
},
|
|
},
|
|
}, nil
|
|
}
|