Skip to content

fix(mcp): handle dict response in list_chats when /chats API returns paginated envelope - #16639

Merged
wangq8 merged 3 commits into
infiniflow:mainfrom
BAIGUANGMEI:fix/mcp-list-chats-dict-response
Jul 6, 2026
Merged

wangq8 merged 3 commits into
infiniflow:mainfrom
BAIGUANGMEI:fix/mcp-list-chats-dict-response

Conversation

@BAIGUANGMEI

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Resolves #16638

The MCP server's list_chats() method in mcp/server/server.py assumes the /api/v1/chats REST API returns data as a list. However, the /chats endpoint returns a paginated dict envelope:

{"code": 0, "data": {"chats": [...], "total": 1}}

When iterating a dict, Python yields its keys (strings), causing 'str' object has no attribute 'get' to be raised on data.get("id"). Since list_chats() is called inside list_tools(), this exception crashes the entire tools/list handler, making all three MCP tools (ragflow_retrieval, ragflow_list_datasets, ragflow_list_chats) unavailable to every MCP client.

What is changed?

Added a type check before iterating: if data is a dict, extract the chats key; otherwise use it directly as a list. This handles both the current dict response shape and any future list response.

# Before (broken):
chat_count = len(res_json.get("data", []))
for data in res_json.get("data", []):

# After (fixed):
_chats_data = res_json.get("data", [])
if isinstance(_chats_data, dict):
    _chats_data = _chats_data.get("chats", [])
chat_count = len(_chats_data)
for data in _chats_data:

How was it tested?

  1. RAGFlow v0.26.3 Docker (CPU image) with MCP server in host mode.
  2. Before fix: tools/list returns error 'str' object has no attribute 'get'.
  3. After fix: tools/list successfully returns all 3 tools (ragflow_retrieval, ragflow_list_datasets, ragflow_list_chats) with correct dataset and chat descriptions.
  4. Verified with multiple chat assistants configured, and with zero chat assistants.

Additional context

…paginated envelope

The /api/v1/chats endpoint returns a paginated envelope:
  {data: {chats: [...], total: N}}
But list_chats() assumed data was always a list, causing:
  'str' object has no attribute 'get'
when iterating dict keys. This crashed the entire tools/list handler,
making all MCP tools (ragflow_retrieval, ragflow_list_datasets,
ragflow_list_chats) unavailable.

Fix: check if data is a dict and extract the 'chats' key.
@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Jul 4, 2026
@coderabbitai

coderabbitai Bot commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 902e3384-01e4-4cd5-b6d8-4773d6433d69

📥 Commits

Reviewing files that changed from the base of the PR and between 0fcfb38 and 59f3da6.

📒 Files selected for processing (1)
  • mcp/server/server.py

📝 Walkthrough

Walkthrough

RAGFlowConnector.list_chats now handles the /chats API response's data field being either a list or a dict containing a chats key, normalizing it before computing chat counts and building the result list.

Changes

Response normalization fix

Layer / File(s) Summary
Normalize chats payload handling
mcp/server/server.py
list_chats reads res_json["data"], extracts data["chats"] when data is a dict, computes and logs the chat count, then builds results from the normalized list.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Possibly related issues

Poem

A rabbit hopped through data's maze,
Found lists and dicts in messy ways,
Now chats are counted, safe and sound,
No crash lurks where JSON's found,
Hop hop hooray for tidy code! 🐇✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the MCP fix and matches the main code change.
Description check ✅ Passed The description is detailed and covers problem, fix, testing, and context, matching the template's intent.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@wangq8 wangq8 added the ci Continue Integration label Jul 6, 2026
@codecov

codecov Bot commented Jul 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.16%. Comparing base (3a247db) to head (733a2d7).
⚠️ Report is 9 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #16639   +/-   ##
=======================================
  Coverage   93.16%   93.16%           
=======================================
  Files          10       10           
  Lines         717      717           
  Branches      118      118           
=======================================
  Hits          668      668           
  Misses         29       29           
  Partials       20       20           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: MCP server crashes on tools/list - list_chats() assumes /chats API returns a list but gets a dict

3 participants