Limit MCP catalog pagination (#36039)

## Why

MCP catalog discovery must not allow a server to keep pagination running or
growing without bounds.

## What changed

- Apply the shared pagination collector to tool, resource, and resource-template
  discovery.
- Limit each catalog to 100 pages and 1,024 items, and reject pagination cursors
  larger than 64 KiB or any repeated cursor.
- Bound the entire pagination operation by the configured tool timeout, falling
  back to 30 seconds when no timeout is configured.

## Testing

Add unit coverage for every limit and an end-to-end MCP test that preserves
valid multi-page tools while excluding a server that returns an oversized
cursor.

GitOrigin-RevId: 961a1bfa5fe8a265daf56e342a919c980cd720fa
This commit is contained in:
jif authored and copyberry committed 2026-07-29 22:37:09 +00:00
1 parent 3834c47ccb
commit 3e3ae08839
7 files changed
+315 -91

No files matched your search

@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use anyhow::Context;
@@ -15,6 +16,7 @@ use tokio::task::JoinSet;
use tracing::warn;
use super::McpConnectionSet;
use crate::pagination::collect_paginated;
use crate::rmcp_client::ManagedClient;
impl McpConnectionSet {
@@ -36,28 +38,15 @@ impl McpConnectionSet {
let timeout = view.tool_timeout;
let client = managed_client.client;
join_set.spawn(async move {
let mut resources = Vec::new();
let mut cursor: Option<String> = None;
loop {
let params = cursor.as_ref().map(|next| {
PaginatedRequestParams::default().with_cursor(Some(next.clone()))
});
let response = match client.list_resources(params, timeout).await {
Ok(result) => result,
Err(error) => return (server_name, Err(error)),
};
resources.extend(response.resources);
match response.next_cursor {
Some(next) if cursor.as_ref() == Some(&next) => {
return (
server_name,
Err(anyhow!("resources/list returned duplicate cursor")),
);
}
Some(next) => cursor = Some(next),
None => return (server_name, Ok(resources)),
let resources = collect_paginated("resources/list", timeout, |params| {
let client = Arc::clone(&client);
async move {
let response = client.list_resources(params, timeout).await?;
Ok((response.resources, response.next_cursor))
}
}
})
.await;
(server_name, resources)
});
}
@@ -96,30 +85,15 @@ impl McpConnectionSet {
let timeout = view.tool_timeout;
let client = managed_client.client;
join_set.spawn(async move {
let mut templates = Vec::new();
let mut cursor: Option<String> = None;
loop {
let params = cursor.as_ref().map(|next| {
PaginatedRequestParams::default().with_cursor(Some(next.clone()))
});
let response = match client.list_resource_templates(params, timeout).await {
Ok(result) => result,
Err(error) => return (server_name, Err(error)),
};
templates.extend(response.resource_templates);
match response.next_cursor {
Some(next) if cursor.as_ref() == Some(&next) => {
return (
server_name,
Err(anyhow!(
"resources/templates/list returned duplicate cursor"
)),
);
}
Some(next) => cursor = Some(next),
None => return (server_name, Ok(templates)),
let templates = collect_paginated("resources/templates/list", timeout, |params| {
let client = Arc::clone(&client);
async move {
let response = client.list_resource_templates(params, timeout).await?;
Ok((response.resource_templates, response.next_cursor))
}
}
})
.await;
(server_name, templates)
});
}