Delphi MCP Server Tools (2/4)

With the release of sgcWebSockets 2025.9.0, developers can now integrate Model Context Protocol (MCP) functionality directly into their servers. This includes the ability to handle MCP Tool Requests, a key component of enabling dynamic and intelligent communication between AI models and backend services.

This guide will walk you through how MCP Tool Requests work, how to handle them in your TsgcWSAPIServer_MCP server, and how to send structured responses back to the client or AI agent. 

Understanding MCP Tool Requests

In the MCP (Model Context Protocol) ecosystem, Tools represent capabilities that an AI model can invoke — for example, querying a database, sending an email, or fetching data from an API.

An MCP server exposes these tools via WebSockets, and clients (like AI agents) can make Tool Requests to trigger specific actions. The TsgcWSAPIServer_MCP component in sgcWebSockets simplifies the entire flow, automatically decoding MCP messages and routing them to the appropriate event handlers in your server. 

How MCP Tool Requests Work

The communication flow typically looks like this:

  1. Client sends a Tool Request (JSON-formatted MCP message) to the MCP server.
  2. sgcWebSockets parses the request and triggers an event like OnToolRequest.
  3. The server executes the requested action — e.g., reads from a CRM, queries a database, or performs a calculation.
  4. The server builds a response in MCP format (including any result data or errors).
  5. The response is sent back to the requesting client.

Each MCP Tool Request is identified by:

  • A tool name (the operation to execute)
  • Parameters (context or data inputs)
  • An ID (used to correlate request and response)

Handling Tool Requests in Delphi

Once you have configured your MCP server using TsgcWSAPIServer_MCP, you can handle incoming tool requests through event handlers.
Below is an example of how to implement a handler for incoming MCP Tool Requests. 

procedure TMainForm.MCPServerMCPRequestTool(Sender: TObject;
  const ASession: TsgcAI_MCP_Session;
  const ARequest: TsgcAI_MCP_Request_ToolsCall;
  const AResponse: TsgcAI_MCP_Response_ToolsCall);
var
  LA, LB: Double;
begin
  if ARequest.Params.Name = 'math.add' then
  begin
    LA := ARequest.Params.Arguments.Node['a'].AsNumber;
    LB := ARequest.Params.Arguments.Node['b'].AsNumber;
    AResponse.Result.Content.AddText(Format('Sum = %.2f', [LA + LB]));
  end
  else
    AResponse.Result.IsError := True;
end; 

Using MCP Tool Metadata

You can also register and expose metadata about available tools using the MCP API.
This allows AI models to discover your available tools dynamically and understand their expected parameters.

For example, you can declare: 

procedure TMainForm.FormCreate(Sender: TObject);
var
  oTool: TsgcAI_MCP_Tool;
begin
  oTool := MCPServer.Tools.AddTool('math.add', 'Adds two numbers');
  oTool.InputSchema.Properties.AddProperty('a', True, aimcpjtNumber, 'Left operand');
  oTool.InputSchema.Properties.AddProperty('b', True, aimcpjtNumber, 'Right operand');
end; 

This information helps AI clients automatically understand how to call your tools, making it easier to create self-documenting AI integrations

Summary

MCP Tool Requests in sgcWebSockets 2025.9.0 provide a simple but powerful way to connect your AI models with real-world systems.
By using the TsgcWSAPIServer_MCP component and its built-in Tool Request handling, you can:

  • Create structured, discoverable tool endpoints
  • Respond to live AI requests in real time
  • Integrate business systems (CRMs, APIs, Databases) seamlessly
  • Maintain full control and transparency over AI-to-system interactions

Learn More

For a deeper look into the MCP API and available server components, visit the official documentation:


👉 sgcWebSockets MCP Tools Server Reference 


Find below a Delphi MCP Server Demo for Windows.

sgcMCP_Server
3.9 mb
×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

Delphi MCP Server Prompts (3/4)
Delphi MCP Server (1/4)