MCP 是将大语言模型连接到外部工具和数据的新兴标准。您只需实现一个协议,而不是为每个 AI 提供商做一套定制集成。
Delphi 中的 Model Context Protocol 意味着您现有的 VCL 或 FMX 应用程序可以变成 AI 可访问的服务。该协议基于 JSON-RPC,定义了三种原语:工具(带类型化参数的可调用函数)、提示词(带参数的可复用模板)和资源(通过 URI 寻址公开的数据)。MCP 服务器发布它们;MCP 客户端(Claude Desktop、Cursor 或您自己的 Delphi 应用程序)发现并调用它们。
服务器组件挂接到 sgcWebSockets HTTP 服务器,同时支持 stdio 和 HTTP 传输,因此同一段代码既能服务以子进程方式启动它的桌面 AI 助手,也能服务通过网络调用它的远程智能体。客户端组件通过 HTTP 或 HTTP Streamable 使用 JSON-RPC,覆盖完整的目录界面:初始化、列出并调用工具、获取提示词、读取资源,还有面向交互式 AI 工作流的采样(sampling)与征询(elicitation)。
TsgcWSAPIServer_MCP:工具、提示词、资源、stdio 和 HTTP 传输
TsgcWSAPIClient_MCP:发现并调用任何兼容 MCP 的服务器
TsgcHTTP_API_OpenAI、TsgcHTTP_API_Anthropic、TsgcHTTP_API_Gemini
Delphi 7 至 RAD Studio 13、C++Builder 10.1 Berlin 至 13、Lazarus 4.4.0
服务器、客户端,以及消费 MCP 所公开内容的 AI 提供商客户端。
Tools.AddTool 注册可调用函数及其 InputSchema 属性;调用连同已解析的参数和结构化响应对象一起到达 OnMCPRequestTool 事件。
带参数的可复用提示词模板和 URI 寻址的资源,通过 OnMCPRequestPrompt 和 OnMCPRequestResource 提供。
Initialize、ListTools、ListPrompts、ListResources、ListResourceTemplates,随后是 RequestTool / RequestPrompt / RequestResource,响应由事件驱动。
客户端支持 MCP 采样(服务器请求的 AI 模型交互)和征询(在工作流中途收集用户输入),即协议的两个交互式扩展。
客户端身份验证选项包括通过 AuthenticationOptions.ApiKey 设置 API 密钥;受 OAuth2 保护的 MCP 服务器请参阅博客。
stdio 用于以子进程方式启动的服务器(Claude Desktop 模式),HTTP 用于网络部署。一个组件,两条通道。
也可以直接与模型对话:对 OpenAI、Anthropic Claude 和 Google Gemini 进行聊天、流式传输和函数调用,同一 AI 家族还包括 DeepSeek、Grok、Mistral 和 Ollama。
Windows 32/64、Linux 64、macOS(Intel 和 ARM)、iOS 和 Android。VCL 和 FireMonkey,附设计时组件。
MCP 服务器、MCP 客户端和 AI API 客户端是 sgcWebSockets 的 Enterprise 版本功能。
在服务器上注册一个带类型化参数的工具;从客户端发现并调用它。
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 argument
with 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);
begin
if aRequest.Params.Name = 'GetTemperature' then
aResponse.Result.Content.AddText('The current temperature in ' +
aRequest.Params.Arguments.Item[0].Value + ' is 22 Celsius');
end;
uses
sgcAI_MCP_Client, sgcAI_MCP_Classes;
var
MCP: TsgcWSAPIClient_MCP;
begin
MCP := TsgcWSAPIClient_MCP.Create(nil);
MCP.MCPOptions.HttpOptions.URL := 'https://mcp.example.com/';
MCP.MCPOptions.ClientInfo.Name := 'sgc-mcp-client';
MCP.MCPOptions.ClientInfo.Title := 'sgc MCP demo';
MCP.MCPOptions.ClientInfo.Version := '1.0.0';
MCP.MCPOptions.AuthenticationOptions.ApiKey.Enabled := True;
MCP.MCPOptions.AuthenticationOptions.ApiKey.Value := 'sk-mcp-...';
MCP.OnMCPInitialize := MCPInit;
MCP.OnMCPListPrompts := MCPListPrompts;
MCP.OnMCPListTools := MCPListTools;
MCP.OnMCPResponseTool := MCPToolResponse;
MCP.Initialize;
MCP.ListPrompts;
MCP.ListResources;
MCP.ListTools;
MCP.RequestTool('GetTemperature', '{"city":"Madrid"}');
end;
让 AI 模型通过受控的类型化工具查询您应用程序的数据。只读还是读写,由您的处理程序按调用决定。
把 CRM、ERP 或工单操作公开为 MCP 工具,让自然语言指令驱动现有的 Delphi 业务逻辑。
把遗留应用程序包装成 stdio MCP 服务器,让 Claude Desktop 等助手访问其功能。
将工具和资源串联起来,在一条 AI 驱动的流水线中完成检索、处理和执行,由 Delphi 侧编排。
什么是 Model Context Protocol,以及服务器与客户端组件如何分工。
TsgcWSAPIServer_MCP 的完整参考:属性、方法、事件。
TsgcWSAPIClient_MCP 的完整参考:目录方法和响应事件。
一个可运行的 Delphi MCP 服务器分步演练。
把 Delphi 客户端连接到 MCP 服务器并调用其工具。
保护 MCP 端点:API 密钥和来自 Delphi 的身份验证流程。