Files
own_assist/own_mcp/src/mcp/mod.rs
T
2026-07-30 21:10:39 +02:00

68 lines
2.0 KiB
Rust

pub mod chat;
pub mod llmclient;
mod translation;
pub use llmclient::mistral::Mistral;
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::RngExt;
use rand::distr::Alphanumeric;
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()
&& let Some(server_info) = &peer_info.server_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
}
}