You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/quickstart/server.mdx
+29-8Lines changed: 29 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,19 +46,40 @@ This quickstart assumes you have familiarity with:
46
46
- Python
47
47
- LLMs like Claude
48
48
49
-
### Guidance on Logging Best Practices in MCP Servers
49
+
### Logging in MCP Servers
50
50
51
-
When implementing MCP servers, it is crucial that developers avoid using print statements that write messages to standard output.
52
-
Certain MCP hosts and clients might be able to deal with these extraneous outputs, but it could result in unexpected behavior in majority of the MCP clients.
51
+
When implementing MCP servers, be careful about how you handle logging:
53
52
54
-
Logging messages to standard output could corrupt the structured output and format expected by the clients thus breaking the protocol. Using print functionality in your programming language of choice to send log messages to standard output could make your MCP server unusable.
53
+
**For STDIO-based servers:** Never write to standard output (stdout). This includes:
54
+
-`print()` statements in Python
55
+
-`console.log()` in JavaScript
56
+
-`fmt.Println()` in Go
57
+
- Similar stdout functions in other languages
55
58
56
-
Here are some tips you can use to avoid this problem:
59
+
Writing to stdout will corrupt the JSON-RPC messages and break your server.
57
60
58
-
- use the built-in logging functionality available in your language specific SDK
59
-
- if you are building your own MCP server from scratch without the SDK, please using logging mechanisms that do not print messages to standard output
60
-
- configure the log levels for your application accordingly
61
+
**For HTTP-based servers:** Standard output logging is fine since it doesn't interfere with HTTP responses.
61
62
63
+
### Best Practices
64
+
65
+
1. Use a logging library that writes to stderr or files, such as `logging` in Python.
66
+
2. For JavaScript, be especially careful - `console.log()` writes to stdout by default
67
+
68
+
### Quick Examples
69
+
70
+
```javascript
71
+
// ❌ Bad (STDIO)
72
+
console.log("Server started");
73
+
74
+
// ✅ Good (STDIO)
75
+
console.error("Server started"); // stderr is safe
0 commit comments