fix(mcpserver): emit one TextContent block when a tool returns an empty list (issue #3305) - #3319
Open
gingeekrishna wants to merge 2 commits into
Conversation
…ty list A tool annotated to return list[T] that actually returns [] was converted to zero content blocks by _convert_to_content, because the chain.from_iterable() flattening of an empty iterable is []. For a client reading unstructured content 'no matches' was indistinguishable from 'the call produced nothing', causing LLMs to retry the same lookup indefinitely. The fix: when the per-item conversion of a list/tuple produces no content blocks, fall back to a single TextContent whose text is the JSON-serialised empty collection ([] for both list and tuple). All non-empty paths are byte-identical to the previous behaviour. structuredContent is populated correctly in all cases and is unaffected. Closes modelcontextprotocol#3305 Signed-off-by: Radha Krishnan P <gingeekrishna@gmail.com>
There was a problem hiding this comment.
All reported issues were addressed across 2 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
listortuple,_convert_to_contentpreviously produced zero content blocks, making "no results" indistinguishable from "call produced nothing" for clients reading unstructured content.TextContentwhose text is the JSON-serialised empty collection ([]for bothlistandtuple).structuredContentis unaffected in all cases.Root cause
_convert_to_content(src/mcp/server/mcpserver/utilities/func_metadata.py:563) flattens a list by chaining the conversion of each item:With zero items there is nothing to concatenate, so the result is
[]. The same "empty" answer then behaves differently depending on return type:[]''[{...}, {...}]""''Fix
Tests added
Four new tests in
tests/server/mcpserver/test_func_metadata.py:test_empty_list_produces_one_text_content_block— core fix forlisttest_empty_tuple_produces_one_text_content_block— same guarantee fortupletest_non_empty_list_content_blocks_unchanged— non-empty path is byte-identicaltest_empty_list_structured_content_unaffected—structuredContentstill correctAll 46 tests in the file pass.
Test plan
uv run --frozen pytest tests/server/mcpserver/test_func_metadata.py— 46/46 pass[]now delivers one content block with text"[]"to the modelCloses #3305