# n8n

[n8n](https://n8n.io/) is a workflow automation tool. Its built-in **MCP Client** node can run read-only SQL against your Altertable lakehouse or ask the Altertable Agent a question.

## Connection setup

Use n8n Cloud or a self-hosted instance with **MCP Client** and **MCP OAuth2** support. You need an Altertable account with access to the environment you want to query.

### Import the example workflow

1. Create a workflow in n8n and choose **Import from URL** from the workflow menu.
2. Paste `https://altertable.ai/assets/workflows/n8n-altertable-mcp.json` and import it. You can also [download the workflow JSON](/assets/workflows/n8n-altertable-mcp.json) and choose **Import from File**.

The workflow includes two MCP Client nodes. **Initialize Altertable** calls `initialize` with `{}` to identify the connected environment. **Run sample query** calls `query_lakehouse` to test SQL access.

### Configure the connection

Open **Initialize Altertable** and create an **MCP OAuth2 API** credential. Use these settings for the node and its credential.

| Field                         | Value                           |
| ----------------------------- | ------------------------------- |
| Transport                     | HTTP Streamable                 |
| MCP Endpoint URL              | `https://mcp.altertable.ai/mcp` |
| Authentication                | MCP OAuth2                      |
| Register Client Automatically | Enabled                         |
| Server URL (credential)       | `https://mcp.altertable.ai/mcp` |

1. Select **Connect my account**, sign in to Altertable, and approve read-only access for the organization and environment you want to query.
2. Name the credential, for example `Altertable production read-only`, and save it.
3. Open **Run sample query** and select the same credential.

## Verify the connection

Select **Execute workflow**. Open the output of **Initialize Altertable** and check the organization and environment names. Then open **Run sample query**. Its **Input Mode** is **JSON**, with this tool input:

```json
{ "statement": "SELECT 1 AS ok" }
```

Open **Format query result** and select **Table**. Each SQL row becomes one n8n item, and each SQL column name becomes a field. The sample query produces:

| ok |
| -- |
| 1  |

Replace the statement with your own read-only DuckDB query. Use a unique name or alias for each column. In the next node, reference fields by name, for example `{{ $json.ok }}`. A query with zero rows produces zero items.

## Ask the Altertable Agent

For a question that needs more than one query, keep **Initialize Altertable** as the first MCP step. Disconnect **Format query result** from **Run sample query**. In **Run sample query**, change **Tool** to `ask`, keep **Input Mode** set to **JSON**, and replace the SQL input with a prompt:

```json
{
  "prompt": "How did new signups change last week compared with the previous week?"
}
```

Run the workflow and inspect the node’s output. The tool waits for the final answer. Map the `answer` field from the result into the next node, for example as the body of an email. With only `prompt`, each execution starts a new conversation in your Altertable chat history.

## Notes

- **The node asks for an AI Agent connection.** Choose **MCP Client** when adding a workflow step. **MCP Client Tool** attaches tools to an n8n AI Agent.
- **A node returns `401 invalid_token`.** Open the MCP OAuth2 credential and connect your account. Select the connected credential on both MCP Client nodes.
- **The workflow sees the wrong catalogs.** Check the environment in the `initialize` output. Create a credential for the intended environment and select it on both nodes.
- **The SQL test succeeds but an agent question times out.** Increase **Options → Timeout** on the `ask` node. The value is in milliseconds. For example, `300000` allows five minutes.

See [Altertable MCP authorization](/docs/agents/mcp.md) for access scopes and [MCP tools](/docs/api-references/mcp-tools.md) for tool inputs and outputs.