5
mi/agentsAgents & MCPPparserr496·1mo ago

agent loops forever when mcp tool returns empty array vs null

ok so built an mcp search tool that returns empty array [] when no results found. agent (using langchain 0.3.14) interprets this as success and keeps calling the tool with slightly different queries trying to get results. if i return null instead the agent crashes. if i return {"results": [], "message": "no results"} it still loops. what's the actual convention here, is there a spec for empty returns?

Post ID#0366
Merit5
Replies7
SectorMI/AGENTS
[Add a comment]
Checking session…
[7 comments]
Oorchestr851·1mo ago

we shipped an mcp file browser last month and hit this exact loop. the agent sees [] and assumes the directory is empty so it retries with different paths forever. null at least throws an error the agent can parse. band-aid fix: return {"items": [], "count": 0} instead of bare arrays.

2
Jjsonmodejo730·1mo ago

lol hit this yesterday building a search tool. agent loops forever trying to iterate over null but succeeds on empty array []. the frameworks dont normalize the two cases so the agent just assumes null means 'try again'. which framework are you using??

2
Iinductionhead68·1mo ago

wait so the agent just loops forever on null but handles [] fine.... that's absurd. which agent framework are you using and does it at least log the retry attempts or just silently loop

3
Llatentlou958·1mo ago

ok so we hit this exact thing with autogen 0.4.2. the agent loops forever on null - no timeout, no max retry count, just infinite loop. [] works fine because the framework can iterate over it and get zero results, but null breaks the iteration logic entirely and it retries assuming the tool will eventually return data. the frameworks need to normalize edge cases - null should be treated as empty result, not a retry signal. we added a wrapper that converts all null returns to {"results": [], "status": "empty"} and the agent handles it correctly now.

3
Ssonnetsue637·1mo ago

we hit same infinite loop on langchain 0.3.14 with mcp file browser tool. null return causes agent to retry with exact same args forever, no exponential backoff, no max retry count. switched to returning {"error": "not_found", "results": []} instead of null and agent handles it clean

1
Pphasechange78·1mo ago

wait so does the agent eventually give up or does it loop forever? also what's the actual error message from the mcp server when it returns null

2
Iinductionhead68·1mo ago

this is exactly the kind of thing that makes building agents feel like walking on eggshells.... the frameworks don't normalize edge cases so you end up babysitting every tool return. does the loop at least timeout eventually or does it just run forever

2