Delphi MCP Server
A Model Context Protocol (MCP) server implementation in Delphi, designed to integrate with Claude Code, Codex, and other MCP-compatible clients for AI-powered Delphi development workflows.
Table of Contents
- Features
- Requirements
- Installation
- Transport Modes
- Protocol Versions and Dual-Era Behaviour
- Using as a Library
- Integration with Claude Code
- Integration with Codex
- Testing with MCP Inspector
- Available Example Tools
- Available Example Prompts
- Available Example Resources
- Configuration
- Authentication
- Network and Security
- License
- Contributing
- About GDK Software
- Support
- Commercial Support
Features
- Dual-era MCP: Serves MCP 2026-07-28 (per-request
_meta,server/discover) and the initialize-based revisions 2025-06-18 and 2025-11-25 on the same endpoint and the same stdio process; see Protocol versions - Dual Transport Support: HTTP (Streamable HTTP with SSE) and STDIO (stdin/stdout)
- Dual Response Mode: Supports both JSON-RPC and Server-Sent Events in the same server
- Tool System: Extensible tool system with RTTI-based discovery and execution
- Resource Management: Modular resource system supporting various content types
- Security:
OriginandHostvalidation against DNS rebinding on every request, loopback binding by default, CORS headers for browser clients, request size and nesting limits, opt-in bearer authentication with OAuth 2.1 resource-server discovery - Multi round-trip requests, streaming and subscriptions:
InputRequiredResultwith signedrequestState, progress and log notifications on the response stream,subscriptions/listenfor change notifications - Native HTTP stack: Indy HTTP server with keep-alive, no external runtime
- Optional Parameters: Support for optional tool parameters using custom attributes
- Cross-Platform: Supports Windows (Win32/Win64) and Linux (x64)
Requirements
- Delphi 11 Alexandria or later. The source uses no language feature beyond inline variables (10.3) and no RTL unit newer than Delphi 11; development happens on Delphi 12 Athens, which is what the default project files target.
- Windows (Win32/Win64) or Linux (x64)
- No external dependencies (all required libraries included)
Building with Delphi 11 Alexandria
build.bat and build-tests.bat compile the .dpr directly, so only DELPHI_PATH needs to point at your installation:
set DELPHI_PATH=C:\Program Files (x86)\Embarcadero\Studio\22.0
build.bat
For the IDE and for the Linux64 build, open src/MCPServer.D11.dproj (tests: tests/MCPServerTests.D11.dproj) instead of the Athens project files. They carry the same units and settings, write their output to a separate D11 subdirectory, and exist because Alexandria will not load a ProjectVersion 20.3 project file.
Installation
For Standalone Usage
- Clone the repository:
git clone https://github.com/GDKsoftware/delphi-mcp-server.git
cd delphi-mcp-server
- Build the project:
Windows Build
build.bat
Or specify configuration and platform:
build.bat Debug Win32
build.bat Release Win64
The script picks up the highest TaurusTLS version installed in the CatalogRepository of the Studio release that DELPHI_PATH points at. To build against a copy somewhere else, set TAURUS_PATH to its Source directory first:
set TAURUS_PATH=C:\path\to\TaurusTLS\Source
build.bat Release Win64
Linux Build
Prerequisites:
- Delphi Enterprise with Linux platform support
- PAServer running on Linux target machine
- Linux SDK configured in RAD Studio
From the batch file:
build.bat Release Linux64
Or from RAD Studio IDE:
- Open MCPServer.dproj
- Select Linux64 platform
- Build
Transport Modes
The server supports two transport modes:
HTTP Transport (Default)
Start the server without arguments for HTTP transport with Server-Sent Events (SSE):
Win32\Debug\MCPServer.exe
The server will listen on http://localhost:3000/mcp by default (configurable via settings.ini).
Use HTTP transport for:
- Claude Code (SSE support)
- MCP Inspector
- Web-based clients
- Remote connections
STDIO Transport
Start the server with --stdio flag for stdin/stdout communication:
Win32\Debug\MCPServer.exe --stdio
The server will:
- Read JSON-RPC messages from stdin, UTF-8, one per line, no byte-order mark
- Write JSON-RPC messages to stdout the same way
- Log diagnostic messages to stderr, never to stdout
- Answer
notifications/cancelledby stopping the named request; it gets no response - Send
notifications/progressfor a request that carries_meta.progressToken, before its response - Exit within
[Server] MaxConcurrentRequestsworker threads' drain time (2 seconds by default) once stdin closes
Use STDIO transport for:
- Codex (OpenAI)
- Local MCP clients that use process spawning
- Automated testing and scripting
Supported flag variants: --stdio, -stdio, /stdio
By default requests are answered one at a time, in the order they arrive.
[Server] MaxConcurrentRequests in settings.ini raises the number of worker
threads for a client that issues concurrent requests over the same process; a
stdio server never writes settings.ini on its own, so this and the other
[Server] limits still need explicit configuration when they should differ
from the defaults.
A tool sees the request it is answering through TMCPRequestContext.Current:
CheckCancelled raises once the client cancels, ReportProgress sends a
notifications/progress when the request carries a progress token, and
Log sends a notifications/message when the request carries
_meta.io.modelcontextprotocol/logLevel and the message's level is at or
above it. See test_tool_with_progress and test_logging_tool in
MCPServer.Tool.ContentSamples for worked examples.
Over HTTP the same notifications reach the client on the response: when the
request accepts text/event-stream and a tool sends one, the response turns
into an SSE stream (chunked, X-Accel-Buffering: no) that carries the
notifications first and the JSON-RPC response as its last event. A request
that sends none is answered as before. A client that closes the stream
cancels the request: the server's next write to it fails and the tool sees
IsCancelled. That is the HTTP cancellation. A notifications/cancelled
naming the same request is answered 202 and dropped, because the tracker a
request consults is its own response stream and a notification always arrives
on a connection of its own; only over stdio, where every message shares one
channel, does the notification stop a running request.
Change notifications (subscriptions/listen)
A modern client that wants to hear about changes opens a long-lived
subscriptions/listen request with a notifications filter
(toolsListChanged, promptsListChanged, resourcesListChanged,
resourceSubscriptions: a list of URIs). TMCPSubscriptionsManager
(MCPServer.SubscriptionsManager) answers with
notifications/subscriptions/acknowledged carrying the honoured filter and
keeps the stream open: over HTTP as an SSE response with a keep-alive comment
every 15 seconds, over stdio on a thread of its own so the worker threads stay
free. Every message on the subscription carries
_meta.io.modelcontextprotocol/subscriptionId, the JSON-RPC id of the
subscriptions/listen request. Closing the SSE stream, or sending
notifications/cancelled for that id over stdio, ends the subscription;
when the server stops (or stdin closes) it answers the request with a
completion result first.
Notifications are delivered synchronously on the thread that causes the change, so a subscriber that stops reading can hold up that thread until its socket buffer drains.
Assign the manager as ChangeNotifier of the tools, prompts and resources
managers, as MCPServer.dpr does, and the tools, prompts and resources
capabilities announce listChanged (and resources.subscribe) to modern
clients. AddTool, RemoveTool, AddPrompt, RemovePrompt, AddResource,
RemoveResource and AddResourceTemplate then notify the subscribed clients,
and TMCPResourcesManager.ResourceUpdated(Uri) reports a changed resource to
the clients that subscribed to that URI. Without a ChangeNotifier nothing is
announced and nothing is sent.
Protocol Versions and Dual-Era Behaviour
The server decides per request which protocol era it is speaking; nothing is negotiated per connection and no session is minted.
| Request | Era | Served as |
|---|---|---|
params._meta with io.modelcontextprotocol/protocolVersion |
modern | 2026-07-28. clientCapabilities is required (-32602); an unknown revision gets -32022 with the supported list; initialize, ping, logging/setLevel and resources/subscribe do not exist in this era (-32601). |
initialize without modern _meta |
legacy | The requested revision when it is 2025-06-18 or 2025-11-25, otherwise 2025-11-25. The result carries capabilities and serverInfo only. |
server/discover without _meta |
modern, malformed | -32602 |
| Anything else | legacy | The revision negotiated by initialize on this stdio process, the MCP-Protocol-Version header on HTTP, or 2025-11-25 when nothing is known. |
Modern results carry resultType, _meta.io.modelcontextprotocol/serverInfo and, on server/discover, tools/list, resources/list, resources/templates/list and resources/read, the cache hints ttlMs and cacheScope. Legacy results are unchanged. Client responses (result or error without method) are ignored.
Legacy clients do not get the features that 2026-07-28 deprecates or removes. logging/setLevel, resources/subscribe and resources/unsubscribe answer -32601, and the initialize result does not announce logging or resources.subscribe. The server sends no requests to the client (sampling/createMessage, elicitation/create, roots/list), so a request whose tool, resource or prompt needs input from the client answers -32603. Modern clients get these through _meta["io.modelcontextprotocol/logLevel"], subscriptions/listen and the multi round-trip requests described under Asking the client for input.
Over HTTP, modern requests must carry MCP-Protocol-Version, Mcp-Method and, for tools/call, resources/read, prompts/get and tasks/*, Mcp-Name (the tool or prompt name, the resource URI or the task id; Base64 sentinel encoding accepted); a missing or different header is 400 with -32020. Modern protocol errors get 400, an unknown method 404; legacy requests get 200 for every JSON-RPC error, except 400 for an unknown MCP-Protocol-Version header. Notifications get 202 with an empty body. Every 4xx to a modern request carries a JSON-RPC error body, so dual-era clients can tell a modern server from a legacy one.
Handlers can read the era, the negotiated revision and the client's declared capabilities through TMCPRequestContext.Current (MCPServer.RequestContext) or by implementing IMCPCapabilityManagerEx, and can raise EMCPError (MCPServer.Errors) to send a specific JSON-RPC error code.
settings.ini keys: [Server] Title, Description, WebsiteUrl and Instructions fill serverInfo and instructions; [Protocol] LenientModernPing answers ping in the modern era anyway, DiscoverListsLegacyVersions also lists the legacy revisions in server/discover, and DiscoverTtlMs is the cache hint on server/discover.
2025-03-26 is accepted on initialize but answered with 2025-11-25; JSON-RPC batch arrays are rejected with -32600.
Using as a Library
The Delphi MCP Server is designed to be used both as a standalone application and as a library for your own MCP server implementations. This section covers how to integrate it into your existing Delphi projects.
Project Setup for Library Usage
Option 1: Git Submodule (Recommended)
# Add MCPServer as a submodule to your project
git submodule add https://github.com/GDKsoftware/delphi-mcp-server.git lib/mcpserver
git submodule update --init --recursive
Option 2: Direct Source Inclusion
Copy the src folder from MCPServer into your project and add the units to your uses clauses.
Delphi Project Configuration
Search Paths: Add the MCPServer source directories to your project search path:
lib\mcpserver\src\Corelib\mcpserver\src\Managerslib\mcpserver\src\Protocollib\mcpserver\src\Serverlib\mcpserver\src\Toolslib\mcpserver\src\Resourceslib\mcpserver\src\Prompts
Required Units:
MCPServer.Hostis the only unit a host needs; it pulls in the managers, the HTTP server and the stdio transport:MCPServer.Host, // TMCPServerHost, the library facade MCPServer.Types, // Interfaces, protocol constants, schema attributes MCPServer.Registration // TMCPRegistry, for self-registering toolsComposing the managers by hand instead needs
MCPServer.Settings,MCPServer.ManagerRegistry,MCPServer.CoreManager, the managers you want, andMCPServer.IdHTTPServerorMCPServer.StdioTransport.
Library Integration
TMCPServerHost (MCPServer.Host) is the whole composition behind one class:
it builds the managers, owns the HTTP server and drives either transport.
StartHttp returns as soon as the server listens, so the host fits into an
application that has a message loop of its own:
program YourMCPServer;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
MCPServer.Host in 'lib\mcpserver\src\Server\MCPServer.Host.pas',
YourProject.Tool.Custom in 'YourProject.Tool.Custom.pas';
begin
const Host = TMCPServerHost.Create;
try
Host.Settings.Port := 3000;
Host.AddTool(TCustomTool.Create);
Host.StartHttp;
Writeln('MCP Server running on port ', Host.BoundPort);
Readln;
Host.Stop;
finally
Host.Free;
end;
end.
What a host starts with. Nothing. A fresh host publishes only the tools,
resources, resource templates and prompts it was handed through AddTool,
AddResource and AddPrompt, so two hosts in one process publish exactly what
each of them was given. SeedFromGlobalRegistry := True takes everything from
TMCPRegistry instead, which is what the standalone server does; set it before
the first call that builds the managers, or it raises
EMCPConfigurationError. [Server] ExposeDiagnosticsResources is honoured
either way: with it off, server://status, logs://recent and logs://{level}
never reach the lists.
Settings. Create reads no file at all and starts from the built-in
defaults, which Host.Settings then lets you change in code.
Create(SettingsFile) reads the .ini you name and writes none.
Create(Settings) takes a TMCPSettings you built yourself and keep owning.
Only the standalone server reads settings.ini from the executable's own
directory.
Ports. Settings.Port := 0 asks the operating system for a free port and
BoundPort reports the one it gave. A loopback server takes that same port on
both its IPv4 and its IPv6 binding, so localhost reaches it whichever family
the client resolves first. StartHttp and Stop are both idempotent.
Transports. RunStdio blocks and is a console entry point only: the stdio
transport claims stdout and redirects the logger to stderr for the whole
process, so a GUI application must never call it. RunStdioWith(Input, Output)
runs the same dispatch over two streams, which is how a test drives one line in
and reads one line out.
Composing the managers by hand still works, and is what to do when you need a manager the host does not build:
var
ManagerRegistry: IMCPManagerRegistry;
begin
const Settings = TMCPSettings.Create;
try
ManagerRegistry := TMCPManagerRegistry.Create;
ManagerRegistry.RegisterManager(TMCPCoreManager.Create(Settings));
ManagerRegistry.RegisterManager(TMCPToolsManager.Create(False));
ManagerRegistry.RegisterManager(TMCPResourcesManager.Create(False));
const Server = TMCPIdHTTPServer.Create(nil);
try
Server.Settings := Settings;
Server.ManagerRegistry := ManagerRegistry;
Server.Start;
Writeln('MCP Server running on port ', Server.BoundPort);
Readln;
Server.Stop;
finally
Server.Free;
end;
finally
Settings.Free;
end;
end.
Library checklist
- Register before you start, or hand them over afterwards. The parameterless
TMCPToolsManager.Create,TMCPResourcesManager.CreateandTMCPPromptsManager.CreatereadTMCPRegistryonce, so a registration made after the managers exist is not picked up: register your tools, resources and prompts (normally from unitinitializationsections) first.Create(False), and aTMCPServerHostleft at its defaultSeedFromGlobalRegistry, read the registry not at all and publish only what you hand them, which you can do at any time. - STDIO: keep stdout clean. Everything on stdout must be an MCP message.
TMCPStdioTransport.CreateforcesTLogger.UseStdErr := Trueand setsTLogger.StdoutReserved, so console logging goes to stderr and an attempt to switch it back is refused with a one-time warning. NeverWritelnfrom tools, managers or resources; log throughTLogger. server://statusis registered by default by the unit initialization ofMCPServer.Resource.Server, and reaches every manager that seeds from the registry.TServerStatusResource.SetNamePrefix('myapp_')renames it toserver://myapp_status; call it before the managers are created.[Server] ExposeDiagnosticsResources = falsekeeps it,logs://recentandlogs://{level}out of the lists altogether.- Error codes and protocol constants live in
MCPServer.Types(JSONRPC_*,MCP_ERROR_*,MCP_PROTOCOL_VERSION_*,MCP_META_*, and the header namesMCP_HEADER_SESSION_ID,MCP_HEADER_PROTOCOL_VERSION,MCP_HEADER_METHODandMCP_HEADER_NAME). TheJSONRPC_*names inMCPServer.JsonRpcProcessorremain as aliases.TMCPHeaderValue.Encode(MCPServer.HttpHeaders) writes a value into a header the wayTryDecodereads one back: a header-safe value passes through, anything else is wrapped in the=?base64?...?=sentinel. - Prompts and completion are optional managers, registered the same way as tools and resources:
ManagerRegistry.RegisterManager(TMCPPromptsManager.Create)and, if you want argument completion,ManagerRegistry.RegisterManager(TMCPCompletionManager.Create(PromptsManager, ResourcesManager))(it needs the concrete manager instances, not theIMCPCapabilityManagerinterface, to look prompts and resource templates up by name). Thepromptsandcompletionscapabilities are only advertised when these managers are registered.
Creating Custom Tools
unit YourProject.Tool.Custom;
interface
uses
MCPServer.Tool.Base,
MCPServer.Types,
MCPServer.Registration;
type
TCustomToolParams = class
private
FInput: string;
FCount: Integer;
public
[SchemaDescription('Text input to process')]
property Input: string read FInput write FInput;
[Optional]
[SchemaDescription('Number of times to repeat (default: 1)')]
property Count: Integer read FCount write FCount;
end;
TCustomTool = class(TMCPToolBase<TCustomToolParams>)
protected
function ExecuteWithParams(const AParams: TCustomToolParams): string; override;
public
constructor Create; override;
end;
implementation
constructor TCustomTool.Create;
begin
inherited;
FName := 'custom_tool';
FDescription := 'A custom tool that processes input';
end;
function TCustomTool.ExecuteWithParams(const AParams: TCustomToolParams): string;
var
I: Integer;
Output: string;
begin
Output := '';
for I := 1 to AParams.Count do
Output := Output + AParams.Input + #13#10;
Result := 'Processed: ' + Output;
end;
initialization
TMCPRegistry.RegisterTool('custom_tool',
function: IMCPTool
begin
Result := TCustomTool.Create;
end
);
end.
Arguments are validated against the generated schema before the tool runs: a
missing property without [Optional], a value of the wrong JSON type or an
unknown enumeration name is answered as an isError result that names the
parameter. Integer properties are published as integer, TDateTime as a
string with format: date-time, enumerations and sets with their names;
[SchemaTitle], [SchemaFormat], [SchemaMinimum] and [SchemaMaximum]
add the corresponding keywords.
Dates and times travel as ISO 8601 strings in both directions, the shape their schema announces:
| Delphi type | Schema | Written as | Read from |
|---|---|---|---|
TDateTime |
string, format: date-time |
2026-10-18T14:30:00.000+02:00 |
an ISO 8601 date-time; without an offset it is local time |
TDate |
string, format: date |
2026-10-18 |
yyyy-mm-dd only |
TTime |
string with a pattern and a description |
14:30:00, or 14:30:00.250 |
hh:mm, hh:mm:ss, hh:mm:ss.fff |
A TDateTime with Z or an offset is converted to the local time of the
server; one without keeps the clock time it was sent with, so
2026-10-18T14:30:00 arrives as 14:30 and goes back out with the local offset.
A TDate is written without its time of day and a TTime without its date.
A TTime carries no zone, so it is written without an offset, and a time with
Z or an offset is refused rather than read as something it is not. That is
why its schema has no format: time: JSON Schema defines that as RFC 3339
full-time, which requires seconds and a zone offset. The pattern accepts
exactly the text the server reads. A value of the wrong shape is refused with
the shape the parameter expects. [SchemaDescription] and [SchemaPattern]
on a TTime replace the generated description and pattern.
Records in a schema
A record is described exactly the way a class is: type: object with one
properties member per public field, and a required array holding the
fields that carry no [Optional]. Records nest, hold arrays, hold classes and
sit inside classes; the walk stops at the same depth guard the class walk uses,
so a record that reaches itself through a TArray<T> cannot recurse forever.
Every attribute that works on a class property works on a record field:
[SchemaDescription], [SchemaTitle], [SchemaFormat], [SchemaMinimum],
[SchemaMaximum], [SchemaMinLength], [SchemaMaxLength], [SchemaPattern],
[SchemaDefault], [SchemaName] and [Optional].
type
TMoney = record
[SchemaDescription('Amount in the smallest unit')]
[SchemaMinimum(0)]
Amount: Double;
[SchemaName('currency_code')]
[SchemaPattern('^[A-Z]{3}$')]
Currency: string;
[Optional]
Note: string;
end;
{
"type": "object",
"properties": {
"amount": { "type": "number", "description": "Amount in the smallest unit", "minimum": 0 },
"currency_code": { "type": "string", "pattern": "^[A-Z]{3}$" },
"note": { "type": "string" }
},
"required": ["amount", "currency_code"]
}
The same shape crosses the wire in both directions: MCPServer.Serializer
builds the record from a JSON object and writes it back out, with enumerations
by name, TDateTime, TDate and TTime as ISO 8601, and an absent
[Optional] field left at the default of its type. A record is a value, so
nothing about it is freed after a call; an object a record holds is owned by
the call and freed with it.
What a record needs from RTTI. The generator reads a record's fields and
their attributes through extended RTTI, so the unit that declares the record
must publish field RTTI for the visibility the fields have. Delphi's default
(FIELDS([vcPrivate, vcProtected, vcPublic])) already does, so a record in an
ordinary unit needs nothing. A unit that narrows the setting must keep public
fields in it:
{$RTTI EXPLICIT FIELDS([vcPublic])}
A record whose fields are invisible has nothing to describe. As a property of a
parameter class it keeps the {"type": "string"} every record was published as
before, so a tool that has always listed goes on listing. As a parameter of a
method tool it is refused instead, naming the record and this directive,
because a tool written against this version should not ship a schema that does
not match what the method takes.
The same split applies to a type with no JSON shape at all: a pointer, a
procedure or method reference, a class reference, an interface or a variant is
published as string on the class walk, the way it always was, and refused as
a method parameter, naming the parameter and its type. One property the
generator cannot describe therefore never costs a tools/list its other tools.
A field, property or array element whose type carries no RTTI at all is refused
wherever it appears, naming the member, because there is nothing left to fall
back on. Private fields are skipped silently, because a private field is not
part of the wire.
TGUID. A TGUID is a record, but its D4 member is an anonymous array
that System publishes no type for, so a TGUID has no members to walk. It is
published as {"type": "string", "format": "uuid"} and travels as
f81d4fae-7dec-11d0-a765-00a0c91e6bf6, written in lower case without braces
and read with or without them.
A tool that returns more than text overrides ExecuteWithContext and builds
a TMCPToolResult (MCPServer.Tool.Result):
function TChartTool.ExecuteWithContext(const AParams: TChartParams;
const Context: IMCPRequestContext): TValue;
begin
Result := TMCPToolResult.Create
.AddText('Chart for ' + AParams.Series)
.AddImage(RenderPng(AParams), 'image/png')
.AddResourceLink('chart://' + AParams.Series, AParams.Series, '', 'image/png');
end;
The builder also has AddAudio, AddEmbeddedText, AddEmbeddedBlob,
WithAnnotations (for the last block), SetStructuredContent, SetMeta and
SetError. Raise EMCPToolError for a failure the model should see as an
isError result; the request context gives the protocol era and the
client's _meta. Tools that inherit from TMCPToolBase<T, R> return an
object that becomes structuredContent plus a text block with the same
JSON. Set FAnnotations or FIcons in the constructor to publish them in
tools/list; MarkReadOnly writes the readOnlyHint and openWorldHint
pair for a tool that only reads (pass True when it reaches outside the
server). MCPServer.Tool.ContentSamples
has one small example per content type.
A tool from a method
TMCPMethodTool (MCPServer.Tool.Method) turns a method you already have into
a tool. The input schema comes from the parameter list, the arguments are
marshalled onto it, the method is invoked, and its result is converted back to
JSON:
type
TOrderService = class
public
function PlaceOrder([SchemaDescription('Customer code')] const Customer: string;
[Optional] const Quantity: Integer): string;
end;
var
Context: TRttiContext;
begin
const Service = TOrderService.Create;
const Method = Context.GetType(TOrderService).GetMethod('PlaceOrder');
Host.AddTool(TMCPMethodTool.Create(TValue.From<TOrderService>(Service), Method,
'place_order', 'Places an order'));
end;
A parameter is published under its lower-cased name, or under [SchemaName]
when it carries one, and is required unless it carries [Optional]; every
schema attribute that works on a class property works on a parameter. The
method needs RTTI, which a public method of a class compiled with the default
{$RTTI} settings has, and MarkReadOnly writes the readOnlyHint pair the
same way it does for TMCPToolBase.
The result of a procedure is {"ok": true} and the result of a function is
{"result": <value>}, which is what GetOutputSchema describes. A descendant
that overrides ResultToJson replaces that object wholesale, so its envelope is
the whole structured result and is not nested under result; such a descendant
overrides GetOutputSchema with it, or the two disagree.
Who frees what. Every object the tool marshalled from the arguments, and
every object the method returned, is freed after the call, along with the
elements of a returned dynamic array of objects. That is the wrong rule for a
method that keeps what it is handed, which is the ordinary Add(Item) shape in
Delphi, and for a method that hands back something it still owns. Both are
virtual, so a descendant says so:
type
TAdoptingTool = class(TMCPMethodTool)
protected
procedure ReleaseArguments(const Owned: TList<TObject>); override;
procedure ReleaseResult(const Value: TValue; const ResultType: TRttiType); override;
end;
procedure TAdoptingTool.ReleaseArguments(const Owned: TList<TObject>);
begin
end;
procedure TAdoptingTool.ReleaseResult(const Value: TValue; const ResultType: TRttiType);
begin
end;
An untyped parameter, and a var or out parameter, have no place in a schema:
a tool answers with its result, not through its arguments. Both are refused when
the tool is created, with an EArgumentException naming the parameter.
The three generator entry points are usable on their own:
TMCPSchemaGenerator.GenerateSchemaFromMethod builds the input schema of a
parameter list, GenerateSchemaFromType the schema of a single type, and
GenerateSchemaFromMethodResult the {"result": ...} wrapper of a return type
(nil for a procedure, and for a return type that has no JSON shape). $schema
from [SchemaDialect] belongs to a root schema only and is never copied into a
parameter or result member. The marshal underneath them is public too:
TMCPSerializer.JsonToValue builds a TValue of a given TRttiType from a
TJSONValue and appends every object it created to the list you pass, and
TMCPSerializer.ValueToJson writes one back out.
Asking the client for input (multi round-trip requests)
MCP 2026-07-28 replaced server-initiated requests (elicitation/create,
sampling/createMessage, roots/list) with multi round-trip requests: the
server answers tools/call, resources/read or prompts/get with an
InputRequiredResult that lists what it needs, the client gathers the
answers and retries the same request with inputResponses (and the
server's opaque requestState). A tool, resource or prompt that needs input
raises EMCPInputRequired (MCPServer.Mrtr); the request context carries
the answers on the retry:
function TGreetTool.ExecuteWithContext(const Params: TNoParams;
const Context: IMCPRequestContext): TValue;
var
Response: TJSONObject;
begin
var Name := '';
if Context.TryGetInputResponse('user_name', Response) then
Name := TMCPInputResponse.ElicitationField(Response, 'name');
if Name = '' then
raise EMCPInputRequired.Create(TMCPInputRequests.Create
.AddElicitation('user_name', 'What is your name?', TMCPInputRequests.FieldSchema('name')));
Result := TMCPToolResult.Text(Format('Hello, %s!', [Name]));
end;
TMCPInputRequests builds the inputRequests map (AddElicitation,
AddSampling, AddListRoots); TMCPInputResponse reads the answers
(ElicitationContent, ElicitationField, SamplingText, Roots). The
processor only sends input requests the client declared a capability for
(elicitation, sampling, roots) and answers -32021 otherwise, so a
tool can check Context.HasClientCapability first and ask for what the
client can deliver. Missing or wrong answers are handled by raising again:
the client gets a fresh InputRequiredResult.
State that must survive the round trip goes into the second constructor
argument: EMCPInputRequired.Create(Requests, State) with a TJSONObject.
The processor seals it into requestState (HMAC-SHA256 over the state, the
method, a digest of the request parameters, the principal and an expiry)
and opens it on the retry into Context.RequestState; a tampered, expired
or foreign token is -32602. [Security] RequestStateKey in settings.ini
is the signing secret (set the same value on every instance behind a load
balancer; empty means a random key per process) and
RequestStateTtlSeconds the token lifetime (600 by default).
Clients on the 2025 revisions cannot answer input requests, so a request
that raises EMCPInputRequired in the legacy era is answered with
-32603. MCPServer.Tool.InputRequiredSamples and
test_input_required_result_prompt are the examples the conformance suite
exercises.
Creating Custom Resources
unit YourProject.Resource.Custom;
interface
uses
System.SysUtils,
MCPServer.Resource.Base,
MCPServer.Registration;
type
TCustomData = class
private
FMessage: string;
FTimestamp: TDateTime;
public
property Message: string read FMessage write FMessage;
property Timestamp: TDateTime read FTimestamp write FTimestamp;
end;
TCustomResource = class(TMCPResourceBase<TCustomData>)
protected
function GetResourceData: TCustomData; override;
public
constructor Create; override;
end;
implementation
constructor TCustomResource.Create;
begin
inherited;
FURI := 'custom://data';
FName := 'Custom Data';
FDescription := 'Custom resource data';
FMimeType := 'application/json';
end;
function TCustomResource.GetResourceData: TCustomData;
begin
Result := TCustomData.Create;
Result.Message := 'Hello from custom resource';
Result.Timestamp := Now;
end;
initialization
TMCPRegistry.RegisterResource('custom://data',
function: IMCPResource
begin
Result := TCustomResource.Create;
end
);
end.
FTitle, FSize and FAnnotations are published in resources/list;
FTtlMs and FCacheScope (private unless set) are the cache hints modern
clients get on resources/read. A binary resource implements
IMCPBinaryResource.ReadBinary and is delivered as a blob;
MCPServer.Resource.Samples shows a text and a binary example. A URI that is
not registered is answered with a JSON-RPC error (-32002 for
initialize-based clients, -32602 for modern clients), a read that raises
with -32603.
Resource Templates
A template matches a family of URIs and resolves the actual resource from
the captured variables. It supports RFC 6570 level 1 ({var}, one path
segment) and a level 2 subset ({+var}, the rest of the URI including
/); {/var} and {?var} are not implemented.
unit YourProject.Resource.CustomTemplate;
interface
uses
MCPServer.Resource.Base,
MCPServer.Registration;
type
TCustomTemplate = class(TMCPResourceTemplateBase)
public
constructor Create; override;
function CreateResource(const URI: string; Vars: TMCPTemplateVars): IMCPResource; override;
end;
implementation
constructor TCustomTemplate.Create;
begin
inherited;
FUriTemplate := 'custom://{id}';
FName := 'Custom item';
FMimeType := 'application/json';
end;
function TCustomTemplate.CreateResource(const URI: string; Vars: TMCPTemplateVars): IMCPResource;
begin
Result := TCustomResource.CreateForId(URI, Vars['id']);
end;
initialization
TMCPRegistry.RegisterResourceTemplate('custom://{id}',
function: IMCPResourceTemplate
begin
Result := TCustomTemplate.Create;
end
);
end.
CreateResource gets the actual requested URI (not the template) and the
captured variables, and returns an ordinary IMCPResource (typically a
TMCPResourceBase<T> with a constructor of your own choosing, since the
registry never constructs a template's resources itself); resources/read
tries an exact match first, then each registered template in order. See
MCPServer.Resource.Samples (test://template/{id}/data) and
MCPServer.Resource.Logs (logs://{level}, reusing the existing log
filtering) for worked examples.