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.
23    pub max_concurrent_calls: usize,
24}
25
26impl Default for Limits {
27    fn default() -> Self {
28        Self {
29            max_message_bytes: 4 * 1024 * 1024,
30            max_json_depth: 64,
31            tool_timeout: Duration::from_secs(30),
32            max_concurrent_calls: 64,
33        }
34    }
35}
36
37impl Limits {
38    /// The effective depth limit after clamping.
39    #[must_use]
40    pub fn effective_json_depth(&self) -> usize {
41        self.max_json_depth.min(MAX_SUPPORTED_JSON_DEPTH)
42    }
43}