mcp-tung-shing
Version:
A Model Context Protocol plugin for Chinese Tung Shing (黄历/通勝/通胜) almanac calculations
292 lines (291 loc) • 12.8 kB
JavaScript
;
var __webpack_require__ = {};
(()=>{
__webpack_require__.n = (module)=>{
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
__webpack_require__.d(getter, {
a: getter
});
return getter;
};
})();
(()=>{
__webpack_require__.d = (exports1, definition)=>{
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
enumerable: true,
get: definition[key]
});
};
})();
(()=>{
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
var __webpack_exports__ = {};
const external_dayjs_namespaceObject = require("dayjs");
var external_dayjs_default = /*#__PURE__*/ __webpack_require__.n(external_dayjs_namespaceObject);
const external_dayjs_plugin_lunar_namespaceObject = require("dayjs-plugin-lunar");
const stdio_js_namespaceObject = require("@modelcontextprotocol/sdk/server/stdio.js");
const mcp_js_namespaceObject = require("@modelcontextprotocol/sdk/server/mcp.js");
const types_js_namespaceObject = require("@modelcontextprotocol/sdk/types.js");
const external_zod_to_json_schema_namespaceObject = require("zod-to-json-schema");
const external_zod_namespaceObject = require("zod");
const external_tyme4ts_namespaceObject = require("tyme4ts");
function handleDirection(direction) {
const directions = {
东: '正东',
南: '正南',
西: '正西',
北: '正北'
};
return directions[direction] || direction;
}
const IGNORE_DAY_TABOO_NAMES = [
'齐醮',
'酬神',
'祀灶',
'焚香',
'订婚',
'挽面',
'开容',
'开井开池',
'作陂放水',
'伐木做梁',
'穿屏扇架',
'盖屋合脊',
'作厕',
'造屋',
'求财',
'买车',
'酝酿',
'作染',
'鼓铸',
'见贵',
'渡水',
'剃头',
'起基动土',
'破屋坏垣',
'造仓库',
'立券交易',
'安机',
'会友',
'求医疗病'
];
const getDayTabooNames = ()=>external_tyme4ts_namespaceObject.Taboo.NAMES.filter((item)=>!IGNORE_DAY_TABOO_NAMES.includes(item));
var types_ContentType = /*#__PURE__*/ function(ContentType) {
ContentType["宜"] = "宜";
ContentType["忌"] = "忌";
ContentType["吉凶"] = "吉凶";
ContentType["五行"] = "五行";
ContentType["冲煞"] = "冲煞";
ContentType["值神"] = "值神";
ContentType["建除十二神"] = "建除十二神";
ContentType["二十八星宿"] = "二十八星宿";
ContentType["吉神宜趋"] = "吉神宜趋";
ContentType["凶煞宜忌"] = "凶煞宜忌";
ContentType["彭祖百忌"] = "彭祖百忌";
ContentType["方位"] = "方位";
return ContentType;
}({});
var types_TabooType = /*#__PURE__*/ function(TabooType) {
TabooType[TabooType["宜"] = 1] = "宜";
TabooType[TabooType["忌"] = 2] = "忌";
return TabooType;
}({});
const tabooFilterSchema = external_zod_namespaceObject.z.object({
type: external_zod_namespaceObject.z.nativeEnum(types_TabooType).describe('过滤类型:宜(1)、忌(2)'),
value: external_zod_namespaceObject.z["enum"](getDayTabooNames()).describe('要筛选的宜忌事项')
});
const getTungShingParamsSchema = external_zod_namespaceObject.z.object({
startDate: external_zod_namespaceObject.z.string().optional().default(new Date().toISOString().split('T')[0]).describe('开始日期,格式为"YYYY-MM-DD"的字符串'),
days: external_zod_namespaceObject.z.union([
external_zod_namespaceObject.z.number().int().min(1),
external_zod_namespaceObject.z.string().regex(/^\d+$/).transform((val)=>Number.parseInt(val))
]).optional().default(1).describe('要获取的连续天数'),
includeHours: external_zod_namespaceObject.z.boolean().optional().default(false).describe('是否包含时辰信息'),
tabooFilters: external_zod_namespaceObject.z.array(tabooFilterSchema).optional().describe('多个筛选宜忌事项,条件之间为或关系')
});
require("dayjs/locale/zh-cn.js");
external_dayjs_default().extend(external_dayjs_plugin_lunar_namespaceObject.PluginLunar);
function getHourlyAlmanac(date) {
const lunarHour = date.toLunarHour();
const sixtyCycle = lunarHour.getSixtyCycle();
const heavenStem = sixtyCycle.getHeavenStem();
const earthBranch = sixtyCycle.getEarthBranch();
return {
[types_ContentType["宜"]]: lunarHour.getRecommends().map((item)=>item.getName()),
[types_ContentType["忌"]]: lunarHour.getAvoids().map((item)=>item.getName()),
[types_ContentType["吉凶"]]: lunarHour.getTwelveStar().getEcliptic().getLuck().toString(),
[types_ContentType["值神"]]: lunarHour.getTwelveStar().toString(),
[types_ContentType["五行"]]: sixtyCycle.getSound().toString(),
[types_ContentType["冲煞"]]: `冲${earthBranch.getOpposite().getZodiac()}煞${earthBranch.getOminous()}`,
[types_ContentType["方位"]]: [
`喜神${handleDirection(heavenStem.getJoyDirection().toString())}`,
`财神${handleDirection(heavenStem.getWealthDirection().toString())}`,
`福神${handleDirection(heavenStem.getMascotDirection().toString())}`
]
};
}
function getDailyAlmanac(date, includeHours = false) {
var _lunarDay_getFestival;
const parsedDate = external_dayjs_default()(date);
if (!parsedDate.isValid()) throw new Error('Invalid date');
const lunarDay = parsedDate.toLunarDay();
const solarDay = lunarDay.getSolarDay();
const sixtyCycle = lunarDay.getSixtyCycle();
const earthBranch = sixtyCycle.getEarthBranch();
const twentyEightStar = lunarDay.getTwentyEightStar();
const gods = lunarDay.getGods().reduce((acc, god)=>{
const category = '吉' === god.getLuck().getName() ? 'auspicious' : 'inauspicious';
acc[category].push(god.getName());
return acc;
}, {
auspicious: [],
inauspicious: []
});
const result = {
公历: parsedDate.locale('zh-cn').format('YYYY 年 M 月 D日(ddd)'),
农历: parsedDate.format('LY年LMLD'),
节日: null === (_lunarDay_getFestival = lunarDay.getFestival()) || void 0 === _lunarDay_getFestival ? void 0 : _lunarDay_getFestival.getName(),
节气: solarDay.getTermDay().toString(),
七十二候: solarDay.getPhenologyDay().toString(),
当日: {
[types_ContentType["宜"]]: lunarDay.getRecommends().map((item)=>item.getName()),
[types_ContentType["忌"]]: lunarDay.getAvoids().map((item)=>item.getName()),
[types_ContentType["吉凶"]]: lunarDay.getTwelveStar().getEcliptic().getLuck().toString(),
[types_ContentType["五行"]]: sixtyCycle.getSound().toString(),
[types_ContentType["冲煞"]]: `冲${earthBranch.getOpposite().getZodiac()}煞${earthBranch.getOminous()}`,
[types_ContentType["值神"]]: lunarDay.getTwelveStar().toString(),
[types_ContentType["建除十二神"]]: lunarDay.getDuty().toString(),
[types_ContentType["二十八星宿"]]: `${twentyEightStar}${twentyEightStar.getSevenStar()}${twentyEightStar.getAnimal()}(${twentyEightStar.getLuck()})`,
[types_ContentType["吉神宜趋"]]: gods.auspicious,
[types_ContentType["凶煞宜忌"]]: gods.inauspicious,
[types_ContentType["彭祖百忌"]]: `${sixtyCycle.getHeavenStem().getPengZuHeavenStem()} ${earthBranch.getPengZuEarthBranch()}`
}
};
if (includeHours) {
result.分时 = {};
for(let i = 0; i < 12; i++){
const hour = parsedDate.addLunar(i, 'dual-hour');
result.分时[hour.format('LH')] = getHourlyAlmanac(hour);
}
}
return result;
}
function createServer() {
const mcpServer = new mcp_js_namespaceObject.McpServer({
name: 'Tung Shing',
version: "1.7.1"
}, {
capabilities: {
tools: {},
prompts: {}
}
});
mcpServer.server.setRequestHandler(types_js_namespaceObject.ListToolsRequestSchema, ()=>({
tools: [
{
name: 'get-tung-shing',
description: '获取通胜黄历,包括公历、农历、宜忌、吉凶、冲煞等信息',
inputSchema: (0, external_zod_to_json_schema_namespaceObject.zodToJsonSchema)(getTungShingParamsSchema)
}
]
}));
mcpServer.server.setRequestHandler(types_js_namespaceObject.CallToolRequestSchema, async (request)=>{
switch(request.params.name){
case 'get-tung-shing':
{
const { startDate, days, includeHours, tabooFilters = [] } = getTungShingParamsSchema.parse(request.params.arguments);
const start = external_dayjs_default()(startDate);
if (!start.isValid()) return {
content: [
{
type: 'text',
text: 'Invalid date'
}
],
isError: true
};
return {
content: Array.from({
length: days
}, (_, i)=>{
const almanac = getDailyAlmanac(start.add(i, 'day'), includeHours);
if (!tabooFilters.length) return {
type: 'text',
text: JSON.stringify(almanac)
};
const recommends = almanac.当日[types_ContentType["宜"]] || [];
const avoids = almanac.当日[types_ContentType["忌"]] || [];
const hasMatch = tabooFilters.some((filter)=>{
if (filter.type === types_TabooType["宜"]) return recommends.includes(filter.value);
if (filter.type === types_TabooType["忌"]) return avoids.includes(filter.value);
return false;
});
if (hasMatch) return {
type: 'text',
text: JSON.stringify(almanac)
};
return null;
}).filter(Boolean)
};
}
default:
return {
content: [
{
type: 'text',
text: `Unknown tool: ${request.params.name}`
}
],
isError: true
};
}
});
mcpServer.server.setRequestHandler(types_js_namespaceObject.ListPromptsRequestSchema, ()=>({
prompts: [
{
name: 'get-taboo',
description: '获取宜忌事项类型'
}
]
}));
mcpServer.server.setRequestHandler(types_js_namespaceObject.GetPromptRequestSchema, (request)=>{
switch(request.params.name){
case 'get-taboo':
return {
messages: [
{
role: 'assistant',
content: {
type: 'text',
text: `宜忌事项类型清单\n${getDayTabooNames().map((name)=>`- ${name}`).join('\n')}`
}
}
]
};
default:
return {
messages: []
};
}
});
return mcpServer;
}
external_dayjs_default().extend(external_dayjs_plugin_lunar_namespaceObject.PluginLunar);
(async ()=>{
try {
const server = createServer();
const transport = new stdio_js_namespaceObject.StdioServerTransport();
await server.connect(transport);
console.error('Tung Shing MCP server started');
} catch (error) {
console.error('Failed to start Tung Shing MCP server:', error);
process.exit(1);
}
})();
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
value: true
});