TsgcWSAPIServer_MCPEvents › OnMCPException

OnMCPException Event

Invoked when an unhandled exception reaches the server; allows overriding the HTTP response code.

Syntax

property OnMCPException: TsgcMCPServerOnExceptionEvent;
// TsgcMCPServerOnExceptionEvent = procedure(Sender: TObject; E: Exception; var aResponseCode: Integer) of object

Default Value

Remarks

Raised whenever the HTTP pipeline or the MCP engine raises an unhandled exception. E holds the original exception and aResponseCode carries the HTTP status that will be returned (default 500). The handler can downgrade the status (for example to 401 on authentication failures, 429 under rate limiting, or 404 when a resource cannot be found) before the JSON-RPC error body is serialized. The body itself always encodes the exception message and the original JSON-RPC request id.

Example

procedure TMainForm.MCPServerMCPException(Sender: TObject;
  E: Exception; var aResponseCode: Integer);
begin
  if E is ERateLimit then
    aResponseCode := 429;
  MemoLog.Lines.Add('#error ' + E.Message);
end;

Back to Events