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: exercises/01.ping/01.solution.connect/README.mdx
+51-10Lines changed: 51 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,27 +30,68 @@ const args = [
30
30
const child =spawn(command, args)
31
31
```
32
32
33
-
Then it listens for messages from the child process and sends them to the MCP
34
-
server:
33
+
Once started, the client and server communicate by sending and receiving messages through standard input and output (stdio). These messages follow the JSON-RPC format, which is a standard way to structure requests and responses.
34
+
35
+
### Example: Ping Request and Response
36
+
37
+
Suppose the client wants to check if the server is running. It sends a `ping` request to the server by writing a line of JSON to the server's standard input:
console.log('Received from server:', data.toString())
66
+
// Should log: {"jsonrpc": "2.0", "id": "123", "result": {}}
39
67
})
40
68
```
41
69
42
-
This is why we use `console.error` in our server, so that the MCP Client can
43
-
print it to the console without interfering with the server's output.
70
+
> **Note:** Each message is typically sent as a single line of JSON, so a newline character (`\n`) is used to separate messages.
44
71
45
-
It also listens for messages from the MCP server and sends them to the child
46
-
process:
72
+
#### ⚠️ What happens if you use `console.log` for logging?
73
+
74
+
If your server uses `console.log` for regular logging, those log messages will be sent to standard output (stdout) along with your protocol messages. This can confuse the client, which expects only valid JSON-RPC messages on stdout.
When the client tries to read and parse the response, it will encounter the plain text `Server started!` and likely throw a parsing error, because it expects only JSON.
90
+
91
+
That's why you should use `console.error` for logging and debugging output. Standard error (stderr) is separate from stdout, so log messages won't interfere with the protocol communication.
92
+
52
93
Other transport mechanisms exist as well, and while the code for them is
53
-
different the underlying concept is the same. Just a server and client
54
-
communicating with each other.
94
+
different the underlying concept is the same: a server and client
95
+
communicating with each other using a standard protocol.
0 commit comments