# Add a tool

Give an agent a tool: a typed function it can call, which says what it needs and when to use it, and runs on the server.

## Before you start

You need an agent. See [Create an agent](/docs/agents/create-agent.md).

## Add a tool

**Studio**

1. In your project, open **Agents**.

2. Click your agent.

3. Click **Edit**.

4. Under **Add tools** > **Project tools**, click **Add tool**.

5. Enter `get-time` as the **Tool ID**.

6. Enter `Get the current time in a time zone, such as Asia/Tokyo.` as the **Description**.

7. Replace what Studio wrote in **Source code** with:

   ```ts
   import { tool } from "veryfront/tool";

   export default tool({
     id: "get-time",
     description: "Get the current time in a time zone, such as Asia/Tokyo.",
     inputSchema: {
       type: "object",
       properties: {
         timeZone: { type: "string", description: "An IANA time zone, such as Asia/Tokyo" },
       },
       required: ["timeZone"],
       additionalProperties: false,
     },
     execute: async ({ timeZone }) => ({
       time: new Date().toLocaleString("en-GB", { timeZone, dateStyle: "full", timeStyle: "short" }),
     }),
   });
   ```

8. Click **Create tool**. The new tool is ticked for you.

9. Click **Save**.

10. Open **Chat**.

11. Pick your agent under the message box.

12. Write "What time is it in Tokyo?" in the message box.

13. Press Enter. You should now see the reply show the tool call and the time in Tokyo.

To edit or delete a tool later, open **Building blocks** > **Tools**.

**Code**

1. Create the tool. Its `id` is how agents name it:

```ts title="tools/get-time.ts"
import { tool } from "veryfront/tool";

export default tool({
  id: "get-time",
  description: "Get the current time in a time zone, such as Asia/Tokyo.",
  inputSchema: {
    type: "object",
    properties: {
      timeZone: { type: "string", description: "An IANA time zone, such as Asia/Tokyo" },
    },
    required: ["timeZone"],
    additionalProperties: false,
  },
  execute: async ({ timeZone }) => ({
    time: new Date().toLocaleString("en-GB", { timeZone, dateStyle: "full", timeStyle: "short" }),
  }),
});
```

2. Add it to your agent's `tools`. An agent uses only the tools it lists:

```ts title="agents/support.ts" {7}
import { agent } from "veryfront/agent";

export default agent({
  id: "support",
  name: "Support assistant",
  system: "Answer customer questions about orders and refunds. Keep replies to two sentences.",
  tools: { "get-time": true },
});
```

3. Start the project with `npm run dev`.
4. Open **Chat** in Studio.
5. Pick Support assistant.
6. Ask "What time is it in Tokyo?". You should now see it call `get-time` and reply with the time in Tokyo.

### Try it in a sandbox

```playground
step: Create a project | npm create -y veryfront@latest support-desk -- --template ai-agent --yes
step: Create the tool | cd support-desk && mkdir -p tools && printf '%s\n' 'import { tool } from "veryfront/tool";' '' 'export default tool({' '  id: "get-time",' '  description: "Get the current time in a time zone, such as Asia/Tokyo.",' '  inputSchema: {' '    type: "object",' '    properties: {' '      timeZone: { type: "string", description: "An IANA time zone, such as Asia/Tokyo" },' '    },' '    required: ["timeZone"],' '    additionalProperties: false,' '  },' '  execute: async ({ timeZone }) => ({' '    time: new Date().toLocaleString("en-GB", { timeZone, dateStyle: "full", timeStyle: "short" }),' '  }),' '});' > tools/get-time.ts
step: Add it to your agent | mkdir -p agents && printf '%s\n' 'import { agent } from "veryfront/agent";' '' 'export default agent({' '  id: "support",' '  name: "Support assistant",' '  system: "Answer customer questions about orders and refunds. Keep replies to two sentences.",' '  tools: { "get-time": true },' '});' > agents/support.ts
step: Start it | npm run dev
agent: support
ask: What time is it in Tokyo?
tool: get-time
expect: Tokyo
```

**API**

1. Set `PROJECT` to your project's ID ([Find your IDs](/docs/authentication/api-keys.md#find-your-ids)): `export PROJECT=<id>`.
2. Save the tool's source as `get-time.ts`, the file in the **Code** tab's step 1.
3. Create the tool from it with your [API key](/docs/authentication/api-keys.md). `jq` sends the file as `source`:

```bash
jq -n --rawfile source get-time.ts '{id: "get-time", source: $source}' | \
  curl -X POST https://api.veryfront.com/projects/$PROJECT/tools \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @-
```

4. Give it to your agent, here `support`:

```bash
curl -X PUT https://api.veryfront.com/projects/$PROJECT/agents/support \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"tool_ids": ["get-time"]}'
```

5. Get the agent. You should now see `"tool_ids": ["get-time"]`:

```bash
curl https://api.veryfront.com/projects/$PROJECT/agents/support \
  -H "Authorization: Bearer $VERYFRONT_API_KEY"
```

### Try it in a sandbox

```playground
step: Set PROJECT | export PROJECT=$VERYFRONT_PROJECT_ID
step: Before you start: an agent | curl -X POST https://api.veryfront.com/projects/$PROJECT/agents -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{"id": "support", "name": "Support assistant", "system_prompt": "Answer customer questions about orders and refunds. Keep replies to two sentences."}'
step: Save the tool's source | printf '%s\n' 'import { tool } from "veryfront/tool";' '' 'export default tool({' '  id: "get-time",' '  description: "Get the current time in a time zone, such as Asia/Tokyo.",' '  inputSchema: {' '    type: "object",' '    properties: {' '      timeZone: { type: "string", description: "An IANA time zone, such as Asia/Tokyo" },' '    },' '    required: ["timeZone"],' '    additionalProperties: false,' '  },' '  execute: async ({ timeZone }) => ({' '    time: new Date().toLocaleString("en-GB", { timeZone, dateStyle: "full", timeStyle: "short" }),' '  }),' '});' > get-time.ts
step: Create the tool | jq -n --rawfile source get-time.ts '{id: "get-time", source: $source}' | curl -X POST https://api.veryfront.com/projects/$PROJECT/tools -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" --data-binary @-
step: Give it to your agent | curl -X PUT https://api.veryfront.com/projects/$PROJECT/agents/support -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{"tool_ids": ["get-time"]}'
check: curl https://api.veryfront.com/projects/$PROJECT/agents/support -H "Authorization: Bearer $VERYFRONT_API_KEY"
expect: "tool_ids":["get-time"]
```

## With MCP

`my-project` is your project's reference: its slug or its ID. Your agent here is `support`; `veryfront__list_agents` lists them.

1. Call `veryfront__create_tool` with `"project_reference": "my-project"` and `"id": "get-time"`. Set `source` to the file in the **Code** tab's step 1. The tool's description comes from the file: a `description` sent with `source` is ignored.
2. Call `veryfront__update_agent` with `"agent_id": "support"`, `"project_reference": "my-project"` and `"tool_ids": ["get-time"]`.
3. Call `veryfront__get_agent_source` with `"agent_id": "support"` and `"project_reference": "my-project"`. It worked when `tool_ids` is `["get-time"]`.

## Next steps

* [Add a skill](/docs/agents/skills.md)
* [Connect an app](/docs/integrations/connect-app.md)
