MCP Client | Authorization

The MCP authorization specification (protocol version 2026-07-28) describes how a client gets an access token for a protected MCP server without any manual configuration. When MCPOptions.Authorization.Enabled is True and the server answers a request with HTTP 401 or 403, TsgcWSAPIClient_MCP discovers the authorization server, registers itself, opens the browser for the user login, gets the token and repeats the request with an Authorization: Bearer header. It works in both protocol eras over the HTTP transports.

This feature is different from MCPOptions.AuthenticationOptions.OAuth2, where you configure a token endpoint and a grant yourself. The authorization members are compiled only when OAuth2 support is enabled in the library (define SGC_OAUTH).

 

Flow

 

Credentials are kept per issuer, so a pre-registered client bound to one issuer is never sent to a different authorization server. Every failure is reported through OnError with a text that starts with MCP authorization:.

MCPOptions.Authorization

PropertyDescription
EnabledEnables the automatic authorization. Default False.
ClientIdClient id of a pre-registered client. Optional.
ClientSecretClient secret of a pre-registered confidential client. Optional.
IssuerPreferred authorization server when the resource lists several. Optional.
ClientMetadataURLhttps URL of the client id metadata document of your application. Optional.
AllowDynamicRegistrationAllows Dynamic Client Registration when no other registration applies. Default True.
ClientNameClient name sent with Dynamic Client Registration.
RedirectURLRedirect URL where the local listener waits for the authorization code. Default http://127.0.0.1:33418/callback.
TimeoutSeconds to wait for the user login. Default 300.
AccessTokenRead only. Bearer token currently in use, empty until authorized.

 

Events

 

TsgcAI_MCP_Client_AuthorizationCredentials has the properties Issuer (read only), ClientId, ClientSecret, AccessToken, RefreshToken, ExpiresAt, Scope and Registration (preregistered, cimd or dcr).

Sample code

procedure TForm1.FormCreate(Sender: TObject);
begin
  MCP.MCPOptions.HttpOptions.URL := 'https://mcp.example.com/mcp';
  MCP.MCPOptions.Authorization.Enabled := True;
  MCP.MCPOptions.Authorization.ClientName := 'My MCP Client';
  MCP.MCPOptions.Authorization.RedirectURL := 'http://127.0.0.1:33418/callback';
  MCP.OnMCPAuthorizationURL := MCPAuthorizationURL;
  MCP.OnMCPAuthorizationCredentials := MCPAuthorizationCredentials;
  MCP.Initialize;
end;

procedure TForm1.MCPAuthorizationURL(Sender: TObject; const aURL: string;
  var Handled: Boolean);
begin
  Log('login: ' + aURL);
  Handled := False; // open the default browser
end;

procedure TForm1.MCPAuthorizationCredentials(Sender: TObject;
  const aIssuer: string;
  const aCredentials: TsgcAI_MCP_Client_AuthorizationCredentials;
  aSave: Boolean);
begin
  if aSave then
    FStore.Values[aIssuer] := aCredentials.RefreshToken
  else
    aCredentials.RefreshToken := FStore.Values[aIssuer];
end;