Blog

All Blog Posts  |  Next Post  |  Previous Post



Introducing Attributes Support for MCP Servers in Delphi

Today

Building MCP (Model Context Protocol) servers just got significantly easier with the new attributes support in TMS AI Studio. This powerful feature allows developers to transform any Delphi class into a fully functional MCP server using simple attribute decorators, eliminating the need for manual tool registration and reducing boilerplate code.

TMS Software Delphi  Components tmsaistudio

What Are MCP Attributes?

MCP attributes are custom Delphi attributes that you can apply to your classes and methods to automatically expose them as MCP tools. Instead of manually creating tool definitions, registering methods, and managing parameters, you simply decorate your existing code with attributes and let the framework handle the rest.

Key Benefits

✅ Rapid Development: Turn existing business logic into MCP tools in minutes rather than hours.
✅ Clean Code: Keep your domain logic separate from MCP infrastructure concerns.
✅ Type Safety: Leverage Delphi's RTTI (Runtime Type Information) for automatic parameter validation and type conversion.
✅ Declarative Style: Express your intent clearly with self-documenting attributes.

Available Attributes

The TMS MCP framework provides a comprehensive set of attributes:

Tool Declaration

  • TTMSMCPTool - Marks a method as an MCP tool

Metadata Attributes

  • TTMSMCPName - Overrides the default name
  • TTMSMCPDescription - Adds descriptive text for AI models

Tool Behavior Hints

  • TTMSMCPReadOnly - Indicates read-only operations
  • TTMSMCPDestructive - Warns of destructive operations
  • TTMSMCPIdempotent - Indicates idempotent operations (safe to repeat)
  • TTMSMCPOpenworld - Allows flexible parameter handling

Parameter Modifiers

  • TTMSMCPOptional - Marks parameters as optional

Quick Start Example

Here's a simple calculator server implementation using attributes:

type
  TCalculatorServer = class(TPersistent)
  public
    [TTMSMCPTool]
    [TTMSMCPName('add')]
    [TTMSMCPDescription('Add two numbers together')]
    function Add(a, b: Integer): Integer;

    [TTMSMCPTool]
    function Multiply(a: Double; [TTMSMCPOptional] b: Double = 5): Double;

    [TTMSMCPTool]
    [TTMSMCPIdempotent]
    function Divide(a, b: Double): Double;
  end;

implementation

function TCalculatorServer.Add(a, b: Integer): Integer;
begin
  Result := a + b;
end;

function TCalculatorServer.Multiply(a, b: Double): Double;
begin
  Result := a * b;
end;

function TCalculatorServer.Divide(a, b: Double): Double;
begin
  if b = 0 then
    raise Exception.Create('Division by zero is not allowed');
  Result := a / b;
end;

Creating the Server

With your class decorated, creating an MCP server is remarkably simple:

var
  MCPServer: TTMSMCPAttributedServer;
  Calculator: TCalculatorServer;
begin
  Calculator := TCalculatorServer.Create;
  MCPServer := TTMSMCPServerFactory.CreateFromObject(Calculator);
  MCPServer.ServerName := 'CalculatorServer';
  
  try
    MCPServer.Start;
    MCPServer.Run;
  finally
    MCPServer.Free;
    Calculator.Free;
  end;
end;

The TTMSMCPServerFactory class provides convenient factory methods:

  • CreateFromObject - Creates a server from an existing object instance
  • CreateFromClass - Creates a server from a class type

Advanced Features

Optional Parameters

Use TTMSMCPOptional to mark parameters as optional with default values:

[TTMSMCPTool]
function Multiply(a: Double;
  [TTMSMCPOptional] b: Double = 5): Double;

Custom Names and Descriptions

Override default method names and add helpful descriptions:

[TTMSMCPTool]
[TTMSMCPName('sqrt')]
[TTMSMCPDescription('Calculate square root of a number')]
function SquareRoot([TTMSMCPName('value')] a: Double): Double;

Tool Behavior Hints

Provide hints to AI models about operation characteristics:

[TTMSMCPTool]
[TTMSMCPIdempotent]  // Safe to call multiple times
function GetBalance(accountId: string): Double;

[TTMSMCPTool]
[TTMSMCPDestructive]  // Warn about data modification
procedure DeleteAccount(accountId: string);

[TTMSMCPTool]
[TTMSMCPReadOnly]  // No side effects
function QueryData(filter: string): TStringList;

Multiple Target Classes

You can register multiple classes to a single server:

MCPServer := TTMSMCPAttributedServer.Create(nil);
MCPServer.AddClass(TCalculatorServer);
MCPServer.AddClass(TFileSystemServer);
MCPServer.AddObject(MyCustomObject);

Under the Hood

The attributes system leverages Delphi's powerful RTTI capabilities to:

  1. Discover methods marked with TTMSMCPTool at runtime
  2. Extract metadata from attribute decorators
  3. Generate MCP tool definitions automatically
  4. Create method wrappers that handle JSON parameter conversion
  5. Execute methods with proper type conversion and error handling

The TTMSMCPRttiTool class extends the base tool class to provide RTTI-based parameter handling and automatic JSON serialization/deserialization.

Best Practices

Use Descriptive Names: Even though attributes allow custom names, choose meaningful method names as a foundation.

Add Descriptions: Help AI models understand your tools by providing clear descriptions.

Handle Errors: Raise meaningful exceptions that will be converted to proper MCP error responses.

Mark Optional Parameters: Use TTMSMCPOptional with default values for better flexibility.

Use Behavior Hints: Apply TTMSMCPIdempotent, TTMSMCPReadOnly, and TTMSMCPDestructive appropriately to guide AI models.

Type Everything: Explicitly declare types using type attributes for clarity and validation.

Start Building the Future Today

The AI revolution is here, and with TMS AI Studio, Delphi developers are perfectly positioned to be at the forefront. Whether you're building intelligent business applications, creating AI-powered tools, or exploring the possibilities of the Model Context Protocol, TMS AI Studio gives you everything you need to succeed.

Ready to transform your development process?

Starting at 225 EUR for a single developer license, TMS AI Studio offers exceptional value for the comprehensive AI development toolkit it provides.

Get TMS AI Studio Now →

Join the growing community of developers who are already building tomorrow's AI applications with TMS AI Studio. The future of intelligent software development starts here.

TMS Software Delphi  Components


TMS AI Studio requires Delphi 11.0 or higher. 




Bradley Velghe


  1. Add AI superpower to your Delphi & C++Builder apps part 1

  2. Add AI superpower to your Delphi & C++Builder apps part 2: function calling

  3. Add AI superpower to your Delphi & C++Builder apps part 3: multimodal LLM use

  4. Add AI superpower to your Delphi & C++Builder apps part 4: create MCP servers

  5. Add AI superpower to your Delphi & C++Builder apps part 5: create your MCP client

  6. Add AI superpower to your Delphi & C++Builder apps part 6: RAG

  7. Introducing TMS AI Studio: Your Complete AI Development Toolkit for Delphi

  8. Automatic invoice data extraction in Delphi apps via AI

  9. Creating an n8n Workflow to use a Logging MCP Server

  10. Introducing Attributes Support for MCP Servers in Delphi



This blog post has not received any comments yet.



Add a new comment

You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
All fields are required.



All Blog Posts  |  Next Post  |  Previous Post