@xingwangzhe/stalux 1.12.0 → 1.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.
- package/package.json +1 -1
- package/src/layouts/Stalux.astro +7 -1
- package/src/scripts/webmcp.ts +163 -48
package/package.json
CHANGED
package/src/layouts/Stalux.astro
CHANGED
|
@@ -87,9 +87,15 @@ const llmPromote = llmPromoteRaw
|
|
|
87
87
|
siteTitle: siteConfig.title,
|
|
88
88
|
siteUrl: siteConfig.url,
|
|
89
89
|
siteDesc: siteConfig.description,
|
|
90
|
+
siteLang: siteConfig.lang,
|
|
90
91
|
}}
|
|
91
92
|
>
|
|
92
|
-
window.__STALUX_SITE_INFO__ = {
|
|
93
|
+
window.__STALUX_SITE_INFO__ = {
|
|
94
|
+
title: siteTitle,
|
|
95
|
+
url: siteUrl,
|
|
96
|
+
description: siteDesc,
|
|
97
|
+
lang: siteLang,
|
|
98
|
+
};
|
|
93
99
|
</script>
|
|
94
100
|
<script>
|
|
95
101
|
import "../scripts/webmcp.ts";
|
package/src/scripts/webmcp.ts
CHANGED
|
@@ -7,6 +7,9 @@
|
|
|
7
7
|
* /posts/{id}.md(源码导出)、/llms.txt / /llms-full.txt(站点信息镜像)、
|
|
8
8
|
* /pagefind/(全文索引)。
|
|
9
9
|
*
|
|
10
|
+
* 工具描述跟随站点语言(site.yml 的 lang):中文站显示中文说明,
|
|
11
|
+
* 英文站显示英文说明,保证 AI 代理与评审(如 webmcp.com)能读懂。
|
|
12
|
+
*
|
|
10
13
|
* 浏览器无原生 modelContext 时,由布局注入的 @mcp-b/webmcp-polyfill
|
|
11
14
|
* 提供兜底实现;两者都没有则静默跳过注册,不影响普通访问者。
|
|
12
15
|
*
|
|
@@ -23,6 +26,7 @@ declare global {
|
|
|
23
26
|
title?: string;
|
|
24
27
|
url?: string;
|
|
25
28
|
description?: string;
|
|
29
|
+
lang?: string;
|
|
26
30
|
};
|
|
27
31
|
}
|
|
28
32
|
}
|
|
@@ -48,6 +52,22 @@ interface DocumentWithModelContext extends Document {
|
|
|
48
52
|
modelContext?: ModelContext;
|
|
49
53
|
}
|
|
50
54
|
|
|
55
|
+
// ---------------------------------------------------------------------------
|
|
56
|
+
// 语言选择(跟随站点 site.yml 的 lang)
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
|
|
59
|
+
const SITE_LANG = (
|
|
60
|
+
typeof window !== "undefined" ? (window.__STALUX_SITE_INFO__?.lang ?? "zh-CN") : "zh-CN"
|
|
61
|
+
).toLowerCase();
|
|
62
|
+
|
|
63
|
+
/** 站点为英文站时为 true */
|
|
64
|
+
const IS_EN = SITE_LANG === "en" || SITE_LANG === "en-us" || SITE_LANG === "en-gb";
|
|
65
|
+
|
|
66
|
+
/** 按站点语言选择文案:pick(中文, English) */
|
|
67
|
+
function pick(zh: string, en: string): string {
|
|
68
|
+
return IS_EN ? en : zh;
|
|
69
|
+
}
|
|
70
|
+
|
|
51
71
|
// ---------------------------------------------------------------------------
|
|
52
72
|
// 工具执行辅助
|
|
53
73
|
// ---------------------------------------------------------------------------
|
|
@@ -150,28 +170,40 @@ function currentAbbrlinkFromPath(): string | null {
|
|
|
150
170
|
}
|
|
151
171
|
|
|
152
172
|
// ---------------------------------------------------------------------------
|
|
153
|
-
//
|
|
173
|
+
// 工具定义(描述中英双语,随站点语言切换)
|
|
154
174
|
// ---------------------------------------------------------------------------
|
|
155
175
|
|
|
156
176
|
function listPostsTool(): WebMCPTool {
|
|
157
177
|
return {
|
|
158
178
|
name: "stalux_list_posts",
|
|
159
|
-
title: "列出博客文章",
|
|
160
|
-
description:
|
|
179
|
+
title: pick("列出博客文章", "List blog posts"),
|
|
180
|
+
description: pick(
|
|
161
181
|
"分页列出博客的全部已发布文章,返回标题、永久链接(abbrlink)、日期、分类、标签、摘要与文章页 URL。" +
|
|
162
|
-
|
|
163
|
-
|
|
182
|
+
"需要浏览全站文章、确认某篇文章的 abbrlink 时使用;不返回正文," +
|
|
183
|
+
"只要某篇元信息用 stalux_get_post,读正文请用 stalux_read_post。",
|
|
184
|
+
"Paginated list of all published posts, returning title, permanent link (abbrlink), date, categories, tags, description and post page URL. " +
|
|
185
|
+
"Use it to browse the whole blog or find a post's abbrlink; it does not return body content — " +
|
|
186
|
+
"use stalux_get_post for one post's metadata, or stalux_read_post for the full body.",
|
|
187
|
+
),
|
|
164
188
|
annotations: { readOnlyHint: true, untrustedContentHint: false },
|
|
165
189
|
inputSchema: {
|
|
166
190
|
type: "object",
|
|
167
191
|
properties: {
|
|
168
|
-
page: {
|
|
192
|
+
page: {
|
|
193
|
+
type: "integer",
|
|
194
|
+
minimum: 1,
|
|
195
|
+
default: 1,
|
|
196
|
+
description: pick("页码,从 1 开始", "Page number, starting at 1"),
|
|
197
|
+
},
|
|
169
198
|
pageSize: {
|
|
170
199
|
type: "integer",
|
|
171
200
|
minimum: 1,
|
|
172
201
|
maximum: MAX_LIST_PAGE_SIZE,
|
|
173
202
|
default: 10,
|
|
174
|
-
description:
|
|
203
|
+
description: pick(
|
|
204
|
+
"每页条数,最大 " + MAX_LIST_PAGE_SIZE,
|
|
205
|
+
"Items per page, max " + MAX_LIST_PAGE_SIZE,
|
|
206
|
+
),
|
|
175
207
|
},
|
|
176
208
|
},
|
|
177
209
|
additionalProperties: false,
|
|
@@ -183,14 +215,21 @@ function listPostsTool(): WebMCPTool {
|
|
|
183
215
|
Math.max(1, Number(input.pageSize) || 10),
|
|
184
216
|
);
|
|
185
217
|
const list = await loadPostsMeta();
|
|
186
|
-
if (!list)
|
|
218
|
+
if (!list)
|
|
219
|
+
return err(
|
|
220
|
+
"INDEX_UNAVAILABLE",
|
|
221
|
+
pick("无法获取文章列表", "Unable to fetch post list"),
|
|
222
|
+
);
|
|
187
223
|
const total = list.length;
|
|
188
224
|
const totalPages = Math.ceil(total / pageSize) || 1;
|
|
189
225
|
const start = (page - 1) * pageSize;
|
|
190
226
|
if (start >= total) {
|
|
191
227
|
return err(
|
|
192
228
|
"OUT_OF_RANGE",
|
|
193
|
-
|
|
229
|
+
pick(
|
|
230
|
+
"第 " + page + " 页没有文章(共 " + totalPages + " 页)",
|
|
231
|
+
"Page " + page + " has no posts (" + totalPages + " pages total)",
|
|
232
|
+
),
|
|
194
233
|
{
|
|
195
234
|
page,
|
|
196
235
|
totalPages,
|
|
@@ -207,19 +246,25 @@ function listPostsTool(): WebMCPTool {
|
|
|
207
246
|
function getPostTool(): WebMCPTool {
|
|
208
247
|
return {
|
|
209
248
|
name: "stalux_get_post",
|
|
210
|
-
title: "查看文章信息",
|
|
211
|
-
description:
|
|
249
|
+
title: pick("查看文章信息", "Get post info"),
|
|
250
|
+
description: pick(
|
|
212
251
|
"按 abbrlink 或标题关键词定位一篇文章,返回其元信息(标题、日期、分类、标签、摘要、字数、文章页 URL)。" +
|
|
213
|
-
|
|
214
|
-
|
|
252
|
+
"只要元信息时用它,比读取正文便宜得多;读正文请用 stalux_read_post。" +
|
|
253
|
+
"获取 abbrlink 可先用 stalux_list_posts 或 stalux_search_posts。",
|
|
254
|
+
"Locate a post by abbrlink or title keyword and return its metadata (title, date, categories, tags, description, word count, post page URL). " +
|
|
255
|
+
"Use it when only metadata is needed — cheaper than reading the full body; use stalux_read_post for the body. " +
|
|
256
|
+
"To get an abbrlink, first use stalux_list_posts or stalux_search_posts.",
|
|
257
|
+
),
|
|
215
258
|
annotations: { readOnlyHint: true, untrustedContentHint: false },
|
|
216
259
|
inputSchema: {
|
|
217
260
|
type: "object",
|
|
218
261
|
properties: {
|
|
219
262
|
id: {
|
|
220
263
|
type: "string",
|
|
221
|
-
description:
|
|
264
|
+
description: pick(
|
|
222
265
|
"文章 abbrlink(如 f4442947)或标题关键词(模糊匹配,取第一条命中)",
|
|
266
|
+
"Post abbrlink (e.g. f4442947) or a title keyword (fuzzy match, first hit wins)",
|
|
267
|
+
),
|
|
223
268
|
},
|
|
224
269
|
},
|
|
225
270
|
required: ["id"],
|
|
@@ -227,11 +272,23 @@ function getPostTool(): WebMCPTool {
|
|
|
227
272
|
},
|
|
228
273
|
execute: async (input) => {
|
|
229
274
|
const id = String(input.id ?? "").trim();
|
|
230
|
-
if (!id)
|
|
275
|
+
if (!id) {
|
|
276
|
+
return err(
|
|
277
|
+
"BAD_INPUT",
|
|
278
|
+
pick(
|
|
279
|
+
"请提供文章 abbrlink 或标题关键词",
|
|
280
|
+
"Provide a post abbrlink or title keyword",
|
|
281
|
+
),
|
|
282
|
+
);
|
|
283
|
+
}
|
|
231
284
|
const list = await loadPostsMeta();
|
|
232
|
-
if (!list)
|
|
285
|
+
if (!list)
|
|
286
|
+
return err(
|
|
287
|
+
"INDEX_UNAVAILABLE",
|
|
288
|
+
pick("无法获取文章索引", "Unable to fetch post index"),
|
|
289
|
+
);
|
|
233
290
|
const post = findById(list, id) ?? findByKeyword(list, id);
|
|
234
|
-
if (!post) return err("NOT_FOUND", "未找到文章: " + id);
|
|
291
|
+
if (!post) return err("NOT_FOUND", pick("未找到文章: ", "Post not found: ") + id);
|
|
235
292
|
return { ok: true, post: briefMeta(post) };
|
|
236
293
|
},
|
|
237
294
|
};
|
|
@@ -240,21 +297,35 @@ function getPostTool(): WebMCPTool {
|
|
|
240
297
|
function currentPostTool(): WebMCPTool {
|
|
241
298
|
return {
|
|
242
299
|
name: "stalux_current_post",
|
|
243
|
-
title: "当前文章信息",
|
|
244
|
-
description:
|
|
300
|
+
title: pick("当前文章信息", "Current post info"),
|
|
301
|
+
description: pick(
|
|
245
302
|
"返回用户当前正在浏览的那篇文章的元信息。当用户说「这篇文章」「当前页面」时先调用它确定上下文," +
|
|
246
|
-
|
|
303
|
+
"比读正文便宜得多。不在文章页时返回错误。",
|
|
304
|
+
"Return the metadata of the post the user is currently viewing. " +
|
|
305
|
+
"Call it first when the user says 'this post' or 'the current page' to establish context — cheaper than reading the body. " +
|
|
306
|
+
"Returns an error when not on a post page.",
|
|
307
|
+
),
|
|
247
308
|
annotations: { readOnlyHint: true, untrustedContentHint: false },
|
|
248
309
|
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
249
310
|
execute: async () => {
|
|
250
311
|
const id = currentAbbrlinkFromPath();
|
|
251
312
|
if (!id) {
|
|
252
|
-
return err(
|
|
313
|
+
return err(
|
|
314
|
+
"NOT_ON_POST_PAGE",
|
|
315
|
+
pick("当前不在文章页(路径: ", "Not on a post page (path: ") +
|
|
316
|
+
location.pathname +
|
|
317
|
+
")",
|
|
318
|
+
);
|
|
253
319
|
}
|
|
254
320
|
const list = await loadPostsMeta();
|
|
255
|
-
if (!list)
|
|
321
|
+
if (!list)
|
|
322
|
+
return err(
|
|
323
|
+
"INDEX_UNAVAILABLE",
|
|
324
|
+
pick("无法获取文章索引", "Unable to fetch post index"),
|
|
325
|
+
);
|
|
256
326
|
const post = findById(list, id);
|
|
257
|
-
if (!post)
|
|
327
|
+
if (!post)
|
|
328
|
+
return err("NOT_FOUND", pick("未找到当前文章: ", "Current post not found: ") + id);
|
|
258
329
|
return { ok: true, post: briefMeta(post) };
|
|
259
330
|
},
|
|
260
331
|
};
|
|
@@ -263,16 +334,22 @@ function currentPostTool(): WebMCPTool {
|
|
|
263
334
|
function randomPostTool(): WebMCPTool {
|
|
264
335
|
return {
|
|
265
336
|
name: "stalux_random_post",
|
|
266
|
-
title: "随机一篇文章",
|
|
267
|
-
description:
|
|
337
|
+
title: pick("随机一篇文章", "Random post"),
|
|
338
|
+
description: pick(
|
|
268
339
|
"随机挑选一篇文章并返回其元信息(标题、abbrlink、日期、摘要、URL)。" +
|
|
269
|
-
|
|
340
|
+
"适合推荐、探索、或不确定从哪篇开始时使用;不返回正文,不导航。",
|
|
341
|
+
"Pick a random post and return its metadata (title, abbrlink, date, description, URL). " +
|
|
342
|
+
"Useful for recommendations, discovery, or when unsure where to start; it does not return body content and does not navigate.",
|
|
343
|
+
),
|
|
270
344
|
annotations: { readOnlyHint: true, untrustedContentHint: false },
|
|
271
345
|
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
272
346
|
execute: async () => {
|
|
273
347
|
const list = await loadPostsMeta();
|
|
274
348
|
if (!list || list.length === 0) {
|
|
275
|
-
return err(
|
|
349
|
+
return err(
|
|
350
|
+
"INDEX_UNAVAILABLE",
|
|
351
|
+
pick("无法获取文章索引", "Unable to fetch post index"),
|
|
352
|
+
);
|
|
276
353
|
}
|
|
277
354
|
const post = list[Math.floor(Math.random() * list.length)];
|
|
278
355
|
return { ok: true, post: briefMeta(post) };
|
|
@@ -283,22 +360,29 @@ function randomPostTool(): WebMCPTool {
|
|
|
283
360
|
function searchPostsTool(): WebMCPTool {
|
|
284
361
|
return {
|
|
285
362
|
name: "stalux_search_posts",
|
|
286
|
-
title: "搜索博客文章",
|
|
287
|
-
description:
|
|
363
|
+
title: pick("搜索博客文章", "Search blog posts"),
|
|
364
|
+
description: pick(
|
|
288
365
|
"用关键词搜索博客文章的标题、标签、分类与正文全文,返回命中文章的标题、链接与摘要片段。" +
|
|
289
|
-
|
|
290
|
-
|
|
366
|
+
"适合「博客里写过 X 吗」「帮我找一下关于 Y 的文章」这类问题;" +
|
|
367
|
+
"定位到具体文章后再用 stalux_read_post 读正文、stalux_get_post 取元信息。",
|
|
368
|
+
"Search the full text of all posts (title, tags, categories, body) by keyword, returning matching posts with title, URL and excerpt snippets. " +
|
|
369
|
+
"Ideal for questions like 'has the blog covered X?' or 'find posts about Y'; " +
|
|
370
|
+
"once a post is located, use stalux_read_post for the body or stalux_get_post for metadata.",
|
|
371
|
+
),
|
|
291
372
|
annotations: { readOnlyHint: true, untrustedContentHint: false },
|
|
292
373
|
inputSchema: {
|
|
293
374
|
type: "object",
|
|
294
375
|
properties: {
|
|
295
|
-
keyword: {
|
|
376
|
+
keyword: {
|
|
377
|
+
type: "string",
|
|
378
|
+
description: pick("搜索关键词", "Search keyword"),
|
|
379
|
+
},
|
|
296
380
|
limit: {
|
|
297
381
|
type: "integer",
|
|
298
382
|
minimum: 1,
|
|
299
383
|
maximum: MAX_SEARCH_RESULTS,
|
|
300
384
|
default: 10,
|
|
301
|
-
description: "最多返回条数",
|
|
385
|
+
description: pick("最多返回条数", "Maximum number of results to return"),
|
|
302
386
|
},
|
|
303
387
|
},
|
|
304
388
|
required: ["keyword"],
|
|
@@ -306,15 +390,24 @@ function searchPostsTool(): WebMCPTool {
|
|
|
306
390
|
},
|
|
307
391
|
execute: async (input) => {
|
|
308
392
|
const keyword = String(input.keyword ?? "").trim();
|
|
309
|
-
if (!keyword)
|
|
393
|
+
if (!keyword)
|
|
394
|
+
return err("BAD_INPUT", pick("请提供搜索关键词", "Provide a search keyword"));
|
|
310
395
|
const limit = Math.min(MAX_SEARCH_RESULTS, Math.max(1, Number(input.limit) || 10));
|
|
311
396
|
const pagefind = await loadPagefind();
|
|
312
|
-
if (!pagefind)
|
|
397
|
+
if (!pagefind) {
|
|
398
|
+
return err(
|
|
399
|
+
"INDEX_UNAVAILABLE",
|
|
400
|
+
pick("全文搜索索引不可用", "Full-text search index unavailable"),
|
|
401
|
+
);
|
|
402
|
+
}
|
|
313
403
|
const res = (await pagefind.search(keyword)) as {
|
|
314
404
|
results?: Array<{ data: () => Promise<unknown> }>;
|
|
315
405
|
};
|
|
316
406
|
if (!res || !Array.isArray(res.results)) {
|
|
317
|
-
return err(
|
|
407
|
+
return err(
|
|
408
|
+
"SEARCH_FAILED",
|
|
409
|
+
pick("搜索失败,请重试", "Search failed, please retry"),
|
|
410
|
+
);
|
|
318
411
|
}
|
|
319
412
|
const items = await Promise.all(
|
|
320
413
|
res.results.slice(0, limit).map(async (r) => {
|
|
@@ -343,27 +436,44 @@ function searchPostsTool(): WebMCPTool {
|
|
|
343
436
|
function readPostTool(): WebMCPTool {
|
|
344
437
|
return {
|
|
345
438
|
name: "stalux_read_post",
|
|
346
|
-
title: "读取文章 Markdown",
|
|
347
|
-
description:
|
|
439
|
+
title: pick("读取文章 Markdown", "Read post Markdown"),
|
|
440
|
+
description: pick(
|
|
348
441
|
"按 abbrlink(文章永久链接 ID)读取一篇博客文章的原始 Markdown 全文,包含 frontmatter 与版权脚注。" +
|
|
349
|
-
|
|
350
|
-
|
|
442
|
+
"需要文章完整内容、引用或摘要时使用;只要元信息用 stalux_get_post(更便宜)。" +
|
|
443
|
+
"获取 abbrlink 可先用 stalux_list_posts 或 stalux_search_posts。",
|
|
444
|
+
"Read the full raw Markdown of a post by abbrlink (permanent link ID), including frontmatter and copyright footer. " +
|
|
445
|
+
"Use it for full content, quotes or summarization; for metadata only, stalux_get_post is cheaper. " +
|
|
446
|
+
"To get an abbrlink, first use stalux_list_posts or stalux_search_posts.",
|
|
447
|
+
),
|
|
351
448
|
annotations: { readOnlyHint: true, untrustedContentHint: false },
|
|
352
449
|
inputSchema: {
|
|
353
450
|
type: "object",
|
|
354
451
|
properties: {
|
|
355
|
-
id: {
|
|
452
|
+
id: {
|
|
453
|
+
type: "string",
|
|
454
|
+
description: pick(
|
|
455
|
+
"文章的 abbrlink,如 f4442947",
|
|
456
|
+
"Post abbrlink, e.g. f4442947",
|
|
457
|
+
),
|
|
458
|
+
},
|
|
356
459
|
},
|
|
357
460
|
required: ["id"],
|
|
358
461
|
additionalProperties: false,
|
|
359
462
|
},
|
|
360
463
|
execute: async (input) => {
|
|
361
464
|
const id = String(input.id ?? "").trim();
|
|
362
|
-
if (!id)
|
|
465
|
+
if (!id)
|
|
466
|
+
return err("BAD_INPUT", pick("请提供文章 abbrlink", "Provide a post abbrlink"));
|
|
363
467
|
const url = "/posts/" + encodeURIComponent(id) + ".md";
|
|
364
468
|
const r = await fetch(url, { headers: { Accept: "text/markdown" } });
|
|
365
|
-
if (r.status === 404)
|
|
366
|
-
|
|
469
|
+
if (r.status === 404)
|
|
470
|
+
return err("NOT_FOUND", pick("未找到文章: ", "Post not found: ") + id);
|
|
471
|
+
if (!r.ok) {
|
|
472
|
+
return err(
|
|
473
|
+
"FETCH_FAILED",
|
|
474
|
+
pick("无法获取文章 Markdown:", "Failed to fetch post Markdown: ") + url,
|
|
475
|
+
);
|
|
476
|
+
}
|
|
367
477
|
const markdown = await r.text();
|
|
368
478
|
return { ok: true, url, markdown: markdown.slice(0, 50000) };
|
|
369
479
|
},
|
|
@@ -373,11 +483,15 @@ function readPostTool(): WebMCPTool {
|
|
|
373
483
|
function siteInfoTool(): WebMCPTool {
|
|
374
484
|
return {
|
|
375
485
|
name: "stalux_site_info",
|
|
376
|
-
title: "站点信息",
|
|
377
|
-
description:
|
|
486
|
+
title: pick("站点信息", "Site info"),
|
|
487
|
+
description: pick(
|
|
378
488
|
"返回博客的站点信息(标题、简介、站点 URL),并给出更完整的机器可读内容入口:" +
|
|
379
|
-
|
|
380
|
-
|
|
489
|
+
"/llms.txt(站点导航与文章链接列表)与 /llms-full.txt(全站 Markdown 镜像,含全部文章正文)。" +
|
|
490
|
+
"适合需要了解博客主题、定位内容、或批量获取全站数据时使用。",
|
|
491
|
+
"Return the blog's site info (title, description, site URL) plus pointers to machine-readable content: " +
|
|
492
|
+
"/llms.txt (site navigation and post link list) and /llms-full.txt (full-site Markdown mirror with all post bodies). " +
|
|
493
|
+
"Use it to understand the blog's topic, orient, or bulk-fetch all content.",
|
|
494
|
+
),
|
|
381
495
|
annotations: { readOnlyHint: true, untrustedContentHint: false },
|
|
382
496
|
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
383
497
|
execute: async () => {
|
|
@@ -387,6 +501,7 @@ function siteInfoTool(): WebMCPTool {
|
|
|
387
501
|
title: info.title ?? "",
|
|
388
502
|
url: info.url ?? "",
|
|
389
503
|
description: info.description ?? "",
|
|
504
|
+
lang: info.lang ?? "",
|
|
390
505
|
llms: (info.url ?? "") + "/llms.txt",
|
|
391
506
|
llmsFull: (info.url ?? "") + "/llms-full.txt",
|
|
392
507
|
};
|