Skip to content

Commit 26c73b9

Browse files
committed
split out logging code
1 parent fb52a13 commit 26c73b9

File tree

2 files changed

+51
-50
lines changed

2 files changed

+51
-50
lines changed

src/always_log.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,50 @@
1+
let verbose = false;
2+
// check CLI args:
3+
if (process.argv.includes("--verbose")) {
4+
verbose = true;
5+
}
6+
7+
if (verbose) {
8+
always_log("INFO: verbose logging enabled");
9+
} else {
10+
always_log("INFO: verbose logging disabled, enable it with --verbose");
11+
}
12+
13+
export function verbose_log(message: string, data?: any) {
14+
// https://modelcontextprotocol.io/docs/tools/debugging - mentions various ways to debug/troubleshoot (including dev tools)
15+
//
16+
// remember STDIO transport means can't log over STDOUT (client expects JSON messages per the spec)
17+
// https://modelcontextprotocol.io/docs/tools/debugging#implementing-logging
18+
// mentions STDERR is captured by the host app (i.e. Claude Desktop app)
19+
// server.sendLoggingMessage is captured by MCP client (not Claude Desktop app)
20+
// SO, IIUC use STDERR for logging into Claude Desktop app logs in:
21+
// '~/Library/Logs/Claude/mcp.log'
22+
if (verbose) {
23+
always_log(message, data);
24+
}
25+
// inspector, catches these logs and shows them on left hand side of screen (sidebar)
26+
27+
// TODO add verbose parameter (CLI arg?)
28+
29+
// IF I wanted to log via MCP client logs (not sure what those are/do):
30+
// I do not see inspector catching these logs :(, there is a server notifications section and it remains empty
31+
//server.sendLoggingMessage({
32+
// level: "info",
33+
// data: message,
34+
//});
35+
// which results in something like:
36+
//server.notification({
37+
// method: "notifications/message",
38+
// params: {
39+
// level: "warning",
40+
// logger: "mcp-server-commands",
41+
// data: "ListToolsRequest2",
42+
// },
43+
//});
44+
//
45+
// FYI client should also requets a log level from the server, so that needs to be here at some point too
46+
}
47+
148
export function always_log(message: string, data?: any) {
249
if (data) {
350
console.error(message + ": " + JSON.stringify(data));

src/index.ts

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env node
2-
import os from 'os'
2+
import os from "os";
33

44
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
55
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
@@ -16,7 +16,7 @@ import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
1616
import { runCommand } from "./run-command.js";
1717

1818
import { createRequire } from "module";
19-
import { always_log } from "./always_log.js";
19+
import { verbose_log } from "./always_log.js";
2020
const require = createRequire(import.meta.url);
2121
const {
2222
name: package_name,
@@ -26,12 +26,6 @@ const {
2626
// TODO use .promises? in node api
2727
const execAsync = promisify(exec);
2828

29-
let verbose = false;
30-
// check CLI args:
31-
if (process.argv.includes("--verbose")) {
32-
verbose = true;
33-
}
34-
3529
const server = new Server(
3630
{
3731
name: package_name,
@@ -48,54 +42,14 @@ const server = new Server(
4842
}
4943
);
5044

51-
if (verbose) {
52-
always_log("INFO: verbose logging enabled");
53-
} else {
54-
always_log("INFO: verbose logging disabled, enable it with --verbose");
55-
}
56-
57-
function verbose_log(message: string, data?: any) {
58-
// https://modelcontextprotocol.io/docs/tools/debugging - mentions various ways to debug/troubleshoot (including dev tools)
59-
//
60-
// remember STDIO transport means can't log over STDOUT (client expects JSON messages per the spec)
61-
// https://modelcontextprotocol.io/docs/tools/debugging#implementing-logging
62-
// mentions STDERR is captured by the host app (i.e. Claude Desktop app)
63-
// server.sendLoggingMessage is captured by MCP client (not Claude Desktop app)
64-
// SO, IIUC use STDERR for logging into Claude Desktop app logs in:
65-
// '~/Library/Logs/Claude/mcp.log'
66-
if (verbose) {
67-
always_log(message, data);
68-
}
69-
// inspector, catches these logs and shows them on left hand side of screen (sidebar)
70-
71-
// TODO add verbose parameter (CLI arg?)
72-
73-
// IF I wanted to log via MCP client logs (not sure what those are/do):
74-
// I do not see inspector catching these logs :(, there is a server notifications section and it remains empty
75-
//server.sendLoggingMessage({
76-
// level: "info",
77-
// data: message,
78-
//});
79-
// which results in something like:
80-
//server.notification({
81-
// method: "notifications/message",
82-
// params: {
83-
// level: "warning",
84-
// logger: "mcp-server-commands",
85-
// data: "ListToolsRequest2",
86-
// },
87-
//});
88-
//
89-
// FYI client should also requets a log level from the server, so that needs to be here at some point too
90-
}
91-
9245
server.setRequestHandler(ListToolsRequestSchema, async () => {
9346
verbose_log("INFO: ListTools");
9447
return {
9548
tools: [
9649
{
9750
name: "run_command",
98-
description: "Run a command on this " + os.platform() + " machine",
51+
description:
52+
"Run a command on this " + os.platform() + " machine",
9953
inputSchema: {
10054
type: "object",
10155
properties: {

0 commit comments

Comments
 (0)