
console.log你的标准 MCP 服务器
概览
在开发带有stdio传输的MCP(模型上下文协议)服务器时,console.log会干扰协议通信。 例如,当你在MCP服务器代码中调用console.log时,通常会在MCP inspector npx @modelcontextprotocol/inspector中得到以下内容: 该包对控制台方法(日志、警告、错误、调试)进行补丁,并将日志重定向到独立终端。 https://github.com/user-attachments/assets/33ff367e-74b4-48e8-9f04-0bcafbad5e00 在你想查看日志的终端中启动日志: 或者如果你只想在开发模式下使用:
概述(AI 翻译)
console.log你的标准 MCP 服务器
为什么?
在开发带有stdio传输的MCP(模型上下文协议)服务器时,console.log会干扰协议通信。
例如,当你调用 MCP 服务器代码中的 console.log 时,通常会在 MCP 检查器 npx @modelcontextprotocol/inspector 中得到这样的信息:
解决方案
该包对控制台方法(日志、警告、错误、调试)进行补丁,并将日志重定向到独立终端。
https://github.com/user-attachments/assets/33ff367e-74b4-48e8-9f04-0bcafbad5e00
用途
在你想查看日志的终端中启动日志:
在你的MCP服务器上安装记录器:
添加到你的MCP服务器入口文件中:
或者如果你只想在开发模式下使用:
本页由 AI 翻译,技术细节请以原文为准。
README
MCP Server Logger
console.log for your stdio MCP server
Why?
When developing an MCP (Model Context Protocol) server with stdio transport, console.log interferes with the protocol communication.
For example, this is what you normally get in MCP inspector npx @modelcontextprotocol/inspector when calling console.log in your MCP server code:
Error from MCP server: SyntaxError: Unexpected token S in JSON at position 0
Solution
This package patches the console methods (log, warn, error, debug) and redirects logs to a separate terminal.
https://github.com/user-attachments/assets/33ff367e-74b4-48e8-9f04-0bcafbad5e00
Usage
Start the logger in a terminal where you want to see the logs:
npx mcps-logger
Install the logger in your MCP server:
npm install mcps-logger
Add it to your MCP server entry file:
import "mcps-logger/console";
Or if you want to use in development mode only:
if (process.env.NODE_ENV !== "production") {
import("mcps-logger/console");
}
安装
npx mcps-logger