How to add your own tool

Enhancing the Sui Agent Kit with custom tools enables you to incorporate specialized functionalities designed to meet your specific requirements. This guide provides step-by-step instructions for creating and integrating a new tool into the existing framework.

Overview

  1. Create a new tool file

  2. Export the new tool

  3. Add supporting functions in SuiAgentKit

  4. Implement the Langchain tool class

  5. Export the Langchain tool

  6. Export your protocol’s langchain tools (if not already exported)

  7. Define Action class for given tool

  8. Export Action

  9. Use the custom tool

Step-by-Step

1. Create a New Tool File

Create a new TypeScript file in the src/tools/your_protocol directory for your tool (e.g., custom_tool.ts). If the src/tools/your_protocol directory does not exist, create it.

2. Export the Tool (if not already exported)

src/tools/index.ts

export * from "./token";
export * from "./defi";
export * from "./your_protocol"; // Add your protocol here if it's not already in the list

3. Add Supporting Functions to SuiAgentKit

src/agent/index.ts

export class SuiAgentKit {
  // ... existing code ...

  async customFunction(input: string): Promise<string> {
    // Implement your custom functionality
    return `Processed input: ${input}`;
  }
}

4. Implement the Langchain Tool Class

src/langchain/your_protocol/custom_tool.ts

import { Tool } from "langchain/tools";
import { SuiAgentKit } from "../../agent";

export class CustomTool extends Tool {
 name = "custom_tool";
 description = "Description of what the custom tool does.";

 constructor(private SuiKit: SuiAgentKit) {
   super();
 }

 protected async _call(input: string): Promise<string> {
   try {
     const result = await this.customFunction(input);
     return JSON.stringify({
       status: "success",
       message: "Custom tool executed successfully",
       data: result,
     });
   } catch (error: any) {
     return JSON.stringify({
       status: "error",
       message: error.message,
       code: error.code || "UNKNOWN_ERROR",
     });
   }
 }
}

5. Export Langchain Tool

src/langchain/your_protocol/index.ts

export * from "./custom_tool";

6. Export your protocol’s langchain tools (if not already exported)

src/langchain/index.ts

export * from "./tiplink";
export * from "./your_protocol"; // Add your protocol here if it's not already in the list

7. Define Action class for given tool

src/actions/your_protocol/custom_action.ts

import { Action } from "../../types/action";
import { SuiAgentKit } from "../../agent";
import { z } from "zod";
import { custom_tool } from "../../tools";

const customAction: Action = {
 name: "CUSTOM_ACTION",
 similes: ["custom tool"],
 description: "Description of what the custom tool does.",
 examples: [
   {
     input: {},
     output: {
       status: "success",
       message: "Custom tool executed successfully",
       data: result,
     },
     explanation: "Custom tool executed successfully",
   },
 ],
 schema: z.object({
   input: z.string(),
 }),
 handler: async (agent: SuiAgentKit, input: Record<string, any>) => {
   const result = await agent.customFunction(input);
   return result;
 },
};

8. Export Action

src/actions/index.ts

import customAction from "./your_protocol/custom_action";

export const ACTIONS = {
   // ... existing actions ...
 CUSTOM_ACTION: customAction,
}

9. Usage Example

Add a code example in the README.md file.

import { SuiAgentKit, createSuiTools } from "sui-agent-kit";

// Initialize with private key and optional RPC URL
const agent = new SuiAgentKit(
 "your_sui_private_key",
 "https://fullnode.mainnet.sui.io:443",
 "your-openai-api-key"
);

const tools = createSuiTools(agent);
const customTool = tools.find(tool => tool.name === "custom_tool");

if (customTool) {
 const result = await customTool._call("your-input");
 console.log(result);
}

// or alternatively
const result = await agent.customFunction("your-input"); // assuming you have implemented `customFunction` method in SuiAgentKit
console.log(result);

Best Practices

  • Implement robust error handling
  • Add security checks for sensitive operations
  • Document your tool’s purpose and usage
  • Write tests for reliability
  • Keep tools focused on single responsibilities

Need Help?

If you face any challenges while implementing your custom tool, you can:

  • Open an issue in the repository.
  • Reach out to the maintainer for assistance.
  • Review existing tools for implementation examples.