Skip to content
Open
3 changes: 3 additions & 0 deletions Document-Processing-toc.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<li>
<a href="/document-processing/ai-agent-tools/customization">Customization</a>
</li>
<li>
<a href="/document-processing/ai-agent-tools/example-prompts">Example prompts</a>
</li>
</ul>
</li>
<li>AI Coding Assistant
Expand Down
71 changes: 32 additions & 39 deletions Document-Processing/ai-agent-tools/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,27 @@ documentation: ug

The Syncfusion Document SDK Agent Tool library is designed to be extensible. This guide walks you through creating a custom agent tool class and registering the tools with an AI agent so they are callable alongside the built-in tools.

---

## Creating a Custom Agent Tool Class

Follow these steps to expose new document operations to the AI agent.

**Step 1: Create a Custom Agent Tool by Inheriting AgentToolBase**

Create a new class that inherits from `AgentToolBase` (in the `Syncfusion.AI.AgentTools.Core` namespace) and accepts a document repository through its constructor:
Create a new class that inherits from `AgentToolBase` (in the `Syncfusion.AI.AgentTools.Core` namespace) and accepts a document Manager through its constructor:

```csharp
using Syncfusion.AI.AgentTools.Core;
using Syncfusion.DocIO.DLS;

public class WordWatermarkAgentTools : AgentToolBase
{
private readonly WordDocumentRepository _repository;
private readonly WordDocumentManager _Manager;

public WordWatermarkAgentTools(WordDocumentRepository repository)
public WordWatermarkAgentTools(WordDocumentManager Manager)
{
ArgumentNullException.ThrowIfNull(repository);
_repository = repository;
ArgumentNullException.ThrowIfNull(Manager);
_Manager = Manager;
}
}
```
Expand All @@ -45,7 +44,7 @@ Add `public` instance methods and decorate each one with `[Tool]`, providing a n
[Tool(
Name = "AddTextWatermark",
Description = "Adds a text watermark to the specified Word document.")]
public CallToolResult AddTextWatermark(...)
public AgentToolResult AddTextWatermark(...)
{
// implementation
}
Expand All @@ -56,7 +55,7 @@ public CallToolResult AddTextWatermark(...)
Decorate each method parameter with `[ToolParameter]` to give the AI a natural-language description of what value to pass:

```csharp
public CallToolResult AddTextWatermark(
public AgentToolResult AddTextWatermark(
[ToolParameter(Description = "The document ID of the Word document.")]
string documentId,
[ToolParameter(Description = "The watermark text to display (e.g., 'DRAFT', 'CONFIDENTIAL').")]
Expand All @@ -65,88 +64,87 @@ public CallToolResult AddTextWatermark(
float fontSize = 72f)
```

**Step 4: Return CallToolResult**
**Step 4: Return AgentToolResult**

All tool methods must return `CallToolResult`. Use the static factory methods to signal success or failure:
All tool methods must return `AgentToolResult`. Use the static factory methods to signal success or failure:

```csharp
// Success
return CallToolResult.Ok("Operation completed successfully.");
return AgentToolResult.Ok("Operation completed successfully.");

// Failure
return CallToolResult.Fail("Reason the operation failed.");
return AgentToolResult.Fail("Reason the operation failed.");
```

### Example
**Example**

