Skip to content

Commit ccaf0f8

Browse files
authored
Simplify example in create tool using secrets (#328)
1 parent 82a9709 commit ccaf0f8

File tree

1 file changed

+22
-71
lines changed

1 file changed

+22
-71
lines changed
Lines changed: 22 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,53 @@
11
---
2-
title: "Adding secrets to your tools"
2+
title: "Create a tool with secrets"
33
description: "Learn how to build custom tools that require secrets using Arcade"
44
---
55

66
import { Steps, Tabs } from "nextra/components";
77

8-
# Adding secrets to your tools
8+
# Create a tool with secrets
99

10-
In this guide, you'll learn how to add secrets to your custom tools, using Arcade.
10+
In this guide, you'll learn how to use secrets in your custom Arcade tools.
1111

12-
Secrets are sensitive strings like passwords, api-keys, or other tokens that grant access to a protected resource or API.
13-
14-
In this example, you'll create a tool that reads data from a postgres database.
12+
Secrets are sensitive strings like passwords, api-keys, or other tokens that grant access to a protected resource or API. Although you could use secrets to transfer any static information to your tool, such as a parameter needed to call a remote API.
1513

1614
<Steps>
1715

1816
### Prerequisites
1917

2018
- [Set up Arcade](/home/quickstart)
19+
- [Create a toolkit](/home/build-tools/create-a-toolkit)
2120
- [Understand Tool Context](/home/build-tools/tool-context)
2221

23-
We will be using `sqlalchemy` and `psycopg2-binary` to access a postgres database.
24-
25-
26-
<Tabs items={["uv", "pip"]}>
27-
<Tabs.Tab>
28-
```bash
29-
uv pip install arcade-ai sqlalchemy psycopg2-binary
30-
```
31-
</Tabs.Tab>
32-
<Tabs.Tab>
33-
```bash
34-
pip install arcade-ai sqlalchemy psycopg2-binary
35-
```
36-
</Tabs.Tab>
37-
</Tabs>
38-
39-
### Define your tool
4022

41-
Create a new Python file, e.g., `sql_tools.py`, and import the necessary modules:
42-
```python file=<rootDir>/examples/code/home/build-tools/create-a-tool/imports.py
43-
```
23+
### Set the secret in the Arcade Dashboard
4424

45-
Now, define your tool using the `@tool` decorator and specify the needed secrets with `requires_secrets`, in this case a `DATABASE_CONNECTION_STRING`. In this example, `DATABASE_CONNECTION_STRING`, is meant to be a JDBC-style database connection URL, e.g. `postgres://user:[email protected]/database_name`. Our `DATABASE_CONNECTION_STRING` will contain a username and password in this case, making it very sensitive.
25+
Go to the [Auth > Secrets section](https://api.arcade.dev/dashboard/auth/secrets) in the Arcade Dashboard.
4626

47-
```python file=<rootDir>/examples/code/home/build-tools/create-a-tool/sql-tool.py
48-
```
27+
![An image showing how the Arcade UI allows users to manage secrets](/images/docs/secrets/secrets-dashboard-2.png)
4928

50-
### Use your tool with Arcade
29+
In the top-right corner, click the **+ Add Secret** button and enter:
5130

52-
Now you can use your custom authorized tool with Arcade in your application.
31+
- **ID**: `MY_SECRET_INFO`
32+
- **Secret Value**: `my-secret-value`
33+
- **Description**: optionally add a description
5334

54-
Here's an example of how to use your tool. Note that for this example, the table schema includes a `users` table and a `messages` table, and `messsages` has a foreign key back to `users`.
35+
Click **Submit** to save the secret.
5536

56-
<details>
5737

58-
<summary>See full schema</summary>
38+
### Define your tool and access the secret
5939

60-
```sql file=<rootDir>/examples/code/home/build-tools/create-a-tool/schema.sql
61-
```
62-
</details>
40+
Use the `@tool` decorator to define the secret requirement. The `context` object has a `get_secret` method that you can use to access the secret value.
6341

42+
```python
43+
from arcade_tdk import ToolContext, tool
6444

65-
```ts file=<rootDir>/examples/code/home/build-tools/create-a-tool/app.ts.fake
45+
@tool(requires_secrets=["MY_SECRET_INFO"])
46+
def my_tool_using_secret(context: ToolContext) -> str:
47+
secret_value = context.get_secret("MY_SECRET_INFO")
48+
return f"The secret value is {secret_value}"
6649
```
6750

68-
You'll get a response like this:
69-
70-
```bash
71-
⚙️ Found the following tools:
72-
Sql_DiscoverTables: Discover all the tables in the database
73-
Sql_ExecuteQuery: Execute a query and return the results
74-
Sql_GetTableSchema: Get the schema of a table
75-
76-
[🔍] Discovered the following tables: users, messages
77-
[📜] Schema for users: id: int,name: str,email: str,password_hash: str,created_at: datetime,updated_at: datetime
78-
[📜] Schema for messages: id: int,body: str,user_id: int,created_at: datetime,updated_at: datetime
79-
80-
81-
[❓] Asking: Get the first 10 users's IDs and Names
82-
[📝] SQL statement: SELECT id, name FROM users ORDER BY id LIMIT 10;
83-
[ "(1, 'Sam')", "(3, 'Evan')", "(12, 'Wils')" ]
84-
85-
[❓] Asking: Who has sent the most chat messages?
86-
[📝] SQL statement: SELECT u.name, COUNT(m.id) AS message_count FROM users u JOIN messages m ON u.id = m.user_id GROUP BY u.name ORDER BY message_count DESC LIMIT 1;
87-
[ "('Evan', 218)" ]
88-
```
51+
When your tool is executed, it will return: `"The secret value is my-secret-value"`. In a real world application, you would use this secret to connect to a remote database, API, etc.
8952

9053
</Steps>
91-
92-
### Supplying the Secret
93-
94-
Note how in the example above we never provided a value for `DATABASE_CONNECTION_STRING`. This is because we want the Arcade engine to manage this for us, keeping the sercets that the tool needs seperate from the environment that is exceuting the LLM calls (our application above).
95-
96-
Using Arcade Cloud, after publishing your tool with [`arcade deploy`](https://docs.arcade.dev/home/serve-tools/arcade-deploy), you will see that your tool requires the `DATABASE_CONNECTION_STRING` secret:
97-
98-
![An image showing how the Arcade UI displays that our new SQL tools require a secret now](/images/docs/secrets/secrets-dashboard-1.png)
99-
100-
You can manage your secrets from the [`secrets` section](https://api.arcade.dev/dashboard/auth/secrets) of the authentication section:
101-
102-
![An image showing how the Arcade UI allows users to manage secrets](/images/docs/secrets/secrets-dashboard-2.png)

0 commit comments

Comments
 (0)