After a successful initialization between the server and the client, a new session is created. The session is always sent in the request and response between the client and the server to identify which session is being used in a request/response flow. When using the HTTP transport, the lifetime of a session is a single HTTP request and response, meaning that once the response has been sent, the session is closed.
Note: sessions only apply to clients that use the protocol version 2025-11-25 or older, which start with an initialize request. Clients that use the protocol version 2026-07-28 do not send initialize and are served statelessly: every request carries its own protocol version, client information and capabilities in _meta, no Mcp-Session-Id header is created, and the events OnMCPSessionNew and OnMCPSessionEnd are not raised for them. The aSession parameter of the request events is still available for modern requests, as a transient object that only lives during the request; use aSession.Request.Era to know the era of the request. See Protocol Versions.
The session object is passed on every event and to know when a session has been created or deleted, use the following events:
OnMCPSessionNew
The event is called when a new session is created. You can access the Session parameter to get the ID of the session, when the session expires, and more.
OnMCPSessionEnd
The event is called when a session has finished.
procedure OnMCPSessionEnd(Sender: TObject; const aSession:
TsgcAI_MCP_Session);
begin
WriteLn('#session_end: ' + aSession.Id);
end;
procedure OnMCPSessionNew(Sender: TObject; const aSession:
TsgcAI_MCP_Session);
begin
WriteLn('#session_new: ' + aSession.Id);
end;