Skip to content

Demos

The Demos folder contains ready-to-run projects, each focused on a specific MARS feature. Open the project group, build, and run the server (most also include a client and a test project). Below is what each one teaches, with a representative snippet.

The MARSTemplate project is also the starting point produced by the MARSCmd bootstrapper.

MARSTemplate

A complete, minimal application scaffold: a helloworld resource, a JWT token resource, an OpenAPI/Swagger endpoint, and host projects for every deployment target (console, VCL, FMX, Windows service, ISAPI, Apache module, FastCGI, Linux daemon). The recommended starting point — see Your First Server.

pascal
[Path('helloworld')]
THelloWorldResource = class
  [GET, Produces(TMediaType.TEXT_PLAIN)]
  function SayHelloWorld: string;
end;

ErrorObjects

How to return errors at three levels of richness: a plain Delphi exception (→ 500), a MARS HTTP exception with a custom status/message, and a MARS exception carrying a structured JSON body — plus how the client reads that body back. See Error Handling.

pascal
raise EMARSWithResponseException.Create('Error Message!',
  TValue.From<TErrorDetails>(LErrorDetails), 530, 'The reason of the error');

SSEDemo

Server-Sent Events: a resource that pushes a heartbeat event every second over a persistent connection, plus a static HTML page that consumes it with the browser EventSource. See Server-Sent Events.

pascal
[GET, Produces(TMediaType.TEXT_EVENT_STREAM)]
function SayHelloWorld: TMARSServerSideEvent;

MCPServer

An MCP (Model Context Protocol) server for AI agents: derive a resource from TMCPResource, mark methods with [MCPTool] and Claude, Claude Code or any MCP client can discover and call them over Streamable HTTP — tool list and JSON Schema are generated automatically via RTTI. See MCP Servers.

pascal
[MCPTool('add_numbers', 'Adds two numbers and returns a structured result')]
function AddNumbers(
  [MCPParam('a', 'First operand')] const A: Double;
  [MCPParam('b', 'Second operand')] const B: Double): TCalculationResult;

TokenRenew

JWT lifecycle management: checking remaining validity and automatically re-issuing the token when it drops below half its duration. See Authentication ▸ Token renewal.

pascal
if LRemainingSecs < (Token.DurationSecs / 2) then
  Token.Build(JWTSecret);   // sliding-expiration renewal

OTPDemo

A full two-factor-authentication example: time-based one-time passwords (TOTP, RFC 6238) compatible with Microsoft/Google Authenticator, QR-code provisioning, and FireDAC-backed user storage. Includes both server and client.

pascal
[GET, Path('/verify/{username}/{otp}')]
function Verify([PathParam] AUserName, AOTP: string): TVerifyOTPResponse;
// Result.verified := TOTP.VerifyTotp(LUser.OTP_Secret, AOTP);

WebStencilsDemo

Server-side HTML rendering with Embarcadero's WebStencils engine, binding live FireDAC datasets into templates. See HTML & Templates.

pascal
FWS.AddVarValue('datasetName', LDatasetName);
FWS.AddDataVar('dataset', LMemTable, True);
Result := FWS.ContentFromFile('dataset.html');

HtmxDemo

A hypermedia front-end with htmx: the server reads its own OpenAPI document and returns the endpoint list, which the page renders client-side without a SPA framework. See HTML & Templates ▸ htmx.

pascal
function THelloworldResource.RetrieveData([Context] AOpenAPI: TOpenAPI): TDataResponse;
begin
  for var LPath in AOpenAPI.paths do
    Result.endpoints := Result.endpoints + [TEndpoint.Create(LPath.Key, LPath.Value.Methods)];
end;

MARS and Embarcadero KAI

There is a video walkthrough of MARS with Embarcadero KAI: YouTube — MARS and KAI.

Released under the Apache License 2.0.