restructure into multiple crates
add mcp server collection
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
pub mod chat;
|
||||
mod translation;
|
||||
|
||||
use rmcp::model::InitializeRequestParams;
|
||||
use rmcp::service::{ClientInitializeError, RunningService};
|
||||
use rmcp::transport::streamable_http_client::StreamableHttpClientTransportConfig;
|
||||
use rmcp::{
|
||||
RoleClient, ServiceExt,
|
||||
model::{ClientCapabilities, ClientInfo, Implementation},
|
||||
transport::StreamableHttpClientTransport,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub type MCPClient = RunningService<RoleClient, InitializeRequestParams>;
|
||||
|
||||
pub async fn get_client(
|
||||
uri: impl Into<Arc<str>>,
|
||||
authentication: Option<impl Into<String>>,
|
||||
implementation: Implementation,
|
||||
) -> Result<MCPClient, ClientInitializeError> {
|
||||
let mut config = StreamableHttpClientTransportConfig::with_uri(uri);
|
||||
|
||||
if let Some(authentication) = authentication {
|
||||
config = config.auth_header(authentication);
|
||||
}
|
||||
|
||||
let client_info = ClientInfo::new(ClientCapabilities::default(), implementation);
|
||||
|
||||
let transport = StreamableHttpClientTransport::from_config(config);
|
||||
|
||||
client_info.serve(transport).await.inspect_err(|e| {
|
||||
log::error!("client error: {:?}", e);
|
||||
})
|
||||
}
|
||||
|
||||
fn generate_random_mcp_server_name() -> String {
|
||||
use rand::distr::Alphanumeric;
|
||||
use rand::RngExt;
|
||||
|
||||
let mut rng = rand::rng();
|
||||
|
||||
(0..5)
|
||||
.map(|_| rng.sample(Alphanumeric) as char)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn guaranteed_mcp_server_name(user_specified_name: Option<String>, client: &MCPClient) -> String {
|
||||
if let Some(user_specified_name) = user_specified_name {
|
||||
user_specified_name
|
||||
} else if let Some(peer_info) = client.peer_info() {
|
||||
peer_info.server_info.name.clone()
|
||||
} else {
|
||||
let random_name = generate_random_mcp_server_name();
|
||||
log::warn!("no name was specified by the user or the client, so a random name had to be generated: `{}` for {:#?}", random_name, client);
|
||||
random_name
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user