@kensnzk/koyu 0.5.0 → 0.13.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 (160) hide show
  1. package/README.ja.md +43 -11
  2. package/README.md +51 -11
  3. package/dist/axo.d.ts +16 -0
  4. package/dist/axo.d.ts.map +1 -0
  5. package/dist/axo.js +211 -0
  6. package/dist/axo.js.map +1 -0
  7. package/dist/check.d.ts +109 -0
  8. package/dist/check.d.ts.map +1 -0
  9. package/dist/check.js +957 -0
  10. package/dist/check.js.map +1 -0
  11. package/dist/cli.d.ts +3 -0
  12. package/dist/cli.d.ts.map +1 -0
  13. package/dist/cli.js +410 -0
  14. package/dist/cli.js.map +1 -0
  15. package/dist/diff.d.ts +105 -0
  16. package/dist/diff.d.ts.map +1 -0
  17. package/dist/diff.js +583 -0
  18. package/dist/diff.js.map +1 -0
  19. package/dist/fabric.d.ts +28 -0
  20. package/dist/fabric.d.ts.map +1 -0
  21. package/dist/fabric.js +105 -0
  22. package/dist/fabric.js.map +1 -0
  23. package/dist/graph.d.ts +118 -0
  24. package/dist/graph.d.ts.map +1 -0
  25. package/dist/graph.js +627 -0
  26. package/dist/graph.js.map +1 -0
  27. package/dist/index.d.ts +12 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +13 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/light.d.ts +15 -0
  32. package/dist/light.d.ts.map +1 -0
  33. package/dist/light.js +53 -0
  34. package/dist/light.js.map +1 -0
  35. package/dist/mcp.d.ts +3 -0
  36. package/dist/mcp.d.ts.map +1 -0
  37. package/dist/mcp.js +361 -0
  38. package/dist/mcp.js.map +1 -0
  39. package/dist/model.d.ts +379 -0
  40. package/dist/model.d.ts.map +1 -0
  41. package/dist/model.js +464 -0
  42. package/dist/model.js.map +1 -0
  43. package/dist/parse-file.d.ts +9 -0
  44. package/dist/parse-file.d.ts.map +1 -0
  45. package/dist/parse-file.js +22 -0
  46. package/dist/parse-file.js.map +1 -0
  47. package/dist/parse.d.ts +16 -0
  48. package/dist/parse.d.ts.map +1 -0
  49. package/dist/parse.js +1040 -0
  50. package/dist/parse.js.map +1 -0
  51. package/dist/plan.d.ts +10 -0
  52. package/dist/plan.d.ts.map +1 -0
  53. package/dist/plan.js +354 -0
  54. package/dist/plan.js.map +1 -0
  55. package/dist/poly.d.ts +78 -0
  56. package/dist/poly.d.ts.map +1 -0
  57. package/dist/poly.js +277 -0
  58. package/dist/poly.js.map +1 -0
  59. package/dist/site.d.ts +24 -0
  60. package/dist/site.d.ts.map +1 -0
  61. package/dist/site.js +54 -0
  62. package/dist/site.js.map +1 -0
  63. package/dist/vertical.d.ts +176 -0
  64. package/dist/vertical.d.ts.map +1 -0
  65. package/dist/vertical.js +669 -0
  66. package/dist/vertical.js.map +1 -0
  67. package/examples/basement/main.muro +84 -0
  68. package/examples/comparison/README.md +64 -1
  69. package/examples/comparison/gen-ifc4.ts +1 -1
  70. package/examples/comparison/gen-ifcx.ts +2 -2
  71. package/examples/comparison/two-rooms.ifcx +1 -1
  72. package/examples/complex/L1.muro +132 -0
  73. package/examples/complex/assets.muro +17 -0
  74. package/examples/complex/basement.muro +40 -0
  75. package/examples/complex/core.muro +40 -0
  76. package/examples/complex/hotel.muro +117 -0
  77. package/examples/complex/main.muro +67 -0
  78. package/examples/complex/office.muro +41 -0
  79. package/examples/complex/plant.muro +34 -0
  80. package/examples/complex/podium.muro +140 -0
  81. package/examples/complex/site.muro +18 -0
  82. package/examples/house/L1.muro +16 -0
  83. package/examples/house/L2.muro +19 -0
  84. package/examples/house/assets.muro +9 -0
  85. package/examples/house/main.muro +27 -0
  86. package/examples/house/site.muro +31 -0
  87. package/examples/house.muro +3 -3
  88. package/examples/mansion.muro +18 -13
  89. package/examples/office.muro +1 -1
  90. package/examples/steps/01-one-room.muro +4 -0
  91. package/examples/steps/02-two-rooms.muro +5 -0
  92. package/examples/steps/03-door.muro +9 -0
  93. package/examples/steps/04-exterior.muro +15 -0
  94. package/examples/steps/05-two-storeys.muro +27 -0
  95. package/examples/steps/06-finished.muro +30 -0
  96. package/examples/steps/README.md +25 -0
  97. package/examples/tower/L1.muro +80 -0
  98. package/examples/tower/L11.muro +49 -0
  99. package/examples/tower/L2.muro +71 -0
  100. package/examples/tower/L3.muro +31 -0
  101. package/examples/tower/assets.muro +14 -0
  102. package/examples/tower/main.muro +37 -0
  103. package/examples/tower/site-geometry.muro +7 -0
  104. package/examples/tower/site.muro +33 -0
  105. package/examples/tower/typical.muro +131 -0
  106. package/examples/two-rooms.canonical.json +30 -3
  107. package/examples/two-rooms.muro +11 -7
  108. package/package.json +29 -2
  109. package/src/axo.ts +258 -0
  110. package/src/check.ts +948 -122
  111. package/src/cli.ts +175 -12
  112. package/src/diff.ts +676 -0
  113. package/src/fabric.ts +123 -0
  114. package/src/graph.ts +451 -61
  115. package/src/index.ts +38 -2
  116. package/src/light.ts +4 -7
  117. package/src/mcp.ts +397 -0
  118. package/src/model.ts +462 -67
  119. package/src/parse-file.ts +28 -0
  120. package/src/parse.ts +682 -45
  121. package/src/plan.ts +152 -14
  122. package/src/poly.ts +295 -0
  123. package/src/site.ts +19 -11
  124. package/src/vertical.ts +884 -0
  125. package/.github/workflows/ci.yml +0 -16
  126. package/.zenodo.json +0 -20
  127. package/CITATION.cff +0 -26
  128. package/docs/decisions/0001-notation-v0.md +0 -29
  129. package/docs/decisions/0002-height-and-offsets.md +0 -27
  130. package/docs/decisions/0003-uncounted-divisions.md +0 -36
  131. package/docs/decisions/0004-typical-floors.md +0 -21
  132. package/docs/decisions/0005-zones-and-unions.md +0 -23
  133. package/docs/decisions/0006-voids-and-light.md +0 -31
  134. package/docs/decisions/0007-semi-outdoor-air.md +0 -27
  135. package/docs/decisions/0008-vocabulary-and-level-attr.md +0 -23
  136. package/docs/decisions/0009-site-and-exterior.md +0 -29
  137. package/docs/ifc-coverage.md +0 -82
  138. package/docs/ifcx-notes.md +0 -25
  139. package/docs/img/house-L1.svg +0 -80
  140. package/docs/img/house-L2.svg +0 -53
  141. package/docs/img/mansion-L1.svg +0 -113
  142. package/docs/img/mansion-L10.svg +0 -111
  143. package/docs/img/mansion-L5.svg +0 -160
  144. package/docs/img/office-L1.svg +0 -116
  145. package/docs/img/office-L2.svg +0 -109
  146. package/docs/img/two-rooms.svg +0 -39
  147. package/docs/log/2026-07-22.md +0 -13
  148. package/docs/log/2026-07-23.md +0 -51
  149. package/docs/roadmap.md +0 -55
  150. package/docs/writing-architecture.md +0 -119
  151. package/spec/notation-v0.md +0 -164
  152. package/spec/vocabulary.md +0 -62
  153. package/test/core.test.ts +0 -171
  154. package/test/design2.test.ts +0 -97
  155. package/test/house.test.ts +0 -74
  156. package/test/mansion.test.ts +0 -99
  157. package/test/office.test.ts +0 -71
  158. package/test/uncounted.test.ts +0 -120
  159. package/test/vertical.test.ts +0 -75
  160. package/tsconfig.json +0 -13
