Skip to content

fix: improve MCP server logging and transport reliability#84

Open
dionlow wants to merge 4 commits into
mainfrom
dl-mcp-logger-fixes
Open

fix: improve MCP server logging and transport reliability#84
dionlow wants to merge 4 commits into
mainfrom
dl-mcp-logger-fixes

Conversation

@dionlow
Copy link
Copy Markdown
Contributor

@dionlow dionlow commented Jan 27, 2026

Summary

  • Fix MCP protocol corruption by replacing console.log with stderr logging
  • Improve logger robustness with better error handling
  • Deduplicate logger code by re-exporting from SDK to mcp-server
  • Fix logger method calls to use proper argument count

Details

MCP Protocol Fix

The MCP protocol uses stdio for JSON-RPC communication. Using console.log writes to stdout, which corrupts the protocol. This PR fixes the issue by:

  • Using console.error (stderr) for all logging
  • Ensuring MCP protocol messages remain clean on stdout

Logger Improvements

  • Error object handling: Preserves stack traces when logging Error objects
  • Circular reference handling: Adds try-catch around JSON.stringify to handle circular references gracefully
  • Code deduplication: Re-exports logger from SDK to mcp-server, eliminating duplicate implementation

Code Quality

  • Corrects logger method calls to use proper argument count
  • Removes unused logger imports

Test plan

  • Verify MCP server communication works without protocol corruption
  • Test logging of Error objects preserves stack traces
  • Test logging of objects with circular references doesn't crash
  • Verify all logger calls use correct argument count

🤖 Generated with Claude Code

Dion Low and others added 4 commits January 27, 2026 15:13
…uption

Replace all console.log() calls with logger.info() using stderr (console.error)
to prevent JSON-RPC protocol corruption in stdio transport. Console.log writes
to stdout which interferes with MCP protocol messages, causing JSON parsing
errors in Claude Desktop.

Changes:
- Add logger utility to mcp-server and sdk packages
- Replace console.log with logger.info in all source files
- Replace console.error with logger.error for consistency
- All logs now include timestamps and structured formatting

Fixes errors like:
- "Unexpected token 'C', 'Connecting'... is not valid JSON"
- "Unexpected token 'W', '[WRITE]'... is not valid JSON"
- "Unexpected token 'A', '[Ampersand]'... is not valid JSON"

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Logger methods (info, error, warn, debug) accept only 2 parameters (message and optional data), but were being called with 3-4 arguments causing TypeScript errors. Consolidated multiple arguments into objects for the data parameter.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
- Export logger from sdk/lib/adapters/mcp.ts for use by mcp-server
- Update all mcp-server files to import logger from @amp-labs/ai/mcp
- Remove duplicate mcp-server/src/logger.ts file
- Make providerWorkspaceRef optional in ClientSettings type
- Improve providerWorkspaceRef handling with proper fallback logic

This eliminates code duplication, ensures consistency across packages, and reduces maintenance burden.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
- Add Error object handling in logger to preserve stack traces
- Add try-catch around JSON.stringify to handle circular references
- Remove unused logger import from write.ts
- Move logger re-export after imports for better organization

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@dionlow
Copy link
Copy Markdown
Contributor Author

dionlow commented Jan 27, 2026

split from previous branch that was reverted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant