|
| 1 | +--- |
| 2 | +description: MCP tools for fetching and tailing logs on B2C Commerce instances. |
| 3 | +--- |
| 4 | + |
| 5 | +# Log Tools |
| 6 | + |
| 7 | +MCP tools for inspecting runtime logs on a B2C Commerce instance via WebDAV. Use them to investigate errors after triggering a request, monitor a job run, or audit recent failures. Available in the **CARTRIDGES**, **DIAGNOSTICS**, and **SCAPI** toolsets. |
| 8 | + |
| 9 | +## Authentication |
| 10 | + |
| 11 | +All log tools that read from the instance (`logs_list_files`, `logs_get_recent`, `logs_watch_start`) require WebDAV-capable credentials. |
| 12 | + |
| 13 | +**Required:** |
| 14 | +- **Basic Auth** preferred: `hostname`, `username`, and `password` (WebDAV access key) for a Business Manager user with WebDAV log read permission. |
| 15 | +- OAuth (client-credentials / implicit) is also supported as a fallback for WebDAV. |
| 16 | + |
| 17 | +**Configuration priority:** Flags → Environment variables → `dw.json` config file |
| 18 | + |
| 19 | +`logs_watch_poll`, `logs_watch_stop`, and `logs_watch_list` operate on server-side state only and do not require fresh credentials per call. |
| 20 | + |
| 21 | +See [Configuration](../configuration) for credential setup. |
| 22 | + |
| 23 | +## When to use which tool |
| 24 | + |
| 25 | +- Quick lookup of recent errors → **`logs_get_recent`**. |
| 26 | +- Discover what log prefixes are active on the instance → **`logs_list_files`**. |
| 27 | +- Monitor logs across a multi-step action you trigger (storefront request, job, debug session) → start a watch with **`logs_watch_start`**, then drain it with **`logs_watch_poll`**, and finish with **`logs_watch_stop`**. The watch buffers entries between calls so nothing is lost between agent turns. |
| 28 | + |
| 29 | +## Recovery from orphaned watches |
| 30 | + |
| 31 | +Log watches are stateful and live in the MCP server process. Only one watch is allowed per hostname. If an agent loses track of an active watch: |
| 32 | + |
| 33 | +1. **List active watches** — call `logs_watch_list` (no args). It returns all watches with their `watch_id`, `hostname`, `prefixes`, and buffered counts. |
| 34 | +2. **Stop orphaned watches** — call `logs_watch_stop` with the `watch_id` to release the underlying tail. |
| 35 | +3. **Idle cleanup** — watches inactive for 30 minutes are automatically destroyed. |
| 36 | +4. **Restart the MCP server** — last resort; destroys all watch state. |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +## One-shot tools |
| 41 | + |
| 42 | +### logs_list_files |
| 43 | + |
| 44 | +List log files on the instance via WebDAV. |
| 45 | + |
| 46 | +| Parameter | Type | Required | Default | Description | |
| 47 | +|-----------|------|----------|---------|-------------| |
| 48 | +| `prefixes` | string[] | No | all | Filter by log prefix (e.g., `["error", "customerror"]`) | |
| 49 | +| `sort_by` | `"date" \| "name" \| "size"` | No | `date` | Sort field | |
| 50 | +| `sort_order` | `"asc" \| "desc"` | No | `desc` | Sort order | |
| 51 | + |
| 52 | +**Returns:** `{count, files: [{name, prefix, size, lastModified, path}]}`. |
| 53 | + |
| 54 | +### logs_get_recent |
| 55 | + |
| 56 | +Fetch recent log entries in a single request/response. Filters (`since`, `level`, `search`) are applied client-side after fetching. |
| 57 | + |
| 58 | +| Parameter | Type | Required | Default | Description | |
| 59 | +|-----------|------|----------|---------|-------------| |
| 60 | +| `prefixes` | string[] | No | `["error", "customerror"]` | Log prefixes to read | |
| 61 | +| `count` | number | No | `50` | Maximum entries to return | |
| 62 | +| `since` | string | No | | Relative time (`"5m"`, `"1h"`, `"2d"`) or ISO 8601 | |
| 63 | +| `level` | string[] | No | | Filter by level (ERROR, WARN, INFO, DEBUG, FATAL, TRACE) | |
| 64 | +| `search` | string | No | | Case-insensitive substring filter | |
| 65 | + |
| 66 | +**Returns:** `{count, entries: [{file, level, timestamp, message, raw}]}`. |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +## Watch lifecycle |
| 71 | + |
| 72 | +### logs_watch_start |
| 73 | + |
| 74 | +Start a background log watch. Returns a `watch_id` immediately. Buffers entries in memory until `logs_watch_poll` drains them. |
| 75 | + |
| 76 | +> **Workflow:** call `logs_watch_start` **before** triggering the action that should produce logs. Otherwise startup may emit only existing entries (controlled by `last_entries`) and miss what you wanted to capture. |
| 77 | +
|
| 78 | +| Parameter | Type | Required | Default | Description | |
| 79 | +|-----------|------|----------|---------|-------------| |
| 80 | +| `prefixes` | string[] | No | `["error", "customerror"]` | Log prefixes to watch | |
| 81 | +| `last_entries` | number | No | `1` | Recent entries per file to emit on startup. `0` skips. | |
| 82 | +| `poll_interval_ms` | number | No | `3000` | How often the underlying tail polls WebDAV | |
| 83 | +| `level` | string[] | No | | Drop entries not matching level before buffering | |
| 84 | +| `search` | string | No | | Drop entries not matching substring before buffering | |
| 85 | + |
| 86 | +**Returns:** `{watch_id, hostname, prefixes, started_at}`. |
| 87 | + |
| 88 | +### logs_watch_poll |
| 89 | + |
| 90 | +Drain buffered entries. If the buffer is empty, blocks up to `timeout_ms` waiting for new entries. |
| 91 | + |
| 92 | +| Parameter | Type | Required | Default | Description | |
| 93 | +|-----------|------|----------|---------|-------------| |
| 94 | +| `watch_id` | string | Yes | | Watch id from `logs_watch_start` | |
| 95 | +| `timeout_ms` | number | No | `5000` | Max time to block when buffer is empty. `0` returns immediately. | |
| 96 | +| `max_entries` | number | No | `200` | Maximum entries returned per call | |
| 97 | + |
| 98 | +**Returns:** `{watch_id, entries, files_discovered, files_rotated, errors, truncated, buffered_remaining, dropped_entries, stopped}`. When `truncated` is `true`, call again to drain the rest. |
| 99 | + |
| 100 | +### logs_watch_stop |
| 101 | + |
| 102 | +Stop a watch and release its underlying tail. |
| 103 | + |
| 104 | +| Parameter | Type | Required | Description | |
| 105 | +|-----------|------|----------|-------------| |
| 106 | +| `watch_id` | string | Yes | Watch id from `logs_watch_start` | |
| 107 | + |
| 108 | +**Returns:** `{watch_id, stopped_at, total_entries_seen}`. |
| 109 | + |
| 110 | +### logs_watch_list |
| 111 | + |
| 112 | +List active watches. Use to recover orphaned watches or inspect buffered counts. |
| 113 | + |
| 114 | +No parameters. |
| 115 | + |
| 116 | +**Returns:** `{watches: [{watch_id, hostname, prefixes, buffered_entries, total_entries_seen, dropped_entries, files_discovered, stopped, created_at, last_activity_at}]}`. |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +## See also |
| 121 | + |
| 122 | +- [`b2c logs tail`](../../cli/logs/tail) — interactive CLI tail |
| 123 | +- [`b2c logs get`](../../cli/logs/get) — one-shot CLI fetch |
| 124 | +- [`b2c logs list`](../../cli/logs/list) — list log files from the CLI |
0 commit comments