|
| 1 | +--- |
| 2 | +title: "Create JavaScript Plugins" |
| 3 | +description: "Instructions for Creating, Publishing and Registering your Hedera Agent Kit Plugin" |
| 4 | +--- |
| 5 | + |
| 6 | +## Creating a Plugin |
| 7 | + |
| 8 | +### Plugin Interface |
| 9 | + |
| 10 | +Every plugin must implement the Plugin interface: |
| 11 | + |
| 12 | +```typescript |
| 13 | +export interface Plugin { |
| 14 | + name: string; |
| 15 | + version?: string; |
| 16 | + description?: string; |
| 17 | + tools: (context: Context) => Tool[]; |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +### Tool Interface |
| 22 | + |
| 23 | +Each tool must implement the Tool interface: |
| 24 | + |
| 25 | +```typescript |
| 26 | +export type Tool = { |
| 27 | + method: string; |
| 28 | + name: string; |
| 29 | + description: string; |
| 30 | + parameters: z.ZodObject<any, any>; |
| 31 | + execute: (client: Client, context: Context, params: any) => Promise<any>; |
| 32 | + // transactionToolOutputParser and untypedQueryOutputParser can be used. If required, define a custom parser |
| 33 | + outputParser?: (rawOutput: string) => { raw: any; humanMessage: string }; |
| 34 | +}; |
| 35 | +``` |
| 36 | + |
| 37 | +See [typescript/src/shared/tools.ts](../typescript/src/shared/tools.ts) for the full definition. |
| 38 | + |
| 39 | +## Step-by-Step Guide |
| 40 | + |
| 41 | +### Step 1: Create Plugin Directory Structure |
| 42 | + |
| 43 | +``` |
| 44 | + my-custom-plugin/ |
| 45 | + ├── index.ts # Plugin definition and exports |
| 46 | + ├── tools/ |
| 47 | + │ └── my-service/ |
| 48 | + │ └── my-tool.ts # Individual tool implementation |
| 49 | +``` |
| 50 | + |
| 51 | +### Step 2: Implement Your Tool |
| 52 | + |
| 53 | +Create your tool file (e.g., tools/my-service/my-tool.ts): |
| 54 | + |
| 55 | +```typescript |
| 56 | +import { z } from "zod"; |
| 57 | +import { Context, Tool, handleTransaction } from "hedera-agent-kit"; |
| 58 | +import { Client, PrivateKey, AccountId } from "@hashgraph/sdk"; |
| 59 | +import dotenv from "dotenv"; |
| 60 | + |
| 61 | +// Load environment variables |
| 62 | +dotenv.config(); |
| 63 | + |
| 64 | +// Define parameter schema |
| 65 | +const myToolParameters = (context: Context = {}) => |
| 66 | + z.object({ |
| 67 | + requiredParam: z.string().describe("Description of required parameter"), |
| 68 | + optionalParam: z |
| 69 | + .string() |
| 70 | + .optional() |
| 71 | + .describe("Description of optional parameter"), |
| 72 | + }); |
| 73 | + |
| 74 | +// Create prompt function |
| 75 | +const myToolPrompt = (context: Context = {}) => { |
| 76 | + return ` |
| 77 | + This tool performs a specific operation. |
| 78 | +
|
| 79 | + Parameters: |
| 80 | + - requiredParam (string, required): Description |
| 81 | + - optionalParam (string, optional): Description |
| 82 | + `; |
| 83 | +}; |
| 84 | + |
| 85 | +// Implement tool logic |
| 86 | +const myToolExecute = async ( |
| 87 | + client: Client, |
| 88 | + context: Context, |
| 89 | + params: z.infer<ReturnType<typeof myToolParameters>>, |
| 90 | +) => { |
| 91 | + try { |
| 92 | + // Your implementation here |
| 93 | + const result = await someHederaOperation(params); |
| 94 | + return result; |
| 95 | + } catch (error) { |
| 96 | + if (error instanceof Error) { |
| 97 | + return error.message; |
| 98 | + } |
| 99 | + return "Operation failed"; |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | +export const MY_TOOL = "my_tool"; |
| 104 | + |
| 105 | +const tool = (context: Context): Tool => ({ |
| 106 | + method: MY_TOOL, |
| 107 | + name: "My Custom Tool", |
| 108 | + description: myToolPrompt(context), |
| 109 | + parameters: myToolParameters(context), |
| 110 | + execute: myToolExecute, |
| 111 | +}); |
| 112 | + |
| 113 | +export default tool; |
| 114 | +``` |
| 115 | + |
| 116 | +### Step 3: Create Plugin Definition |
| 117 | + |
| 118 | +Create your plugin index file (index.ts): |
| 119 | + |
| 120 | +```typescript |
| 121 | + import { Context } from '@/shared'; |
| 122 | + import { Plugin } from '@/shared/plugin'; |
| 123 | + import myTool, { MY_TOOL } from './tools/my-service/my-tool'; |
| 124 | + |
| 125 | + export const myCustomPlugin: Plugin = { |
| 126 | + name: 'my-custom-plugin', |
| 127 | + version: '1.0.0', |
| 128 | + description: 'A plugin for custom functionality', |
| 129 | + tools: (context: Context) => { |
| 130 | + return [myTool(context)]; |
| 131 | + }, |
| 132 | + }; |
| 133 | + |
| 134 | + export const myCustomPluginToolNames = { |
| 135 | + MY_TOOL, |
| 136 | + } as const; |
| 137 | + |
| 138 | + export default { myCustomPlugin, myCustomPluginToolNames }; |
| 139 | +``` |
| 140 | + |
| 141 | +### Best Practices |
| 142 | + |
| 143 | +**Parameter Validation** |
| 144 | + |
| 145 | +- Use Zod schemas for robust input validation |
| 146 | +- Provide clear descriptions for all parameters |
| 147 | +- Mark required vs optional parameters appropriately |
| 148 | + |
| 149 | +**Tool Organization** |
| 150 | + |
| 151 | +- Group related tools by service type |
| 152 | +- Use consistent naming conventions |
| 153 | +- Follow the established directory structure |
| 154 | + |
| 155 | +**Transaction Handling** |
| 156 | + |
| 157 | +- Use `handleTransaction()` to facilitate human-in-the-loop and autonomous execution flows |
| 158 | +- Respect the AgentMode (`AUTONOMOUS` vs `RETURN_BYTES`) |
| 159 | +- Implement proper transaction building patterns |
| 160 | + |
| 161 | +### Tool Output Parsing |
| 162 | + |
| 163 | +The Hedera Agent Kit tools return a structured JSON output that needs to be parsed to be useful for the agent and the user. |
| 164 | + |
| 165 | +**LangChain v0.3 (Classic)** |
| 166 | +In the classic approach, the agent handles the tool output automatically, but you may need to parse it if you are handling tool calls manually. |
| 167 | + |
| 168 | +**LangChain v1 (New)** |
| 169 | +In LangChain v1, we use the `ResponseParserService` to handle tool outputs. This service normalizes the output from both transaction and query tools into a consistent format: |
| 170 | + |
| 171 | +```typescript |
| 172 | +{ |
| 173 | + raw: any; // The raw data returned by the tool (e.g., transaction receipt, query result) |
| 174 | + humanMessage: string; // A human-readable message describing the result |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | +This allows you to easily display a user-friendly message while still having access to the raw data for further processing. |
| 179 | + |
| 180 | +### Using Your Custom Plugin |
| 181 | + |
| 182 | +**LangChain v0.3 (Classic)** |
| 183 | + |
| 184 | +```typescript |
| 185 | +import { HederaLangchainToolkit } from "hedera-agent-kit"; |
| 186 | +import { |
| 187 | + myCustomPlugin, |
| 188 | + myCustomPluginToolNames, |
| 189 | +} from "./plugins/my-custom-plugin"; |
| 190 | + |
| 191 | +const toolkit = new HederaLangchainToolkit({ |
| 192 | + client, |
| 193 | + configuration: { |
| 194 | + tools: [myCustomPluginToolNames.MY_TOOL], |
| 195 | + plugins: [myCustomPlugin], |
| 196 | + context: { |
| 197 | + mode: AgentMode.AUTONOMOUS, |
| 198 | + }, |
| 199 | + }, |
| 200 | +}); |
| 201 | +``` |
| 202 | + |
| 203 | +**LangChain v1 (New)** |
| 204 | + |
| 205 | +```typescript |
| 206 | +import { HederaLangchainToolkit, ResponseParserService } from "hedera-agent-kit"; |
| 207 | +import { |
| 208 | + myCustomPlugin, |
| 209 | + myCustomPluginToolNames, |
| 210 | +} from "./plugins/my-custom-plugin"; |
| 211 | + |
| 212 | +// Initialize toolkit |
| 213 | +const toolkit = new HederaLangchainToolkit({ |
| 214 | + client, |
| 215 | + configuration: { |
| 216 | + tools: [myCustomPluginToolNames.MY_TOOL], |
| 217 | + plugins: [myCustomPlugin], |
| 218 | + context: { |
| 219 | + mode: AgentMode.AUTONOMOUS, |
| 220 | + }, |
| 221 | + }, |
| 222 | +}); |
| 223 | + |
| 224 | +// Initialize response parser |
| 225 | +const responseParsingService = new ResponseParserService(toolkit.getTools()); |
| 226 | + |
| 227 | +// ... inside your agent loop ... |
| 228 | +const response = await agent.invoke({ messages: [/* ... */] }); |
| 229 | + |
| 230 | +// Parse tool outputs |
| 231 | +const parsedToolData = responseParsingService.parseNewToolMessages(response); |
| 232 | +const toolCall = parsedToolData[0]; // assuming only one tool was called |
| 233 | + |
| 234 | +if (toolCall) { |
| 235 | + console.log('Human Message:', toolCall.parsedData.humanMessage); |
| 236 | + console.log('Raw Data:', toolCall.parsedData.raw); |
| 237 | +} |
| 238 | +``` |
| 239 | + |
| 240 | +#### Examples and References |
| 241 | + |
| 242 | +- See existing core plugins in typescript/src/plugins/core-\*-plugin/ |
| 243 | +- Follow the patterns established in tools like [transfer-hbar.ts](../typescript/src/plugins/core-account-plugin/tools/account/transfer-hbar.ts) |
| 244 | +- See [typescript/examples/langchain-v1/tool-calling-agent.ts](../typescript/examples/langchain/tool-calling-agent.ts) for usage examples |
| 245 | + |
| 246 | +## Publish and Register Your Plugin |
| 247 | + |
| 248 | +To create a plugin to be use with the Hedera Agent Kit, you will need to create a plugin in your own repository, publish an npm package, and provide a description of the functionality included in that plugin, as well as the required and optional parameters. |
| 249 | + |
| 250 | +Once you have a repository, published npm package, and a README with a description of the functionality included in that plugin in your plugin's repo, as well, add it to the Hedera Agent Kit by forking and opening a Pull Request that includes: |
| 251 | + |
| 252 | +1. Include the plugin as a bullet point under the **Third Party Plugin** section in the [README.md in the hedera-agent-kit-js](https://github.com/hashgraph/hedera-agent-kit-js/blob/main/README.md#third-party-plugins). |
| 253 | + * Include the name, a brief description, and a link to the repository with the README, as well the URL linked to the published npm package. |
| 254 | +2. If you would like to include your plugin functionality in the Hedera plugin built for ElizaOS simply make a PR to [add your plugin name to the `plugins` array in the Hedera ElizaOS plugin](https://github.com/elizaos-plugins/plugin-hedera/blob/1.x/src/adapter-plugin/plugin.ts#L72) where the configuration is initiated. The hedera-agent-kit adaptor architecture means your plugin functionality will be usable with no additional configuration needed. |
| 255 | + |
| 256 | +Please also reach out in the Hedera Discord in the **Support > developer-help-desk** channel or create an Issue in this repository for help building, publishing, and promoting your plugin. |
| 257 | + |
| 258 | +### Plugin README Template |
| 259 | + |
| 260 | +```` |
| 261 | +## Plugin Name |
| 262 | +This plugin was built by <?> for the <project, platform, etc>. It was built to enable <who?> to <do what?> |
| 263 | +
|
| 264 | +_Feel free to include a description of your project and how it can be used with the Hedera Agent Kit. |
| 265 | +
|
| 266 | +### Installation |
| 267 | +
|
| 268 | +```bash |
| 269 | +npm install <plugin-name> |
| 270 | +``` |
| 271 | +
|
| 272 | +### Usage |
| 273 | +
|
| 274 | +```javascript |
| 275 | +import { myPlugin } from '<plugin-name>'; |
| 276 | +``` |
| 277 | +
|
| 278 | +```javascript |
| 279 | + const hederaAgentToolkit = new HederaLangchainToolkit({ |
| 280 | + client, |
| 281 | + configuration: { |
| 282 | + context: { |
| 283 | + mode: AgentMode.AUTONOMOUS, |
| 284 | + }, |
| 285 | + plugins: [coreTokenPlugin, coreAccountPlugin, coreConsensusPlugin, coreQueriesPlugin, myPlugin], |
| 286 | + }, |
| 287 | + }); |
| 288 | +``` |
| 289 | +
|
| 290 | +### Functionality |
| 291 | +Describe the different tools or individual pieces of functionality included in this plugin, and how to use them. |
| 292 | +
|
| 293 | +**Plugin Name** |
| 294 | +_High level description of the plugin_ |
| 295 | +
|
| 296 | +| Tool Name | Description |Usage | |
| 297 | +| ----------------------------------------------- | -------------------------------------------------- |--------------------------------------------------------- | |
| 298 | +| `YOUR_PLUGIN_TOOL_NAME`| What it does | How to use. Include a list of parameters and their descriptions| |
| 299 | +
|
| 300 | +```` |
| 301 | + |
| 302 | +## Resources |
| 303 | + |
| 304 | +- **GitHub**: https://github.com/hashgraph/hedera-agent-kit-js |
| 305 | +- **Discord**: https://hedera.com/discord |
0 commit comments