Testing the Tool Run feature
Learn how to use the Tool Run feature.
Tool Test Run
Tool Run is a scalable way to build your tool and test it in real-time. With this feature, it's simple to debug problems and create a tool that is both scalable and performant at the same time.
What is required to run a tool?
First, you need to have your tool. If you don't know how to create a tool or still have questions about this subject, you can read the tools page Tools.
Next, you need to write your test. Let's take the following tool as an example:
_14tools:_14 - get_address:_14 name: "Get Address"_14 source: _14 path: "tools/get_address"_14 entrypoint: "main.GetAddressWithAuth"_14 path_test: "test_definition.yaml"_14 description: "Function to get the address from the postal code"_14 parameters:_14 - cep:_14 description: "postal code of a place"_14 type: "string"_14 required: true_14 contact_field: true
Notice that my tool has a specific parameter called "cep". Therefore, the expected input for this tool to be processed is a postal code. This way, I'll create a test file for this tool in the same directory as the specific tool code.
My test file would look something like this:
test_definition.yaml:
_10tests:_10 test_1:_10 parameters:_10 cep: "57160000"_10 test_2:_10 parameters:_10 cep: "57038-635"_10 test_3:_10 parameters:_10 cep: "57160-000"
Make sure that path_test corresponds to the correct path of your tool test.
It's also important to note that if your tool uses any external libraries, you must include a requirements.txt file in the same directory as your tool code. This file should list all the dependencies needed for your tool to run properly.
How to run a test
Following the previous steps, we can now run a test, but how?
The command works as follows:
_10weni run [agent_definition_file] [agent_key] [tool_key] [-f FILE] [-v]
A practical example of the command considering the agent definition that was mentioned above would be:
_10weni run agent_definition.yaml cep_agent get_address
Result:
An error occurred while loading the image https://github.com/vtexdocs/dev-portal-content/main/docs/guides/Weni-by-VTEX/CLI/assets/run-no-v.png
There is also a variation of this command in case you need to include ways to debug the code. You can add the -v argument at the end of the command to get more detailed logs of your tool, like this:
_10weni run agent_definition.yaml cep_agent get_address -v
Choosing a test file
- Use
-f/--fileto specify a test definition YAML located in the tool folder. - If
-fis omitted, the CLI looks fortest_definition.yamlin the tool'ssource.pathdirectory. - You can also set
source.path_testin your agent definition to point to a custom test file; when present, the CLI uses that path by default.
Credentials and globals discovery
For development, the CLI will read optional files from the tool directory:
.envfor credentials exposed to your tool viacontext.credentials.globalsfor key/value pairs exposed viacontext.globals
Both files follow a simple KEY=VALUE per line format.
Result:
An error occurred while loading the image https://github.com/vtexdocs/dev-portal-content/main/docs/guides/Weni-by-VTEX/CLI/assets/run-with-v.png
Running Tools That Require Credentials
When your tool needs to interact with external services that require authentication, you'll need to provide credentials during testing. The official method for handling credentials with weni run is using your agent definition and a .env file.
Using Agent Definition and .env File
This approach mimics how credentials work in production, making your local development environment consistent with deployment.
First, update your agent definition to include credentials:
_27agents:_27 cep_agent:_27 credentials:_27 api_key:_27 label: "API Key"_27 placeholder: "Api Key"_27 name: "CEP Agent"_27 description: "Weni's CEP agent"_27 instructions:_27 - "You are an expert in providing addresses to the user based on a postal code provided by the user"_27 - "The user will send a ZIP code (postal code) and you must provide the address corresponding to this code."_27 guardrails:_27 - "Don't talk about politics, religion or any other sensitive topic. Keep it neutral."_27 tools:_27 - get_address:_27 name: "Get Address"_27 source: _27 path: "tools/get_address"_27 entrypoint: "main.GetAddressWithAuth"_27 path_test: "test_definition.yaml"_27 description: "Function to get the address from the postal code"_27 parameters:_27 - cep:_27 description: "postal code of a place"_27 type: "string"_27 required: true_27 contact_field: true
Then, create a .env file inside the tool folder (e.g., tools/get_address/.env) with the actual credential values:
_10api_key=your_actual_api_key_here
Now you can run your tool test with credentials using the same command:
_10weni run agent_definition.yaml cep_agent get_address
The CLI will automatically pick up the credentials from the tool folder .env file and make them available to your tool during execution.
Accessing Credentials in Your Tool Code
Your tool code should access credentials through the Context object as shown below:
_27from weni import Tool_27from weni.context import Context_27from weni.responses import TextResponse_27import requests_27_27class GetAddressWithAuth(Tool):_27 def execute(self, context: Context) -> TextResponse:_27 # Get parameters from context_27 cep = context.parameters.get("cep", "")_27 _27 # Get credentials from context_27 api_key = context.credentials.get("api_key")_27 _27 address_response = self.get_address_by_cep(cep=cep, api_key=api_key)_27 _27 return TextResponse(data=address_response)_27 _27 def get_address_by_cep(self, cep, api_key):_27 url = f"https://viacep.com.br/ws/{cep}/json/"_27 _27 headers = {_27 "Authorization": f"Bearer {api_key}"_27 }_27 _27 response = requests.get(url, headers=headers)_27 _27 return response.json()
The key line is api_key = context.credentials.get("api_key"), which retrieves the credential value that was defined in your agent definition and stored in your tool folder .env file.
Important: Never hardcode credentials in your tool code. Always access them through the Context object to ensure your code remains secure and works consistently across different environments.