@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
52 lines • 2.34 kB
JavaScript
// Server logging notifications (todo 42, logging half — tool-call progress
// already flows through callMcpTool's onprogress → the executing tool's
// onUpdate callback since W4).
//
// notifications/message routes into the per-server MCP logger with RFC-5424
// levels folded onto the logger's four methods; the server config's
// `logLevel` field (inert since W1) now filters below-threshold messages, and
// a token bucket caps bursts at 10 messages/second so a chatty server cannot
// flood the ring buffer — the final message of a burst always lands because
// the bucket refills continuously.
import { LoggingMessageNotificationSchema, registerMcpNotificationHandler } from "./notification-schemas.js";
export const MCP_LOG_RATE_LIMIT_PER_SECOND = 10;
/** RFC-5424 severity order, most→least severe. */
const LEVEL_ORDER = ["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"];
function severity(level) {
const index = LEVEL_ORDER.indexOf(level);
return index === -1 ? LEVEL_ORDER.indexOf("info") : index;
}
function loggerMethod(level) {
switch (level) {
case "debug":
return "debug";
case "info":
case "notice":
return "info";
case "warning":
return "warn";
default:
return "error";
}
}
export function subscribeMcpServerLogging(client, options) {
const threshold = options.logLevel === undefined ? undefined : severity(options.logLevel);
const rate = options.ratePerSecond ?? MCP_LOG_RATE_LIMIT_PER_SECOND;
const now = options.now ?? Date.now;
let tokens = rate;
let lastRefill = now();
registerMcpNotificationHandler(client, LoggingMessageNotificationSchema, (notification) => {
const { level, data, logger: serverLogger } = notification.params;
if (threshold !== undefined && severity(level) > threshold)
return;
const at = now();
tokens = Math.min(rate, tokens + ((at - lastRefill) / 1000) * rate);
lastRefill = at;
if (tokens < 1)
return;
tokens -= 1;
const text = typeof data === "string" ? data : JSON.stringify(data);
options.logger[loggerMethod(level)](`[server${serverLogger ? `:${serverLogger}` : ""}] ${text}`);
});
}
//# sourceMappingURL=logging.js.map