You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Keep conversation history and handle stop_reason in the clients - #281
All five clients now keep the message list for the whole session, tool_use and tool_result pairs included, so a follow-up like "and the alerts for that state?" has the earlier turn to work from. A new clear command starts a fresh conversation.
Every request sends the same short system prompt, and the loop checks stop_reason after each call:
max_tokens: print the partial text and a notice.
refusal: print a notice and drop the query from history, so a follow-up doesn't send it again.
A reply that was cut off, refused or stopped at the tool-turn limit is not added to history, so the next request stays valid. The loop now calls the API in one place instead of two.
The tool-loop test now types seven queries plus clear. It checks the system prompt and the history on every request, and adds a refusal, a max_tokens cut-off and a query after clear.
How Has This Been Tested?
Locally
Breaking Changes
N/a
Types of changes
Bug fix (non-breaking change which fixes an issue)
New feature (non-breaking change which adds functionality)
Breaking change (fix or feature that would cause existing functionality to change)
Keep conversation history and handle stop_reason in the clients
All five clients now keep the message list for the whole session,
tool_use and tool_result pairs included, so a follow-up like "and the
alerts for that state?" has the earlier turn to work from. A new
`clear` command starts a fresh conversation.
Every request sends the same short system prompt, and the loop checks
stop_reason after each call:
- max_tokens: print the partial text and a notice.
- refusal: print a notice and drop the query from history, so a
follow-up doesn't send it again.
A reply that was cut off, refused or stopped at the tool-turn limit is
not added to history, so the next request stays valid. The loop now
calls the API in one place instead of two.
The tool-loop test now types seven queries plus `clear`. It checks the
system prompt and the history on every request, and adds a refusal, a
max_tokens cut-off and a query after `clear`.
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Blank input can leave invalid messages in Python and TypeScript chat histories, breaking subsequent queries, while the cross-query test misses discarded tool-use/result pairs.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Across all five clients, API failures leave rejected queries in history and empty completed responses are saved, causing subsequent requests to fail.
No open issues 🎉
2 previously missed issues
In code that hasn't changed since last review
Roll back failed queries from session history
mcp-client-python/client.py:120
An API error leaves the current query in session history, so a query rejected for exceeding the input limit is resent with every short follow-up and keeps failing. Restore the saved history length before propagating the error, preserving earlier conversation turns. Apply the same rollback in mcp-client-typescript/index.ts:148, mcp-client-go/main.go:258, mcp-client-rust/src/main.rs:262, and mcp-client-ruby/client.rb:130.
Skip empty completed responses in conversation history
mcp-client-python/client.py:134
An end_turn response can have an empty content list; saving it makes the next query replay an empty non-final assistant message, which the Messages API rejects. Skip appending empty completed responses while retaining the earlier tool-use/result pairs. Apply the same guard in mcp-client-typescript/index.ts:172, mcp-client-go/main.go:280, mcp-client-rust/src/main.rs:287, and mcp-client-ruby/client.rb:152.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The TypeScript client does not trim input before checking clear, so leading or trailing spaces send it to the model instead of resetting conversation history, unlike the other clients.
No open issues 🎉
1 previously missed issue
In code that hasn't changed since last review
Trim input before checking the clear command
mcp-client-typescript/index.ts:264
A trailing or leading space makes clear go to the model as a query instead of resetting the conversation. The other four clients trim input before checking commands (for example, mcp-client-python/client.py:189 and mcp-client-go/main.go:357), so the same input clears history there but retains it here. Trim the input for this comparison to keep the command's behavior consistent across clients.
The reason will be displayed to describe this comment to others. Learn more.
Read through all five clients and the test. The loop is the same everywhere: the query goes into session history, each response is checked for refusal, max_tokens, a final answer and the tool-turn cap, and only complete replies and tool rounds are kept. A refusal or an API error rolls history back to where the query started, so a follow-up never resends a bad request. The new scenarios cover each path, and the history validator on every request is a good guard. CI is green with the new lint step. Merging.
Two small notes, neither blocking:
The test cannot confirm that a max_tokens reply is left out of history, because clear comes right after it. I checked the code path in each client and none of them appends it, so this is a coverage gap only.
The description says seven queries plus clear; the test now types nine. Worth a one-line edit for anyone reading the history later.
Since this changes code the build-client tutorial quotes, a matching docs PR per the new AGENTS.md would be good to link when it exists.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
Closes #274 and #275
All five clients now keep the message list for the whole session, tool_use and tool_result pairs included, so a follow-up like "and the alerts for that state?" has the earlier turn to work from. A new
clearcommand starts a fresh conversation.Every request sends the same short system prompt, and the loop checks stop_reason after each call:
A reply that was cut off, refused or stopped at the tool-turn limit is not added to history, so the next request stays valid. The loop now calls the API in one place instead of two.
The tool-loop test now types seven queries plus
clear. It checks the system prompt and the history on every request, and adds a refusal, a max_tokens cut-off and a query afterclear.How Has This Been Tested?
Locally
Breaking Changes
N/a
Types of changes
Checklist
Additional context
Assisted by Claude.