Skip to content

Commit bf04fb3

Browse files
authored
addressing some of Sergio's concerns on the secret docs (#518)
1 parent e77c2e8 commit bf04fb3

1 file changed

Lines changed: 78 additions & 8 deletions

File tree

  • app/en/home/build-tools/create-a-tool-with-secrets

‎app/en/home/build-tools/create-a-tool-with-secrets/page.mdx‎

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,102 @@ Build an MCP tool that can read a secret from Context and return a masked confir
2626
<GuideOverview.YouWillLearn>
2727

2828
- How to read secrets from environment and `.env` files securely using a tool's Context.
29+
- How to configure secrets in the Arcade Dashboard.
2930

3031
</GuideOverview.YouWillLearn>
3132
</GuideOverview>
3233

33-
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. In this guide, you'll learn how to use secrets in your custom Arcade tools.
34+
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.
35+
36+
## Why use secrets in your tools?
37+
38+
Secrets enable you to securely deploy a function that requires sensitive information at runtime. And while these can be consumed directly from the runtime environment inside your tool function, this becomes very inconvenient, expensive, hard to maintain, and insecure when deploying at scale.
39+
40+
For example, if your tool requires an API key to use an external service, but only _after_ doing some computationally expensive work, you need to ensure that the API key is present _before_ the computationally expensive work is done. The function below would fail if the API key is not present.
41+
42+
```python
43+
import os
44+
45+
def my_tool(task: str) -> str:
46+
result = expensive_computation(task)
47+
48+
API_KEY = os.getenv("API_KEY")
49+
50+
# The line below will fail if the API key is not present
51+
success = upload_result_to_service(result, API_KEY)
52+
53+
if success:
54+
return "Result uploaded successfully"
55+
else:
56+
return "Failed to upload result"
57+
```
58+
59+
We can work around this by carefully checking for the API key before doing the computationally expensive work, of course, but this is error prone and difficult to maintain, and you may only become aware of the issue after deploying multiple instances of your server.
60+
61+
Arcade provides a way to securely store and access secrets inside your tools in a way that is easy to manage across multiple instances of your servers, and that will prevent the tool from running if the secret is not provided. In this guide, you'll learn how to use secrets in your custom Arcade tools.
3462

3563
<Steps>
3664

3765
## Store your secret
3866

39-
Secrets can be provided via environment variables.
67+
Depending on where you're running your server, you can store your secret in a few different ways.
4068

41-
You can set the environment variable in your terminal like so:
69+
<Tabs items={["Environment Variable", "Arcade Dashboard", "Terminal"]}>
70+
<Tabs.Tab>
71+
You can create a `.env` file in the root of your project and add your secret:
72+
73+
```env filename=".env"
74+
MY_SECRET_KEY="my-secret-value"
75+
```
76+
77+
The project includes a `.env.example` file with the secret key name and example value.
78+
You can rename it to `.env` to start using it.
4279

4380
```bash
44-
export SECRET_KEY="my-secret-value"
81+
mv .env.example .env
4582
```
4683

47-
Or you can create a `.env` file in the root of your project and add your secret:
84+
<Callout type="info">
85+
Using environment variables is ok for local development, but you should use
86+
the Arcade Dashboard for production deployments.
87+
</Callout>
4888

49-
```env filename=".env"
50-
SECRET_KEY="my-secret-value"
89+
</Tabs.Tab>
90+
<Tabs.Tab>
91+
You can store your secret in the Arcade Dashboard by:
92+
93+
- Navigating to the "Secrets" section of the Arcade Dashboard
94+
- Clicking the "Add Secret" button
95+
- Entering the secret ID and value
96+
- Clicking the "Create" button
97+
98+
This will make the secret available to your MCP server, when deployed to Arcade.
99+
100+
</Tabs.Tab>
101+
<Tabs.Tab>
102+
You can set the environment variable in your terminal directly with this command:
103+
104+
```bash
105+
export MY_SECRET_KEY="my-secret-value"
51106
```
52107

108+
<Callout type="info">
109+
Using environment variables is ok for local development, but you should use
110+
the Arcade Dashboard for production deployments.
111+
</Callout>
112+
113+
</Tabs.Tab>
114+
</Tabs>
115+
53116
### Define your tool and access the secret
54117

118+
<Callout type="info">
119+
This is only an illustrative example of how Arcade will ensure that the secret
120+
is present before the tool is executed. In a real world application, you would
121+
use this secret to store sensitive information like API keys, database
122+
credentials, etc, and not to simply print a confirmation string.
123+
</Callout>
124+
55125
In your [MCP Server](/home/build-tools/create-a-mcp-server), create a new tool that uses the secret:
56126

57127
- Use the `requires_secrets` parameter to declare which secrets your tool needs (`"SECRET_KEY"` in this example).
@@ -98,7 +168,7 @@ When your tool is executed, it will return: `"Got SECRET_KEY of length..."`. In
98168

99169
### Environment Variables
100170

101-
```env
171+
```env filename=".env"
102172
SECRET_KEY="supersecret"
103173
```
104174

0 commit comments

Comments
 (0)