Skip to content

Commit e61249c

Browse files
authored
Merge pull request modelcontextprotocol#888 from projectAcetylcholine/mcp-logging-best-practices
docs: Update server.mdx to including MCP server logging best practices
2 parents ec14189 + 17d4fed commit e61249c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

docs/quickstart/server.mdx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,41 @@ This quickstart assumes you have familiarity with:
4646
- Python
4747
- LLMs like Claude
4848

49+
### Logging in MCP Servers
50+
51+
When implementing MCP servers, be careful about how you handle logging:
52+
53+
**For STDIO-based servers:** Never write to standard output (stdout). This includes:
54+
55+
- `print()` statements in Python
56+
- `console.log()` in JavaScript
57+
- `fmt.Println()` in Go
58+
- Similar stdout functions in other languages
59+
60+
Writing to stdout will corrupt the JSON-RPC messages and break your server.
61+
62+
**For HTTP-based servers:** Standard output logging is fine since it doesn't interfere with HTTP responses.
63+
64+
### Best Practices
65+
66+
1. Use a logging library that writes to stderr or files, such as `logging` in Python.
67+
2. For JavaScript, be especially careful - `console.log()` writes to stdout by default
68+
69+
### Quick Examples
70+
71+
````javascript
72+
// ❌ Bad (STDIO)
73+
console.log("Server started");
74+
75+
// ✅ Good (STDIO)
76+
console.error("Server started"); // stderr is safe
77+
```python
78+
# ❌ Bad (STDIO)
79+
print("Processing request")
80+
81+
# ✅ Good (STDIO)
82+
import logging
83+
logging.info("Processing request")
4984
### System requirements
5085
5186
- Python 3.10 or higher installed.

0 commit comments

Comments
 (0)