Model Context Protocol in OCaml

Fix JSON-RPC protocol handling in example servers

- Replace stdout prints with stderr to avoid corrupting JSON-RPC protocol
- Fix issue with log messages being sent over stdout instead of stderr
- Ensure all debug/info messages don't interfere with protocol communication
- Apply fix to all example servers for consistent behavior

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

+9 -7
bin/audio_example.ml
···
(* Define startup and shutdown hooks *)
let startup () =
-
Printf.printf "AudioExampleServer is starting up!\n";
-
flush stdout;
+
(* Use stderr for direct printing to avoid interfering with JSON-RPC protocol *)
+
Printf.fprintf stderr "AudioExampleServer is starting up!\n";
+
flush stderr;
Log.info "AudioExampleServer is starting up!"
let shutdown () =
-
Printf.printf "AudioExampleServer is shutting down. Goodbye!\n";
-
flush stdout;
+
Printf.fprintf stderr "AudioExampleServer is shutting down. Goodbye!\n";
+
flush stderr;
Log.info "AudioExampleServer is shutting down. Goodbye!"
(* Register the hooks *)
···
(* Main function *)
let () =
-
(* Print directly to ensure we see output *)
-
Printf.printf "Starting AudioExampleServer...\n";
-
flush stdout;
+
(* Use stderr for direct printing to avoid interfering with JSON-RPC protocol *)
+
Printf.fprintf stderr "Starting AudioExampleServer...\n";
+
flush stderr;
+
Log.info "Starting AudioExampleServer...";
(* Configure the server with appropriate capabilities *)
ignore (configure_server server ());
+9 -7
bin/capitalize_sdk.ml
···
(* Define startup and shutdown hooks *)
let startup () =
-
Printf.printf "CapitalizeServer is starting up!\n";
-
flush stdout;
+
(* Use stderr for direct printing to avoid interfering with JSON-RPC protocol *)
+
Printf.fprintf stderr "CapitalizeServer is starting up!\n";
+
flush stderr;
Log.info "CapitalizeServer is starting up!"
let shutdown () =
-
Printf.printf "CapitalizeServer is shutting down. Goodbye!\n";
-
flush stdout;
+
Printf.fprintf stderr "CapitalizeServer is shutting down. Goodbye!\n";
+
flush stderr;
Log.info "CapitalizeServer is shutting down. Goodbye!"
(* Register the hooks *)
···
(* Main function *)
let () =
-
(* Print directly to ensure we see output *)
-
Printf.printf "Starting CapitalizeServer...\n";
-
flush stdout;
+
(* Use stderr for direct printing to avoid interfering with JSON-RPC protocol *)
+
Printf.fprintf stderr "Starting CapitalizeServer...\n";
+
flush stderr;
+
Log.info "Starting CapitalizeServer...";
(* Configure the server with appropriate capabilities *)
ignore (configure_server server ());
+9 -7
bin/completion_example.ml
···
(* Define startup and shutdown hooks *)
let startup () =
-
Printf.printf "CompletionServer is starting up!\n";
-
flush stdout;
+
(* Use stderr for direct printing to avoid interfering with JSON-RPC protocol *)
+
Printf.fprintf stderr "CompletionServer is starting up!\n";
+
flush stderr;
Log.info "CompletionServer is starting up!"
let shutdown () =
-
Printf.printf "CompletionServer is shutting down. Goodbye!\n";
-
flush stdout;
+
Printf.fprintf stderr "CompletionServer is shutting down. Goodbye!\n";
+
flush stderr;
Log.info "CompletionServer is shutting down. Goodbye!"
(* Register the hooks *)
···
(* Main function *)
let () =
-
(* Print directly to ensure we see output *)
-
Printf.printf "Starting CompletionServer...\n";
-
flush stdout;
+
(* Use stderr for direct printing to avoid interfering with JSON-RPC protocol *)
+
Printf.fprintf stderr "Starting CompletionServer...\n";
+
flush stderr;
+
Log.info "Starting CompletionServer...";
(* Set custom capabilities to indicate support for completions *)
let capabilities = `Assoc [
+8 -7
bin/resource_template_example.ml
···
(* Define startup and shutdown hooks *)
let startup () =
-
Printf.printf "ResourceTemplateServer is starting up!\n";
-
flush stdout;
+
(* Use stderr for direct printing to avoid interfering with JSON-RPC protocol *)
+
Printf.fprintf stderr "ResourceTemplateServer is starting up!\n";
+
flush stderr;
Log.info "ResourceTemplateServer is starting up!"
let shutdown () =
-
Printf.printf "ResourceTemplateServer is shutting down. Goodbye!\n";
-
flush stdout;
+
Printf.fprintf stderr "ResourceTemplateServer is shutting down. Goodbye!\n";
+
flush stderr;
Log.info "ResourceTemplateServer is shutting down. Goodbye!"
(* Register the hooks *)
···
(* Main function *)
let () =
-
(* Print directly to ensure we see output *)
-
Printf.printf "Starting ResourceTemplateServer...\n";
-
flush stdout;
+
(* Instead of printing directly to stdout which messes up the JSON-RPC protocol,
+
use the logging system which sends output to stderr *)
+
Log.info "Starting ResourceTemplateServer...";
(* Configure the server with appropriate capabilities *)
ignore (configure_server server ());