@first-ch/tools-mcp 0.36.0 → 0.37.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@first-ch/tools-mcp",
3
- "version": "0.36.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 列と入れ子の相互展開)・全角⇄半角の変換とテキストの掃除(文字種ごとの向き指定・半角カナの合成/分解・空白と空行の整理)・IPアドレス/CIDRの計算(ネットワーク・ブロードキャスト・ホスト範囲とホスト数・IPv4/IPv6・サブネット分割・包含判定・IP範囲→最小CIDR集合の逆算)・日数/営業日の計算(2日付間の日数と営業日数・起点日+N日/N営業日の期日・締め日と支払サイトからの支払日・土日と日本の祝日を内蔵)・HTML→Markdownの変換(見出し/リスト/表/リンク/画像/コード・script/styleや装飾タグの除去・本文だけの抽出・相対URLの絶対化)・和暦⇄西暦の変換(令和〜明治・自動判定・曜日と干支・存在しない和暦の指摘・満年齢と入学/卒業の年・元号一覧と早見表)・日本語タイトルからのURLスラッグ生成(かな→ヘボン式ローマ字・記号除去・ハイフン/アンダースコア連結・小文字化・長音の省略/表記))をMCPツールとして提供するサーバー",
3
+ "version": "0.37.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 列と入れ子の相互展開)・全角⇄半角の変換とテキストの掃除(文字種ごとの向き指定・半角カナの合成/分解・空白と空行の整理)・IPアドレス/CIDRの計算(ネットワーク・ブロードキャスト・ホスト範囲とホスト数・IPv4/IPv6・サブネット分割・包含判定・IP範囲→最小CIDR集合の逆算)・日数/営業日の計算(2日付間の日数と営業日数・起点日+N日/N営業日の期日・締め日と支払サイトからの支払日・土日と日本の祝日を内蔵)・HTML→Markdownの変換(見出し/リスト/表/リンク/画像/コード・script/styleや装飾タグの除去・本文だけの抽出・相対URLの絶対化)・和暦⇄西暦の変換(令和〜明治・自動判定・曜日と干支・存在しない和暦の指摘・満年齢と入学/卒業の年・元号一覧と早見表)・日本語タイトルからのURLスラッグ生成(かな→ヘボン式ローマ字・記号除去・ハイフン/アンダースコア連結・小文字化・長音の省略/表記)・Basic認証用 htpasswd の生成と照合(bcrypt / apr1-md5・パスワードの自動生成・Apache / nginx の設定スニペット)・OpenAPI/Swagger仕様書から日本語のMarkdownドキュメントとTypeScript型定義の生成(OpenAPI 3.x / Swagger 2.0・YAML/JSON・$ref/allOf/oneOf 対応・エンドポイントごとの引数/レスポンス型・外部 $ref は読みに行かない)・FAQ構造化データ(FAQPage の JSON-LD・Markdown・details/summary の HTML を同時生成・回答のリンク/箇条書きを HTML 化・「Q: / A:」形式の一括貼り付けの解析)・テキストの行操作(重複行の削除・数値を認識する並べ替え・シード付きシャッフル・行番号・空行除去・前後トリム・接頭辞/接尾辞)・印刷サイズとDPIの計算(用紙×解像度×塗り足しから必要なピクセル数・手持ちの画像の実効dpiと判定・早見表・mm/inch/px/pt/級の換算)・SERPのメタタグ文字数チェック(title / description が検索結果に収まるかをピクセル幅で判定・切り詰め位置と表示行・パンくず表示・https や階層の指摘)・Flexbox / CSS GridのCSS生成(既定値を省いた宣言・repeat()への畳み込み・grid-template-areasの検証・効かない組み合わせの指摘・定番プリセット))をMCPツールとして提供するサーバー。あわせて apps.first-ch.com のWebアプリ33本(GEO・メタタグ・セキュリティヘッダー・SSL・DNS・リダイレクト・リンク切れ・構造化データ・AIクローラー許可・和文可読性・ビジネス文面チェック等)の JSON API を呼ぶ app_* ツールも提供する(こちらは入力を apps.first-ch.com へ送るため既定では無効・FIRSTCH_ENABLE_APPS=1 で有効)",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "firstch-tools-mcp": "server.mjs"
@@ -9,7 +9,7 @@
9
9
  "main": "lib.mjs",
