Skip to main content

How to Add Your Own Tool

Extending the Solana Agent Kit with custom tools allows you to add specialized functionalities tailored to your needs. This guide walks you through 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 SolanaAgentKit
  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

Implementation Guide

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

3. Add Supporting Functions to SolanaAgentKit

src/agent/index.ts

4. Implement the Langchain Tool Class

src/langchain/your_protocol/custom_tool.ts

5. Export Langchain Tool

src/langchain/your_protocol/index.ts

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

src/langchain/index.ts

7. Define Action class for given tool

src/actions/your_protocol/custom_action.ts

8. Export Action

src/actions/index.ts

9. Usage Example

Add a code example in the README.md file.

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

Example: Token Price Fetching Tool

Here’s a complete example of implementing a tool to fetch token prices:
src/tools/fetch_token_price.ts
Add the supporting function to SolanaAgentKit:
src/agent/index.ts
Add Action for given tool:
src/actions/fetch_token_price.ts
Then it can be used as such:

Need Help?

If you encounter any issues while implementing your custom tool:
  • Open an issue in the repository
  • Contact the maintainer
  • Check existing tools for implementation examples