forked from punkpeye/fastmcp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom-logger.ts
More file actions
201 lines (173 loc) · 5.19 KB
/
Copy pathcustom-logger.ts
File metadata and controls
201 lines (173 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
* Example FastMCP server demonstrating custom logger implementations.
*
* Features demonstrated:
* - Simple custom logger implementation
* - File-based logging example
* - Winston logger adapter
* - Pino logger adapter
*
*/
import { z } from "zod";
import { FastMCP, Logger } from "../FastMCP.js";
// Example 1: Simple Custom Logger Implementation
class SimpleCustomLogger implements Logger {
debug(...args: unknown[]): void {
console.log("[CUSTOM DEBUG]", new Date().toISOString(), ...args);
}
error(...args: unknown[]): void {
console.error("[CUSTOM ERROR]", new Date().toISOString(), ...args);
}
info(...args: unknown[]): void {
console.info("[CUSTOM INFO]", new Date().toISOString(), ...args);
}
log(...args: unknown[]): void {
console.log("[CUSTOM LOG]", new Date().toISOString(), ...args);
}
warn(...args: unknown[]): void {
console.warn("[CUSTOM WARN]", new Date().toISOString(), ...args);
}
}
// Example 2: File-based Logger
// class FileLogger implements Logger {
// debug(...args: unknown[]): void {
// this.logToFile('DEBUG', ...args);
// }
// error(...args: unknown[]): void {
// this.logToFile('ERROR', ...args);
// }
// info(...args: unknown[]): void {
// this.logToFile('INFO', ...args);
// }
// log(...args: unknown[]): void {
// this.logToFile('LOG', ...args);
// }
// warn(...args: unknown[]): void {
// this.logToFile('WARN', ...args);
// }
// private logToFile(level: string, ...args: unknown[]): void {
// const timestamp = new Date().toISOString();
// const message = `[${timestamp}] [${level}] ${args.map(arg =>
// typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
// ).join(' ')}\n`;
// // In a real implementation, you might use fs.appendFile or a logging library
// console.log(message.trim());
// }
// }
// Example 3: Winston Logger Adapter
// To use this example, install winston: npm install winston
// import winston from 'winston';
// class WinstonLoggerAdapter implements Logger {
// private winston: winston.Logger;
// constructor() {
// this.winston = winston.createLogger({
// level: 'debug',
// format: winston.format.combine(
// winston.format.timestamp(),
// winston.format.errors({ stack: true }),
// winston.format.json()
// ),
// transports: [
// new winston.transports.Console({
// format: winston.format.combine(
// winston.format.colorize(),
// winston.format.simple()
// )
// }),
// new winston.transports.File({ filename: 'fastmcp.log' })
// ]
// });
// }
// debug(...args: unknown[]): void {
// this.winston.debug(this.formatArgs(args));
// }
// error(...args: unknown[]): void {
// this.winston.error(this.formatArgs(args));
// }
// info(...args: unknown[]): void {
// this.winston.info(this.formatArgs(args));
// }
// log(...args: unknown[]): void {
// this.winston.info(this.formatArgs(args));
// }
// warn(...args: unknown[]): void {
// this.winston.warn(this.formatArgs(args));
// }
// private formatArgs(args: unknown[]): string {
// return args.map(arg =>
// typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
// ).join(' ');
// }
// }
// Example 4: Pino Logger Adapter
// To use this example, install pino: npm install pino
// import pino from 'pino';
//
// class PinoLoggerAdapter implements Logger {
// private pino: pino.Logger;
//
// constructor() {
// this.pino = pino({
// level: 'debug',
// transport: {
// target: 'pino-pretty',
// options: {
// colorize: true,
// translateTime: 'SYS:standard',
// ignore: 'pid,hostname'
// }
// }
// });
// }
//
// debug(...args: unknown[]): void {
// this.pino.debug(this.formatMessage(args));
// }
//
// error(...args: unknown[]): void {
// this.pino.error(this.formatMessage(args));
// }
//
// info(...args: unknown[]): void {
// this.pino.info(this.formatMessage(args));
// }
//
// log(...args: unknown[]): void {
// this.pino.info(this.formatMessage(args));
// }
//
// warn(...args: unknown[]): void {
// this.pino.warn(this.formatMessage(args));
// }
//
// private formatMessage(args: unknown[]): string {
// return args.map(arg =>
// typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
// ).join(' ');
// }
// }
// Choose which logger to use (uncomment the one you want to use)
const logger = new SimpleCustomLogger();
// const logger = new FileLogger();
// const logger = new WinstonLoggerAdapter();
// const logger = new PinoLoggerAdapter();
const server = new FastMCP({
logger: logger,
name: "custom-logger-example",
version: "1.0.0",
});
server.addTool({
description: "A test tool that demonstrates custom logging",
execute: async (args) => {
return `Received: ${args.message}`;
},
name: "test_tool",
parameters: z.object({
message: z.string().describe("A message to log"),
}),
});
// Start the server with stdio transport
server.start({ transportType: "stdio" }).catch((error: unknown) => {
console.error("Failed to start server:", error);
process.exit(1);
});