forked from modelcontextprotocol/servers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdio.ts
More file actions
63 lines (50 loc) · 2.14 KB
/
stdio.ts
File metadata and controls
63 lines (50 loc) · 2.14 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
#!/usr/bin/env node
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { createServer } from "./everything.js";
import {
LoggingLevel,
LoggingLevelSchema,
LoggingMessageNotification,
SetLevelRequestSchema
} from "@modelcontextprotocol/sdk/types.js";
console.error('Starting default (STDIO) server...');
async function main() {
const transport = new StdioServerTransport();
const {server, cleanup, startNotificationIntervals } = createServer();
// Currently, for STDIO servers, automatic log-level support is not available, as levels are tracked by sessionId.
// The listener will be set, so if the STDIO server advertises support for logging, and the client sends a setLevel
// request, it will be handled and thus not throw a "Method not found" error. However, the STDIO server will need to
// implement its own listener and level handling for now. This will be remediated in a future SDK version.
let logLevel: LoggingLevel = "debug";
server.setRequestHandler(SetLevelRequestSchema, async (request) => {
const { level } = request.params;
logLevel = level;
return {};
});
server.sendLoggingMessage = async (params: LoggingMessageNotification["params"], _: string|undefined): Promise<void> => {
const LOG_LEVEL_SEVERITY = new Map(
LoggingLevelSchema.options.map((level, index) => [level, index])
);
const isMessageIgnored = (level: LoggingLevel): boolean => {
const currentLevel = logLevel;
return (currentLevel)
? LOG_LEVEL_SEVERITY.get(level)! < LOG_LEVEL_SEVERITY.get(currentLevel)!
: false;
};
if (!isMessageIgnored(params.level)) {
return server.notification({method: "notifications/message", params})
}
}
await server.connect(transport);
startNotificationIntervals();
// Cleanup on exit
process.on("SIGINT", async () => {
await cleanup();
await server.close();
process.exit(0);
});
}
main().catch((error) => {
console.error("Server error:", error);
process.exit(1);
});