add gpl license

improve mcp server error handling
This commit is contained in:
2026-05-25 14:55:48 +02:00
parent de0e665f57
commit 9ba1c3bae3
10 changed files with 728 additions and 25 deletions
+1
View File
@@ -2,6 +2,7 @@
name = "mcp_server_collection"
version = "0.2.2"
edition = "2024"
license = "GPL-3"
[dependencies]
rmcp = { version = "1.7.0", features = ["server", "transport-streamable-http-server-session", "transport-streamable-http-server"] }
+16 -12
View File
@@ -14,9 +14,10 @@ use crate::datetime::DateTimeHandler;
use crate::server_handler::{McpServerHandler, McpServerHandlerError};
use axum::Router;
use axum::response::Json;
use own_assist_common::exit_msg;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::select;
use tokio::task::JoinHandle;
use tokio_util::sync::CancellationToken;
use tracing::{Level, event};
@@ -60,7 +61,15 @@ fn bind_address_format(url: url::Url) -> String {
)
}
pub async fn serve(config: Config) -> CancellationToken {
#[derive(Debug, Error)]
pub enum McpServerError {
#[error(transparent)]
McpServerHandlerError(#[from] McpServerHandlerError),
#[error(transparent)]
IoError(#[from] std::io::Error),
}
pub async fn serve_in_background(config: Config) -> Result<(CancellationToken, JoinHandle<std::io::Result<()>>), McpServerError> {
let routes = Json(
config
.servers
@@ -71,9 +80,7 @@ pub async fn serve(config: Config) -> CancellationToken {
);
let router = mcp_router(&config.servers)
.await
.inspect_err(exit_msg!("Error handling server: {e}"))
.unwrap()
.await?
.route("/", axum::routing::get(|| async { routes }));
let bind_address = config
@@ -82,21 +89,18 @@ pub async fn serve(config: Config) -> CancellationToken {
event!(Level::INFO, "binding address at {bind_address}");
let tcp_listener = tokio::net::TcpListener::bind(bind_address_format(bind_address))
.await
.inspect_err(exit_msg!("Error bind tcp listener: {e:#?}"))
.unwrap();
.await?;
let cancellation_token = CancellationToken::new();
let cloned_cancellation_token = cancellation_token.clone();
let _ = axum::serve(tcp_listener, router)
let handle = tokio::task::spawn(axum::serve(tcp_listener, router)
.with_graceful_shutdown(async move {
select! {
_ = cloned_cancellation_token.cancelled() => (),
_ = tokio::signal::ctrl_c() => (),
}
})
.await;
}).into_future());
cancellation_token
Ok((cancellation_token, handle))
}
+8 -3
View File
@@ -1,6 +1,6 @@
use mcp_server_collection::config::Config;
use mcp_server_collection::serve;
use own_assist_common::{exit_msg, init_tracing_subscriber};
use mcp_server_collection::{serve_in_background, McpServerError};
use own_assist_common::{exit_msg, exit_with_error_message, init_tracing_subscriber};
use tokio::{main};
#[main]
@@ -11,5 +11,10 @@ async fn main() {
.inspect_err(exit_msg!("Error loading config"))
.unwrap();
serve(config).await;
let (_, join_handle) = serve_in_background(config).await.inspect_err(|err| match err {
McpServerError::McpServerHandlerError(error) => exit_with_error_message(error, "Error handling server"),
McpServerError::IoError(error) => exit_with_error_message(error, "Error binding tcp listener")
}).unwrap();
join_handle.await.unwrap().unwrap();
}