Skip to content

Commit 9425ced

Browse files
committed
feat: basic logger
1 parent c461cad commit 9425ced

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

TODO.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
## TODO
22

33
NEXT:
4-
- Add error to make sure people don't pass objects as args to the functions that are decorated. This is critical.
4+
- Logging
5+
- configurable logger class w/log levels
6+
- serializes to client
7+
- on a context object
8+
- Add error to make sure people don't pass objects as args to the functions that are decorated. This is critical for the experimental API
59
- Prompt in theory accepts inputs by the TS types don't suggest it can and it doesn't seem to share them.
610
- Conversion and formatting of messages and responses
711
- What are the supported data types

lib/EasyMCP.ts

+15
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ import {
3232
type ReadResourceResult,
3333
type Root,
3434
type ServerCapabilities,
35+
LoggingMessageNotificationSchema,
36+
LoggingLevelSchema,
37+
type LoggingLevel,
3538
} from "@modelcontextprotocol/sdk/types.js";
3639
import ToolManager from "./ToolManager";
3740
import PromptManager from "./PromptManager";
3841
import RootsManager from "./RootsManager";
3942
import { metadataKey } from "./MagicConfig";
43+
import LogFormatter from "./LogFormatter";
4044

4145
class BaseMCP {
4246
name: string;
@@ -250,6 +254,17 @@ class BaseMCP {
250254
console.log("Registered ListRoots endpoint");
251255
}
252256

257+
sendLog({ level, message }: { level: LoggingLevel; message: string }) {
258+
if (!this.server) {
259+
throw new Error("Server not initialized. Call serve() first.");
260+
}
261+
262+
this.server.sendLoggingMessage({
263+
level,
264+
message: LogFormatter.format(level, message),
265+
});
266+
}
267+
253268
static create(name: string, opts: ServerOptions) {
254269
return new BaseMCP(name, opts);
255270
}

lib/LogFormatter.ts

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import type { LoggingLevel } from "@modelcontextprotocol/sdk/types.js";
2+
3+
/**
4+
* Prefixes messages with their log level
5+
*/
6+
export default class LogFormatter {
7+
static format(level: LoggingLevel, message: string): string {
8+
switch (level) {
9+
case "debug":
10+
return this.debug(message);
11+
case "info":
12+
return this.info(message);
13+
case "warning":
14+
return this.warning(message);
15+
case "error":
16+
return this.error(message);
17+
case "notice":
18+
return this.notice(message);
19+
case "critical":
20+
return this.critical(message);
21+
case "alert":
22+
return this.alert(message);
23+
case "emergency":
24+
return this.emergency(message);
25+
default:
26+
console.warn(
27+
"Invalid log level passed to LogFormatter. This should never happen.",
28+
level,
29+
);
30+
return message;
31+
}
32+
}
33+
34+
private static debug(message: string) {
35+
return "debug: " + message;
36+
}
37+
38+
private static notice(message: string) {
39+
return "notice: " + message;
40+
}
41+
42+
private static critical(message: string) {
43+
return "critical: " + message;
44+
}
45+
46+
private static alert(message: string) {
47+
return "alert: " + message;
48+
}
49+
50+
private static emergency(message: string) {
51+
return "emergency: " + message;
52+
}
53+
54+
private static info(message: string) {
55+
return "info: " + message;
56+
}
57+
58+
private static warning(message: string) {
59+
return "warning: " + message;
60+
}
61+
62+
private static error(message: string) {
63+
return "error: " + message;
64+
}
65+
}

0 commit comments

Comments
 (0)