feat(mcp): add $history variable and structured response format#17132
feat(mcp): add $history variable and structured response format#17132ayax79 merged 15 commits intonushell:mainfrom
Conversation
- Add `$history` variable that stores all previous command outputs - Access by index: `$history.0`, `$history.1`, etc. - Wrap all responses in structured record with: - `history_index`: 0-based index of result in history - `cwd`: current working directory after command - `output`: command output (possibly truncated) - Truncate large outputs (>10k chars by default) and reference history - Configurable threshold via `NU_MCP_OUTPUT_TRUNCATE` env var - Remove moka caching (incompatible with history tracking) - Update instructions.md with history documentation 🤖 Generated with [Claude Code](https://claude.com/claude-code)
- Rename NU_MCP_OUTPUT_TRUNCATE to NU_MCP_OUTPUT_LIMIT - Read from $env.NU_MCP_OUTPUT_LIMIT first (nushell native) - Fall back to process env var if not set in nushell - Add test for nushell env var configuration
7ffbdbb to
d3bf1e3
Compare
…ions - Remove process env var fallback for NU_MCP_OUTPUT_LIMIT - Only use nushell's $env.NU_MCP_OUTPUT_LIMIT (typed int) - Convert output_limit and register_history_variable to free-standing functions - Use inline paths instead of grouped imports (per Rust conventions) - Update instructions.md to clarify $history is list<any> and limit is bytes 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Include unit (bytes) in the env var name for clarity. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- NU_MCP_OUTPUT_LIMIT now accepts filesize (e.g., 10kb) - No default fallback - truncation only happens when explicitly configured - Returns Option<usize> instead of silently defaulting
966252f to
fb1d8ce
Compare
Response format now has distinct fields:
- Normal: {cwd, history_index, output}
- Truncated: {cwd, history_index, note}
🤖 Generated with [Claude Code](https://claude.com/claude-code)
|
I'm not sure how I feel about this history functionality taking infinite memory. Yes, we can truncate each at 10k or whatever is set but a bazillion 10k's will still be too much memory. There should be a way to reclaim unused or old items. |
Previously truncation only occurred when NU_MCP_OUTPUT_LIMIT was explicitly set. Now defaults to 10kb, with 0 to disable truncation. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
it is just a variable so the model could technically modify it itself I think? have any specific ideas? we could alternateively also have |
|
I'd vote for a ring buffer for X items of Y count/bytes/filesize but $history would probably also need a timedate stamp to know which item is the latest. |
Addresses reviewer feedback about unbounded memory growth: - History is now stored as a ring buffer (VecDeque) with configurable limit - Default limit is 100 entries, configurable via NU_MCP_HISTORY_LIMIT env var - Each history entry is now a record with `timestamp` and `value` fields - Oldest entries are evicted when the limit is reached Breaking change: History entries are now accessed via $history.N.value instead of $history.N directly. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
- Move timestamp from history entries to the response record - History now stores just values directly ($history.0 instead of $history.0.value) - Response includes: cwd, history_index, timestamp, output/note - Ring buffer behavior unchanged (default 100 entries, configurable) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
- Replace inline paths with proper use statements at file top - Extract history-related code into dedicated history.rs module - History struct now encapsulates ring buffer and variable registration - Cleaner separation of concerns between evaluation and history management 🤖 Generated with [Claude Code](https://claude.com/claude-code)
|
@fdncred All review threads addressed and resolved. Let me know if there's anything else needed for approval! |
|
@andrewgazelka I am doing a bit of testing. If all goes well, I'll merge tomorrow. |
|
working well so far, thanks! |
$historyvariable that stores all previous command outputs$history.0,$history.1, etc.history_index: 0-based index of result in historycwd: current working directory after commandoutput: command output (possibly truncated)NU_MCP_OUTPUT_TRUNCATEenv var🤖 Generated with Claude Code