10
10
  "scripts": {
11
11
  "start": "node server.mjs",
12
- "test": "node test.mjs"
12
+ "test": "node test.mjs && node test-apps.mjs"
13
13
  },
14
14
  "dependencies": {
15
15
  "@marp-team/marp-core": "^4.4.0",
@@ -192,7 +192,60 @@
192
192
  "romaji",
193
193
  "hepburn",
194
194
  "permalink",
195
- "wordpress-slug"
195
+ "wordpress-slug",
196
+ "htpasswd",
197
+ "basic-auth",
198
+ "bcrypt",
199
+ "apr1",
200
+ "htaccess",
201
+ "nginx",
202
+ "staging",
203
+ "openapi",
204
+ "swagger",
205
+ "openapi-to-typescript",
206
+ "api-documentation",
207
+ "typescript",
208
+ "type-generation",
209
+ "faq",
210
+ "faqpage",
211
+ "structured-data",
212
+ "schema-org",
213
+ "rich-results",
214
+ "lines",
215
+ "dedupe",
216
+ "remove-duplicates",
217
+ "sort-lines",
218
+ "shuffle",
219
+ "line-numbers",
220
+ "text-tools",
221
+ "dpi",
222
+ "ppi",
223
+ "print",
224
+ "print-size",
225
+ "resolution",
226
+ "bleed",
227
+ "paper-size",
228
+ "a4",
229
+ "dtp",
230
+ "prepress",
231
+ "serp",
232
+ "serp-preview",
233
+ "meta-description",
234
+ "title-tag",
235
+ "pixel-width",
236
+ "search-snippet",
237
+ "flexbox",
238
+ "css-grid",
239
+ "grid-template-areas",
240
+ "layout",
241
+ "css-generator",
242
+ "seo-audit",
243
+ "dns-lookup",
244
+ "ssl-checker",
245
+ "security-headers",
246
+ "broken-links",
247
+ "geo",
248
+ "llmo"
196
249
  ],
197
250
  "author": "First CH LLC (https://first-ch.com)",
198
251
  "homepage": "https://tools.first-ch.com",
@@ -211,6 +264,7 @@
211
264
  },
