@mastra/mcp 0.10.2-alpha.1 → 0.10.3-alpha.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/src/logger.ts DELETED
@@ -1,104 +0,0 @@
1
- import * as fs from 'fs';
2
- import * as os from 'os';
3
- import * as path from 'path';
4
- import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
5
-
6
- // Logger interface for type safety
7
- export interface Logger {
8
- info: (message: string, data?: any) => Promise<void>;
9
- warning: (message: string, data?: any) => Promise<void>;
10
- error: (message: string, error?: any) => Promise<void>;
11
- debug: (message: string, data?: any) => Promise<void>;
12
- }
13
-
14
- export const writeErrorLog = (message: string, data?: any) => {
15
- const now = new Date();
16
- const timestamp = now.toISOString();
17
- const hourTimestamp = timestamp.slice(0, 13); // YYYY-MM-DDTHH
18
-
19
- // Create log message
20
- const logMessage = {
21
- timestamp,
22
- message,
23
- ...(data ? (typeof data === 'object' ? data : { data }) : {}),
24
- };
25
-
26
- // Write to file
27
- try {
28
- // Ensure cache directory exists
29
- const cacheDir = path.join(os.homedir(), '.cache', 'mastra', 'mcp-docs-server-logs');
30
- fs.mkdirSync(cacheDir, { recursive: true });
31
-
32
- // Create log file path with timestamp
33
- const logFile = path.join(cacheDir, `${hourTimestamp}.log`);
34
-
35
- // Append log entry to file
36
- fs.appendFileSync(logFile, JSON.stringify(logMessage) + '\n', 'utf8');
37
- } catch (err) {
38
- // If file writing fails, at least we still have stderr
39
- console.error('Failed to write to log file:', err);
40
- }
41
- };
42
-
43
- // Create logger factory to inject server instance
44
- export function createLogger(server?: Server): Logger {
45
- const sendLog = async (level: 'error' | 'debug' | 'info' | 'warning', message: string, data?: any) => {
46
- if (!server) return;
47
-
48
- try {
49
- await server.sendLoggingMessage({
50
- level,
51
- data: {
52
- message,
53
- ...(data ? (typeof data === 'object' ? data : { data }) : {}),
54
- },
55
- });
56
- } catch (error) {
57
- if (
58
- error instanceof Error &&
59
- (error.message === 'Not connected' ||
60
- error.message.includes('does not support logging') ||
61
- error.message.includes('Connection closed'))
62
- ) {
63
- return;
64
- }
65
- console.error(`Failed to send ${level} log:`, error instanceof Error ? error.message : error);
66
- }
67
- };
68
-
69
- return {
70
- info: async (message: string, data?: any) => {
71
- // Log to stderr to avoid MCP protocol conflicts on stdout
72
- console.error(message, data ? data : '');
73
- await sendLog('info', message, data);
74
- },
75
- warning: async (message: string, data?: any) => {
76
- // Log to stderr to avoid MCP protocol conflicts on stdout
77
- console.error(message, data ? data : '');
78
- await sendLog('warning', message, data);
79
- },
80
- error: async (message: string, error?: any) => {
81
- const errorData =
82
- error instanceof Error
83
- ? {
84
- message: error.message,
85
- stack: error.stack,
86
- name: error.name,
87
- }
88
- : error;
89
- writeErrorLog(message, errorData);
90
- console.error(message, errorData ? errorData : '');
91
- await sendLog('error', message, errorData);
92
- },
93
- debug: async (message: string, data?: any) => {
94
- if (process.env.DEBUG || process.env.NODE_ENV === 'development') {
95
- // Log to stderr to avoid MCP protocol conflicts on stdout
96
- console.error(message, data ? data : '');
97
- await sendLog('debug', message, data);
98
- }
99
- },
100
- };
101
- }
102
-
103
- // Create a default logger instance
104
- export const logger = createLogger();