Delphi MCP Components — Model Context Protocol Client and Server
Connect Delphi applications to the AI ecosystem through the Model Context Protocol: TsgcWSAPIServer_MCP exposes your application's tools, prompts and resources to AI models, and TsgcWSAPIClient_MCP consumes any MCP-compatible server. The same library ships AI API clients for OpenAI, Anthropic Claude and Google Gemini.
MCP is the emerging standard for connecting large language models to external tools and data. Instead of one custom integration per AI provider, you implement one protocol.
Model Context Protocol in Delphi means your existing VCL or FMX application can become an AI-accessible service. The protocol is JSON-RPC based and defines three primitives: tools (callable functions with typed parameters), prompts (reusable templates with arguments) and resources (data exposed through URI addressing). An MCP server publishes them; an MCP client (Claude Desktop, Cursor, or your own Delphi application) discovers and calls them.
The server component attaches to an sgcWebSockets HTTP server and supports both stdio and HTTP transports, so the same code serves a desktop AI assistant launching it as a subprocess and a remote agent calling it over the network. The client component talks JSON-RPC over HTTP or HTTP Streamable and covers the full catalogue surface: initialize, list and call tools, retrieve prompts, read resources, plus sampling and elicitation for interactive AI workflows.
Both components implement the MCP 2026-07-28 specification: stateless requests with per-request _meta, server/discover, subscriptions/listen, Multi Round-Trip Requests and the Tasks extension. They stay backward compatible with 2025-11-25. The same server answers both eras, and the client falls back automatically when a server only speaks the older protocol.
MCP Server
TsgcWSAPIServer_MCP: tools, prompts, resources, stdio and HTTP transports
MCP Client
TsgcWSAPIClient_MCP: discover and call any MCP-compatible server
Delphi 7 through RAD Studio 13, C++Builder 10.1 Berlin through 13, Lazarus 4.4.0
Features
Both sides of the protocol
Server, client and the AI provider clients that consume what MCP exposes.
Tools with typed schemas
Tools.AddTool registers a callable function and its InputSchema properties; calls arrive in the OnMCPRequestTool event with parsed arguments and a structured response object.
Prompts & resources
Reusable prompt templates with arguments and URI-addressed resources, served through OnMCPRequestPrompt and OnMCPRequestResource.
Client catalogue surface
Initialize, ListTools, ListPrompts, ListResources, ListResourceTemplates, then RequestTool / RequestPrompt / RequestResource with event-driven responses.
Sampling & elicitation
The client supports MCP sampling (server-requested AI model interaction) and elicitation (gathering user input mid-workflow), the two interactive extensions of the protocol.
Authentication
Client-side authentication options include API keys via AuthenticationOptions.ApiKey; see the blog for OAuth2-protected MCP servers.
MCP 2026-07-28
Stateless requests, server/discover, Mcp-Method / Mcp-Name / Mcp-Param headers with validation, cache hints (ttlMs, cacheScope) and the new error codes. MCPOptions.ProtocolEra on the client selects Auto, Modern or Legacy.
Multi Round-Trip Requests
A tool asks for elicitation (form or URL), sampling or roots through aResponse.InputRequired; the client answers in OnMCPInputRequired and retries with the HMAC-signed requestState.
Subscriptions & tasks
subscriptions/listen streams list changes and resource updates. The io.modelcontextprotocol/tasks extension (CreateTask) runs long tools in the background, and the client component polls it with MCPOptions.Tasks.Enabled or fetches it directly with TasksGet.
MCP client OAuth
MCPOptions.Authorization: protected resource metadata discovery, Client ID Metadata Documents, dynamic registration fallback, PKCE with resource indicators, RFC 9207 iss validation and per-issuer credentials.
Two transports
Stdio for subprocess-launched servers (the Claude Desktop model) and HTTP for network deployments. One component, both channels.
AI client family
Talk to the models directly too: chat, streaming and function calling against OpenAI, Anthropic Claude and Google Gemini, plus DeepSeek, Grok, Mistral and Ollama in the same AI family.
Cross-platform
Windows 32/64, Linux 64, macOS (Intel and ARM), iOS and Android. VCL and FireMonkey, with design-time components.
Edition
The MCP server, MCP client and AI API clients are Enterprise edition features of sgcWebSockets.
QuickStart
An MCP server and an MCP client in Delphi
Register a tool with a typed argument on the server; discover and call it from the client.
uses
sgcWebSocket_Server, sgcAI, sgcAI_MCP_Classes, sgcAI_MCP_Server;
procedure TForm1.SetupMCPServer;
begin// Attach the MCP API component to a sgcWebSockets HTTP server
MCPServer.Server := Server;
MCPServer.EndpointOptions.Endpoint := '/mcp';
MCPServer.MCPOptions.ServerInfo.Name := 'sgc-mcp-server';
MCPServer.MCPOptions.ServerInfo.Version := '1.0.0';
// Register a callable tool with a typed argumentwith MCPServer.Tools.AddTool('GetTemperature',
'Get the actual temperature in a city.') do
InputSchema.Properties.AddProperty('city', True);
MCPServer.OnMCPRequestTool := MCPRequestTool;
Server.Port := 8080;
Server.Active := True;
end;
procedure TForm1.MCPRequestTool(Sender: TObject;
const aSession: TsgcAI_MCP_Session;
const aRequest: TsgcAI_MCP_Request_ToolsCall;
const aResponse: TsgcAI_MCP_Response_ToolsCall);
beginif aRequest.Params.Name = 'GetTemperature'then
aResponse.Result.Content.AddText('The current temperature in ' +
aRequest.Params.Arguments.Item[0].Value + ' is 22 Celsius');
end;