212
265
  "files": [
213
266
  "server.mjs",
267
+ "apps.mjs",
214
268
  "lib.mjs",
215
269
  "fs-safe.mjs",
216
270
  "webp.mjs",
@@ -243,6 +297,13 @@
243
297
  "html-md.mjs",
244
298
  "wareki.mjs",
245
299
  "slug.mjs",
300
+ "htpasswd.mjs",
301
+ "openapi-doc.mjs",
302
+ "faq-jsonld.mjs",
303
+ "lines.mjs",
304
+ "dpi.mjs",
305
+ "serp.mjs",
306
+ "css-layout.mjs",
246
307
  "date-holidays.json",
247
308
  "vendor/",
248
309
  "README.md",
package/serp.mjs ADDED
@@ -0,0 +1,309 @@
1
+ /* SERPプレビュー&メタタグ文字数チェック(serp_check)
2
+ * title / description / URL から、Google 検索結果での切り詰め(ピクセル幅基準)・文字数・
3
+ * パンくず表示・指摘事項・貼り付け用のタグを返す。
4
+ *
5
+ * 行分割と切り詰めのコア(DEVICES / CJK / NO_START / NO_END / tokenize / wrap / layout / urlParts /
6
+ * count / esc / buildTags)は FirstCHTools の site/serp/app.js と同一
7
+ * (2箇所ルール: site側が正本・片方を直したらもう片方も同じ内容で反映する)。
8
+ * site 側はモジュールではなく目印のコメントも無いため、関数単位で写している。
9
+ * 同期の確認は、両ファイルから同名の関数を `awk '/^function wrap/,/^}/'` のように切り出し
10
+ * (こちらは行頭の export を落とす)、diff が空になることで行う。
11
+ * 判定(状態と指摘の並び)は site 側 update() の「計測」「指摘」部分を DOM から切り離して写したもの(serpJudge)。
12
+ *
13
+ * site と違うのは文字幅の測り方だけ。site はブラウザの canvas measureText で実測するが、
14
+ * Node には canvas が無いので、ここでは Arial の字幅表(欧文。Arial / Liberation Sans の公表値)と、
15
+ * 和文・全角記号=1em、半角カナ=0.5em、ハングル=0.865em、絵文字=20px で22px・14px で18px の近似で測る
16
+ * (macOS の Chrome で site の measure() と突き合わせた値)。カーニング(AV・Te や、ヒラギノのかなと句読点の詰め)は
17
+ * 入れていないので、ブラウザの実測より数px広く出ることがある=切り詰めの判定は早め(安全側)に寄る。
18
+ * site 側の実測も端末のフォント(Mac のヒラギノ・Windows の游ゴシック/メイリオ)で変わるため、
19
+ * 境目ぎりぎりの1〜2文字は目安として扱う。
20
+ * URLを取りに行く処理は無い。完全ローカル処理・ネットワーク送信なし。 */
21
+
22
+ // ---- 計測の基準値(site と同一) ----
23
+ export const DEVICES = {
24
+ desktop: { width: 600, title: { size: 20, lines: 1 }, desc: { size: 14, lines: 2 } },
25
+ mobile: { width: 380, title: { size: 20, lines: 2 }, desc: { size: 14, lines: 3 } },
26
+ };
27
+
28
+ // site の canvas が使うフォント指定(参考。ここでは下の字幅表で測る)
29
+ export const FONT = 'Arial, "Helvetica Neue", Helvetica, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic", YuGothic, Meiryo, sans-serif';
30
+
31
+ // ---- 字幅表(site との違いはここだけ) ----
32
+ // Arial の advance width(unitsPerEm=2048)。U+0020〜U+007E と U+00A0〜U+017F
33
+ const ARIAL_ASCII = [569, 569, 727, 1139, 1139, 1821, 1366, 391, 682, 682, 797, 1196, 569, 682, 569, 569, 1139, 1139, 1139, 1139, 1139, 1139, 1139, 1139, 1139, 1139, 569, 569, 1196, 1196, 1196, 1139, 2079, 1366, 1366, 1479, 1479, 1366, 1251, 1593, 1479, 569, 1024, 1366, 1139, 1706, 1479, 1593, 1366, 1593, 1479, 1366, 1251, 1479, 1366, 1933, 1366, 1366, 1251, 569, 569, 569, 961, 1139, 682, 1139, 1139, 1024, 1139, 1139, 569, 1139, 1139, 455, 455, 1024, 455, 1706, 1139, 1139, 1139, 1139, 682, 1024, 569, 1139, 1024, 1479, 1024, 1024, 1024, 684, 532, 684, 1196];
34
+ const ARIAL_LATIN1 = [569, 682, 1139, 1139, 1139, 1139, 532, 1139, 682, 1509, 758, 1139, 1196, 682, 1509, 1131, 819, 1124, 682, 682, 682, 1180, 1100, 682, 682, 682, 748, 1139, 1708, 1708, 1708, 1251, 1366, 1366, 1366, 1366, 1366, 1366, 2048, 1479, 1366, 1366, 1366, 1366, 569, 569, 569, 569, 1479, 1479, 1593, 1593, 1593, 1593, 1593, 1196, 1593, 1479, 1479, 1479, 1479, 1366, 1366, 1251, 1139, 1139, 1139, 1139, 1139, 1139, 1821, 1024, 1139, 1139, 1139, 1139, 569, 569, 569, 569, 1139, 1139, 1139, 1139, 1139, 1139, 1139, 1124, 1251, 1139, 1139, 1139, 1139, 1024, 1139, 1024, 1366, 1139, 1366, 1139, 1366, 1139, 1479, 1024, 1479, 1024, 1479, 1024, 1479, 1024, 1479, 1259, 1479, 1139, 1366, 1139, 1366, 1139, 1366, 1139, 1366, 1139, 1366, 1139, 1593, 1139, 1593, 1139, 1593, 1139, 1593, 1139, 1479, 1139, 1479, 1139, 569, 569, 569, 569, 569, 569, 569, 455, 569, 569, 1505, 909, 1024, 455, 1366, 1024, 1024, 1139, 455, 1139, 455, 1139, 597, 1139, 684, 1139, 455, 1479, 1139, 1479, 1139, 1479, 1139, 1237, 1481, 1139, 1593, 1139, 1593, 1139, 1593, 1139, 2048, 1933, 1479, 682, 1479, 682, 1479, 682, 1366, 1024, 1366, 1024, 1366, 1024, 1366, 1024, 1251, 569, 1251, 768, 1251, 569, 1479, 1139, 1479, 1139, 1479, 1139, 1479, 1139, 1479, 1139, 1479, 1139, 1933, 1479, 1366, 1024, 1366, 1251, 1024, 1251, 1024, 1251, 1024, 455];
35
+ const ARIAL_PUNCT = { 0x2013: 1139, 0x2014: 2048, 0x2018: 455, 0x2019: 455, 0x201c: 682, 0x201d: 682, 0x2022: 717, 0x2026: 2048, 0x2039: 682, 0x203a: 682, 0x20ac: 1139, 0x2122: 2048 };
36
+ const PICTO = /\p{Extended_Pictographic}/u;
37
+
38
+ function charWidth(ch, size) {
39
+ const cp = ch.codePointAt(0);
40
+ if (cp >= 0x20 && cp <= 0x7e) return (ARIAL_ASCII[cp - 0x20] * size) / 2048;
41
+ if (cp >= 0xa0 && cp <= 0x17f) return (ARIAL_LATIN1[cp - 0xa0] * size) / 2048;
42
+ if (ARIAL_PUNCT[cp]) return (ARIAL_PUNCT[cp] * size) / 2048;
43
+ if (cp === 0x09) return (ARIAL_ASCII[0] * size) / 2048;
44
+ if (cp >= 0x300 && cp <= 0x36f) return 0; // 結合文字
45
+ if (cp === 0x200d || (cp >= 0xfe00 && cp <= 0xfe0f)) return 0; // ZWJ・異体字セレクタ
46
+ if (cp >= 0xff61 && cp <= 0xff9f) return size / 2; // 半角カナ
47
+ if (cp >= 0xac00 && cp <= 0xd7a3) return size * 0.865; // ハングル(Apple SD Gothic Neo)
48
+ if (PICTO.test(ch)) return size === 20 ? 22 : size === 14 ? 18 : size * 1.1; // カラー絵文字
49
+ return size; // 和文・全角記号ほか
50
+ }
51
+
52
+ /** 文字列の表示幅(px)。site の measure() の代わり */
53
+ export function measure(text, size) {
54
+ let w = 0;
55
+ for (const ch of text) w += charWidth(ch, size);
56
+ return w;
57
+ }
58
+
59
+ // ---- 行分割(site と同一) ----
60
+ // 和文は文字単位、欧文は単語単位で折り返す。約物の行頭・行末禁則だけ簡易に処理する。
61
+ export const CJK = /[⺀-〿぀-ヿ㐀-䶿一-鿿豈-﫿＀-￯]/;
62
+ export const NO_START = '、。,.・:;?!゛゜ゝゞヽヾーァィゥェォッャュョヮヵヶぁぃぅぇぉっゃゅょゎ々)]}〉》」』】〕”’⦆»,.:;?!)]}%';
63
+ export const NO_END = '([{〈《「『【〔“‘⦅«([{$¥';
64
+
65
+ export function tokenize(text) {
66
+ const out = [];
67
+ let buf = '';
68
+ for (const ch of text) {
69
+ if (ch === ' ' || ch === '\t' || ch === ' ') {
70
+ if (buf) { out.push(buf); buf = ''; }
71
+ out.push(ch);
72
+ } else if (CJK.test(ch)) {
73
+ if (buf) { out.push(buf); buf = ''; }
74
+ out.push(ch);
75
+ } else {
76
+ buf += ch;
77
+ }
78
+ }
79
+ if (buf) out.push(buf);
80
+ return out;
81
+ }
82
+
83
+ export function wrap(text, width, size) {
84
+ const lines = [];
85
+ let cur = '';
86
+ for (const tok of tokenize(text)) {
87
+ const next = cur + tok;
88
+ if (cur === '' && (tok === ' ' || tok === '\t')) continue;
89
+ if (measure(next, size) <= width) { cur = next; continue; }
90
+ // 行頭に置けない約物は、はみ出しても前の行にぶら下げる
91
+ if (tok.length === 1 && NO_START.includes(tok)) { cur = next; continue; }
92
+ if (cur === '') { cur = tok; continue; }
93
+ // 行末に置けない括弧類は次の行へ送る
94
+ let carry = '';
95
+ if (NO_END.includes(cur.slice(-1))) { carry = cur.slice(-1); cur = cur.slice(0, -1); }
96
+ lines.push(cur);
97
+ cur = (carry + tok).replace(/^[ \t]+/, '');
98
+ }
99
+ if (cur) lines.push(cur);
100
+ return lines;
101
+ }
102
+
103
+ // 行数の上限を超えたら、最終行を「…」が入る幅まで削って切り詰め位置を返す。
104
+ export function layout(text, width, size, maxLines) {
105
+ const src = text.replace(/\s+/g, ' ').trim();
106
+ if (!src) return { lines: [], cut: '', truncated: false, width: 0 };
107
+ const all = wrap(src, width, size);
108
+ const total = measure(src, size);
109
+ if (all.length <= maxLines) return { lines: all, cut: '', truncated: false, width: total };
110
+ const kept = all.slice(0, maxLines);
111
+ const rest = all.slice(maxLines).join('');
112
+ let last = kept[maxLines - 1];
113
+ let dropped = '';
114
+ while (last && measure(last + '…', size) > width) {
115
+ dropped = last.slice(-1) + dropped;
116
+ last = last.slice(0, -1);
117
+ }
118
+ kept[maxLines - 1] = last.replace(/\s+$/, '') + '…';
119
+ return { lines: kept, cut: (dropped + rest).trim(), truncated: true, width: total };
120
+ }
121
+
122
+ // ---- URL の表示(Googleのパンくず風・site と同一) ----
123
+ export function urlParts(raw) {
124
+ const s = raw.trim();
125
+ if (!s) return null;
126
+ let u;
127
+ try { u = new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(s) ? s : 'https://' + s); } catch { return null; }
128
+ let host = u.hostname.replace(/^www\./, '');
129
+ try { host = decodeURIComponent(host); } catch { /* そのまま */ }
130
+ const segs = u.pathname.split('/').filter(Boolean).map((p) => {
131
+ try { return decodeURIComponent(p); } catch { return p; }
132
+ });
133
+ return { host, segs, protocol: u.protocol, raw: s };
134
+ }
135
+
136
+ // ---- 文字数(絵文字などを1文字として数える・site と同一) ----
137
+ const seg = typeof Intl !== 'undefined' && Intl.Segmenter ? new Intl.Segmenter(undefined, { granularity: 'grapheme' }) : null;
138
+ export const count = (s) => (seg ? [...seg.segment(s)].length : [...s].length);
139
+
140
+ const esc = (s) => s.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
141
+
142
+ export function buildTags(title, desc, parts) {
143
+ const out = [];
144
+ if (title) out.push(`<title>${esc(title)}</title>`);
145
+ if (desc) out.push(`<meta name="description" content="${esc(desc)}">`);
146
+ if (parts) out.push(`<link rel="canonical" href="${esc(parts.raw)}">`);
147
+ return out.join('\n');
148
+ }
149
+
150
+ /**
151
+ * 判定(site の update() の「計測」「指摘」部分)。入力の空白は site の v() と同じく1つに畳んで前後を落とす。
152
+ * @param {{title?:string, description?:string, url?:string}} input
153
+ * @param {'desktop'|'mobile'} deviceName
154
+ */
155
+ export function serpJudge(input, deviceName) {
156
+ const dev = DEVICES[deviceName] || DEVICES.desktop;
157
+ const norm = (s) => String(s == null ? '' : s).replace(/\s+/g, ' ').trim();
158
+ const title = norm(input.title);
159
+ const desc = norm(input.description);
160
+ const parts = urlParts(String(input.url == null ? '' : input.url));
161
+
162
+ const tl = layout(title, dev.width, dev.title.size, dev.title.lines);
163
+ const dl = layout(desc, dev.width, dev.desc.size, dev.desc.lines);
164
+ const titleLimit = dev.width * dev.title.lines;
165
+ const descLimit = dev.width * dev.desc.lines;
166
+
167
+ const st = (empty, truncated, short) => (empty ? 'empty' : truncated ? 'cut' : short ? 'short' : 'ok');
168
+ const tState = st(!title, tl.truncated, tl.width < titleLimit * 0.5);
169
+ const dState = st(!desc, dl.truncated, dl.width < descLimit * 0.5);
170
+
171
+ const checks = [];
172
+ if (!title) checks.push(['fail', 'TITLE_EMPTY']);
173
+ else {
174
+ if (tl.truncated) checks.push(['warn', 'TITLE_CUT']);
175
+ else if (tState === 'short') checks.push(['warn', 'TITLE_SHORT']);
176
+ if (!/[||\-–—::·]/.test(title)) checks.push(['warn', 'TITLE_NO_BRAND']);
177
+ }
178
+ if (!desc) checks.push(['fail', 'DESC_EMPTY']);
179
+ else if (dl.truncated) checks.push(['warn', 'DESC_CUT']);
180
+ else if (dState === 'short') checks.push(['warn', 'DESC_SHORT']);
181
+ if (!parts) checks.push(['warn', 'URL_EMPTY']);
182
+ else {
183
+ if (parts.protocol !== 'https:') checks.push(['warn', 'URL_HTTP']);
184
+ if (parts.segs.length > 3) checks.push(['warn', 'URL_LONG']);
185
+ if (/%[0-9a-f]{2}/i.test(parts.raw)) checks.push(['warn', 'URL_ENCODED']);
186
+ }
187
+
188
+ return {
189
+ device: DEVICES[deviceName] ? deviceName : 'desktop',
190
+ width: dev.width,
191
+ title: { text: title, chars: count(title), width: tl.width, limit: titleLimit, maxLines: dev.title.lines, lines: tl.lines, truncated: tl.truncated, cut: tl.cut, state: tState },
192
+ description: { text: desc, chars: count(desc), width: dl.width, limit: descLimit, maxLines: dev.desc.lines, lines: dl.lines, truncated: dl.truncated, cut: dl.cut, state: dState },
193
+ url: parts,
194
+ checks,
195
+ tags: buildTags(title, desc, parts),
196
+ };
197
+ }
198
+
199
+ /* ==================== ここから MCP 側だけ(ツールの入出力) ==================== */
200
+
201
+ // 指摘の文言(site の T と同じ文)
202
+ const CHECK_TEXT = {
203
+ ja: {
204
+ TITLE_EMPTY: 'タイトルが未入力です。検索結果の見出しになる、ページ内で最も効く要素です。',
205
+ TITLE_CUT: 'タイトルが切り詰められます。伝えたい語を前半に置くか、短くしてください。',
206
+ TITLE_SHORT: 'タイトルに余裕があります。狙う検索語を足せます。',
207
+ TITLE_NO_BRAND: 'サイト名が見当たりません。「ページ名 | サイト名」が通例です(Googleが書き換え・追記することもあります)。',
208
+ DESC_EMPTY: '説明文が未入力です。Googleが本文から抜粋を作るため、出る文章を選べません。',
209
+ DESC_CUT: '説明文が切り詰められます。要点を1行目に置いてください。',
210
+ DESC_SHORT: '説明文に余裕があります。もう1文入ります。',
211
+ URL_EMPTY: 'URLが未入力です。タイトル上のパンくず表示に使われます。',
212
+ URL_HTTP: 'https ではありません。Chromeは http のページに「保護されていない通信」を出します。',
213
+ URL_LONG: 'パスが長いため、検索結果のパンくず表示自体も省略されます。',
214
+ URL_ENCODED: 'URLにパーセントエンコードが含まれます。Googleはデコードして表示します(このプレビューも同じ)。',
215
+ },
216
+ en: {
217
+ TITLE_EMPTY: 'The title is empty. The <title> element is the strongest on-page signal for what the result is called.',
218
+ TITLE_CUT: 'The title is cut off. Put the words that matter first, or shorten it.',
219
+ TITLE_SHORT: 'The title is short. There is room left for the query the page should win.',
220
+ TITLE_NO_BRAND: 'No site name found. “Page name | Site name” is the usual shape (Google may rewrite or append it either way).',
221
+ DESC_EMPTY: 'The description is empty. Google then builds the snippet from the page body, and you do not choose it.',
222
+ DESC_CUT: 'The description is cut off. Say the point in the first line.',
223
+ DESC_SHORT: 'The description is short — there is room for another sentence.',
224
+ URL_EMPTY: 'No URL entered. The breadcrumb above the title is drawn from it.',
225
+ URL_HTTP: 'Not https. Chrome marks http pages as “Not secure”.',
226
+ URL_LONG: 'The path is long, so the breadcrumb itself gets shortened in the result.',
227
+ URL_ENCODED: 'The URL contains percent-encoded characters. Google shows them decoded, which is what the preview does here.',
228
+ },
229
+ };
230
+
231
+ const MEASURE_NOTE = {
232
+ ja: 'ピクセル幅は Arial の字幅表と和文1em の近似(カーニングなし)。ブラウザの実測より数px広く出ることがあり、切り詰めの判定は早めに寄る。',
233
+ en: 'Pixel widths use Arial advance widths and 1em for CJK (no kerning). They can run a few px wider than a browser measurement, so truncation is flagged slightly early.',
234
+ };
235
+
236
+ export const SERP_MAX_ITEMS = 500;
237
+
238
+ export class SerpToolError extends Error {}
239
+
240
+ function field(f) {
241
+ return {
242
+ text: f.text,
243
+ chars: f.chars,
244
+ width_px: Math.round(f.width),
245
+ limit_px: f.limit,
246
+ max_lines: f.maxLines,
247
+ lines: f.lines,
248
+ truncated: f.truncated,
249
+ cut: f.cut,
250
+ state: f.state,
251
+ };
252
+ }
253
+
254
+ function one(input, device, lang) {
255
+ const r = serpJudge(input, device);
256
+ return {
257
+ device: r.device,
258
+ result_width_px: r.width,
259
+ title: field(r.title),
260
+ description: field(r.description),
261
+ url: r.url ? { host: r.url.host, segments: r.url.segs, breadcrumb: [r.url.host, ...r.url.segs].join(' › '), protocol: r.url.protocol } : null,
262
+ pass: r.checks.length === 0,
263
+ checks: r.checks.map(([level, code]) => ({ level, code, message: CHECK_TEXT[lang][code] })),
264
+ tags: r.tags,
265
+ };
266
+ }
267
+
268
+ function check(input, devices, lang) {
269
+ if (devices.length === 1) return one(input, devices[0], lang);
270
+ const out = {};
271
+ for (const d of devices) out[d] = one(input, d, lang);
272
+ return { pass: devices.every((d) => out[d].pass), ...out };
273
+ }
274
+
275
+ /**
276
+ * title / description / URL を検索結果の表示幅で判定する。
277
+ * @param {object} opts
278
+ * @param {string} [opts.title]
279
+ * @param {string} [opts.description]
280
+ * @param {string} [opts.url]
281
+ * @param {{title?:string, description?:string, url?:string}[]} [opts.items] まとめて判定(500件まで)
282
+ * @param {'desktop'|'mobile'|'both'} [opts.device='desktop']
283
+ * @param {'ja'|'en'} [opts.lang='ja']
284
+ */
285
+ export async function serpCheckTool(opts = {}) {
286
+ const lang = opts.lang || 'ja';
287
+ if (lang !== 'ja' && lang !== 'en') throw new SerpToolError(`lang は ja か en: ${opts.lang}`);
288
+ const device = opts.device === undefined ? 'desktop' : opts.device;
289
+ if (!['desktop', 'mobile', 'both'].includes(device)) throw new SerpToolError(`device は desktop / mobile / both: ${opts.device}`);
290
+ const devices = device === 'both' ? ['desktop', 'mobile'] : [device];
291
+ for (const k of ['title', 'description', 'url']) {
292
+ if (opts[k] !== undefined && typeof opts[k] !== 'string') throw new SerpToolError(`${k} は文字列で渡してください`);
293
+ }
294
+
295
+ if (opts.items !== undefined) {
296
+ if (opts.title !== undefined || opts.description !== undefined || opts.url !== undefined) {
297
+ throw new SerpToolError('items と title/description/url は同時に渡せません');
298
+ }
299
+ if (!Array.isArray(opts.items) || !opts.items.length) throw new SerpToolError('items は1件以上の配列で渡してください');
300
+ if (opts.items.length > SERP_MAX_ITEMS) throw new SerpToolError(`items は ${SERP_MAX_ITEMS} 件まで: ${opts.items.length}`);
301
+ const results = opts.items.map((it) => check(it || {}, devices, lang));
302
+ return { count: results.length, device, passed: results.filter((r) => r.pass).length, results, measurement: MEASURE_NOTE[lang] };
303
+ }
304
+
305
+ if (opts.title === undefined && opts.description === undefined && opts.url === undefined) {
306
+ throw new SerpToolError('title・description・url のどれか(まとめて判定するなら items)を渡してください');
307
+ }
308
+ return { device, ...check(opts, devices, lang), measurement: MEASURE_NOTE[lang] };
309
+ }