Skip to content

Commit 6c682e7

Browse files
authored
Merge pull request #86 from microsoft/mossaka/log-to-stderr
fix(wassette): send log to stderr for stdio transport
2 parents c426064 + a056f87 commit 6c682e7

File tree

1 file changed

+52
-49
lines changed

1 file changed

+52
-49
lines changed

src/main.rs

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -176,18 +176,6 @@ Key points:
176176

177177
#[tokio::main]
178178
async fn main() -> Result<()> {
179-
tracing_subscriber::registry()
180-
.with(
181-
tracing_subscriber::EnvFilter::try_from_default_env()
182-
.unwrap_or_else(|_| {
183-
"info,cranelift_codegen=warn,cranelift_entity=warn,cranelift_bforest=warn,cranelift_frontend=warn"
184-
.to_string()
185-
.into()
186-
}),
187-
)
188-
.with(tracing_subscriber::fmt::layer())
189-
.init();
190-
191179
let cli = Cli::parse();
192180

193181
match &cli.command {
@@ -196,49 +184,64 @@ async fn main() -> Result<()> {
196184
stdio,
197185
http,
198186
} => {
187+
// Initialize logging based on transport type
188+
let use_stdio_transport = match (*stdio, *http) {
189+
(false, false) => true, // Default case: use stdio transport
190+
(true, false) => true, // Stdio transport only
191+
(false, true) => false, // HTTP transport only
192+
(true, true) => {
193+
return Err(anyhow::anyhow!(
194+
"Running both stdio and HTTP transports simultaneously is not supported. Please choose one."
195+
));
196+
}
197+
};
198+
199+
// Configure logging - use stderr for stdio transport to avoid interfering with MCP protocol
200+
let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
201+
.unwrap_or_else(|_| {
202+
"info,cranelift_codegen=warn,cranelift_entity=warn,cranelift_bforest=warn,cranelift_frontend=warn"
203+
.to_string()
204+
.into()
205+
});
206+
207+
let registry = tracing_subscriber::registry().with(env_filter);
208+
209+
if use_stdio_transport {
210+
registry
211+
.with(
212+
tracing_subscriber::fmt::layer()
213+
.with_writer(std::io::stderr)
214+
.with_ansi(false),
215+
)
216+
.init();
217+
} else {
218+
registry.with(tracing_subscriber::fmt::layer()).init();
219+
}
220+
199221
let components_dir = PathBuf::from(plugin_dir);
200222

201223
let lifecycle_manager = LifecycleManager::new(&components_dir).await?;
202224

203225
let server = McpServer::new(lifecycle_manager);
204226

205-
match (*stdio, *http) {
206-
(false, false) => {
207-
// Default case: use stdio transport
208-
tracing::info!("Starting MCP server with stdio transport (default)");
209-
let transport = stdio_transport();
210-
let running_service = serve_server(server, transport).await?;
211-
212-
tokio::signal::ctrl_c().await?;
213-
let _ = running_service.cancel().await;
214-
}
215-
(true, false) => {
216-
// Stdio transport only
217-
tracing::info!("Starting MCP server with stdio transport");
218-
let transport = stdio_transport();
219-
let running_service = serve_server(server, transport).await?;
220-
221-
tokio::signal::ctrl_c().await?;
222-
let _ = running_service.cancel().await;
223-
}
224-
(false, true) => {
225-
// HTTP transport only
226-
tracing::info!(
227-
"Starting MCP server on {} with HTTP transport",
228-
BIND_ADDRESS
229-
);
230-
let ct = SseServer::serve(BIND_ADDRESS.parse().unwrap())
231-
.await?
232-
.with_service(move || server.clone());
233-
234-
tokio::signal::ctrl_c().await?;
235-
ct.cancel();
236-
}
237-
(true, true) => {
238-
return Err(anyhow::anyhow!(
239-
"Running both stdio and HTTP transports simultaneously is not supported. Please choose one."
240-
));
241-
}
227+
if use_stdio_transport {
228+
tracing::info!("Starting MCP server with stdio transport");
229+
let transport = stdio_transport();
230+
let running_service = serve_server(server, transport).await?;
231+
232+
tokio::signal::ctrl_c().await?;
233+
let _ = running_service.cancel().await;
234+
} else {
235+
tracing::info!(
236+
"Starting MCP server on {} with HTTP transport",
237+
BIND_ADDRESS
238+
);
239+
let ct = SseServer::serve(BIND_ADDRESS.parse().unwrap())
240+
.await?
241+
.with_service(move || server.clone());
242+
243+
tokio::signal::ctrl_c().await?;
244+
ct.cancel();
242245
}
243246

244247
tracing::info!("MCP server shutting down");

0 commit comments

Comments
 (0)