How do you fix JSON-RPC stdout corruption in MCP?
JSON-RPC communication in MCP servers uses the standard output (`stdout`) channel. Standard library console print calls (like `console.log` in TypeScript) pollute this channel, causing protocol parser failures. Redirecting all log utilities (`console.log`, `console.info`, `console.dir`) to standard error (`stderr`) resolves standard output corruption.
const originalLog = console.log;
console.log = console.info = console.dir = (...args) => {
console.error("[INFO Log Redirect]:", ...args);
};How do you prevent Node.exe zombie processes on MCP server shutdown?
During local stdio execution, if the host parent client terminates or crashes, Node.exe processes can continue running in the background. Attaching an event listener to `process.stdin` for `'close'` events and invoking `process.exit(0)` prevents zombie Node.exe processes on host shutdown.
process.stdin.on("close", () => {
console.error("Stdin closed, shutting down server...");
process.exit(0);
});How do you define optional parameters inside FastMCP python tool decorators?
FastMCP relies on Python type annotations and Pydantic validation to verify incoming arguments. When tool parameters are optional, specifying `= None` as the default value in the signature prevents validation errors on omitted arguments.
@mcp.tool()
def get_weather(city: str, format: str = None) -> str:
"""Fetch weather reports for a city."""
return f"Weather in {city}: sunny"How do you register dynamic resource paths under the MCP specification?
Standard resources are registered under `ListResourcesRequestSchema`. However, parameterized dynamic resource paths containing path variables (like {category}) must be registered under `ListResourceTemplatesRequestSchema` as `uriTemplate` definitions to be valid under the Model Context Protocol.
server.setRequestHandler(ListResourceTemplatesRequestSchema, async () => {
return {
resourceTemplates: [
{
uriTemplate: "logs://{category}/runtime",
name: "system_logs",
description: "Dynamic runtime log streams"
}
]
};
});