|
1 | 1 | --- |
2 | | -title: "Adding secrets to your tools" |
| 2 | +title: "Create a tool with secrets" |
3 | 3 | description: "Learn how to build custom tools that require secrets using Arcade" |
4 | 4 | --- |
5 | 5 |
|
6 | 6 | import { Steps, Tabs } from "nextra/components"; |
7 | 7 |
|
8 | | -# Adding secrets to your tools |
| 8 | +# Create a tool with secrets |
9 | 9 |
|
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. |
11 | 11 |
|
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. |
15 | 13 |
|
16 | 14 | <Steps> |
17 | 15 |
|
18 | 16 | ### Prerequisites |
19 | 17 |
|
20 | 18 | - [Set up Arcade](/home/quickstart) |
| 19 | +- [Create a toolkit](/home/build-tools/create-a-toolkit) |
21 | 20 | - [Understand Tool Context](/home/build-tools/tool-context) |
22 | 21 |
|
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 |
40 | 22 |
|
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 |
44 | 24 |
|
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. |
46 | 26 |
|
47 | | -```python file=<rootDir>/examples/code/home/build-tools/create-a-tool/sql-tool.py |
48 | | -``` |
| 27 | + |
49 | 28 |
|
50 | | -### Use your tool with Arcade |
| 29 | +In the top-right corner, click the **+ Add Secret** button and enter: |
51 | 30 |
|
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 |
53 | 34 |
|
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. |
55 | 36 |
|
56 | | -<details> |
57 | 37 |
|
58 | | -<summary>See full schema</summary> |
| 38 | +### Define your tool and access the secret |
59 | 39 |
|
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. |
63 | 41 |
|
| 42 | +```python |
| 43 | +from arcade_tdk import ToolContext, tool |
64 | 44 |
|
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}" |
66 | 49 | ``` |
67 | 50 |
|
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. |
89 | 52 |
|
90 | 53 | </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 | | - |
99 | | -
|
100 | | -You can manage your secrets from the [`secrets` section](https://api.arcade.dev/dashboard/auth/secrets) of the authentication section: |
101 | | -
|
102 | | - |
0 commit comments