@first-ch/tools-mcp 0.26.0 → 0.27.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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @first-ch/tools-mcp
2
2
 
3
- MCP server exposing [First CH Tools](https://tools.first-ch.com)' free web-tool logic — WCAG contrast, JP character/X-weight counting, WebP conversion, JSON-LD generation, llms.txt generation, encoding/line-ending conversion, Marp Markdown→slide rendering, Japanese/English test-data generation, text/code diffing, cron-expression explanation, Base64/data-URI encoding, URL query-parameter editing, HTML entity escaping/unescaping, JSON⇄YAML conversion, px ⇄ rem/em unit conversion, colour-code conversion with alpha compositing, MD5/SHA-1/SHA-256/SHA-384/SHA-512 hashing, JWT decoding with expiry and signature checks, User-Agent parsing with device/bot detection, bulk UUID v4 / ULID generation, aspect-ratio calculation with responsive sizing, and case conversion between camelCase, snake_case and kebab-case — to AI agents such as Claude Code.
3
+ MCP server exposing [First CH Tools](https://tools.first-ch.com)' free web-tool logic — WCAG contrast, JP character/X-weight counting, WebP conversion, JSON-LD generation, llms.txt generation, encoding/line-ending conversion, Marp Markdown→slide rendering, Japanese/English test-data generation, text/code diffing, cron-expression explanation, Base64/data-URI encoding, URL query-parameter editing, HTML entity escaping/unescaping, JSON⇄YAML conversion, px ⇄ rem/em unit conversion, colour-code conversion with alpha compositing, MD5/SHA-1/SHA-256/SHA-384/SHA-512 hashing, JWT decoding with expiry and signature checks, User-Agent parsing with device/bot detection, bulk UUID v4 / ULID generation, aspect-ratio calculation with responsive sizing, case conversion between camelCase, snake_case and kebab-case, and CSV/TSV ⇄ JSON conversion — to AI agents such as Claude Code.
4
4
 
5
5
  日本語版は [後半セクション](#日本語) を参照してください。
6
6
 
@@ -72,6 +72,7 @@ This server is also registered in the [MCP Registry](https://registry.modelconte
72
72
  | `unixtime_convert` | Converts Unix seconds, milliseconds, microseconds and nanoseconds to and from ISO 8601 and ordinary date strings — the same logic as the browser tool at tools.first-ch.com/unixtime/. The unit of a plain number is detected from its digit count (up to 10 digits seconds, up to 13 milliseconds, up to 16 microseconds, longer nanoseconds); `unit` states it outright instead. Digits below the millisecond are dropped and reported in `notes`. On the date side it accepts `2026-08-24T09:30:00Z` (ISO 8601), `2026-08-24 18:30`, `2026/8/24 9:05`, the Japanese `2026年8月24日 18時30分`, `Sun, 24 Aug 2026 03:00:00 GMT` (HTTP-date) and `now`, stripping the quotes, brackets and trailing commas that come with a paste from CSV or JSON. `input` takes one value per line, up to 500 lines at a time, and every row comes back with Unix seconds, milliseconds, ISO 8601 in UTC, local time in `timeZone` (with weekday and UTC offset) and a relative reading ("30分後" / "in 30 minutes"); a line that cannot be read fails on its own without taking the others down. **A date with no offset is read as a wall-clock time in `timeZone`** and always says so in `notes`; an explicit `+09:00` or `Z` in the input wins over `timeZone`. A wall-clock time that daylight saving skips is moved forward to the moment after the change, also noted. An 8-digit number such as `20260824` is Unix seconds here, so it is flagged in case a date was meant. Negative values (before 1970) and fractional seconds work | `input`, `timeZone?` (IANA name, default `UTC`), `unit?` (`auto` default / `s` / `ms` / `us` / `ns`), `now?` (Unix milliseconds or a date string, default the current time), `lang?` (`ja` default / `en`) |
73
73
  | `robotstxt_generate` | Assembles a robots.txt from crawl rules, sitemap declarations and a preset for AI crawlers — the same logic as the browser tool at tools.first-ch.com/robots-txt/. `ai.preset` decides how 24 known AI crawlers are treated in one move: `training` (the default: refuse the ones that collect training data — GPTBot, ClaudeBot, Google-Extended, Bytespider, CCBot and the rest — while allowing AI search and on-demand fetchers such as OAI-SearchBot, Claude-SearchBot, PerplexityBot and ChatGPT-User), `allow`, `block`, `none` (leave AI crawlers out) or `custom` with `ai.overrides` naming them one by one. **A crawler obeys exactly one matching group**, so writing a group for GPTBot stops the `User-agent: *` rules from ever reaching it; by default the shared disallow paths are copied into the AI groups to close that gap (`ai.inherit: false` turns it off). Paths get a leading slash added and full URLs reduced to their path, and `warnings` reports spaces inside a path, non-ASCII paths that need percent-encoding, wildcards that not every crawler supports, a `Crawl-delay` Googlebot will ignore, a missing sitemap, a User-agent listed twice and a site blocked in full. `listCrawlers: true` returns just the crawler list (name, vendor, purpose) without generating anything | `siteUrl?`, `disallow?`, `allow?`, `userAgents?` (default `*`), `crawlDelay?`, `blockCrawlers?`, `sitemaps?`, `ai?` (`preset?`, `overrides?`, `inherit?`), `groups?`, `comments?` (default true), `allowStyle?` (`disallow-empty` default / `allow-slash`), `listCrawlers?`, `lang?` (`ja` default / `en`), `outputPath?` |
74
74
  | `case_convert` | Converts identifiers between eleven cases — camelCase, PascalCase, snake_case, CONSTANT_CASE, kebab-case, Train-Case, dot.case, Title Case, Sentence case, lower case and UPPER CASE — the same logic as the browser tool at tools.first-ch.com/case/. Built for the mechanical jobs at the boundary between layers: renaming variables in bulk, moving between `snake_case` database columns and `camelCase` JSON keys, tidying CSV headers. `scope` sets the unit of work: `lines` (the default, one item per line), `items` (every comma or tab separated field of a line, for CSV header rows) or `whole`. **Leading and trailing spaces, indentation, line endings (LF/CRLF) and the separators are preserved**, so only the names change. A run of capitals is broken only before a capital followed by a lower-case letter (`XMLHttpRequest` → `XML` / `Http` / `Request`), and digits stay attached to the word before them (`sha256Hash` → `sha256` / `Hash`; `splitDigits` splits them). Acronyms fold to `parseXmlData` by default and stay capitalised with `keepAcronyms`, except the first word of a camelCase name, which is always lower-cased (`URLParser` → `urlParser`). Characters with no letter case (Japanese, for example) pass through rather than being transliterated. `notes` reports **names that collide after conversion** (`first name` and `first_name` both become `first_name`, which is how a column silently disappears on import), results starting with a digit that cannot be identifiers, and items containing acronyms; `items[].detected` names the case each input was written in. `allFormats` expands every item into all eleven cases, `listFormats` returns just the list of cases with what each is used for | `text?` or `path?`, `outputPath?`, `format?` (`camel` default), `scope?` (`lines` default / `items` / `whole`), `splitDigits?`, `keepAcronyms?`, `allFormats?`, `listFormats?`, `lang?` (`ja` default / `en`) |
75
+ | `csv_convert` | Converts CSV/TSV into a JSON array and back — the same logic as the browser tool at tools.first-ch.com/csv-json/. For moving data you already have between a spreadsheet and an API: turning an exported table into JSON a request body can carry, or an API response into something Excel opens. With `direction` omitted it guesses `json2csv` when the input starts with `[` or `{` and `csv2json` otherwise. The delimiter is detected by looking at the first 20 lines for the character that appears the same number of times on every line, and can be pinned with `delimiter`. **Column names such as `stock.qty` are read and written as nested objects, and `tags.0` / `tags.1` (`tags[0]` means the same) become an array** — the reverse direction flattens by the same rule, so a round trip through CSV restores the original structure (`nest: false` writes nested values into a single cell as JSON text). **Type inference is decided by round trip — a value becomes a number only when converting it back to text is character-for-character identical** — so `0123`, `+1`, `1.50` and integers beyond 2^53 (`12345678901234567890`) stay strings and postcodes, phone numbers, invoice numbers and version strings survive. Reading follows RFC 4180: quoted cells, `""` escapes, line breaks inside cells, CRLF and a BOM are all handled, and the output side offers `bom` and `newline` for Excel. On the JSON side it accepts an array, a single object, an object wrapping an array such as `{"data":[…]}`, and JSON Lines. **A JSON syntax error comes back as an `ok: false` result with a line, a column and an excerpt rather than an exception**, so the reply itself says where to fix it. `notes` also reports rows whose column count does not match the header, duplicate header names (kept as `name_2`), empty headers (named `column4`), values that would lose digits, cells starting with `=`, `+`, `-` or `@` that a spreadsheet may execute as a formula (CSV injection), and objects with a different set of keys | `text?` or `path?`, `outputPath?`, `direction?` (`auto` default / `csv2json` / `json2csv`), `delimiter?` (`auto` default / `comma` / `tab` / `semicolon` / `pipe`), `header?`, `nest?`, `types?`, `trim?`, `emptyNull?`, `indent?` (`2` default / 0–8 / `tab`), `newline?` (`lf` default / `crlf`), `quoteAll?`, `bom?`, `lang?` (`ja` default / `en`) |
75
76
 
76
77
  See [`server.mjs`](./server.mjs) for the exact Zod input schemas.
77
78
 
@@ -116,7 +117,7 @@ Release rule: a push to `main` automatically publishes a new package version thr
116
117
 
117
118
  ## 日本語
118
119
 
119
- `@first-ch/tools-mcp` は、[First CH Tools](https://tools.first-ch.com)(無料Webツール集)の計算ロジック — WCAGコントラスト比・日本語文字数/Xウェイト計測・WebP変換・JSON-LD生成・llms.txt生成・文字コード/改行コード変換・Marp Markdown→スライド レンダリング・テストデータ生成・テキスト/コード差分・Cron式の解説・Base64/Data URI変換・URLパラメータの分解/編集/再構築・HTML特殊文字のエスケープ/エンティティのデコード・JSON⇄YAMLの相互変換・px⇄rem/emの単位換算とCSSの一括変換・カラーコードの相互変換とアルファ透過の合成・MD5/SHA-1/SHA-256/SHA-384/SHA-512のハッシュ生成と照合・JWTのデコードと有効期限/署名の検証・User-Agent文字列の解析とデバイス判定・UUID v4 / ULIDの一括生成・アスペクト比の計算とレスポンシブサイズの算出・camelCase / snake_case / kebab-case などの文字列ケース変換 — をAIエージェント(Claude Code等)向けMCPツールとして提供するサーバーです。
120
+ `@first-ch/tools-mcp` は、[First CH Tools](https://tools.first-ch.com)(無料Webツール集)の計算ロジック — WCAGコントラスト比・日本語文字数/Xウェイト計測・WebP変換・JSON-LD生成・llms.txt生成・文字コード/改行コード変換・Marp Markdown→スライド レンダリング・テストデータ生成・テキスト/コード差分・Cron式の解説・Base64/Data URI変換・URLパラメータの分解/編集/再構築・HTML特殊文字のエスケープ/エンティティのデコード・JSON⇄YAMLの相互変換・px⇄rem/emの単位換算とCSSの一括変換・カラーコードの相互変換とアルファ透過の合成・MD5/SHA-1/SHA-256/SHA-384/SHA-512のハッシュ生成と照合・JWTのデコードと有効期限/署名の検証・User-Agent文字列の解析とデバイス判定・UUID v4 / ULIDの一括生成・アスペクト比の計算とレスポンシブサイズの算出・camelCase / snake_case / kebab-case などの文字列ケース変換・CSV/TSV ⇄ JSON の相互変換 — をAIエージェント(Claude Code等)向けMCPツールとして提供するサーバーです。
120
121
 
121
122
  ### インストール
122
123
 
@@ -186,6 +187,7 @@ claude mcp add firstch-tools -- npx -y @first-ch/tools-mcp
186
187
  | `unixtime_convert` | UNIX秒・ミリ秒・マイクロ秒・ナノ秒と、ISO 8601や日時文字列を相互に変換する。tools.first-ch.com/unixtime/ と同一の仕様。数値の単位は桁数から自動判定し(10桁までを秒 / 13桁までをミリ秒 / 16桁までをマイクロ秒 / それ以上をナノ秒)、`unit` を渡せばその単位で読む。ミリ秒より下の桁は切り捨て、その旨を `notes` で返す。日時側は `2026-08-24T09:30:00Z`(ISO 8601)・`2026-08-24 18:30`・`2026/8/24 9:05`・`2026年8月24日 18時30分`・`Sun, 24 Aug 2026 03:00:00 GMT`(HTTP-date)・`now` を受け付け、CSVやJSONから貼ったときの引用符・角括弧・行末カンマは自動で外す。`input` は1行1件で最大500行までまとめて変換し、行ごとにUNIX秒・ミリ秒・ISO 8601(UTC)・`timeZone` の現地時刻(曜日・UTCオフセット付き)・相対表示(○分前 / ○日後)を返す。読めなかった行は他の行を巻き込まずその行だけ `error` になる。**オフセットを持たない日時は `timeZone` の壁時計として解釈**し、必ず `notes` で明示する(入力に `+09:00` や `Z` があればそちらが優先)。夏時間で存在しない時刻は切り替え後へ繰り上げ、これも `notes` に出す。8桁の数字(`20260824`)はUNIX秒として読むため、日付のつもりの入力には注意を返す。負の値(1970年より前)と小数点付きの秒にも対応 | `input`、`timeZone?`(IANA名・既定 `UTC`)、`unit?`(既定 `auto` / `s` / `ms` / `us` / `ns`)、`now?`(UNIXミリ秒か日時文字列・既定は実行時刻)、`lang?`(既定 `ja` / `en`) |
187
188
  | `robotstxt_generate` | クロールの許可/禁止ルール・サイトマップ宣言・AIクローラーの許可プリセットから robots.txt を組み立てる。tools.first-ch.com/robots-txt/ と同一の仕様。`ai.preset` で24種のAIクローラーの扱いをまとめて決められる: `training`(既定=GPTBot・ClaudeBot・Google-Extended・Bytespider・CCBot など**学習用データを集めるクローラーだけを拒否**し、OAI-SearchBot・Claude-SearchBot・PerplexityBot・ChatGPT-User などAI検索と都度取得は許可)/ `allow` / `block` / `none`(AIクローラーを書かない)/ `custom`(`ai.overrides` で1件ずつ指定)。**クローラーは自分に一致するグループを1つだけ読む**ため、GPTBot 用のグループを作った時点で `User-agent: *` の禁止ルールはそのクローラーへ届かなくなる。既定では共通の禁止パスをAI側のグループへ書き写してこの取りこぼしを防ぐ(`ai.inherit: false` で止められる)。パスは先頭の `/` を補い、絶対URLからはパス部分だけを取り出す。`warnings` では空白を含むパス・パーセントエンコードしていない日本語のパス・対応していないクローラーのあるワイルドカード・Googlebotが読まない `Crawl-delay`・サイトマップの宣言漏れ・同じUser-agentの重複・サイト全体の拒否を指摘する。`listCrawlers: true` を渡すと生成せずAIクローラーの一覧(名前・提供元・目的)だけを返す | `siteUrl?`、`disallow?`、`allow?`、`userAgents?`(既定 `*`)、`crawlDelay?`、`blockCrawlers?`、`sitemaps?`、`ai?`(`preset?` / `overrides?` / `inherit?`)、`groups?`、`comments?`(既定 true)、`allowStyle?`(既定 `disallow-empty` / `allow-slash`)、`listCrawlers?`、`lang?`(既定 `ja` / `en`)、`outputPath?` |
188
189
  | `case_convert` | 識別子を camelCase / PascalCase / snake_case / CONSTANT_CASE / kebab-case・Train-Case・dot.case・Title Case・Sentence case・lower case・UPPER CASE の11形式へ相互変換する。tools.first-ch.com/case/ と同一ロジック。変数名の一括リネーム、`snake_case` のDB列名と `camelCase` のJSONキーの読み替え、CSVヘッダーの整形といった、層の境目で発生する定型処理のための道具。`scope` で処理の単位を選ぶ: `lines`(既定=1行1件)/ `items`(行内のカンマ・タブ区切りで1件ずつ=CSVヘッダー)/ `whole`(全体で1件)。**前後の空白・インデント・改行コード(LF/CRLF)・区切り記号はそのまま残す**ので、名前だけが置き換わる。大文字の連なりは直後に「大文字+小文字」が続くときだけ手前で切り(`XMLHttpRequest` → `XML` / `Http` / `Request`)、数字は既定で直前の語へ付く(`sha256Hash` → `sha256` / `Hash`。`splitDigits` で分けられる)。頭字語は既定で `parseXmlData` へ畳み、`keepAcronyms` で `parseXMLData` のまま残す(camelCase の先頭語はどちらでも小文字=`URLParser` → `urlParser`)。大文字小文字を持たない文字(日本語など)はそのまま出力し、ローマ字化はしない。`notes` では**変換後に同じ名前へ衝突する項目**(`first name` と `first_name` はどちらも `first_name`=インポート時に片方が黙って消える事故の元)・数字で始まって識別子にできない結果・頭字語を含む項目を指摘し、`items[].detected` では各入力がどの形式で書かれていたかを返す。`allFormats` で各項目を11形式すべてへ展開、`listFormats` で形式の一覧と使いどころだけを返す | `text?` または `path?`、`outputPath?`、`format?`(既定 `camel`)、`scope?`(既定 `lines` / `items` / `whole`)、`splitDigits?`、`keepAcronyms?`、`allFormats?`、`listFormats?`、`lang?`(既定 `ja` / `en`) |
190
+ | `csv_convert` | CSV/TSVとJSON配列を相互変換する。tools.first-ch.com/csv-json/ と同一ロジック。表計算ソフトから書き出した表をAPIに渡せるJSONにする、逆にAPIの応答をExcelで開ける表にする、という**手持ちのデータを形式だけ移し替える**ための道具。`direction` を省くと、入力の先頭が `[` か `{` なら `json2csv`、そうでなければ `csv2json` と推定する。区切り文字は先頭20行で「どの記号が各行に同じ個数だけ現れるか」を見て自動判定し、`delimiter` で固定もできる。**`stock.qty` のような列名は入れ子のオブジェクトとして読み書きし、`tags.0` / `tags.1`(`tags[0]` も同義)は配列になる**。逆向きも同じ規則で平らにするので、CSVへ出してJSONへ戻すと元の構造に復元される(`nest: false` なら入れ子はJSONの文字列としてセル1つに入る)。**型の読み替えは「一度数値にして文字列へ戻したとき元の表記と1文字も違わないか」で決める**ため、`0123`・`+1`・`1.50`・2の53乗を超えるID(`12345678901234567890`)は文字列のまま残り、郵便番号・電話番号・伝票番号・バージョン番号が壊れない。読み取りは RFC 4180 準拠で、引用符・`""` エスケープ・セル内改行・CRLF・BOM に対応する(書き出し側は Excel 向けに `bom` と `newline` を選べる)。JSONは配列・単体のオブジェクト・`{"data":[…]}` のように配列を包んだもの・1行1件のJSON Lines を受け取れる。**JSONの構文エラーは例外にせず、行・桁と前後の抜粋を持つ `ok: false` の結果として返す**ので、応答そのものが直しどころになる。`notes` では、列数が見出しと合わない行・重複した列名(`name_2` として残す)・空の見出し(`column4` を割り当てる)・桁落ちする数値・`=` `+` `-` `@` で始まり表計算ソフトが数式として実行しうるセル(CSVインジェクション)・キーの並びが揃わないオブジェクトを指摘する | `text?` または `path?`、`outputPath?`、`direction?`(既定 `auto` / `csv2json` / `json2csv`)、`delimiter?`(既定 `auto` / `comma` / `tab` / `semicolon` / `pipe`)、`header?`、`nest?`、`types?`、`trim?`、`emptyNull?`、`indent?`(既定 `2` / 0〜8 / `tab`)、`newline?`(既定 `lf` / `crlf`)、`quoteAll?`、`bom?`、`lang?`(既定 `ja` / `en`) |
189
191
 
190
192
  正確な入力スキーマ(Zod定義)は [`server.mjs`](./server.mjs) を参照してください。
191
193
 
package/csv-json.mjs ADDED
@@ -0,0 +1,865 @@
1
+ /* CSV/TSV ⇄ JSON 相互変換(csv_convert)
2
+ * 変換コア(csvParseDelimited / csvParseJson / csvToJson / csvFromJson / csvJsonConvert)は
3
+ * FirstCHTools の site/csv-json/app.js と同一
4
+ * (2箇所ルール: site側が正本・片方を直したらもう片方も同じ内容で反映する)。
5
+ * 両ファイルからコアの開始/終了コメントに挟まれた範囲を sed で切り出し
6
+ * (site側は字下げ2文字を落とす・こちらは行頭の export を落とす)、
7
+ * diff が空になることで同期を機械的に確認できる。
8
+ *
9
+ * 使っているのは String/RegExp/JSON だけなので、ブラウザ版のコードをそのまま持ってこられる。
10
+ * 完全ローカル処理・ネットワーク送信なし。 */
11
+ import { readFile, writeFile } from 'node:fs/promises';
12
+ import { basename } from 'node:path';
13
+
14
+ /* ==================== ここから変換コア(site / MCP で同一) ==================== */
15
+
16
+ // 区切り文字の呼び名 ⇄ 実体
17
+ export const CSV_DELIMS = { comma: ',', tab: '\t', semicolon: ';', pipe: '|' };
18
+ export const CSV_DELIM_NAMES = { ',': 'comma', '\t': 'tab', ';': 'semicolon', '|': 'pipe' };
19
+
20
+ // 数値として読み替えてよい形。先頭の0(0123)や + 記号を弾くのは、
21
+ // 数値にすると表記が変わってしまう(電話番号・郵便番号・伝票番号が壊れる)ため。
22
+ export const CSV_NUM_RE = /^-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?$/;
23
+ // 数値に見えるが表記が保てないもの(先頭の0・+付き・桁あふれ)を見つけるための緩い形
24
+ export const CSV_NUMISH_RE = /^[+-]?\d[\d.eE+-]*$/;
25
+ // 表計算ソフトが数式として解釈しはじめる先頭文字(CSVインジェクション)
26
+ export const CSV_FORMULA_RE = /^[=+\-@\t\r]/;
27
+
28
+ /** 先頭20行を見て区切り文字を推定する。引用符の中は数に入れない */
29
+ export function csvDetectDelimiter(text) {
30
+ const head = String(text).replace(/^/, '').split(/\r\n|\n|\r/).slice(0, 20).join('\n');
31
+ const bare = head.replace(/"(?:[^"]|"")*"/g, '');
32
+ const lines = bare.split('\n').filter((l) => l !== '');
33
+ if (!lines.length) return 'comma';
34
+ let best = 'comma';
35
+ let bestScore = 0;
36
+ for (const name of Object.keys(CSV_DELIMS)) {
37
+ const d = CSV_DELIMS[name];
38
+ const counts = lines.map((l) => l.split(d).length - 1);
39
+ if (!counts.length || counts[0] === 0) continue;
40
+ // 「どの行でも同じ個数で並んでいるか」を重く見る(本文にたまたま混ざった記号を選ばない)
41
+ const same = counts.filter((c) => c === counts[0]).length / counts.length;
42
+ const score = counts[0] * (0.2 + same);
43
+ if (score > bestScore) {
44
+ bestScore = score;
45
+ best = name;
46
+ }
47
+ }
48
+ return best;
49
+ }
50
+
51
+ /**
52
+ * 区切りテキストを行×セルへ割る(RFC 4180)。
53
+ * 引用符の中の改行・区切り記号・"" によるエスケープを扱い、
54
+ * 壊れた引用符は文字として読み進めたうえで指摘に残す。
55
+ * @returns {{rows: string[][], quirks: {stray: number[], newlineInCell: number}, error: object|null}}
56
+ */
57
+ export function csvParseDelimited(text, delim) {
58
+ const src = String(text);
59
+ const rows = [];
60
+ const stray = [];
61
+ let newlineInCell = 0;
62
+ let row = [];
63
+ let field = '';
64
+ let i = 0;
65
+ let line = 1;
66
+ let col = 1;
67
+ let quoted = false;
68
+ let openLine = 1;
69
+ let openCol = 1;
70
+ let fieldStarted = false; // 引用符を開いてよいのは、そのセルの先頭にいるときだけ
71
+
72
+ while (i < src.length) {
73
+ const ch = src[i];
74
+ if (quoted) {
75
+ if (ch === '"') {
76
+ if (src[i + 1] === '"') { field += '"'; i += 2; col += 2; continue; }
77
+ quoted = false;
78
+ i += 1;
79
+ col += 1;
80
+ continue;
81
+ }
82
+ if (ch === '\r' || ch === '\n') {
83
+ // 引用符の中の改行はセルの中身。改行コードはLFへ揃える
84
+ field += '\n';
85
+ i += ch === '\r' && src[i + 1] === '\n' ? 2 : 1;
86
+ line += 1;
87
+ col = 1;
88
+ newlineInCell += 1;
89
+ continue;
90
+ }
91
+ field += ch;
92
+ i += 1;
93
+ col += 1;
94
+ continue;
95
+ }
96
+ if (ch === '"') {
97
+ if (!fieldStarted) {
98
+ quoted = true;
99
+ fieldStarted = true;
100
+ openLine = line;
101
+ openCol = col;
102
+ i += 1;
103
+ col += 1;
104
+ continue;
105
+ }
106
+ // セルの途中や閉じたあとに出た " は文字として扱う(Excelが書いた壊れた行を読めるように)
107
+ if (stray.indexOf(line) === -1) stray.push(line);
108
+ field += ch;
109
+ i += 1;
110
+ col += 1;
111
+ continue;
112
+ }
113
+ if (ch === delim) {
114
+ row.push(field);
115
+ field = '';
116
+ fieldStarted = false;
117
+ i += 1;
118
+ col += 1;
119
+ continue;
120
+ }
121
+ if (ch === '\r' || ch === '\n') {
122
+ row.push(field);
123
+ rows.push(row);
124
+ row = [];
125
+ field = '';
126
+ fieldStarted = false;
127
+ i += ch === '\r' && src[i + 1] === '\n' ? 2 : 1;
128
+ line += 1;
129
+ col = 1;
130
+ continue;
131
+ }
132
+ field += ch;
133
+ fieldStarted = true;
134
+ i += 1;
135
+ col += 1;
136
+ }
137
+
138
+ if (quoted) {
139
+ return { rows: [], quirks: { stray, newlineInCell }, error: { code: 'UNCLOSED_QUOTE', line: openLine, col: openCol, span: 1 } };
140
+ }
141
+ row.push(field);
142
+ rows.push(row);
143
+ // 末尾の改行が作る空の行は落とす
144
+ if (rows.length && rows[rows.length - 1].length === 1 && rows[rows.length - 1][0] === '') rows.pop();
145
+ return { rows, quirks: { stray, newlineInCell }, error: null };
146
+ }
147
+
148
+ /** 列名を経路へ割る。a.b → ['a','b']、a[0].b / a.0.b → ['a',0,'b'] */
149
+ export function csvParsePath(key) {
150
+ const out = [];
151
+ for (const seg of String(key).replace(/\[(\d+)\]/g, '.$1').split('.')) {
152
+ if (seg === '') continue;
153
+ out.push(/^\d+$/.test(seg) ? Number(seg) : seg);
154
+ }
155
+ return out.length ? out : [String(key)];
156
+ }
157
+
158
+ /** 経路に沿って値を置く。数字の区間は配列になる。置けなければ false を返す */
159
+ export function csvSetPath(target, path, value) {
160
+ let node = target;
161
+ for (let i = 0; i < path.length - 1; i += 1) {
162
+ const seg = path[i];
163
+ const nextIsIndex = typeof path[i + 1] === 'number';
164
+ const cur = node[seg];
165
+ if (cur === undefined || cur === null) {
166
+ node[seg] = nextIsIndex ? [] : {};
167
+ } else if (typeof cur !== 'object') {
168
+ return false; // 同じ名前がスカラーで埋まっている(a と a.b が両方ある)
169
+ }
170
+ node = node[seg];
171
+ }
172
+ const last = path[path.length - 1];
173
+ if (node[last] !== undefined && typeof node[last] === 'object' && node[last] !== null) return false;
174
+ node[last] = value;
175
+ return true;
176
+ }
177
+
178
+ /** セル1つを値へ読み替える。読み替えないときは文字列のまま */
179
+ export function csvInferValue(raw, opts) {
180
+ const s = opts.trim ? raw.trim() : raw;
181
+ if (s === '') return opts.emptyNull ? null : '';
182
+ if (!opts.types) return s;
183
+ const lower = s.toLowerCase();
184
+ if (lower === 'true') return true;
185
+ if (lower === 'false') return false;
186
+ if (lower === 'null') return null;
187
+ // 「数値にして文字列へ戻したとき元通りか」で判定する。
188
+ // これだけで 0123・+1・1e999・9007199254740993 のような復元できない値を弾ける。
189
+ if (CSV_NUM_RE.test(s)) {
190
+ const num = Number(s);
191
+ if (Number.isFinite(num) && String(num) === s) return num;
192
+ }
193
+ return s;
194
+ }
195
+
196
+ /** 値の入れ子の深さ */
197
+ export function csvDepth(value) {
198
+ if (value === null || typeof value !== 'object') return 0;
199
+ let max = 0;
200
+ for (const key of Object.keys(value)) {
201
+ const d = csvDepth(value[key]);
202
+ if (d > max) max = d;
203
+ }
204
+ return max + 1;
205
+ }
206
+
207
+ /** JSONの値をCSVのセル1つへ平らにする。nest=true なら a.b / a.0 の列へ割る */
208
+ export function csvFlatten(value, prefix, out, nest, note) {
209
+ if (value === null || value === undefined) {
210
+ out.push([prefix, '']);
211
+ return;
212
+ }
213
+ if (typeof value !== 'object') {
214
+ out.push([prefix, typeof value === 'string' ? value : String(value)]);
215
+ return;
216
+ }
217
+ const keys = Array.isArray(value) ? value.map((_, i) => i) : Object.keys(value);
218
+ if (!nest || !keys.length) {
219
+ // 展開しない指定・空の {} と [] は、意味を落とさないようJSONの文字列として1セルに入れる
220
+ out.push([prefix, JSON.stringify(value)]);
221
+ if (keys.length) note.stringified += 1;
222
+ return;
223
+ }
224
+ if (Array.isArray(value)) note.arrays += 1;
225
+ for (const key of keys) {
226
+ csvFlatten(value[key], prefix === '' ? String(key) : prefix + '.' + key, out, nest, note);
227
+ }
228
+ }
229
+
230
+ /** セルを区切りテキストの1項目へ書く(RFC 4180 の引用規則) */
231
+ export function csvQuoteCell(s, delim, quoteAll) {
232
+ const need = quoteAll
233
+ || s.indexOf(delim) !== -1
234
+ || s.indexOf('"') !== -1
235
+ || /[\r\n]/.test(s)
236
+ || /^\s|\s$/.test(s);
237
+ return need ? '"' + s.replace(/"/g, '""') + '"' : s;
238
+ }
239
+
240
+ /* ---- JSONの読み取り(行・桁つき。ブラウザ差のあるメッセージに頼らない) ---- */
241
+
242
+ export function csvJsonError(code, line, col, found) {
243
+ return { code, line, col, span: 1, found: found === undefined ? '' : found };
244
+ }
245
+
246
+ /**
247
+ * JSONを読む。仕様どおりの厳密な読み取りで、失敗したときは行と桁を返す。
248
+ * @returns {{ok: true, value: *, duplicates: string[]}|{ok: false, error: object}}
249
+ */
250
+ export function csvParseJson(text) {
251
+ const src = String(text);
252
+ let i = 0;
253
+ let line = 1;
254
+ let col = 1;
255
+ const duplicates = [];
256
+
257
+ const fail = (code, found) => {
258
+ const err = csvJsonError(code, line, col, found);
259
+ throw err;
260
+ };
261
+ const step = (n) => {
262
+ for (let k = 0; k < n; k += 1) {
263
+ if (src[i] === '\n') { line += 1; col = 1; } else { col += 1; }
264
+ i += 1;
265
+ }
266
+ };
267
+ const ws = () => {
268
+ while (i < src.length && (src[i] === ' ' || src[i] === '\t' || src[i] === '\n' || src[i] === '\r')) step(1);
269
+ };
270
+ const readString = () => {
271
+ step(1); // 開きの "
272
+ let out = '';
273
+ while (true) {
274
+ if (i >= src.length) fail('UNEXPECTED_END');
275
+ const ch = src[i];
276
+ if (ch === '"') { step(1); return out; }
277
+ if (ch === '\n' || ch === '\r') fail('NEWLINE_IN_STRING');
278
+ if (ch === '\\') {
279
+ const esc = src[i + 1];
280
+ if (esc === undefined) fail('UNEXPECTED_END');
281
+ if ('"\\/'.indexOf(esc) !== -1) { out += esc; step(2); continue; }
282
+ if (esc === 'b') { out += '\b'; step(2); continue; }
283
+ if (esc === 'f') { out += '\f'; step(2); continue; }
284
+ if (esc === 'n') { out += '\n'; step(2); continue; }
285
+ if (esc === 'r') { out += '\r'; step(2); continue; }
286
+ if (esc === 't') { out += '\t'; step(2); continue; }
287
+ if (esc === 'u') {
288
+ const hex = src.slice(i + 2, i + 6);
289
+ if (!/^[0-9a-fA-F]{4}$/.test(hex)) fail('BAD_ESCAPE', '\\u' + hex);
290
+ out += String.fromCharCode(parseInt(hex, 16));
291
+ step(6);
292
+ continue;
293
+ }
294
+ fail('BAD_ESCAPE', '\\' + esc);
295
+ }
296
+ out += ch;
297
+ step(1);
298
+ }
299
+ };
300
+ const readValue = () => {
301
+ ws();
302
+ if (i >= src.length) fail('UNEXPECTED_END');
303
+ const ch = src[i];
304
+ if (ch === '{') {
305
+ step(1);
306
+ const obj = {};
307
+ ws();
308
+ if (src[i] === '}') { step(1); return obj; }
309
+ while (true) {
310
+ ws();
311
+ if (src[i] !== '"') fail('EXPECTED_KEY', src[i]);
312
+ const key = readString();
313
+ if (Object.prototype.hasOwnProperty.call(obj, key) && duplicates.indexOf(key) === -1) duplicates.push(key);
314
+ ws();
315
+ if (src[i] !== ':') fail('EXPECTED_COLON', src[i]);
316
+ step(1);
317
+ obj[key] = readValue();
318
+ ws();
319
+ if (src[i] === ',') { step(1); continue; }
320
+ if (src[i] === '}') { step(1); return obj; }
321
+ fail(i >= src.length ? 'UNEXPECTED_END' : 'EXPECTED_COMMA', src[i]);
322
+ }
323
+ }
324
+ if (ch === '[') {
325
+ step(1);
326
+ const arr = [];
327
+ ws();
328
+ if (src[i] === ']') { step(1); return arr; }
329
+ while (true) {
330
+ arr.push(readValue());
331
+ ws();
332
+ if (src[i] === ',') { step(1); continue; }
333
+ if (src[i] === ']') { step(1); return arr; }
334
+ fail(i >= src.length ? 'UNEXPECTED_END' : 'EXPECTED_COMMA', src[i]);
335
+ }
336
+ }
337
+ if (ch === '"') return readString();
338
+ if (src.startsWith('true', i)) { step(4); return true; }
339
+ if (src.startsWith('false', i)) { step(5); return false; }
340
+ if (src.startsWith('null', i)) { step(4); return null; }
341
+ const num = /^-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?/.exec(src.slice(i));
342
+ if (num) {
343
+ const value = Number(num[0]);
344
+ step(num[0].length);
345
+ return value;
346
+ }
347
+ fail('UNEXPECTED_CHAR', ch);
348
+ return null;
349
+ };
350
+
351
+ try {
352
+ const value = readValue();
353
+ ws();
354
+ if (i < src.length) fail('TRAILING_TEXT', src[i]);
355
+ return { ok: true, value, duplicates };
356
+ } catch (e) {
357
+ if (e && e.code) return { ok: false, error: e };
358
+ throw e;
359
+ }
360
+ }
361
+
362
+ /** 1行1JSON(JSON Lines / NDJSON)として読めるか試す */
363
+ export function csvParseJsonLines(text) {
364
+ const lines = String(text).split(/\r\n|\n|\r/).map((l) => l.trim()).filter((l) => l !== '');
365
+ if (lines.length < 2 || !lines.every((l) => l.startsWith('{') || l.startsWith('['))) return null;
366
+ const rows = [];
367
+ for (const l of lines) {
368
+ const r = csvParseJson(l);
369
+ if (!r.ok) return null;
370
+ rows.push(r.value);
371
+ }
372
+ return rows;
373
+ }
374
+
375
+ /** エラーの前後2行を抜き出す(画面で ^ を立てるため) */
376
+ export function csvExcerpt(text, error) {
377
+ if (!error) return null;
378
+ const lines = String(text).split(/\r\n|\n|\r/);
379
+ const from = Math.max(1, error.line - 2);
380
+ const to = Math.min(lines.length, error.line + 2);
381
+ const rows = [];
382
+ for (let n = from; n <= to; n += 1) rows.push({ line: n, text: lines[n - 1] || '', error: n === error.line });
383
+ return { rows, col: error.col, span: error.span || 1 };
384
+ }
385
+
386
+ /* ---- 変換の本体 ---- */
387
+
388
+ /**
389
+ * CSV/TSV → JSON
390
+ * @param {string} text
391
+ * @param {object} o delimiter/header/nest/types/trim/emptyNull/skipEmpty/indent
392
+ */
393
+ export function csvToJson(text, o) {
394
+ const notes = [];
395
+ const raw = String(text);
396
+ const hadBom = raw.charCodeAt(0) === 0xfeff;
397
+ const body = hadBom ? raw.slice(1) : raw;
398
+ if (hadBom) notes.push({ code: 'BOM', level: 'info' });
399
+ if (/\r\n/.test(body)) notes.push({ code: 'CRLF', level: 'info' });
400
+
401
+ const auto = !o.delimiter || o.delimiter === 'auto';
402
+ const name = auto ? csvDetectDelimiter(body) : o.delimiter;
403
+ const delim = CSV_DELIMS[name] || ',';
404
+ if (auto) notes.push({ code: 'DELIM_AUTO', level: 'info', delimiter: name });
405
+
406
+ const parsed = csvParseDelimited(body, delim);
407
+ if (parsed.error) return { ok: false, error: parsed.error, notes, excerpt: csvExcerpt(body, parsed.error) };
408
+
409
+ let rows = parsed.rows;
410
+ if (o.skipEmpty !== false) rows = rows.filter((r) => r.some((c) => c.trim() !== ''));
411
+ if (!rows.length) return { ok: true, empty: true, notes, delimiter: name };
412
+
413
+ if (parsed.quirks.stray.length) {
414
+ notes.push({ code: 'STRAY_QUOTE', level: 'warn', count: parsed.quirks.stray.length, lines: parsed.quirks.stray.slice(0, 6) });
415
+ }
416
+ if (parsed.quirks.newlineInCell) notes.push({ code: 'NEWLINE_IN_CELL', level: 'info', count: parsed.quirks.newlineInCell });
417
+
418
+ const width = Math.max.apply(null, rows.map((r) => r.length));
419
+ let headers = null;
420
+ let dataRows = rows;
421
+ if (o.header !== false) {
422
+ headers = rows[0].map((h) => (o.trim === false ? h : h.trim()));
423
+ dataRows = rows.slice(1);
424
+ const seen = {};
425
+ const dups = [];
426
+ let blank = 0;
427
+ headers = headers.map((h, idx) => {
428
+ let key = h;
429
+ if (key === '') {
430
+ blank += 1;
431
+ key = 'column' + (idx + 1);
432
+ }
433
+ if (Object.prototype.hasOwnProperty.call(seen, key)) {
434
+ if (dups.indexOf(key) === -1) dups.push(key);
435
+ seen[key] += 1;
436
+ key = key + '_' + seen[key];
437
+ } else {
438
+ seen[key] = 1;
439
+ }
440
+ return key;
441
+ });
442
+ if (dups.length) notes.push({ code: 'DUP_HEADER', level: 'warn', count: dups.length, items: dups.slice(0, 6) });
443
+ if (blank) notes.push({ code: 'EMPTY_HEADER', level: 'warn', count: blank });
444
+ // 見出しが1行しか無い=データが0件
445
+ if (!dataRows.length) notes.push({ code: 'HEADER_ONLY', level: 'info' });
446
+ } else {
447
+ notes.push({ code: 'NO_HEADER', level: 'info' });
448
+ }
449
+
450
+ const ragged = [];
451
+ const expect = headers ? headers.length : width;
452
+ dataRows.forEach((r, idx) => {
453
+ if (r.length !== expect) ragged.push(idx + (o.header !== false ? 2 : 1));
454
+ });
455
+ if (ragged.length) notes.push({ code: 'RAGGED', level: 'warn', count: ragged.length, lines: ragged.slice(0, 6), expect });
456
+
457
+ let bigNumbers = 0;
458
+ let keptText = 0;
459
+ let formulas = 0;
460
+ const conflicts = [];
461
+ const out = [];
462
+
463
+ for (const r of dataRows) {
464
+ let obj;
465
+ if (headers) {
466
+ obj = {};
467
+ for (let c = 0; c < headers.length; c += 1) {
468
+ const cell = r[c] === undefined ? '' : r[c];
469
+ if (CSV_FORMULA_RE.test(cell)) formulas += 1;
470
+ if (o.types !== false && cell !== '' && CSV_NUM_RE.test(cell) && String(Number(cell)) !== cell) bigNumbers += 1;
471
+ else if (o.types !== false && cell !== '' && !CSV_NUM_RE.test(cell) && CSV_NUMISH_RE.test(cell)) keptText += 1;
472
+ const value = csvInferValue(cell, o);
473
+ const nested = o.nest !== false && (headers[c].indexOf('.') !== -1 || /\[\d+\]/.test(headers[c]));
474
+ if (nested) {
475
+ if (!csvSetPath(obj, csvParsePath(headers[c]), value)) {
476
+ if (conflicts.indexOf(headers[c]) === -1) conflicts.push(headers[c]);
477
+ obj[headers[c]] = value;
478
+ }
479
+ } else {
480
+ obj[headers[c]] = value;
481
+ }
482
+ }
483
+ } else {
484
+ obj = r.map((cell) => {
485
+ if (CSV_FORMULA_RE.test(cell)) formulas += 1;
486
+ return csvInferValue(cell, o);
487
+ });
488
+ }
489
+ out.push(obj);
490
+ }
491
+
492
+ if (conflicts.length) notes.push({ code: 'NEST_CONFLICT', level: 'warn', count: conflicts.length, items: conflicts.slice(0, 6) });
493
+ if (bigNumbers) notes.push({ code: 'BIG_NUMBER', level: 'warn', count: bigNumbers });
494
+ if (keptText) notes.push({ code: 'KEPT_TEXT', level: 'info', count: keptText });
495
+ if (formulas) notes.push({ code: 'FORMULA', level: 'warn', count: formulas });
496
+
497
+ const indent = o.indent === 'tab' ? '\t' : Number(o.indent === undefined ? 2 : o.indent);
498
+ const output = JSON.stringify(out, null, indent) + '\n';
499
+ let depth = 0;
500
+ for (const obj of out) {
501
+ const d = csvDepth(obj);
502
+ if (d > depth) depth = d;
503
+ }
504
+
505
+ return {
506
+ ok: true,
507
+ output,
508
+ json: out,
509
+ delimiter: name,
510
+ columns: headers || [],
511
+ notes,
512
+ stats: { rows: out.length, columns: expect, cells: out.length * expect, depth },
513
+ };
514
+ }
515
+
516
+ /**
517
+ * JSON → CSV/TSV
518
+ * @param {string} text
519
+ * @param {object} o delimiter/header/nest/newline/quoteAll/bom
520
+ */
521
+ export function csvFromJson(text, o) {
522
+ const notes = [];
523
+ const src = String(text);
524
+ const trimmed = src.trim();
525
+ if (trimmed === '') return { ok: true, empty: true, notes };
526
+
527
+ let data;
528
+ const jsonl = csvParseJsonLines(trimmed);
529
+ if (jsonl) {
530
+ data = jsonl;
531
+ notes.push({ code: 'JSONL', level: 'info', count: jsonl.length });
532
+ } else {
533
+ const parsed = csvParseJson(trimmed);
534
+ if (!parsed.ok) return { ok: false, error: parsed.error, notes, excerpt: csvExcerpt(src, parsed.error) };
535
+ data = parsed.value;
536
+ if (parsed.duplicates.length) {
537
+ notes.push({ code: 'DUP_KEY', level: 'warn', count: parsed.duplicates.length, items: parsed.duplicates.slice(0, 6) });
538
+ }
539
+ }
540
+
541
+ if (!Array.isArray(data)) {
542
+ // 配列でない=1件のオブジェクト、または { data: [...] } のような包み
543
+ if (data && typeof data === 'object') {
544
+ const keys = Object.keys(data);
545
+ const wrapped = keys.filter((k) => Array.isArray(data[k]));
546
+ if (wrapped.length === 1 && keys.length <= 3) {
547
+ notes.push({ code: 'UNWRAPPED', level: 'info', key: wrapped[0] });
548
+ data = data[wrapped[0]];
549
+ } else {
550
+ notes.push({ code: 'NOT_ARRAY', level: 'info' });
551
+ data = [data];
552
+ }
553
+ } else {
554
+ notes.push({ code: 'NOT_ARRAY', level: 'info' });
555
+ data = [data];
556
+ }
557
+ }
558
+ if (!data.length) return { ok: true, empty: true, notes };
559
+
560
+ const nest = o.nest !== false;
561
+ const flatNote = { stringified: 0, arrays: 0 };
562
+ const columns = [];
563
+ const rowMaps = [];
564
+ let scalarRows = 0;
565
+
566
+ for (const item of data) {
567
+ const pairs = [];
568
+ if (item === null || typeof item !== 'object') {
569
+ scalarRows += 1;
570
+ pairs.push(['value', item === null ? '' : String(item)]);
571
+ } else if (Array.isArray(item)) {
572
+ item.forEach((v, idx) => csvFlatten(v, String(idx + 1), pairs, nest, flatNote));
573
+ } else {
574
+ for (const key of Object.keys(item)) csvFlatten(item[key], key, pairs, nest, flatNote);
575
+ }
576
+ const map = {};
577
+ for (const [key, value] of pairs) {
578
+ map[key] = value;
579
+ if (columns.indexOf(key) === -1) columns.push(key);
580
+ }
581
+ rowMaps.push(map);
582
+ }
583
+
584
+ const missing = rowMaps.filter((m) => Object.keys(m).length !== columns.length).length;
585
+ if (missing) notes.push({ code: 'MIXED_KEYS', level: 'warn', count: missing, columns: columns.length });
586
+ if (flatNote.stringified) notes.push({ code: 'STRINGIFIED', level: 'info', count: flatNote.stringified });
587
+ if (scalarRows) notes.push({ code: 'SCALAR_ROWS', level: 'info', count: scalarRows });
588
+
589
+ const name = !o.delimiter || o.delimiter === 'auto' ? 'comma' : o.delimiter;
590
+ const delim = CSV_DELIMS[name] || ',';
591
+ const nl = o.newline === 'crlf' ? '\r\n' : '\n';
592
+ const quoteAll = Boolean(o.quoteAll);
593
+ const lines = [];
594
+ if (o.header !== false) lines.push(columns.map((c) => csvQuoteCell(c, delim, quoteAll)).join(delim));
595
+ let formulas = 0;
596
+ let multiline = 0;
597
+ for (const map of rowMaps) {
598
+ const cells = columns.map((c) => {
599
+ const v = map[c] === undefined ? '' : map[c];
600
+ if (CSV_FORMULA_RE.test(v)) formulas += 1;
601
+ if (/[\r\n]/.test(v)) multiline += 1;
602
+ return csvQuoteCell(v, delim, quoteAll);
603
+ });
604
+ lines.push(cells.join(delim));
605
+ }
606
+ if (formulas) notes.push({ code: 'FORMULA', level: 'warn', count: formulas });
607
+ if (multiline) notes.push({ code: 'NEWLINE_IN_CELL', level: 'info', count: multiline });
608
+
609
+ const output = (o.bom ? '' : '') + lines.join(nl) + nl;
610
+ let depth = 0;
611
+ for (const item of data) {
612
+ const d = csvDepth(item);
613
+ if (d > depth) depth = d;
614
+ }
615
+
616
+ return {
617
+ ok: true,
618
+ output,
619
+ delimiter: name,
620
+ columns,
621
+ notes,
622
+ stats: { rows: rowMaps.length, columns: columns.length, cells: rowMaps.length * columns.length, depth },
623
+ };
624
+ }
625
+
626
+ /**
627
+ * CSV/TSV ⇄ JSON の相互変換。
628
+ *
629
+ * @param {object} opts
630
+ * @param {'csv2json'|'json2csv'} opts.direction 変換の向き
631
+ * @param {string} opts.text 入力
632
+ * @param {'auto'|'comma'|'tab'|'semicolon'|'pipe'} [opts.delimiter='auto'] 区切り文字(json2csv では auto=カンマ)
633
+ * @param {boolean} [opts.header=true] 1行目を見出しとして扱う/書き出す
634
+ * @param {boolean} [opts.nest=true] a.b 列 ⇄ 入れ子オブジェクト
635
+ * @param {boolean} [opts.types=true] true/false/null と数値を読み替える(csv2json)
636
+ * @param {boolean} [opts.trim=true] セル前後の空白を落とす(csv2json)
637
+ * @param {boolean} [opts.emptyNull=false] 空のセルを null にする(csv2json)
638
+ * @param {number|'tab'} [opts.indent=2] JSONのインデント(csv2json)
639
+ * @param {'lf'|'crlf'} [opts.newline='lf'] 改行コード(json2csv)
640
+ * @param {boolean} [opts.quoteAll=false] すべてのセルを引用符で囲む(json2csv)
641
+ * @param {boolean} [opts.bom=false] 先頭にBOMを付ける(json2csv・Excel向け)
642
+ */
643
+ export function csvJsonConvert(opts) {
644
+ const o = opts || {};
645
+ const text = typeof o.text === 'string' ? o.text : '';
646
+ const settings = {
647
+ delimiter: o.delimiter || 'auto',
648
+ header: o.header !== false,
649
+ nest: o.nest !== false,
650
+ types: o.types !== false,
651
+ trim: o.trim !== false,
652
+ emptyNull: Boolean(o.emptyNull),
653
+ skipEmpty: o.skipEmpty !== false,
654
+ indent: o.indent === undefined ? 2 : o.indent,
655
+ newline: o.newline === 'crlf' ? 'crlf' : 'lf',
656
+ quoteAll: Boolean(o.quoteAll),
657
+ bom: Boolean(o.bom),
658
+ };
659
+ if (text.trim() === '') return { ok: true, empty: true, notes: [], settings, input_bytes: 0, output_bytes: 0 };
660
+
661
+ const r = o.direction === 'json2csv' ? csvFromJson(text, settings) : csvToJson(text, settings);
662
+ r.direction = o.direction === 'json2csv' ? 'json2csv' : 'csv2json';
663
+ r.settings = settings;
664
+ r.input_bytes = csvByteLength(text);
665
+ r.output_bytes = r.output ? csvByteLength(r.output) : 0;
666
+ if (!r.notes) r.notes = [];
667
+ return r;
668
+ }
669
+
670
+ /** UTF-8としてのバイト数 */
671
+ export function csvByteLength(s) {
672
+ let bytes = 0;
673
+ for (const ch of String(s)) {
674
+ const code = ch.codePointAt(0);
675
+ bytes += code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4;
676
+ }
677
+ return bytes;
678
+ }
679
+
680
+ /* ==================== ここまで変換コア ==================== */
681
+
682
+ /* ---- ここから下はMCP側だけの層(入出力・文言)。site側の app.js には UI 用の同じ文言表がある ---- */
683
+
684
+ export class CsvConvertError extends Error {}
685
+
686
+ const DELIM_JA = { comma: 'カンマ', tab: 'タブ', semicolon: 'セミコロン', pipe: 'パイプ' };
687
+ const DELIM_EN = { comma: 'comma', tab: 'tab', semicolon: 'semicolon', pipe: 'pipe' };
688
+
689
+ const ERR_JA = {
690
+ UNCLOSED_QUOTE: () => '引用符 " が閉じられていません。ここから後ろがすべて1つのセルとして読まれています。セルの中の " は "" と2つ重ねて書きます。',
691
+ UNEXPECTED_END: () => 'JSONが途中で終わっています。閉じ括弧か引用符が足りません。',
692
+ NEWLINE_IN_STRING: () => '文字列の途中で改行しています。JSONの文字列に生の改行は書けないので \\n と書きます。',
693
+ BAD_ESCAPE: (e) => `使えないエスケープです(${e.found})。JSONで書けるのは \\" \\\\ \\/ \\b \\f \\n \\r \\t と \\u に続く16進4桁だけです。`,
694
+ EXPECTED_KEY: (e) => `オブジェクトのキーが要ります(${e.found ? '見つかったのは ' + e.found : '入力がここで終わっています'})。キーは必ず " で囲みます。`,
695
+ EXPECTED_COLON: () => 'キーの後ろにコロン : が要ります。',
696
+ EXPECTED_COMMA: (e) => `区切りのカンマか閉じ括弧が要ります(${e.found ? '見つかったのは ' + e.found : '入力がここで終わっています'})。最後の要素の後ろにカンマは書けません。`,
697
+ UNEXPECTED_CHAR: (e) => `値として読めない文字です(${e.found})。文字列は " で囲み、true / false / null は小文字で書きます。`,
698
+ TRAILING_TEXT: (e) => `JSONの終わりの後ろに文字が残っています(${e.found})。1行1件のJSON Lines として渡すこともできます。`,
699
+ };
700
+
701
+ const ERR_EN = {
702
+ UNCLOSED_QUOTE: () => 'A quotation mark is never closed, so everything after it is being read as one cell. Write a literal " as "" inside a quoted cell.',
703
+ UNEXPECTED_END: () => 'The JSON ends early — a closing bracket or quote is missing.',
704
+ NEWLINE_IN_STRING: () => 'A string contains a raw line break. JSON strings must write it as \\n.',
705
+ BAD_ESCAPE: (e) => `Unsupported escape (${e.found}). JSON allows \\" \\\\ \\/ \\b \\f \\n \\r \\t and \\u followed by four hex digits.`,
706
+ EXPECTED_KEY: (e) => `An object key is expected here${e.found ? ` (found ${e.found})` : ' (the input ends here)'}. Keys must be quoted with ".`,
707
+ EXPECTED_COLON: () => 'A colon : is expected after the key.',
708
+ EXPECTED_COMMA: (e) => `A comma or a closing bracket is expected${e.found ? ` (found ${e.found})` : ' (the input ends here)'}. A trailing comma after the last element is not valid JSON.`,
709
+ UNEXPECTED_CHAR: (e) => `This character cannot start a value (${e.found}). Strings need double quotes, and true / false / null are lower case.`,
710
+ TRAILING_TEXT: (e) => `There is text after the end of the JSON value (${e.found}). You can also paste JSON Lines — one object per line.`,
711
+ };
712
+
713
+ const NOTE_JA = {
714
+ BOM: () => '先頭にBOMが付いていました(Excelが書いたCSVによくあります)。読み取り時に取り除いています。',
715
+ CRLF: () => '改行コードはCRLF(Windows)です。JSONへの変換では影響しません。',
716
+ DELIM_AUTO: (n) => `区切り文字は${DELIM_JA[n.delimiter]}と判定しました。ずれている場合は delimiter で固定できます。`,
717
+ STRAY_QUOTE: (n) => `セルの途中に閉じられていない引用符があります(${n.count}行)。文字として読み進めましたが、書き出した側が壊れている可能性があります。`,
718
+ NEWLINE_IN_CELL: (n) => `セルの中に改行を含む値が ${n.count} 件あります。引用符で囲んで保持しています。`,
719
+ DUP_HEADER: (n) => `見出しに同じ列名が ${n.count} 組あります: ${n.items.join(' / ')} — 後ろの列は ${n.items[0]}_2 のように連番を付けて区別しました。`,
720
+ EMPTY_HEADER: (n) => `見出しが空の列が ${n.count} 列あります。column1 のような仮の名前を付けています。`,
721
+ HEADER_ONLY: () => '見出し行だけでデータ行がありません。結果は空の配列になります。',
722
+ NO_HEADER: () => '見出し行なしとして読んだので、各行は配列(値の並び)になります。',
723
+ RAGGED: (n) => `列数が見出しと合わない行が ${n.count} 行あります(見出しは ${n.expect} 列)。足りない分は空、多い分は捨てています。`,
724
+ NEST_CONFLICT: (n) => `入れ子にできない列名が ${n.count} 件あります: ${n.items.join(' / ')} — 同じ名前がすでに値で埋まっているため、その列は平らなキーのままにしました。`,
725
+ BIG_NUMBER: (n) => `数値として正確に表せない値が ${n.count} 件あります(2の53乗を超える整数など)。桁落ちを避けるため文字列のまま残しました。`,
726
+ KEPT_TEXT: (n) => `数字に見えるが表記が変わってしまう値が ${n.count} 件あります(0123・+1・090-1234-5678 など)。文字列のまま残しました。`,
727
+ FORMULA: (n) => `=・+・-・@ で始まるセルが ${n.count} 件あります。表計算ソフトが数式として実行することがあるため、外部から受け取ったCSVでは中身を確認してください。`,
728
+ JSONL: (n) => `1行1JSON(JSON Lines)として ${n.count} 件読み取りました。`,
729
+ DUP_KEY: (n) => `同じキーが重複しているオブジェクトがあります: ${n.items.join(' / ')} — 後に書いた値が勝ちます。`,
730
+ UNWRAPPED: (n) => `配列を包んでいるキー「${n.key}」の中身を表にしました。`,
731
+ NOT_ARRAY: () => '配列ではなかったので、1件のデータとして1行に書き出しました。',
732
+ MIXED_KEYS: (n) => `キーの並びが他と違うオブジェクトが ${n.count} 件あります。全体で ${n.columns} 列に揃え、無い項目は空にしました。`,
733
+ STRINGIFIED: (n) => `入れ子の値 ${n.count} 件を、JSONの文字列としてセルに入れました。nest=true にすると列へ割れます。`,
734
+ SCALAR_ROWS: (n) => `オブジェクトでない要素が ${n.count} 件あります。value 列に入れました。`,
735
+ };
736
+
737
+ const NOTE_EN = {
738
+ BOM: () => 'The input started with a BOM (common in CSV written by Excel). It was removed while reading.',
739
+ CRLF: () => 'Line endings are CRLF (Windows). This does not affect the JSON output.',
740
+ DELIM_AUTO: (n) => `Detected the ${DELIM_EN[n.delimiter]} as the delimiter. Set delimiter explicitly if that is wrong.`,
741
+ STRAY_QUOTE: (n) => `A quotation mark appears in the middle of a cell on ${n.count} line(s). It was read as a literal character, but whatever produced this file may be broken.`,
742
+ NEWLINE_IN_CELL: (n) => `${n.count} value(s) contain a line break. They are kept, wrapped in quotes.`,
743
+ DUP_HEADER: (n) => `${n.count} header name(s) appear more than once: ${n.items.join(' / ')} — later columns were numbered (${n.items[0]}_2) to keep them apart.`,
744
+ EMPTY_HEADER: (n) => `${n.count} column(s) have an empty header. They were named column1, column2 and so on.`,
745
+ HEADER_ONLY: () => 'There is a header row but no data rows, so the result is an empty array.',
746
+ NO_HEADER: () => 'Read without a header row, so every row becomes an array of values.',
747
+ RAGGED: (n) => `${n.count} row(s) do not have the same number of columns as the header (${n.expect}). Missing cells are empty; extra cells are dropped.`,
748
+ NEST_CONFLICT: (n) => `${n.count} column(s) could not be nested: ${n.items.join(' / ')} — the same name already holds a value, so they were kept as flat keys.`,
749
+ BIG_NUMBER: (n) => `${n.count} value(s) cannot be represented exactly as a number (integers beyond 2^53, for example). They were kept as strings so no digits are lost.`,
750
+ KEPT_TEXT: (n) => `${n.count} value(s) look numeric but would change if converted (0123, +1, 090-1234-5678). They were kept as strings.`,
751
+ FORMULA: (n) => `${n.count} cell(s) start with =, +, - or @. Spreadsheets may execute those as formulas, so check them when the CSV came from outside.`,
752
+ JSONL: (n) => `Read ${n.count} records as JSON Lines (one object per line).`,
753
+ DUP_KEY: (n) => `Some objects repeat the same key: ${n.items.join(' / ')} — the last one wins.`,
754
+ UNWRAPPED: (n) => `Used the array inside the wrapper key “${n.key}” as the table.`,
755
+ NOT_ARRAY: () => 'The value was not an array, so it was written as a single row.',
756
+ MIXED_KEYS: (n) => `${n.count} object(s) have a different set of keys. The table was squared off to ${n.columns} columns and missing values are empty.`,
757
+ STRINGIFIED: (n) => `${n.count} nested value(s) were written into a single cell as JSON text. Set nest=true to split them into columns.`,
758
+ SCALAR_ROWS: (n) => `${n.count} element(s) are not objects. They were placed in a “value” column.`,
759
+ };
760
+
761
+ /** 向きを指定されなかったときの推定。JSONは [ か { で始まる */
762
+ export function csvGuessDirection(text) {
763
+ const head = String(text).replace(/^/, '').trim();
764
+ return head.startsWith('[') || head.startsWith('{') ? 'json2csv' : 'csv2json';
765
+ }
766
+
767
+ /**
768
+ * MCPツール csv_convert の本体。text / path を受け取り、変換結果と指摘事項を返す。
769
+ * 変換そのものは csvJsonConvert(site版と同一のコア)に任せ、ここは入出力と文言だけを持つ。
770
+ */
771
+ export async function csvConvertTool(opts = {}) {
772
+ const o = opts || {};
773
+ const lang = o.lang === undefined ? 'ja' : o.lang;
774
+ if (lang !== 'ja' && lang !== 'en') throw new CsvConvertError(`lang は ja か en: ${o.lang}`);
775
+
776
+ const hasText = typeof o.text === 'string';
777
+ if (hasText === Boolean(o.path)) throw new CsvConvertError('text か path のどちらか一方を渡してください');
778
+
779
+ if (o.delimiter !== undefined && o.delimiter !== 'auto' && !CSV_DELIMS[o.delimiter]) {
780
+ throw new CsvConvertError(`delimiter は auto / ${Object.keys(CSV_DELIMS).join(' / ')} のいずれか: ${o.delimiter}`);
781
+ }
782
+ if (o.indent !== undefined && o.indent !== 'tab' && !(Number.isInteger(o.indent) && o.indent >= 0 && o.indent <= 8)) {
783
+ throw new CsvConvertError(`indent は 0-8 の整数か "tab": ${o.indent}`);
784
+ }
785
+ if (o.newline !== undefined && o.newline !== 'lf' && o.newline !== 'crlf') {
786
+ throw new CsvConvertError(`newline は lf か crlf: ${o.newline}`);
787
+ }
788
+ if (o.direction !== undefined && ['auto', 'csv2json', 'json2csv'].indexOf(o.direction) === -1) {
789
+ throw new CsvConvertError(`direction は auto / csv2json / json2csv のいずれか: ${o.direction}`);
790
+ }
791
+
792
+ const input = hasText ? o.text : await readFile(o.path, 'utf8');
793
+ const guessed = !o.direction || o.direction === 'auto';
794
+ const direction = guessed ? csvGuessDirection(input) : o.direction;
795
+
796
+ const r = csvJsonConvert({
797
+ direction,
798
+ text: input,
799
+ delimiter: o.delimiter,
800
+ header: o.header,
801
+ nest: o.nest,
802
+ types: o.types,
803
+ trim: o.trim,
804
+ emptyNull: o.emptyNull,
805
+ indent: o.indent,
806
+ newline: o.newline,
807
+ quoteAll: o.quoteAll,
808
+ bom: o.bom,
809
+ });
810
+
811
+ const source = hasText ? { type: 'text' } : { path: o.path, name: basename(o.path) };
812
+
813
+ if (r.ok === false) {
814
+ const table = lang === 'en' ? ERR_EN : ERR_JA;
815
+ const e = r.error;
816
+ const message = table[e.code] ? table[e.code](e) : e.code;
817
+ // 入力が壊れているのは「呼び出し方の誤り」ではないので、例外ではなく結果として返す。
818
+ // 行・桁・前後の抜粋がそのまま直しどころになる。
819
+ return {
820
+ ok: false,
821
+ direction,
822
+ direction_guessed: guessed,
823
+ source,
824
+ error: { code: e.code, message, line: e.line, column: e.col, found: e.found },
825
+ excerpt: r.excerpt,
826
+ notes: renderNotes(r.notes, lang),
827
+ };
828
+ }
829
+
830
+ const result = {
831
+ ok: true,
832
+ direction,
833
+ direction_guessed: guessed,
834
+ source,
835
+ settings: r.settings,
836
+ notes: renderNotes(r.notes, lang),
837
+ };
838
+ if (r.empty) {
839
+ result.empty = true;
840
+ return result;
841
+ }
842
+ result.delimiter = r.delimiter;
843
+ result.columns = r.columns;
844
+ result.stats = r.stats;
845
+ result.input_bytes = r.input_bytes;
846
+ result.output_bytes = r.output_bytes;
847
+
848
+ if (o.outputPath) {
849
+ await writeFile(o.outputPath, r.output, 'utf8');
850
+ result.output_path = o.outputPath;
851
+ // ファイルへ書けたなら本文は重複した重い情報でしかない
852
+ } else {
853
+ result.output = r.output;
854
+ }
855
+ return result;
856
+ }
857
+
858
+ function renderNotes(notes, lang) {
859
+ const table = lang === 'en' ? NOTE_EN : NOTE_JA;
860
+ return (notes || []).map((n) => ({
861
+ code: n.code,
862
+ level: n.level || 'info',
863
+ message: table[n.code] ? table[n.code](n) : n.code,
864
+ }));
865
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@first-ch/tools-mcp",
3
- "version": "0.26.0",
4
- "description": "First CH Tools の計算ロジック(WCAGコントラスト比・文字数/Xウェイト・WebP変換・JSON-LD生成・llms.txt生成・文字コード/改行コード変換・Marp Markdown→スライド レンダリング・テストデータ生成・テキスト/コード差分・Cron式の解説・Base64/Data URI変換・URLパラメータの分解/再構築・HTML特殊文字のエスケープ/エンティティのデコード・JSON⇄YAMLの相互変換・px⇄rem/emの単位換算とCSSの一括変換・カラーコードの相互変換とアルファ透過の合成・MD5/SHA-1/SHA-256/SHA-384/SHA-512のハッシュ生成と照合・JWTのデコードと有効期限/署名の検証・User-Agent文字列の解析とデバイス判定・UUID v4/ULIDの一括生成・アスペクト比の計算とレスポンシブサイズの算出・TSV/CSVからMarkdownテーブルへの整形と相互変換・SQLクエリの整形と予約語の大文字化・URL/テキストからのQRコード生成・UNIXタイムスタンプと日時表記の相互変換・robots.txtの生成(AIクローラーの許可プリセット付き)・文字列のケース変換(camelCase/snake_case/kebab-case など11形式))をMCPツールとして提供するサーバー",
3
+ "version": "0.27.0",
4
+ "description": "First CH Tools の計算ロジック(WCAGコントラスト比・文字数/Xウェイト・WebP変換・JSON-LD生成・llms.txt生成・文字コード/改行コード変換・Marp Markdown→スライド レンダリング・テストデータ生成・テキスト/コード差分・Cron式の解説・Base64/Data URI変換・URLパラメータの分解/再構築・HTML特殊文字のエスケープ/エンティティのデコード・JSON⇄YAMLの相互変換・px⇄rem/emの単位換算とCSSの一括変換・カラーコードの相互変換とアルファ透過の合成・MD5/SHA-1/SHA-256/SHA-384/SHA-512のハッシュ生成と照合・JWTのデコードと有効期限/署名の検証・User-Agent文字列の解析とデバイス判定・UUID v4/ULIDの一括生成・アスペクト比の計算とレスポンシブサイズの算出・TSV/CSVからMarkdownテーブルへの整形と相互変換・SQLクエリの整形と予約語の大文字化・URL/テキストからのQRコード生成・UNIXタイムスタンプと日時表記の相互変換・robots.txtの生成(AIクローラーの許可プリセット付き)・文字列のケース変換(camelCase/snake_case/kebab-case など11形式)・CSV/TSVとJSON配列の相互変換(区切りの自動判定・a.b 列と入れ子の相互展開))をMCPツールとして提供するサーバー",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "firstch-tools-mcp": "server.mjs"
@@ -191,6 +191,7 @@
191
191
  "unixtime.mjs",
192
192
  "robots-txt.mjs",
193
193
  "case-convert.mjs",
194
+ "csv-json.mjs",
194
195
  "vendor/",
195
196
  "README.md",
196
197
  "LICENSE"
package/server.mjs CHANGED
@@ -29,6 +29,7 @@ import { qrGenerateTool } from './qr.mjs';
29
29
  import { unixtimeConvert } from './unixtime.mjs';
30
30
  import { robotsTxtGenerate } from './robots-txt.mjs';
31
31
  import { caseConvertTool } from './case-convert.mjs';
32
+ import { csvConvertTool } from './csv-json.mjs';
32
33
 
33
34
  const { version } = createRequire(import.meta.url)('./package.json');
34
35
  const server = new McpServer({ name: 'firstch-tools', version });
@@ -1241,4 +1242,53 @@ server.registerTool(
1241
1242
  },
1242
1243
  );
1243
1244
 
1245
+ server.registerTool(
1246
+ 'csv_convert',
1247
+ {
1248
+ title: 'CSV/TSV ⇄ JSON の相互変換',
1249
+ description:
1250
+ 'CSV/TSVとJSON配列を相互変換する(tools.first-ch.com/csv-json/ と同一ロジック)。' +
1251
+ '表計算ソフトから書き出した表をAPIに渡せるJSONにする、逆にAPIの応答をExcelで開ける表にする、といった用途に使う。' +
1252
+ 'direction を省くと入力の先頭が [ か { なら json2csv、そうでなければ csv2json と推定する。' +
1253
+ '区切り文字は先頭20行で「どの記号が各行に同じ個数だけ現れるか」を見て自動判定し、delimiter で固定もできる。' +
1254
+ '**stock.qty のような列名は入れ子のオブジェクトとして読み書きし、tags.0 / tags.1(tags[0] も同義)は配列になる**。' +
1255
+ '逆向きも同じ規則で平らにするので、CSVへ出してJSONへ戻すと元の構造に復元される(nest=false なら入れ子はJSONの文字列としてセル1つに入る)。' +
1256
+ '**型の読み替えは「一度数値にして文字列へ戻したとき元の表記と1文字も違わないか」で決める**ため、' +
1257
+ '0123・+1・1.50・2の53乗を超えるID(12345678901234567890)は文字列のまま残り、郵便番号・電話番号・伝票番号・バージョン番号が壊れない。' +
1258
+ '読み取りは RFC 4180 準拠で、引用符・"" エスケープ・セル内改行・CRLF・BOM に対応する(書き出し側は bom / newline を選べる)。' +
1259
+ 'JSONは配列・単体のオブジェクト・{"data":[…]} のように配列を包んだもの・1行1件のJSON Lines を受け取れる。' +
1260
+ '**JSONの構文エラーは行・桁と前後の抜粋を返す**(例外にせず ok:false の結果として返すので、そのまま直しどころになる)。' +
1261
+ 'あわせて、列数が見出しと合わない行・重複した列名(name_2 として残す)・空の見出し(column4 を割り当てる)・桁落ちする数値・' +
1262
+ '= + - @ で始まり表計算ソフトが数式として実行しうるセル(CSVインジェクション)・キーの並びが揃わないオブジェクトを notes で指摘する。' +
1263
+ 'outputPath を渡すとそのパスへ書き出す(本文は返さない)。完全ローカル処理・ネットワーク送信なし。',
1264
+ inputSchema: {
1265
+ text: z.string().optional().describe('変換する入力(CSV/TSV か JSON。path と排他)'),
1266
+ path: z.string().optional().describe('変換するファイルの絶対パス(UTF-8として読む。text と排他)'),
1267
+ outputPath: z.string().optional().describe('結果を書き出す絶対パス(指定すると本文は返さない)'),
1268
+ direction: z
1269
+ .enum(['auto', 'csv2json', 'json2csv'])
1270
+ .optional()
1271
+ .describe("変換の向き(既定 'auto'=入力の先頭が [ か { なら json2csv)"),
1272
+ delimiter: z
1273
+ .enum(['auto', 'comma', 'tab', 'semicolon', 'pipe'])
1274
+ .optional()
1275
+ .describe("区切り文字(既定 'auto'。json2csv では auto はカンマ)"),
1276
+ header: z.boolean().optional().describe('1行目を見出しとして扱う/書き出す(既定 true。false なら各行は配列になる)'),
1277
+ nest: z.boolean().optional().describe('a.b 列 ⇄ 入れ子オブジェクトを相互展開する(既定 true)'),
1278
+ types: z.boolean().optional().describe('数値・true/false/null を読み替える(既定 true・csv2json のみ)'),
1279
+ trim: z.boolean().optional().describe('セル前後の空白を落とす(既定 true・csv2json のみ)'),
1280
+ emptyNull: z.boolean().optional().describe('空のセルを null にする(既定 false=空文字列・csv2json のみ)'),
1281
+ indent: z.union([z.number().int().min(0).max(8), z.literal('tab')]).optional().describe('JSONのインデント(既定 2・csv2json のみ)'),
1282
+ newline: z.enum(['lf', 'crlf']).optional().describe("改行コード(既定 'lf'・json2csv のみ)"),
1283
+ quoteAll: z.boolean().optional().describe('すべてのセルを引用符で囲む(既定 false・json2csv のみ)'),
1284
+ bom: z.boolean().optional().describe('先頭にBOMを付ける(既定 false。ExcelでUTF-8のCSVを開くとき・json2csv のみ)'),
1285
+ lang: z.enum(['ja', 'en']).optional().describe("指摘事項とエラーの言語(既定 'ja')"),
1286
+ },
1287
+ },
1288
+ async (opts) => {
1289
+ await logUsage('csv_convert');
1290
+ return asText(await csvConvertTool(opts));
1291
+ },
1292
+ );
1293
+
1244
1294
  await server.connect(new StdioServerTransport());