For a stdio server, standard output is the line to the client. Every line there must be a valid MCP message. A plain print() for debugging lands on the same line – the client tries to read it as JSON and fails.
Symptom
pydantic_core._pydantic_core.ValidationError: 1 validation error for union[JSONRPCRequest,JSONRPCNotification,JSONRPCResponse,JSONRPCError] Invalid JSON: expected value at line 1 column 1 [type=json_invalid, input_value='Berechne /', input_type=str]
In the course, the first line of the tool function is print("Berechne", pfad) (“calculating”). The client starts the server, calls the tool – and reports this error.
Diagnosis
Read input_value: it shows the line the client could not understand. It does not come from the protocol but from your code. Start the client as in the course:
uv run python client.py
Causes
print() to stdoutDebug output on the protocol line
print() writes to stdout by default. With stdio, that is exactly the channel server and client use for JSON-RPC. The specification only allows valid MCP messages there.
library writes to stdoutForeign output at startup
The same happens when an imported package or a start script writes messages to stdout. The client only sees lines that do not start with {.
Fix
import sys
…
print("Berechne", pfad, file=sys.stderr)
Don't: Ignore the error because the Python SDK skips the line. Other clients drop the connection on such a line.
MCPA hands-on, Day 1: your first MCP server, a client in a few lines and the messages that flow between them.
Go to day 1 →
FAQ
Why does the server keep working anyway?
The Python SDK skips lines that are not JSON and carries on. You cannot rely on that tolerance with other clients.
Does this also apply to Streamable HTTP?
No. Over HTTP, the response body carries the message; print() only ends up in the server's log.