#!/usr/bin/env node
/**
* CBETA MCP Server
* 为 CBETA Online(中华电子佛典)提供 MCP 服务
*/
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import {
searchSutra,
getWorkInfo,
getJuanHtml,
getCatalog,
htmlToText,
} from "./cbeta-api.js";
// 创建 MCP 服务器
const server = new McpServer({
name: "cbeta-mcp",
version: "1.0.0",
});
// ============ Tools ============
// 搜索佛典经文
server.tool(
"search_sutra",
"搜索 CBETA 佛典经文。可以搜索经名、内容、作者等。",
{
query: z.string().describe("搜索关键词"),
scope: z.string().optional().describe("搜索范围,如 T(大正藏)、X(续藏经)等"),
page: z.number().optional().describe("页码,默认为 1"),
limit: z.number().optional().describe("每页结果数,默认为 10"),
},
async ({ query, scope, page, limit }) => {
const result = await searchSutra(query, {
scope,
page: page || 1,
per_page: limit || 10,
});
if (result.results.length === 0) {
return {
content: [
{
type: "text" as const,
text: `未找到与「${query}」相关的经文。请尝试其他关键词。`,
},
],
};
}
const formattedResults = result.results
.map((r, i) => `${i + 1}. **${r.title}** (${r.work_id})\n 卷${r.juan},命中 ${r.hits} 处`)
.join("\n\n");
return {
content: [
{
type: "text" as const,
text: `## 搜索结果\n\n共找到 ${result.total} 条结果:\n\n${formattedResults}`,
},
],
};
}
);
// 获取经文内容
server.tool(
"get_sutra_content",
"获取指定佛典的内容。需要提供作品 ID 和可选的卷号。",
{
work_id: z.string().describe("作品 ID,如 T0001(长阿含经)、T0262(法华经)等"),
juan: z.number().optional().describe("卷号,默认为 1"),
format: z.enum(["text", "html"]).optional().describe("输出格式:text(纯文本)或 html,默认为 text"),
},
async ({ work_id, juan, format }) => {
// 先获取作品信息
const workInfo = await getWorkInfo(work_id);
// 获取卷内容
const content = await getJuanHtml(work_id, juan || 1);
if (!content) {
return {
content: [
{
type: "text" as const,
text: `无法获取作品 ${work_id} 的内容。请检查作品 ID 是否正确。`,
},
],
};
}
const text = format === "html" ? content.html : htmlToText(content.html);
const title = workInfo?.title || content.title || work_id;
return {
content: [
{
type: "text" as const,
text: `## ${title}\n\n**卷 ${content.juan}**\n\n${text}`,
},
],
};
}
);
// 浏览藏经目录
server.tool(
"browse_catalog",
"浏览 CBETA 藏经目录,了解收录的经典分类。",
{
catalog_id: z.string().optional().describe("目录 ID,如 T(大正藏)、X(续藏经)等。不填则返回所有藏经列表。"),
},
async ({ catalog_id }) => {
const catalog = await getCatalog(catalog_id || "CBETA");
const formatted = catalog
.map((item) => `- **${item.label}** (${item.id})`)
.join("\n");
return {
content: [
{
type: "text" as const,
text: `## CBETA 藏经目录\n\n${formatted}\n\n---\n使用 \`get_sutra_content\` 工具并提供作品 ID(如 T0001)来获取经文内容。`,
},
],
};
}
);
// 获取作品信息
server.tool(
"get_work_info",
"获取指定佛典作品的详细信息,包括作者、卷数等。",
{
work_id: z.string().describe("作品 ID,如 T0001、T0262 等"),
},
async ({ work_id }) => {
const info = await getWorkInfo(work_id);
if (!info) {
return {
content: [
{
type: "text" as const,
text: `未找到作品 ${work_id}。请检查 ID 是否正确。`,
},
],
};
}
return {
content: [
{
type: "text" as const,
text: `## ${info.title}\n\n- **作品 ID**: ${info.work_id}\n- **作者/译者**: ${info.byline || "未知"}\n- **卷数**: ${info.juan_count} 卷\n- **分类**: ${info.category || "未分类"}`,
},
],
};
}
);
// ============ Resources ============
// 藏经目录资源
server.resource(
"cbeta://catalog",
"CBETA 藏经目录列表",
async () => {
const catalog = await getCatalog();
return {
contents: [
{
uri: "cbeta://catalog",
mimeType: "application/json",
text: JSON.stringify(catalog, null, 2),
},
],
};
}
);
// ============ Prompts ============
// 佛经解释 Prompt
server.prompt(
"explain_sutra",
"解释佛经段落的含义",
{
text: z.string().describe("要解释的经文段落"),
style: z.enum(["简单", "学术", "现代"]).optional().describe("解释风格"),
},
async ({ text, style }) => {
const styleGuide = {
"简单": "请用简单易懂的语言解释",
"学术": "请从佛学学术角度进行解读",
"现代": "请结合现代生活场景来解释",
};
return {
messages: [
{
role: "user" as const,
content: {
type: "text" as const,
text: `${styleGuide[style || "简单"]}以下佛经段落的含义:\n\n「${text}」\n\n请解释其核心思想和智慧。`,
},
},
],
};
}
);
// 佛学术语 Prompt
server.prompt(
"buddhist_term",
"查询佛学术语的含义",
{
term: z.string().describe("佛学术语"),
},
async ({ term }) => {
return {
messages: [
{
role: "user" as const,
content: {
type: "text" as const,
text: `请解释佛学术语「${term}」的含义,包括:\n1. 梵文/巴利文原词(如果有)\n2. 基本含义\n3. 在不同宗派中的理解\n4. 相关的经典引用`,
},
},
],
};
}
);
// ============ 启动服务器 ============
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("CBETA MCP Server is running...");
}
main().catch((error) => {
console.error("Server error:", error);
process.exit(1);
});