Back to Nushell

Evaluate Tool

crates/nu-mcp/src/evaluate_tool.md

0.112.24.8 KB
Original Source

Execute a command in the nushell. Successful evaluations return a structured NUON record string with metadata fields including cwd, history_index, timestamp, and either output or note. When evaluation fails, the MCP tool response is marked as an error and the content contains a structured NUON error record.

Avoid commands that produce a large amount of output, and consider piping those outputs to files. If you need to run a long lived command, background it - e.g. job spawn { uvicorn main:app } so that this tool does not run indefinitely.

Command Equivalents to Bash:

Bash CommandNushell CommandDescription
mkdir -p <path>mkdir <path>Creates the given path, creating parents as necessary
> <path>o> <path>Save command output to a file
>> <path>o>> <path>Append command output to a file
> /dev/nullignoreDiscard command output
> /dev/null 2>&1o+e>| ignoreDiscard command output, including stderr
command 2>&1command o+e>| ...Redirect stderr to stdout (use o+e> or out+err>)
cmd1 | tee log.txt | cmd2cmd1 | tee { save log.txt } | cmd2Tee command output to a log file
command | head -5command | first 5Limit the output to the first 5 rows of an internal command (see also last and skip)
cat <path>open --raw <path>Display the contents of the given file
cat <(<command1>) <(<command2>)[(command1), (command2)] | str joinConcatenate the outputs of command1 and command2
cat <path> <(<command>)[(open --raw <path>), (command)] | str joinConcatenate the contents of the given file and output of command
for f in *.md; do echo $f; donels *.md | each { $in.name }Iterate over a list and return results
for i in $(seq 1 10); do echo $i; donefor i in 1..10 { print $i }Iterate over a list and run a command on results
cp <source> <dest>cp <source> <dest>Copy file to new location
rm -rf <path>rm -r <path>Recursively removes the given path
date -d <date>"<date>" | into datetime -f <format>Parse a date (format documentation)
sedstr replaceFind and replace a pattern in a string
grep <pattern>where $it =~ <substring> or find <substring>Filter strings that contain the substring
command1 && command2command1; command2Run a command, and if it's successful run a second
stat $(which git)stat ...(which git).pathUse command output as argument for other command
echo /tmp/$RANDOM$"/tmp/(random int)"Use command output in a string
cargo b --jobs=$(nproc)cargo b $"--jobs=(sys cpu | length)"Use command output in an option
echo $PATH$env.PATH (Non-Windows) or $env.Path (Windows)See the current path
echo $?$env.LAST_EXIT_CODESee the exit status of the last executed command
export$envList the current environment variables
FOO=BAR ./binFOO=BAR ./binUpdate environment for a command
echo $FOO$env.FOOUse environment variables
echo ${FOO:-fallback}$env.FOO? | default "ABC"Use a fallback in place of an unset variable
type FOOwhich FOODisplay information about a command (builtin, alias, or executable)
\( <command> )A command can span multiple lines when wrapped with ( and )

If the polars commands are available, prefer it for working with parquet, jsonl, ndjson, csv files, and avro files. It is much more efficient than the other Nushell commands or other non-nushell commands. It exposes much of the functionality of the polars dataframe library. Start the pipeline with plugin use polars

An example of converting a nushell table output to a polars dataframe:

nu
ps | polars into-df | polars collect

An example of converting a polars dataframe back to a nushell table in order to run other nushell commands:

nu
polars open file.parquet | polars into-nu

An example of opening a parquet file, selecting columns, and saving to a new parquet file:

nu
polars open file.parquet | polars select name status | polars save file2

Important: The glob command should be used exclusively when you need to locate a file or a code reference, other solutions may produce too large output because of hidden files! For example do not use find or ls -r. Use command_help tool to learn more about the glob command.

Important: Variables and environment changes persist across tool calls (REPL-style). You can set a variable in one call and access it in subsequent calls:

  • let x = 42 in one call, then $x in the next call returns 42
  • $env.MY_VAR = "hello" persists for later calls

However, external processes run in their own environment. Use absolute paths when possible.