feat(logging): 添加统一日志输出函数以改善控制台信息

This commit is contained in:
worry 2026-01-19 16:11:50 +08:00
parent 836e2f3161
commit 4ad2b8fb88

View File

@ -39,6 +39,13 @@ if (!allowedTransports.includes(cliOptions.transport)) {
const TRANSPORT_TYPE = (cliOptions.transport || "stdio") as "stdio" | "http" | "sse";
const PORT = parseInt(cliOptions.port, 10);
const logInfo = (...args: unknown[]) => {
if (TRANSPORT_TYPE === "stdio") {
console.error(...args);
} else {
console.log(...args);
}
};
// SSE transports
const sseTransports: Record<string, SSEServerTransport> = {};
// 创建MCP服务器实例
@ -67,7 +74,7 @@ function loadRecipeData() {
try {
recipes = fetchRecipes();
categories = getAllCategories(recipes);
console.log(`📚 已加载 ${recipes.length} 个菜谱`);
logInfo(`Loaded ${recipes.length} recipes.`);
} catch (error) {
console.error('加载菜谱数据失败:', error);
recipes = [];
@ -171,14 +178,14 @@ async function main() {
});
httpServer.listen(PORT, () => {
console.log(`🚀 HowToCook MCP ${TRANSPORT_TYPE.toUpperCase()} 服务器启动成功`);
logInfo(`HowToCook MCP ${TRANSPORT_TYPE.toUpperCase()} server started.`);
if(TRANSPORT_TYPE === "http"){
console.log(`🔗 MCP 端点: http://localhost:${PORT}/mcp`);
logInfo(`MCP endpoint: http://localhost:${PORT}/mcp`);
}else if(TRANSPORT_TYPE === "sse"){
console.log(`🔗 MCP 端点: http://localhost:${PORT}/sse`);
logInfo(`MCP endpoint: http://localhost:${PORT}/sse`);
}
console.log(`💡 健康检查: http://localhost:${PORT}/health`);
console.log(` 服务器信息: http://localhost:${PORT}/info`);
logInfo(`Health: http://localhost:${PORT}/health`);
logInfo(`Info: http://localhost:${PORT}/info`);
});
} else {
// stdio 模式
@ -186,7 +193,7 @@ async function main() {
const transport = new StdioServerTransport();
try {
await server.connect(transport);
console.log('HowToCook MCP STDIO 服务器启动成功');
logInfo("HowToCook MCP STDIO server started.");
} catch (error) {
console.error('服务器启动失败:', error);
process.exit(1);
@ -196,12 +203,12 @@ async function main() {
// 优雅关闭
process.on('SIGINT', async () => {
console.log('\n正在关闭服务器...');
logInfo("\nShutting down server...");
process.exit(0);
});
process.on('SIGTERM', async () => {
console.log('\n收到终止信号正在关闭服务器...');
logInfo("\nReceived termination signal, shutting down server...");
process.exit(0);
});