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