Skip to main content

flymcprust_core/
limits.rs

1//! Resource limits with secure defaults.
2
3use std::time::Duration;
4
5/// Hard upper bound for [`Limits::max_json_depth`]: `serde_json` refuses to
6/// nest deeper than this regardless of configuration.
7pub const MAX_SUPPORTED_JSON_DEPTH: usize = 128;
8
9/// Resource limits applied to every request.
10///
11/// The defaults are deliberately conservative. Every value can be changed
12/// through the [`ServerBuilder`](crate::ServerBuilder).
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct Limits {
15    /// Maximum size of one JSON-RPC message in bytes (HTTP body or stdio line).
16    pub max_message_bytes: usize,
17    /// Maximum nesting depth of a JSON-RPC message (objects and arrays).
18    /// Values above [`MAX_SUPPORTED_JSON_DEPTH`] are clamped.
19    pub max_json_depth: usize,
20    /// Maximum time a single tool call may run.
21    pub tool_timeout: Duration,
22    /// Maximum number of tool calls executing at the same time. Calls
23    /// waiting on other services hold a slot while they wait, so the default
24    /// leaves room for them; memory is bounded by the transports (see
25    /// `HttpConfig::max_body_bytes_in_flight`).
26    pub max_concurrent_calls: usize,
27}
28
29impl Default for Limits {
30    fn default() -> Self {
31        Self {
32            max_message_bytes: 4 * 1024 * 1024,
33            max_json_depth: 64,
34            tool_timeout: Duration::from_secs(30),
35            max_concurrent_calls: 1024,
36        }
37    }
38}
39
40impl Limits {
41    /// The effective depth limit after clamping.
42    #[must_use]
43    pub fn effective_json_depth(&self) -> usize {
44        self.max_json_depth.min(MAX_SUPPORTED_JSON_DEPTH)
45    }
46}