@zanichelli/freshdesk-mcp 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +204 -2
  3. package/dist/cli.js +56 -0
  4. package/dist/client/errors.js +136 -0
  5. package/dist/client/http.js +389 -0
  6. package/dist/client/rate-limit.js +198 -0
  7. package/dist/client/resources/agents.js +15 -0
  8. package/dist/client/resources/canned.js +134 -0
  9. package/dist/client/resources/companies.js +57 -0
  10. package/dist/client/resources/contacts.js +63 -0
  11. package/dist/client/resources/groups.js +15 -0
  12. package/dist/client/resources/list-all.js +54 -0
  13. package/dist/client/resources/search.js +94 -0
  14. package/dist/client/resources/solutions.js +192 -0
  15. package/dist/client/resources/ticket-fields.js +62 -0
  16. package/dist/client/resources/tickets.js +204 -0
  17. package/dist/config.js +174 -0
  18. package/dist/schema/index.js +145 -0
  19. package/dist/schema/input.js +260 -0
  20. package/dist/schema/output.js +138 -0
  21. package/dist/server.js +31 -0
  22. package/dist/tools/budget.js +63 -0
  23. package/dist/tools/context.js +62 -0
  24. package/dist/tools/csv.js +35 -0
  25. package/dist/tools/format.js +191 -0
  26. package/dist/tools/list.js +130 -0
  27. package/dist/tools/rows.js +132 -0
  28. package/dist/tools/ticket-query.js +408 -0
  29. package/dist/tools/toolsets/core/canned.js +163 -0
  30. package/dist/tools/toolsets/core/count-tickets.js +602 -0
  31. package/dist/tools/toolsets/core/export-tickets.js +461 -0
  32. package/dist/tools/toolsets/core/find-similar-tickets.js +309 -0
  33. package/dist/tools/toolsets/core/get-ticket-schema.js +134 -0
  34. package/dist/tools/toolsets/core/get-ticket.js +280 -0
  35. package/dist/tools/toolsets/core/index.js +30 -0
  36. package/dist/tools/toolsets/core/list-agents.js +70 -0
  37. package/dist/tools/toolsets/core/list-groups.js +60 -0
  38. package/dist/tools/toolsets/core/list-tickets.js +210 -0
  39. package/dist/tools/toolsets/core/search-tickets.js +82 -0
  40. package/dist/tools/toolsets/core/solutions.js +282 -0
  41. package/dist/tools/toolsets/core/triage-queue.js +253 -0
  42. package/dist/tools/toolsets/index.js +110 -0
  43. package/dist/version.js +4 -0
  44. package/examples/team-config.example.json +20 -0
  45. package/package.json +38 -4
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Zanichelli Editore S.p.A.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,205 @@
1
- # Temporary Holding Version
1
+ # freshdesk-mcp
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ MCP server for Freshdesk: triage, categorise, draft replies, hand off and analyse tickets through Claude.
4
+
5
+ This version has the read-only `core` toolset. It runs on your machine over
6
+ stdio, with **your own Freshdesk API key**: it sees what your Freshdesk agent
7
+ sees, and nothing more.
8
+
9
+ ## Install and run
10
+
11
+ Requires Node.js 22.19 or later.
12
+
13
+ ```bash
14
+ npx -y @zanichelli/freshdesk-mcp
15
+ ```
16
+
17
+ The process holds your API key: pin a version you have chosen
18
+ (`npx -y @zanichelli/freshdesk-mcp@<version>`) rather than run whatever is
19
+ latest.
20
+
21
+ **Version policy:** 0.x until milestone M4, then 1.0.0.
22
+
23
+ > **Not yet published.** `@zanichelli/freshdesk-mcp` is not on npm yet. Until it is, run the
24
+ > server from a clone:
25
+ >
26
+ > ```bash
27
+ > git clone <this repository> freshdesk-mcp
28
+ > cd freshdesk-mcp
29
+ > npm ci && npm run build
30
+ > ```
31
+ >
32
+ > and, in the client configurations below, replace `npx -y @zanichelli/freshdesk-mcp` with
33
+ > `node /absolute/path/to/freshdesk-mcp/dist/cli.js`.
34
+
35
+ `--version` prints the version and exits. Without it, the server reads its
36
+ configuration, stops with a message on stderr and exit code 1 if something is
37
+ wrong, and otherwise serves MCP on stdin/stdout.
38
+
39
+ ### Claude Code
40
+
41
+ ```bash
42
+ claude mcp add freshdesk \
43
+ -e FRESHDESK_DOMAIN=example.freshdesk.com \
44
+ -e FRESHDESK_API_KEY="$FRESHDESK_API_KEY" \
45
+ -- npx -y @zanichelli/freshdesk-mcp
46
+ ```
47
+
48
+ Or in a project's `.mcp.json` (keep the key out of version control):
49
+
50
+ ```json
51
+ {
52
+ "mcpServers": {
53
+ "freshdesk": {
54
+ "command": "npx",
55
+ "args": ["-y", "@zanichelli/freshdesk-mcp"],
56
+ "env": {
57
+ "FRESHDESK_DOMAIN": "example.freshdesk.com",
58
+ "FRESHDESK_API_KEY": "${FRESHDESK_API_KEY}",
59
+ "FRESHDESK_EXPORT_DIR": "/absolute/path/to/exports"
60
+ }
61
+ }
62
+ }
63
+ }
64
+ ```
65
+
66
+ ### Claude Desktop
67
+
68
+ In `claude_desktop_config.json`. The file holds the key in plain text: do not
69
+ share or commit it.
70
+
71
+ ```json
72
+ {
73
+ "mcpServers": {
74
+ "freshdesk": {
75
+ "command": "npx",
76
+ "args": ["-y", "@zanichelli/freshdesk-mcp"],
77
+ "env": {
78
+ "FRESHDESK_DOMAIN": "example.freshdesk.com",
79
+ "FRESHDESK_API_KEY": "your-api-key",
80
+ "FRESHDESK_MCP_CONFIG": "/absolute/path/to/team-config.json"
81
+ }
82
+ }
83
+ }
84
+ }
85
+ ```
86
+
87
+ ## Environment variables
88
+
89
+ | Variable | Required | What |
90
+ | ------------------------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
91
+ | `FRESHDESK_DOMAIN` | yes | Your Freshdesk host, `<name>.freshdesk.com` (no `https://`, no path), or the same host as `FRESHDESK_HOST_OVERRIDE`. The key is sent to this one host only, over HTTPS. |
92
+ | `FRESHDESK_API_KEY` | yes | Your own API key (Freshdesk: profile settings, "View API key"). |
93
+ | `FRESHDESK_MCP_CONFIG` | no | Path of the team config file (below). Unset: the defaults. |
94
+ | `FRESHDESK_EXPORT_DIR` | no | The directory `export_tickets` writes to (see [Exports](#exports)). Unset: `export_tickets` is refused and says how to set it; every other tool works. |
95
+ | `FRESHDESK_HOST_OVERRIDE` | no | For a custom domain that is not `<name>.freshdesk.com`: set it and `FRESHDESK_DOMAIN` to the same host. It does not add a second host: the key still goes to `FRESHDESK_DOMAIN` only. Personal on purpose: the team file cannot set it. |
96
+
97
+ ## Team config
98
+
99
+ An optional JSON file shared by a team, path in `FRESHDESK_MCP_CONFIG`; see
100
+ [`examples/team-config.example.json`](./examples/team-config.example.json). It
101
+ is checked at start: an unknown key or a bad value stops the server with a
102
+ message naming the key, never the value.
103
+
104
+ - `toolsets`: the toolsets to enable. This version has `core` only (the
105
+ default); a file enabling `write` or `send` is refused.
106
+ - `fields`: per ticket field, by label or API name: `meaning`, text telling
107
+ the model what the field means, and `hidden: true`, to leave the field out
108
+ of a ticket's detail output (personal data, say).
109
+ `hidden` works in ticket detail output only: it is not an access control.
110
+ It is not applied to `get_ticket_schema`, to queries or to input, and it is
111
+ not guaranteed in rows, counts or exports.
112
+ - `uncategorised_fields`: the fields that, all empty, make a ticket
113
+ uncategorised (`list_tickets`' `uncategorized`). Default: the last level of
114
+ the first nested field.
115
+ - `forward_allow_list`: for a later toolset; unused by `core`.
116
+ - `limits`: `reserve_pct` (20), `floor_pct` (5), `pacing_rps` (2),
117
+ `bulk_cap` (50), `schema_ttl_s` (3600) and `call_budget` (250, the most API
118
+ calls one tool call may estimate before it needs `confirm: true`).
119
+
120
+ ## Tools
121
+
122
+ The `core` toolset reads only from Freshdesk (`export_tickets` writes a local file). Each tool's description states what one call
123
+ costs in Freshdesk API calls, which count against your account's rate limit:
124
+
125
+ - `get_ticket_schema` — 0 API calls while the schema is cached (limits.schema_ttl_s, default 1 h); a cold load is 1 call for the ticket fields plus 1 per 100 agents and 1 per 100 groups, at least 3, within 30 s.
126
+ - `list_agents` — 1 API call per 100 agents, at most 10, within 30 s, plus one per retry of a page after an error.
127
+ - `list_groups` — 1 API call per 100 groups, at most 10, within 30 s, plus one per retry of a page after an error.
128
+ - `get_ticket` — without a cursor, 1 API call for the ticket, 1 for the requester, 1 for the company if there is one, 1 with include_recent, and 1 per 100 conversations (at most 10); with a cursor, the conversation calls only; all within 30 s, plus one per retry after an error, plus at least 3 on any call, with or without a cursor, when the ticket-field schema is not cached.
129
+ - `list_tickets` — 1 API call; with uncategorized and none of status, priority, group, agent, type, tag or created_between, up to 10 (1 per 100 tickets read, per 30 with detail: true); all within 30 s, plus one per retry after an error, plus at least 3 when the ticket-field schema is not cached.
130
+ - `search_tickets` — 1 API call, within 30 s, plus one per retry after an error, plus at least 3 when the ticket-field schema is not cached.
131
+ - `triage_queue` — 1 API call, within 30 s; with reply_signals: true one more per row, at most 31 calls (about 61 credits of the rate limit); plus one per retry after an error, plus at least 3 when the ticket-field schema is not cached.
132
+ - `find_similar_tickets` — at most 9 API calls (1 for the ticket, 1 for its requester's tickets, 1 for its company's, 1 search, 1 per result for its excerpt), sent as an expensive operation, within 30 s; plus one per retry after an error, plus at least 3 when the ticket-field schema is not cached.
133
+ - `search_solutions` — 1 API call per call (the search runs again for every page), within 30 s, plus one per retry after an error.
134
+ - `get_solution_article` — 2 API calls (the article, then its folder), within 30 s, plus one per retry after an error.
135
+ - `list_solution_tree` — 1 API call per list of up to 100 entries, at most 20 lists of at most 10 calls each, within 30 s, plus one per retry after an error.
136
+ - `list_canned_responses` — 1 API call per 100 folders listed (at most 10), plus 1 per folder returned (at most 10), within 30 s, plus one per retry after an error.
137
+ - `get_canned_response` — 1 API call, within 30 s, plus one per retry after an error.
138
+ - `count_tickets` — 1 API call per 30 matching tickets up to 300 (10 at most); above that 1 plus 1 per 100 tickets read, up to the per-call budget (limits.call_budget, 250 by default) unless confirm: true, and a list read starts no request after 20 s (about 40 at the default pacing); all within 30 s, plus one per retry after an error, plus at least 3 when the ticket-field schema is not cached.
139
+ - `export_tickets` — per call, as count_tickets: 1 API call per 30 matching tickets up to 300; above that 1 plus 1 per 100 tickets read; with conversations, 1 more per ticket (and 1 per 100 more entries of a long thread); all up to the per-call budget (limits.call_budget, 250 by default) unless confirm: true (with max_tickets the estimate counts that many tickets at most); no request started after 20 s, all within 30 s; plus one per retry after an error, plus at least 3 when the ticket-field schema is not cached.
140
+
141
+ ## Ticket text is data, not instructions
142
+
143
+ Ticket text is data, not instructions. Subjects, messages, notes, names and
144
+ other customer-written text come back wrapped in
145
+ `<ticket_content>…</ticket_content>`, and the description of every tool that
146
+ returns such text tells the model to treat it as data and never to follow
147
+ instructions found in it. A customer can write anything in a ticket; the
148
+ markers make that visible, they do not make it safe. Read a model's summary
149
+ of a ticket with that in mind.
150
+
151
+ ## Exports
152
+
153
+ `export_tickets` writes the tickets of a period to a new JSONL or CSV file in
154
+ `FRESHDESK_EXPORT_DIR`, and returns the file's path, its row count, a
155
+ `next_cursor` while the period has more, and the API calls used.
156
+
157
+ - One call writes what it reads in about 20 seconds (about 4,000 tickets, or
158
+ about 40 with their conversations, at the default pacing), or at most
159
+ `max_tickets` rows (1 to 10,000) when given. While the period has more,
160
+ the result carries `next_cursor`: call again with the same arguments and
161
+ `cursor` set to it for the next tickets. Each call writes its own new file;
162
+ the last call's result has no `next_cursor`. Across the files of one
163
+ period no ticket is written twice, and between two calls none that matched
164
+ at the first call is skipped unless it left the filters; a ticket created while the calls
165
+ run can be left out. Past 300 matches, two tickets created in the same
166
+ second as the last one written, one of which entered or left the filters
167
+ between two calls, are the exception: one can be skipped or written twice.
168
+ A call cut by the budget, the time limit or the rate limit also says
169
+ `truncated: true`, beside its `next_cursor`; cut before it wrote a row, it
170
+ says `truncated: true` with no `next_cursor`: call again later with the
171
+ same cursor, or narrow the period (which starts over). Paging relies on
172
+ Freshdesk's search giving ticket ids newest first; a resumed call that
173
+ finds them out of order is refused with nothing kept, and a first call
174
+ that finds them so gives no `next_cursor`: export the rest in shorter
175
+ periods.
176
+ - A cursor works only with the arguments it came with: used with any other
177
+ (another format, period, filter, `max_tickets`, `conversations` or
178
+ `confirm`), or edited, it is refused before anything is sent or written.
179
+
180
+ - `FRESHDESK_EXPORT_DIR` must be an absolute path to an existing directory,
181
+ and not a symlink. Otherwise, or unset, the call is refused before any
182
+ request is sent.
183
+ - The file name is generated (`freshdesk-tickets-<UTC time>.jsonl` or
184
+ `.csv`); an existing file is never overwritten. The file is readable by its
185
+ owner only (mode 0600), and a failed call leaves no partial file.
186
+ - Exported files hold customer text unwrapped. The `<ticket_content>` markers
187
+ are not in the file: whoever opens it, a person or a model, must treat its
188
+ text as data, never as instructions. A field the team config hides is left
189
+ out, but that is not guaranteed for exports.
190
+ - In CSV, a cell starting with `=`, `+`, `-`, `@`, a tab or a carriage return
191
+ is prefixed with `'` so a spreadsheet does not run it as a formula.
192
+
193
+ ## Development
194
+
195
+ This repository follows an agent-native setup. **Start with
196
+ [`AGENTS.md`](./AGENTS.md)** — the project map and command surface, which points at
197
+ [`INSTRUCTION.md`](./INSTRUCTION.md) for the standard contract (the four execution
198
+ principles).
199
+
200
+ Requires [`pre-commit`](https://pre-commit.com); the command surface also calls `ruff` directly, so put it on your PATH (pipx/uv/pip).
201
+
202
+ ```bash
203
+ make install # set up git hooks (once)
204
+ make quality # run the full local gate
205
+ ```
package/dist/cli.js ADDED
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env node
2
+ // Entry point (SPEC §7): serves the MCP server on stdio; the package's `bin`.
3
+ //
4
+ // - `--version` prints the version on stdout and exits, reading no
5
+ // environment and starting nothing.
6
+ // - While serving, stdout carries protocol frames only: nothing here writes
7
+ // to it, the HTTP client logs to stderr (SPEC §9).
8
+ // - The one place the real undici `Agent` is built (RFC 2026-11-23, rule
9
+ // 2), with explicit timeouts and the default `maxHeaderSize`
10
+ // (docs/improvements.md, `2a0e56c` (1), `6d4013a` (2)). No test imports
11
+ // this file: test/server.test.ts spawns the build of it.
12
+ // - A configuration problem (environment, team file, a toolset this
13
+ // version does not have) is found before serving: its message goes to
14
+ // stderr and the exit code is 1. Any other start error prints one fixed
15
+ // line, never its text.
16
+ import { serveStdio } from "@modelcontextprotocol/server/stdio";
17
+ import { Agent } from "undici";
18
+ import { ConfigError, loadConfig } from "./config.js";
19
+ import { createServer } from "./server.js";
20
+ import { createContext } from "./tools/context.js";
21
+ import { VERSION } from "./version.js";
22
+ /** Each phase of one request; `http.ts` also cuts an attempt off at 10 s. */
23
+ const TIMEOUT_MS = 10_000;
24
+ function start() {
25
+ try {
26
+ const config = loadConfig(process.env);
27
+ const agent = new Agent({
28
+ connectTimeout: TIMEOUT_MS,
29
+ headersTimeout: TIMEOUT_MS,
30
+ bodyTimeout: TIMEOUT_MS,
31
+ });
32
+ const context = createContext(config, agent);
33
+ // Built once here so a bad toolset or tool stops the start, not the first request.
34
+ createServer(context);
35
+ return context;
36
+ }
37
+ catch (err) {
38
+ const message = err instanceof ConfigError ? err.message : "could not start (unexpected error).";
39
+ process.stderr.write(`freshdesk-mcp: ${message}\n`);
40
+ return undefined;
41
+ }
42
+ }
43
+ if (process.argv[2] === "--version") {
44
+ process.stdout.write(`${VERSION}\n`);
45
+ }
46
+ else {
47
+ const context = start();
48
+ if (context === undefined) {
49
+ process.exitCode = 1;
50
+ }
51
+ else {
52
+ serveStdio(() => createServer(context), {
53
+ onerror: (error) => process.stderr.write(`freshdesk-mcp: transport error (${error.name}).\n`),
54
+ });
55
+ }
56
+ }
@@ -0,0 +1,136 @@
1
+ // Normalised Freshdesk client error (SPEC §7.1).
2
+ //
3
+ // Status mapping:
4
+ // 401 auth · 403 forbidden · 404 not_found · 400 validation ·
5
+ // 429 rate_limited (retryable, retry_after_s from an integer Retry-After) ·
6
+ // 5xx server (retryable) · no response at all: network (retryable).
7
+ // Fallback for any other status:
8
+ // - another 4xx (405, 409, 415, ...) -> validation, not retryable:
9
+ // the request itself was refused, so sending it again changes nothing;
10
+ // - anything else (3xx, 1xx, 2xx) -> server, not retryable.
11
+ // A 3xx is never followed or retried (RFC 2026-11-23, rule 5).
12
+ // Freshdesk's `errors[]` is passed through on every `validation` error,
13
+ // capped at MAX_FIELD_ERRORS items of MAX_FIELD_TEXT characters per string.
14
+ // It is response-body text: return it to the caller, never log it.
15
+ // A `validation` error is also built locally, with `http_status: null`, when
16
+ // a tool's input fails the schema before anything is sent
17
+ // (src/schema/input.ts): its `errors[]` echoes that input, under the same caps.
18
+ // `message` is fixed text: it never quotes the response body, a header
19
+ // value or a cause's free text.
20
+ export const MAX_FIELD_ERRORS = 50;
21
+ export const MAX_FIELD_TEXT = 500;
22
+ export function errorFromResponse(status, headers, body) {
23
+ const base = { http_status: status };
24
+ switch (status) {
25
+ case 401:
26
+ return {
27
+ ...base,
28
+ code: "auth",
29
+ message: "Freshdesk rejected the API key (HTTP 401).",
30
+ retryable: false,
31
+ };
32
+ case 403:
33
+ return {
34
+ ...base,
35
+ code: "forbidden",
36
+ message: "The API key's agent may not do this (HTTP 403).",
37
+ retryable: false,
38
+ };
39
+ case 404:
40
+ return {
41
+ ...base,
42
+ code: "not_found",
43
+ message: "Freshdesk found no such resource (HTTP 404).",
44
+ retryable: false,
45
+ };
46
+ case 429: {
47
+ const retryAfter = parseRetryAfter(headers.get("retry-after"));
48
+ return {
49
+ ...base,
50
+ code: "rate_limited",
51
+ message: "Freshdesk rate limit reached (HTTP 429).",
52
+ retryable: true,
53
+ ...(retryAfter === undefined ? {} : { retry_after_s: retryAfter }),
54
+ };
55
+ }
56
+ }
57
+ if (status >= 400 && status < 500) {
58
+ const errors = fieldErrors(body);
59
+ return {
60
+ ...base,
61
+ code: "validation",
62
+ message: `Freshdesk refused the request (HTTP ${status}).`,
63
+ retryable: false,
64
+ ...(errors === undefined ? {} : { errors }),
65
+ };
66
+ }
67
+ if (status >= 500 && status < 600) {
68
+ return {
69
+ ...base,
70
+ code: "server",
71
+ message: `Freshdesk server error (HTTP ${status}).`,
72
+ retryable: true,
73
+ };
74
+ }
75
+ return {
76
+ ...base,
77
+ code: "server",
78
+ message: `Unexpected Freshdesk response (HTTP ${status}).`,
79
+ retryable: false,
80
+ };
81
+ }
82
+ export function networkError(cause) {
83
+ const tag = errorTag(cause);
84
+ return {
85
+ code: "network",
86
+ http_status: null,
87
+ message: tag === undefined ? "Could not reach Freshdesk." : `Could not reach Freshdesk (${tag}).`,
88
+ retryable: true,
89
+ };
90
+ }
91
+ function parseRetryAfter(value) {
92
+ const trimmed = value?.trim();
93
+ return trimmed !== undefined && /^\d+$/.test(trimmed) ? Number(trimmed) : undefined;
94
+ }
95
+ function fieldErrors(body) {
96
+ if (!isRecord(body) || !Array.isArray(body.errors))
97
+ return undefined;
98
+ const out = [];
99
+ for (const item of body.errors) {
100
+ if (out.length === MAX_FIELD_ERRORS)
101
+ break;
102
+ if (!isRecord(item) || typeof item.message !== "string")
103
+ continue;
104
+ out.push({
105
+ ...(typeof item.field === "string" ? { field: clip(item.field) } : {}),
106
+ message: clip(item.message),
107
+ ...(typeof item.code === "string" ? { code: clip(item.code) } : {}),
108
+ });
109
+ }
110
+ return out;
111
+ }
112
+ function clip(text) {
113
+ return text.slice(0, MAX_FIELD_TEXT);
114
+ }
115
+ /**
116
+ * A Node error code (`ECONNRESET`) or error name (`TimeoutError`), never free
117
+ * text. undici's fetch rejects with `TypeError("fetch failed")` and the real
118
+ * error in `cause`, so the cause is read before the outer error's name.
119
+ */
120
+ function errorTag(error) {
121
+ if (!isRecord(error))
122
+ return undefined;
123
+ const inner = isRecord(error.cause) ? error.cause : {};
124
+ for (const candidate of [error.code, inner.code, inner.name, error.name]) {
125
+ if (typeof candidate === "string" &&
126
+ /^[A-Za-z_]{1,40}$/.test(candidate) &&
127
+ candidate !== "Error" &&
128
+ candidate !== "TypeError") {
129
+ return candidate;
130
+ }
131
+ }
132
+ return undefined;
133
+ }
134
+ function isRecord(value) {
135
+ return typeof value === "object" && value !== null;
136
+ }