# Add knowledge

Add a document to a project as knowledge, so an agent can search it and answer from it.

## Before you start

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

## Add knowledge

**Studio**

1. In your project, open **Building blocks** > **Knowledge**.

2. Click **Add Knowledge** > **New Markdown file**, at the top right.

3. Enter `returns-policy.md` as the **Name**.

4. Click **Create**.

5. Write the document:

   ```text
   Returns are accepted within 45 days of delivery. Refunds are issued to the original payment method within 5 business days of approval.
   ```

6. Click **Save**.

7. Open **Agents**.

8. Click your agent.

9. Click **Edit**.

10. Under **Add knowledge**, pick **All**.

11. Click **Save**.

12. Open **Chat**.

13. Pick your agent under the message box.

14. Write "How many days do I have to return an item?" in the message box.

15. Press Enter. You should now see the reply say 45 days.

**Code**

1. Add the document under `knowledge/`:

```markdown title="knowledge/returns-policy.md"
---
title: Returns policy
description: How long customers have to return an item, and when refunds arrive.
---

Returns are accepted within 45 days of delivery.
Refunds are issued to the original payment method within 5 business days of approval.
```

2. Give your agent a search tool for it:

```ts title="tools/search_knowledge.ts"
import { createSearchKnowledgeTool } from "veryfront/knowledge";

export default createSearchKnowledgeTool({
  id: "search_knowledge",
  description: "Search the project's returns and refunds policy.",
});
```

3. Add the tool to your agent's `tools`, and tell it to read the document it finds:

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

export default agent({
  id: "support",
  name: "Support assistant",
  system: "Answer customer questions about orders and refunds from the project's knowledge. Find the document with search_knowledge, then read it by its path before you answer. Keep replies to two sentences.",
  tools: { search_knowledge: true },
});
```

4. Start the project with `npm run dev`.
5. Open **Chat** in Studio.
6. Pick Support assistant.
7. Ask "How many days do I have to return an item?". You should now see it call `search_knowledge` and reply 45 days.

### Try it in a sandbox

```playground
step: Create a project | npm create -y veryfront@latest support-desk -- --template ai-agent --yes
step: Add the document | cd support-desk && mkdir -p knowledge && printf '%s\n' '---' 'title: Returns policy' 'description: How long customers have to return an item, and when refunds arrive.' '---' '' 'Returns are accepted within 45 days of delivery.' 'Refunds are issued to the original payment method within 5 business days of approval.' > knowledge/returns-policy.md
step: Add the search tool | mkdir -p tools && printf '%s\n' 'import { createSearchKnowledgeTool } from "veryfront/knowledge";' '' 'export default createSearchKnowledgeTool({' '  id: "search_knowledge",' '  description: "Search the project'\''s returns and refunds policy.",' '});' > tools/search_knowledge.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 from the project'\''s knowledge. Find the document with search_knowledge, then read it by its path before you answer. Keep replies to two sentences.",' '  tools: { search_knowledge: true },' '});' > agents/support.ts
step: Start it | npm run dev
agent: support
ask: How many days do I have to return an item?
tool: search_knowledge
expect: 45 days
```

**API**

1. Set `PROJECT` to your project's ID ([Find your IDs](/docs/authentication/api-keys.md#find-your-ids)): `export PROJECT=<id>`.
2. Add the document under `knowledge/` with your [API key](/docs/authentication/api-keys.md). The `/` in its path is written `%2F`:

```bash
curl -X PUT https://api.veryfront.com/projects/$PROJECT/files/knowledge%2Freturns-policy.md \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  --data-binary @- <<'EOF'
{
  "content": "---\ntitle: Returns policy\ndescription: How long customers have to return an item, and when refunds arrive.\n---\n\nReturns are accepted within 45 days of delivery.\nRefunds are issued to the original payment method within 5 business days of approval.\n"
}
EOF
```

3. Let your agent, here `support`, use the project's knowledge:

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

4. Search the knowledge. You should now see `knowledge/returns-policy.md` in `data`, with the title `Returns policy`:

```bash
curl -X POST https://api.veryfront.com/projects/$PROJECT/knowledge/search \
  -H "Authorization: Bearer $VERYFRONT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"query": "returns"}'
```

### 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: Add the document | curl -X PUT https://api.veryfront.com/projects/$PROJECT/files/knowledge%2Freturns-policy.md -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{"content": "---\ntitle: Returns policy\ndescription: How long customers have to return an item, and when refunds arrive.\n---\n\nReturns are accepted within 45 days of delivery.\nRefunds are issued to the original payment method within 5 business days of approval.\n"}'
step: Let your agent use it | curl -X PUT https://api.veryfront.com/projects/$PROJECT/agents/support -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{"knowledge": true}'
check: curl -X POST https://api.veryfront.com/projects/$PROJECT/knowledge/search -H "Authorization: Bearer $VERYFRONT_API_KEY" -H "Content-Type: application/json" -d '{"query": "returns"}'
expect: "path":"knowledge/returns-policy.md"
```

## 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_file` for the document:

```json
{
  "project_reference": "my-project",
  "path": "knowledge/returns-policy.md",
  "content": "---\ntitle: Returns policy\ndescription: How long customers have to return an item, and when refunds arrive.\n---\n\nReturns are accepted within 45 days of delivery.\nRefunds are issued to the original payment method within 5 business days of approval.\n"
}
```

2. Call `veryfront__update_agent` with `"agent_id": "support"`, `"project_reference": "my-project"` and `"knowledge": true`.
3. Call `veryfront__search_knowledge` with `"project_reference": "my-project"` and `"query": "returns"`. It worked when `data` holds `knowledge/returns-policy.md`.

## Next steps

* [Upload files](/docs/projects/uploads.md)
* [Add a tool](/docs/agents/tools.md)
