{"record":{"id":"de5cc619fd78f18b","repo":"PrefectHQ/fastmcp","slug":"max-size-must-be-positive-got-max-size","errorCode":null,"errorMessage":"max_size must be positive, got {max_size}","messagePattern":"max_size must be positive, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"fastmcp_slim/fastmcp/server/middleware/response_limiting.py","lineNumber":67,"sourceCode":"    \"\"\"\n\n    def __init__(\n        self,\n        *,\n        max_size: int = 1_000_000,\n        truncation_suffix: str = \"\\n\\n[Response truncated due to size limit]\",\n        tools: list[str] | None = None,\n    ) -> None:\n        \"\"\"Initialize response limiting middleware.\n\n        Args:\n            max_size: Maximum response size in bytes. Defaults to 1MB (1,000,000).\n            truncation_suffix: Suffix to append when truncating responses.\n                Defaults to \"\\\\n\\\\n[Response truncated due to size limit]\".\n            tools: List of tool names to apply limiting to. If None, applies to all.\n        \"\"\"\n        if max_size <= 0:\n            raise ValueError(f\"max_size must be positive, got {max_size}\")\n        self.max_size = max_size\n        self.truncation_suffix = truncation_suffix\n        self.tools = set(tools) if tools is not None else None\n\n    def _limits_tool(self, name: str) -> bool:\n        return self.tools is None or name in self.tools\n\n    def _truncate_to_result(\n        self,\n        text: str,\n        meta: dict[str, Any] | None = None,\n    ) -> ToolResult:\n        \"\"\"Truncate text to fit within max_size and wrap in ToolResult.\"\"\"\n        suffix_bytes = len(self.truncation_suffix.encode(\"utf-8\"))\n        # Account for JSON wrapper overhead: {\"content\":[{\"type\":\"text\",\"text\":\"...\"}]}\n        overhead = 50\n        target_size = self.max_size - suffix_bytes - overhead\n","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/PrefectHQ/fastmcp/blob/1f021142978e0861cd910c8df4e8074bc7cf3978/fastmcp_slim/fastmcp/server/middleware/response_limiting.py#L49-L85","documentation":"A ValueError from ResponseLimitingMiddleware.__init__ when max_size is zero or negative. The middleware truncates responses larger than max_size bytes, so a non-positive cap is invalid and construction fails fast with the offending value included in the message.","triggerScenarios":"ResponseLimitingMiddleware(max_size=0) or a negative value, typically from an unset/zero config value or a unit confusion (e.g. passing size in KB while the code expects bytes, yielding 0).","commonSituations":"MAX_RESPONSE_SIZE env var defaulting to 0; integer division like kb_to_bytes(0); mixing up max_size semantics with a 'limit disabled' sentinel of 0.","solutions":["Pass a positive max_size in bytes (default 1,000,000).","If you intended 'no limit', do not register the middleware instead of passing 0.","Validate config-derived sizes before constructing the middleware."],"exampleFix":"// before\nsize = int(os.getenv(\"MAX_RESPONSE_SIZE\", \"0\"))\nmw = ResponseLimitingMiddleware(max_size=size)  # ValueError\n\n// after\nsize = int(os.getenv(\"MAX_RESPONSE_SIZE\", \"1000000\"))\nmw = ResponseLimitingMiddleware(max_size=max(size, 1))","handlingStrategy":"validation","validationCode":"max_size = int(os.getenv(\"MAX_RESPONSE_SIZE\", \"1000000\"))\nif max_size <= 0:\n    raise ValueError(f\"MAX_RESPONSE_SIZE must be positive bytes, got {max_size}\")\nmw = ResponseLimitingMiddleware(max_size=max_size)","typeGuard":null,"tryCatchPattern":"try:\n    mw = ResponseLimitingMiddleware(max_size=max_size)\nexcept ValueError as e:\n    logging.warning(\"bad max_size, using default: %s\", e)\n    mw = ResponseLimitingMiddleware()  # 1MB default","preventionTips":["Express config sizes in bytes and validate positivity before construction.","Do not use 0 as a 'disabled' sentinel — skip registering the middleware instead.","Watch unit conversions (KB/MB → bytes) for zero-producing arithmetic.","Set sane non-zero defaults in env templates."],"tags":["configuration","validation","middleware"],"backgroundTag":"invalid-parameter-value","analyzedSha":"1f021142978e0861cd910c8df4e8074bc7cf3978","analyzedAt":"2026-08-29T14:31:16.082Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}