Skip to content

Commit db9781d

Browse files
authored
Merge pull request #459 from danielaskdd/yangdx
Fix webpage content pagination logic error
2 parents 91c630a + ce72760 commit db9781d

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/fetch/src/mcp_server_fetch/server.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,21 @@ async def call_tool(name, arguments: dict) -> list[TextContent]:
233233
content, prefix = await fetch_url(
234234
url, user_agent_autonomous, force_raw=args.raw
235235
)
236-
if len(content) > args.max_length:
237-
content = content[args.start_index : args.start_index + args.max_length]
238-
content += f"\n\n<error>Content truncated. Call the fetch tool with a start_index of {args.start_index + args.max_length} to get more content.</error>"
236+
original_length = len(content)
237+
if args.start_index >= original_length:
238+
content = "<error>No more content available.</error>"
239+
else:
240+
truncated_content = content[args.start_index : args.start_index + args.max_length]
241+
if not truncated_content:
242+
content = "<error>No more content available.</error>"
243+
else:
244+
content = truncated_content
245+
actual_content_length = len(truncated_content)
246+
remaining_content = original_length - (args.start_index + actual_content_length)
247+
# Only add the prompt to continue fetching if there is still remaining content
248+
if actual_content_length == args.max_length and remaining_content > 0:
249+
next_start = args.start_index + actual_content_length
250+
content += f"\n\n<error>Content truncated. Call the fetch tool with a start_index of {next_start} to get more content.</error>"
239251
return [TextContent(type="text", text=f"{prefix}Contents of {url}:\n{content}")]
240252

241253
@server.get_prompt()

0 commit comments

Comments
 (0)