How to Add Tools
Last updated
Last updated
LumoKit's power lies in its extensible tools system, allowing you to integrate new on-chain actions and research capabilities. Adding a new tool involves implementing its logic in the backend, registering it within the core system and chat controller, and then making it available and configurable in the frontend. This guide will walk you through the comprehensive process.
Adding a new tool to LumoKit generally follows these steps:
Plan Your Tool: Define its purpose, functionality, required inputs, and a unique identifier.
Backend Implementation & Registration: Create the core logic for your tool and integrate it into the LumoKit backend's tool system and chat controller.
Frontend Configuration: Define the tool's metadata and add its icon to the LumoKit frontend.
Thorough Testing: Ensure the tool works as expected end-to-end.
Let's break down each step:
LumoKit
Backend)The backend is where your tool's core functionality resides and where it's made available to the AI agent.
A. Create Your Tool Class
File Location:
Create a new Python file in the src/tools/
directory (e.g., src/tools/my_new_tool.py
).
Alternatively, you can add your tool to an existing relevant file within src/tools/
.
Define Input Schema (Pydantic):
If your tool requires specific input parameters, define a Pydantic BaseModel
for its arguments. This ensures data validation.
Implement the Tool Class:
Your tool class must inherit from langchain.tools.BaseTool
.
Define class variables:
name
: A unique string identifier for the tool (e.g., "my_new_tool_identifier"
). This is crucial and will be used by the AI agent and for linking with the frontend.
description
: A clear explanation of what the tool does, its inputs, and expected outputs. This description is vital for the LLM to understand when and how to use your tool.
args_schema
: Link to your Pydantic input schema class.
Implement the _arun
asynchronous method for the tool's main logic.
Implement a _run
synchronous method (often, this can just state that only async is supported if that's the case).
B. Register Your Tool in the Backend System
This involves making your tool recognizable by the LumoKit system.
Export from src/tools/__init__.py
:
Open src/tools/__init__.py
.
Import your new tool class.
Add your tool class name to the __all__
list. This makes it easier to import tools from the tools
module.
LLM Tool Descriptions: The backend README previously mentioned adding tool descriptions to TOOL_DESCRIPTIONS
in init.py
(or a similar central mapping). This dictionary helps the LLM understand available tools. Ensure your tool's name
and its AI-facing description
are correctly registered here if this pattern is used in your project version. This is often vital for the AI agent's tool selection process.
Integrate into Chat Controller (src/api/chat/controllers.py
): This is a critical step to make your tool available to the chat interface and the AI agent processing chat requests.
Import your tool: Add an import statement for your new tool class at the top of src/api/chat/controllers.py
alongside other tool imports.
Instantiate your tool: Within the relevant function or method where other tools are initialized (often in the main chat request handler), create an instance of your tool.
Add to available_tools
dictionary: Add your instantiated tool to the available_tools
dictionary. The key for the dictionary entry must be the string identifier of your tool (i.e., MyNewTool.name
, which is "my_new_tool_identifier"
in our example). This dictionary is used to dynamically provide tools to the AI agent based on user selections or requests.
Python
lumokit-frontend
)Once the backend logic is in place and registered, you need to make the tool accessible and configurable from the LumoKit frontend.
A. Define the Tool in data/tools.json
Locate the File:
Open the data/tools.json
file in the root of your lumokit-frontend
project.
Add a New Tool Entry:
Add a new JSON object to the array in data/tools.json
for your tool.
Key Fields Explained:
icon_url
: Path to the tool's icon (relative to /public
).
default_status
: Boolean (true
if enabled by default, false
otherwise).
tool_identifier
: String. Critical field. Must exactly match the name
class variable in your backend tool class (e.g., "my_new_tool_identifier"
) AND the key used in the available_tools
dictionary in src/api/chat/controllers.py
.
name
: String. User-friendly display name in the UI.
category
: String. Groups tools in "Tools & Settings".
description
: String. Short (10-15 words) user-facing explanation.
read_more
: String. Optional URL to detailed documentation.
B. Add the Tool's Icon
Create/Obtain Icon: SVG or PNG recommended.
Place Icon: In /public/
or a subdirectory (e.g., /public/icons/
) in lumokit-frontend
.
Ensure icon_url
in data/tools.json
points to it correctly.
Thorough testing is essential.
Backend Tests: Write unit/integration tests for your tool's logic.
Restart Services: Rebuild and restart both the LumoKit backend and frontend development server to load all changes.
End-to-End Frontend UI Testing:
Open LumoKit in your browser.
Navigate to "Tools & Settings" and verify your tool appears correctly.
Enable your tool.
Use the chat interface with prompts designed to trigger your new tool. Check:
Correct tool selection by the AI.
Proper argument passing.
Successful execution in the backend (check logs).
Correct results/display in the frontend.
Iterate: Debug and refine as necessary. Pay close attention to logs and console outputs.
Consistent Naming: Use lowercase with underscores for the shared tool_identifier
/name
.
Clear Descriptions:
Backend description
(in tool class): Explicit for the LLM (capabilities, inputs, use cases).
Frontend description
(in tools.json
): Concise and user-friendly.
Robust Error Handling: Implement in your backend tool's _arun
method.
Performance: Be mindful of tool execution time. Limit default tools.
Security: Adhere to security best practices if handling sensitive data or actions.
Documentation: Use read_more
for complex tools.
By following these updated and detailed steps, you can effectively extend LumoKit's capabilities by adding new, powerful tools tailored to your needs within the Solana ecosystem!