```csharp
using Syncfusion.AI.AgentTools.Core;
using Syncfusion.DocIO.DLS;

public class WordWatermarkAgentTools : AgentToolBase
{
private readonly WordDocumentRepository _repository;
private readonly WordDocumentManager _Manager;

public WordWatermarkAgentTools(WordDocumentRepository repository)
public WordWatermarkAgentTools(WordDocumentManager Manager)
{
ArgumentNullException.ThrowIfNull(repository);
_repository = repository;
ArgumentNullException.ThrowIfNull(Manager);
_Manager = Manager;
}

[Tool(
Name = "AddTextWatermark",
Description = "Adds a text watermark to the specified Word document.")]
public CallToolResult AddTextWatermark(
public AgentToolResult AddTextWatermark(
[ToolParameter(Description = "The document ID of the Word document.")]
string documentId,
[ToolParameter(Description = "The watermark text to display (e.g., 'DRAFT', 'CONFIDENTIAL').")]
string watermarkText)
{
try
{
WordDocument? doc = _repository.GetDocument(documentId);
WordDocument? doc = _Manager.GetDocument(documentId);
if (doc == null)
return CallToolResult.Fail($"Document not found: {documentId}");
return AgentToolResult.Fail($"Document not found: {documentId}");

TextWatermark watermark = new TextWatermark(watermarkText, "", 250, 100);
watermark.Color = Syncfusion.Drawing.Color.LightGray;
watermark.Layout = WatermarkLayout.Diagonal;
doc.Watermark = watermark;

return CallToolResult.Ok(
return AgentToolResult.Ok(
$"Watermark '{watermarkText}' applied to document '{documentId}'.");
}
catch (Exception ex)
{
return CallToolResult.Fail(ex.Message);
return AgentToolResult.Fail(ex.Message);
}
}

[Tool(
Name = "RemoveWatermark",
Description = "Removes the watermark from the specified Word document.")]
public CallToolResult RemoveWatermark(
public AgentToolResult RemoveWatermark(
[ToolParameter(Description = "The document ID of the Word document.")]
string documentId)
{
try
{
WordDocument? doc = _repository.GetDocument(documentId);
WordDocument? doc = _Manager.GetDocument(documentId);
if (doc == null)
return CallToolResult.Fail($"Document not found: {documentId}");
return AgentToolResult.Fail($"Document not found: {documentId}");

doc.Watermark = null;
return CallToolResult.Ok($"Watermark removed from document '{documentId}'.");
return AgentToolResult.Ok($"Watermark removed from document '{documentId}'.");
}
catch (Exception ex)
{
return CallToolResult.Fail(ex.Message);
return AgentToolResult.Fail(ex.Message);
}
}
}
```

---

## Registering Custom Tools with the AI Agent

Expand All @@ -155,7 +153,7 @@ Once your custom tool class is created, register it alongside the built-in tools
**Step 1: Instantiate the Custom Tool Class**

```csharp
var wordRepo = new WordDocumentRepository(TimeSpan.FromMinutes(5));
var wordRepo = new WordDocumentManager(TimeSpan.FromMinutes(5));

// Built-in tools
var wordDocTools = new WordDocumentAgentTools(wordRepo, outputDirectory);
Expand All @@ -180,7 +178,7 @@ using Microsoft.Extensions.AI;
var msAiTools = allSyncfusionTools
.Select(t => AIFunctionFactory.Create(t.Method, t.Instance, new AIFunctionFactoryOptions
{
Name = t.Name,
Name = t.Name,
Description = t.Description
}))
.Cast<AITool>()
Expand All @@ -198,8 +196,6 @@ var agent = openAIClient.AsAIAgent(

Your custom tool methods are now callable by the AI agent the same way as all built-in tools.

---

## Example Prompts

Once the custom watermark tools are registered, you can interact with the AI agent using natural language. The following examples show typical prompts and the tool calls the agent will make in response.
Expand All @@ -210,20 +206,17 @@ Once the custom watermark tools are registered, you can interact with the AI age

The agent will call `Word_CreateDocument` to load the file, then `Word_AddTextWatermark` with `watermarkText = "CONFIDENTIAL"`, and finally `Word_ExportDocument` to save the result.

---

## Customizing the System Prompt

The system prompt shapes how the AI agent uses the tools. Tailor it to your use case:

```csharp
string systemPrompt = """
You are an expert document-processing assistant with access to tools for Word operations.
""";
string systemPrompt = "You are an expert document-processing assistant with access to tools for Word operations.";
```

## See Also

- [Overview](./overview.md)
- [Tools Reference](./tools.md)
- [Getting Started](./getting-started.md)
- [Overview](https://helpstaging.syncfusion.com/document-processing/ai-agent-tools/overview)
- [Tools](https://helpstaging.syncfusion.com/document-processing/ai-agent-tools/tools)
- [Getting Started](https://helpstaging.syncfusion.com/document-processing/ai-agent-tools/getting-started)
Loading