How to Add Tools

🛠️ How to Add New Tools to LumoKit (v1.0.0)
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.
🌟 Overview of the 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:
Step 1: Backend Implementation & Registration (LumoKit Backend)
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
BaseModelfor 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
_arunasynchronous method for the tool's main logic.Implement a
_runsynchronous 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 thetoolsmodule.
LLM Tool Descriptions: The backend README previously mentioned adding tool descriptions to
TOOL_DESCRIPTIONSininit.py(or a similar central mapping). This dictionary helps the LLM understand available tools. Ensure your tool'snameand its AI-facingdescriptionare 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.pyalongside 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_toolsdictionary: Add your instantiated tool to theavailable_toolsdictionary. 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
Step 2: Frontend Configuration (lumokit-frontend)
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.jsonfile in the root of yourlumokit-frontendproject.
Add a New Tool Entry:
Add a new JSON object to the array in
data/tools.jsonfor your tool.
Key Fields Explained:
icon_url: Path to the tool's icon (relative to/public).default_status: Boolean (trueif enabled by default,falseotherwise).tool_identifier: String. Critical field. Must exactly match thenameclass variable in your backend tool class (e.g.,"my_new_tool_identifier") AND the key used in theavailable_toolsdictionary insrc/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/) inlumokit-frontend.Ensure
icon_urlindata/tools.jsonpoints to it correctly.
Step 3: Testing Your New Tool
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.
✨ Best Practices & Important Considerations
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(intools.json): Concise and user-friendly.
Robust Error Handling: Implement in your backend tool's
_arunmethod.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_morefor 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!
Last updated