Skip to content

Commit c071714

Browse files
izzymsftdsp-ant
andauthored
Saving Copilot suggested changes for modelcontextprotocol#840
Co-authored-by: David Soria Parra <[email protected]>
1 parent 57a6181 commit c071714

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

docs/quickstart/server.mdx

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,40 @@ This quickstart assumes you have familiarity with:
4646
- Python
4747
- LLMs like Claude
4848

49-
### Guidance on Logging Best Practices in MCP Servers
49+
### Logging in MCP Servers
5050

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:
5352

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
5558

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.
5760

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.
6162

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
76+
```python
77+
# ❌ Bad (STDIO)
78+
print("Processing request")
79+
80+
# ✅ Good (STDIO)
81+
import logging
82+
logging.info("Processing request")
6283
### System requirements
6384
6485
- Python 3.10 or higher installed.

0 commit comments

Comments
 (0)