Skip to content

Issue Report: Infinite Loop in MCP Client When Handling Empty nextCursor During Tool Listing #628

@Moyuyanli

Description

@Moyuyanli

1. Title

Infinite Loop in MCP Client When Processing Empty nextCursor During listTools() Call

2. Description

The MCP Java SDK client enters an infinite loop when processing the /tools response from Alibaba Cloud DashScope MCP applications. The client incorrectly interprets an empty nextCursor value ("") as a signal to continue pagination, resulting in repeated requests that eventually time out (10s default timeout).

Affected SDK Version: io.modelcontextprotocol.client:client-java:0.14.1 (and likely all prior versions)

3. Steps to Reproduce

  1. Configure MCP client to connect to Alibaba Cloud DashScope MCP application with market-cmapi00035184 (Global IP Geolocation)
  2. Call client.listTools()
  3. Observe repeated HTTP requests to /tools endpoint
java
编辑
McpClientConfig config = McpClientConfig.builder()
    .addHeader("Authorization", "Bearer YOUR_API_KEY")
    .build();

McpSyncClient client = McpClient.connect(
"https://dashscope.aliyuncs.com/api/v1/mcps/market-cmapi00035184/mcp",
config
);

List<Tool> tools = client.listTools(); // Triggers infinite loop

4. Expected Behavior

The client should:

  1. Receive a single /tools response
  2. Detect that nextCursor is empty ("")
  3. Exit pagination loop immediately
  4. Return the tool list without timeout

5. Actual Behavior

The client:

  1. Receives valid tool list response with nextCursor: ""
  2. Incorrectly interprets empty string as needing pagination
  3. Sends repeated /tools requests (e.g., 2e9e1828-904, 2e9e1828-905, 2e9e1828-906)
  4. Eventually fails with TimeoutException after 10s

Error Log:

log
编辑
[DEBUG] Sending message for method tools/list (ID: 2e9e1828-904)
[DEBUG] Received JSON message: {"jsonrpc":"2.0","id":"2e9e1828-904","result":{"nextCursor":"","tools":[...]}}
[DEBUG] Sending message for method tools/list (ID: 2e9e1828-905)
[DEBUG] Received JSON message: {"jsonrpc":"2.0","id":"2e9e1828-905","result":{"nextCursor":"","tools":[...]}}
[DEBUG] Sending message for method tools/list (ID: 2e9e1828-906)
...
[ERROR] TimeoutException: Did not observe any item within 10000ms

6. Key Evidence

Test Case Application ID HTTP Response SDK Behavior
Working market-cmapi00035184 (IP Geolocation) {"nextCursor":"","tools":[...]} Infinite loop → Timeout
Working market-cmapi00035184 (IP Geolocation) curl -H "Authorization: Bearer KEY" https://dashscope.aliyuncs.com/api/v1/mcps/market-cmapi00035184/mcp/tools Returns valid JSON (no loop)

12. Resolution Plan

  1. Patch: Implement the fix in McpClientSession.java (as shown in Section 8)
  2. Test: Add unit tests for empty string handling (see Test Plan)
  3. Release: New SDK version 1.0.1
  4. Documentation: Update README with pagination best practices

Note: The fix does not affect applications that return nextCursor: null


Reported By: moyuyanli
Date: October 20, 2025
SDK Version: io.modelcontextprotocol.client:client-java:0.14.1
Platform: Java 11+
Environment: Alibaba Cloud DashScope MCP (v1.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions