{"record":{"id":"b619237b31f260cc","repo":"rohitg00/ai-engineering-from-scratch","slug":"state-secret-must-not-be-empty","errorCode":null,"errorMessage":"state secret must not be empty","messagePattern":"state secret must not be empty","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"certifications/claude/lessons/11-mcp-server-design-and-integration/code/main.py","lineNumber":75,"sourceCode":"            \"string\": lambda value: isinstance(value, str),\n        }\n        for name, value in arguments.items():\n            if name not in properties:\n                continue\n            expected = properties[name].get(\"type\")\n            if expected not in checks:\n                raise ValueError(f\"unsupported schema type for {name}: {expected!r}\")\n            if not checks[expected](value):\n                raise ValueError(f\"{name} must be {expected}\")\n        return arguments\n\n\nclass RequestStateSigner:\n    \"\"\"Produces an opaque, integrity-protected MRTR requestState value.\"\"\"\n\n    def __init__(self, secret: bytes) -> None:\n        if not secret:\n            raise ValueError(\"state secret must not be empty\")\n        self.secret = secret\n\n    def issue(self, method: str, tool: str, arguments: dict[str, Any]) -> str:\n        payload = json.dumps(\n            {\"method\": method, \"tool\": tool, \"arguments\": arguments},\n            sort_keys=True,\n            separators=(\",\", \":\"),\n        ).encode(\"utf-8\")\n        encoded = base64.urlsafe_b64encode(payload).decode(\"ascii\").rstrip(\"=\")\n        signature = hmac.new(self.secret, encoded.encode(\"ascii\"), hashlib.sha256).hexdigest()\n        return f\"{encoded}.{signature}\"\n\n    def verify(self, token: str, method: str, tool: str, arguments: dict[str, Any]) -> None:\n        if not isinstance(token, str) or \".\" not in token:\n            raise ValueError(\"requestState is malformed\")\n        encoded, supplied_signature = token.rsplit(\".\", 1)\n        expected_signature = hmac.new(\n            self.secret, encoded.encode(\"ascii\"), hashlib.sha256","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/certifications/claude/lessons/11-mcp-server-design-and-integration/code/main.py#L57-L93","documentation":"Raised in RequestStateSigner.__init__ when the signing secret for the MRTR requestState is an empty string. The signer cannot produce an integrity-protected opaque value without key material, so construction is refused immediately rather than emitting unsigned, forgeable state.","triggerScenarios":"Thrown at certifications/claude/lessons/11-mcp-server-design-and-integration/code/main.py:75 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Pass a non-empty, high-entropy secret generated per deployment (e.g. secrets.token_urlsafe(32))","Load the secret from an environment variable and fail fast at startup if it is unset or blank","Inject the secret via constructor argument in tests so no test constructs the signer with an empty key"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}