package/src/mcp.ts ADDED
@@ -0,0 +1,397 @@
1
+ #!/usr/bin/env node
2
+ // koyu MCPサーバー — 建物をLLMエージェントの作業対象にする (ADR-0012 / docs/horizon.md 軸1)。
3
+ // stdio上の手書きJSON-RPC 2.0。依存ゼロの方針をここでも守る (SDKは使わない)。
4
+ // ステートレス: 毎回 parseFile で合成する (towerでも数ms)。書き込みは .muro に限定し、履歴はgitが持つ。
5
+ //
6
+ // 使い方: koyu-mcp (カレントディレクトリ基準の相対パスでファイルを指定)
7
+ // エージェントのループ: layers で読む → write_layer で書く → check が門番 → doors/stats/light/site で帰結を確かめる
8
+
9
+ import { mkdirSync, readFileSync, realpathSync, renameSync, writeFileSync } from "node:fs";
10
+ import { dirname, isAbsolute, relative, resolve, sep } from "node:path";
11
+ import {
12
+ areaM2,
13
+ isIndoor,
14
+ daylight,
15
+ displayName,
16
+ doorsBetween,
17
+ effectiveUse,
18
+ isSemiOutdoor,
19
+ levelsSorted,
20
+ polygonAreaM2,
21
+ toCanonical,
22
+ zoneAreaM2,
23
+ type Model,
24
+ type Space,
25
+ } from "./index.js";
26
+ import { check, checkDiagnostics } from "./check.js";
27
+ import { svgPlan } from "./plan.js";
28
+ import { siteReport } from "./site.js";
29
+ import { parseFile, parseFileWith } from "./parse-file.js";
30
+
31
+ // ---- モデルの読み込みと要約 ----
32
+
33
+ function load(file: string): Model {
34
+ return parseFile(resolve(file));
35
+ }
36
+
37
+ /** 合成に参加した全レイヤー — 要素の有無によらずModelが記録する (grid/levelだけの層も落ちない) */
38
+ function layerFiles(model: Model, entry: string): string[] {
39
+ return [...new Set([resolve(entry), ...model.layers])].sort();
40
+ }
41
+
42
+ /** entryディレクトリ境界の検査 — 文字列prefixではなく相対パスで判定する (ADR-0013) */
43
+ function assertInside(entryDir: string, targetDir: string): void {
44
+ const rel = relative(entryDir, targetDir);
45
+ if (rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel)) {
46
+ throw new Error("entryのディレクトリの外へは書き込めません");
47
+ }
48
+ }
49
+
50
+ function summarize(model: Model, file: string): unknown {
51
+ const rooms = [...model.spaces.values()].filter((s) => s.rects.length > 0 && s.level);
52
+ const indoor = rooms.filter((s) => isIndoor(model, s));
53
+ const semi = rooms.filter((s) => s.type !== "void" && isSemiOutdoor(model, s));
54
+ const byLevel: Record<string, { rooms: number; subtotalM2: number }> = {};
55
+ for (const lv of levelsSorted(model)) {
56
+ const rs = indoor.filter((s) => s.level === lv.name);
57
+ if (rs.length === 0) continue;
58
+ byLevel[lv.name] = {
59
+ rooms: rs.length,
60
+ subtotalM2: Math.round(rs.reduce((a, s) => a + (areaM2(s) ?? 0), 0) * 100) / 100,
61
+ };
62
+ }
63
+ const byUse: Record<string, number> = {};
64
+ for (const s of indoor) {
65
+ const u = effectiveUse(model, s) ?? "(未指定)";
66
+ byUse[u] = Math.round(((byUse[u] ?? 0) + (areaM2(s) ?? 0)) * 100) / 100;
67
+ }
68
+ const r = check(model);
69
+ return {
70
+ name: model.name,
71
+ unit: model.unit,
72
+ layers: layerFiles(model, file),
73
+ levels: levelsSorted(model).map((l) => ({
74
+ name: l.name,
75
+ z: l.z,
76
+ ...(l.h !== undefined ? { h: l.h } : {}),
77
+ ...(l.slab !== undefined ? { slab: l.slab } : {}),
78
+ })),
79
+ spaces: model.spaces.size,
80
+ boundaries: model.boundaries.length,
81
+ // 敷地ゾーンに「専有床面積」の言葉は成り立たない — 敷地形状から導いた面積を返す
82
+ // (同じ敷地について 160 と 3854 の二つの数字が返る、という状態を作らない。ADR-0028)
83
+ zones: [...model.zones.values()].map((z) => {
84
+ const site = z.attrs["site"] === 1;
85
+ const poly = site ? model.polygons.get(z.path) : undefined;
86
+ return {
87
+ path: z.path,
88
+ ...(typeof z.attrs["name"] === "string" ? { name: z.attrs["name"] } : {}),
89
+ ...(site ? { site: true } : {}),
90
+ areaM2: poly ? polygonAreaM2(poly.points) : zoneAreaM2(model, z.path),
91
+ };
92
+ }),
93
+ assets: [...model.assets.values()].map((a) => ({ name: a.name, kind: a.kind, attrs: a.attrs })),
94
+ ...(model.polygons.size
95
+ ? { sitePolygons: [...model.polygons.keys()] }
96
+ : {}),
97
+ totalFloorM2: Math.round(indoor.reduce((a, s) => a + (areaM2(s) ?? 0), 0) * 100) / 100,
98
+ semiOutdoorM2: Math.round(semi.reduce((a, s) => a + (areaM2(s) ?? 0), 0) * 100) / 100,
99
+ floorsM2: byLevel,
100
+ byUseM2: byUse,
101
+ check: { errors: r.errors.length, warnings: r.warnings.length },
102
+ hint: "レイヤーの中身は layers で、検査は check で、変更は write_layer で (checkが門番)。",
103
+ };
104
+ }
105
+
106
+ function spaceInfo(model: Model, s: Space): unknown {
107
+ return {
108
+ path: s.path,
109
+ type: s.type,
110
+ name: displayName(s),
111
+ level: s.level,
112
+ areaM2: areaM2(s),
113
+ semiOutdoor: isSemiOutdoor(model, s),
114
+ ...(s.file ? { layer: s.file } : {}),
115
+ };
116
+ }
117
+
118
+ // ---- ツール定義 ----
119
+
120
+ interface Tool {
121
+ description: string;
122
+ schema: Record<string, unknown>;
123
+ run(args: Record<string, unknown>): unknown;
124
+ }
125
+
126
+ const FILE_PROP = {
127
+ file: { type: "string", description: "entryの.muroファイルパス (importは自動で合成される)" },
128
+ };
129
+ const str = (v: unknown, name: string): string => {
130
+ if (typeof v !== "string" || !v) throw new Error(`${name} (文字列) が必要です`);
131
+ return v;
132
+ };
133
+
134
+ const TOOLS: Record<string, Tool> = {
135
+ model_summary: {
136
+ description:
137
+ "建物の要約 (名前・レベル・レイヤー構成・ゾーン・建具アセット・面積・check結果)。まずこれを呼ぶ",
138
+ schema: { type: "object", properties: FILE_PROP, required: ["file"] },
139
+ run: (a) => {
140
+ const file = str(a.file, "file");
141
+ return summarize(load(file), file);
142
+ },
143
+ },
144
+ check: {
145
+ description:
146
+ "一棟のビルドの門番: 合成して整合を検査する。エラー・警告は出所レイヤー:行つき。編集のたびに呼ぶこと",
147
+ schema: { type: "object", properties: FILE_PROP, required: ["file"] },
148
+ run: (a) => {
149
+ const m = load(str(a.file, "file"));
150
+ const r = check(m);
151
+ return {
152
+ ok: r.errors.length === 0,
153
+ spaces: m.spaces.size,
154
+ boundaries: m.boundaries.length,
155
+ ...r,
156
+ // 構造化診断 (ADR-0016) — errors/warningsの文字列と同件・同順。code/severity/path/relatedつき
157
+ diagnostics: checkDiagnostics(m),
158
+ };
159
+ },
160
+ },
161
+ layers: {
162
+ description: "合成に参加している全レイヤー (.muroファイル) の名前と中身を返す — 原本を読む",
163
+ schema: { type: "object", properties: FILE_PROP, required: ["file"] },
164
+ run: (a) => {
165
+ const file = str(a.file, "file");
166
+ const m = load(file);
167
+ return layerFiles(m, file).map((f) => ({ file: f, source: readFileSync(f, "utf8") }));
168
+ },
169
+ },
170
+ write_layer: {
171
+ description:
172
+ "レイヤー (.muroファイル) を検査してから書き換える。parse不能な合成になる内容は書き込まれない (原本不変)。" +
173
+ "checkのエラーは返すが書き込みは行う (複数レイヤーにまたがる編集の途中を許す) — 直して再度書くこと。履歴はgitに任せる",
174
+ schema: {
175
+ type: "object",
176
+ properties: {
177
+ ...FILE_PROP,
178
+ layer: { type: "string", description: "書き込む.muroファイルパス (entryからの相対または絶対)" },
179
+ content: { type: "string", description: "レイヤーの全文 (全置換)" },
180
+ },
181
+ required: ["file", "layer", "content"],
182
+ },
183
+ run: (a) => {
184
+ const file = str(a.file, "file");
185
+ const content = str(a.content, "content");
186
+ const entryDir = resolve(dirname(resolve(file)));
187
+ const target = resolve(entryDir, str(a.layer, "layer"));
188
+ if (!target.endsWith(".muro")) throw new Error("書き込みは .muro ファイルに限ります");
189
+ assertInside(entryDir, dirname(target));
190
+ // 門番は書き込みの前 — 差し替え内容で仮想合成し、parse不能なら原本に触れない (ADR-0013)。
191
+ // 合成に参加しないファイルの内容は検証されない (importされた時のcheckが捕まえる)
192
+ let m: Model;
193
+ try {
194
+ m = parseFileWith(resolve(file), (p) => (p === target ? content : undefined));
195
+ } catch (e) {
196
+ return {
197
+ written: false,
198
+ target,
199
+ ok: false,
200
+ parseError: e instanceof Error ? e.message : String(e),
201
+ };
202
+ }
203
+ const r = check(m);
204
+ mkdirSync(dirname(target), { recursive: true });
205
+ assertInside(realpathSync(entryDir), realpathSync(dirname(target))); // symlink経由の脱出も塞ぐ
206
+ const tmp = `${target}.tmp-${process.pid}`;
207
+ writeFileSync(tmp, content);
208
+ renameSync(tmp, target); // 同一ディレクトリ内のrename — 中途半端なファイルを残さない
209
+ return { written: target, ok: r.errors.length === 0, spaces: m.spaces.size, ...r };
210
+ },
211
+ },
212
+ doors: {
213
+ description: "避難・動線の問い: 空間Aから空間Bまで扉を何枚通るか (最少扉数の経路)",
214
+ schema: {
215
+ type: "object",
216
+ properties: {
217
+ ...FILE_PROP,
218
+ from: { type: "string", description: "起点の空間パス (例 /L9/A/ldk)" },
219
+ to: { type: "string", description: "終点の空間パス (例 /out/road-s)" },
220
+ },
221
+ required: ["file", "from", "to"],
222
+ },
223
+ run: (a) => {
224
+ const route = doorsBetween(load(str(a.file, "file")), str(a.from, "from"), str(a.to, "to"));
225
+ return route ?? { unreachable: true };
226
+ },
227
+ },
228
+ spaces: {
229
+ description: "空間の一覧 (パス・型・レベル・面積・半屋外・出所レイヤー)。levelで絞り込み可",
230
+ schema: {
231
+ type: "object",
232
+ properties: { ...FILE_PROP, level: { type: "string", description: "レベル名で絞る (省略可)" } },
233
+ required: ["file"],
234
+ },
235
+ run: (a) => {
236
+ const m = load(str(a.file, "file"));
237
+ return [...m.spaces.values()]
238
+ .filter((s) => !a.level || s.level === a.level)
239
+ .map((s) => spaceInfo(m, s));
240
+ },
241
+ },
242
+ light: {
243
+ description: "採光の検査: daylight:1 を書いた室で窓面積/床面積 ≥ 1/7 (バルコニー・庇下は0.7掛け)",
244
+ schema: { type: "object", properties: FILE_PROP, required: ["file"] },
245
+ run: (a) => {
246
+ const m = load(str(a.file, "file"));
247
+ return daylight(m).map((r) => ({
248
+ path: r.space.path,
249
+ name: displayName(r.space),
250
+ ok: r.ok,
251
+ windowM2: r.window,
252
+ floorM2: r.floor,
253
+ needM2: r.need,
254
+ }));
255
+ },
256
+ },
257
+ site: {
258
+ description: "敷地の問い: 敷地面積 (宣言/導出の照合)・接道・建築面積・建蔽率・容積率",
259
+ schema: { type: "object", properties: FILE_PROP, required: ["file"] },
260
+ run: (a) => {
261
+ const r = siteReport(load(str(a.file, "file")));
262
+ const site = r.declaredArea ?? r.derivedArea;
263
+ return {
264
+ ...(r.siteZone ? { siteZone: r.siteZone.path } : {}),
265
+ ...(r.polygon ? { polygonVertices: r.polygon.points.length } : {}),
266
+ declaredAreaM2: r.declaredArea,
267
+ derivedAreaM2: r.derivedArea,
268
+ ...(r.declaredArea !== undefined && r.derivedArea !== undefined
269
+ ? { areaMatch: Math.abs(r.declaredArea - r.derivedArea) < 0.05 }
270
+ : {}),
271
+ footprintM2: r.footprint,
272
+ totalFloorM2: r.totalFloor,
273
+ coverageRatio: site ? Math.round((r.footprint / site) * 1000) / 10 : undefined,
274
+ floorAreaRatio: site ? Math.round((r.totalFloor / site) * 1000) / 10 : undefined,
275
+ roads: r.roads.map((rd) => ({
276
+ path: rd.road.path,
277
+ name: displayName(rd.road),
278
+ widthMm: rd.width,
279
+ frontageMm: rd.frontage,
280
+ })),
281
+ };
282
+ },
283
+ },
284
+ plan_svg: {
285
+ description: "指定レベルの平面図SVGを生成して返す (形は生成物 — 最下階は配置図兼用)",
286
+ schema: {
287
+ type: "object",
288
+ properties: { ...FILE_PROP, level: { type: "string", description: "レベル名 (例 L5)" } },
289
+ required: ["file", "level"],
290
+ },
291
+ run: (a) => svgPlan(load(str(a.file, "file")), { level: str(a.level, "level") }),
292
+ },
293
+ canonical_json: {
294
+ description: "正準JSON (機械形式 — 合成後の単一モデル、バイト安定)。diff・外部接続の土台",
295
+ schema: { type: "object", properties: FILE_PROP, required: ["file"] },
296
+ run: (a) => JSON.parse(toCanonical(load(str(a.file, "file")))),
297
+ },
298
+ };
299
+
300
+ // ---- JSON-RPC 2.0 over stdio (行区切りJSON) ----
301
+
302
+ type Json = Record<string, unknown>;
303
+
304
+ function send(msg: Json): void {
305
+ process.stdout.write(JSON.stringify(msg) + "\n");
306
+ }
307
+
308
+ function result(id: unknown, res: unknown): void {
309
+ send({ jsonrpc: "2.0", id, result: res } as Json);
310
+ }
311
+
312
+ function rpcError(id: unknown, code: number, message: string): void {
313
+ send({ jsonrpc: "2.0", id, error: { code, message } } as Json);
314
+ }
315
+
316
+ function handle(msg: Json): void {
317
+ const id = msg.id;
318
+ const method = msg.method as string | undefined;
319
+ if (method === undefined) return; // 応答は来ない想定
320
+ const params = (msg.params ?? {}) as Json;
321
+
322
+ // 通知 (idなし) は応答しない
323
+ if (id === undefined) return;
324
+
325
+ switch (method) {
326
+ case "initialize": {
327
+ result(id, {
328
+ protocolVersion: (params.protocolVersion as string) ?? "2025-06-18",
329
+ capabilities: { tools: {} },
330
+ serverInfo: { name: "koyu", version: "0.13.0" },
331
+ instructions:
332
+ "空間一次の建築記述koyuのサーバー。model_summaryで建物を掴み、layersで原本 (.muroレイヤー群) を読み、" +
333
+ "write_layerで編集する。checkが一棟のビルドの門番 — エラーは出所レイヤー:行つきで返る。" +
334
+ "doors/light/site/spacesは同じ記述への異なる問い。形 (plan_svg) は生成物。",
335
+ });
336
+ return;
337
+ }
338
+ case "ping":
339
+ result(id, {});
340
+ return;
341
+ case "tools/list": {
342
+ result(id, {
343
+ tools: Object.entries(TOOLS).map(([name, t]) => ({
344
+ name,
345
+ description: t.description,
346
+ inputSchema: t.schema,
347
+ })),
348
+ });
349
+ return;
350
+ }
351
+ case "tools/call": {
352
+ const name = params.name as string;
353
+ const tool = TOOLS[name];
354
+ if (!tool) {
355
+ rpcError(id, -32602, `未知のツールです: ${name}`);
356
+ return;
357
+ }
358
+ try {
359
+ const out = tool.run((params.arguments ?? {}) as Json);
360
+ const text = typeof out === "string" ? out : JSON.stringify(out, null, 1);
361
+ result(id, { content: [{ type: "text", text }] });
362
+ } catch (e) {
363
+ result(id, {
364
+ content: [{ type: "text", text: e instanceof Error ? e.message : String(e) }],
365
+ isError: true,
366
+ });
367
+ }
368
+ return;
369
+ }
370
+ case "resources/list":
371
+ result(id, { resources: [] });
372
+ return;
373
+ case "prompts/list":
374
+ result(id, { prompts: [] });
375
+ return;
376
+ default:
377
+ rpcError(id, -32601, `未対応のメソッドです: ${method}`);
378
+ }
379
+ }
380
+
381
+ let buf = "";
382
+ process.stdin.setEncoding("utf8");
383
+ process.stdin.on("data", (chunk: string) => {
384
+ buf += chunk;
385
+ let nl: number;
386
+ while ((nl = buf.indexOf("\n")) >= 0) {
387
+ const line = buf.slice(0, nl).trim();
388
+ buf = buf.slice(nl + 1);
389
+ if (!line) continue;
390
+ try {
391
+ handle(JSON.parse(line) as Json);
392
+ } catch {
393
+ // 壊れた行は黙って捨てる (stdioの流儀)
394
+ }
395
+ }
396
+ });
397
+ process.stdin.on("end", () => process.exit(0));