use std::ffi::OsString;
use std::path::PathBuf;
use std::sync::Arc;
use rmcp::model::{CallToolResult, JsonObject};
use rmcp::transport::auth::CredentialStore;
use rmcp::transport::AuthorizationManager;
use crate::extension::ExtensionConfig as TransportConfig;
use crate::extension::{
name_to_key, ExtensionConfigResolver, ExtensionError, ExtensionResult,
DEFAULT_EXTENSION_TIMEOUT,
};
use crate::mcp::client::{ConnectContext, McpClientTrait};
use crate::mcp::container::Container;
pub trait ExtensionSpec: Clone + PartialEq + std::fmt::Debug + Send + Sync + 'static {
fn name(&self) -> String;
fn key(&self) -> String {
name_to_key(&self.name())
}
fn is_tool_available(&self, tool: &str) -> bool;
fn transport(&self) -> Option<&TransportConfig>;
}
impl ExtensionSpec for TransportConfig {
fn name(&self) -> String {
TransportConfig::name(self)
}
fn is_tool_available(&self, tool: &str) -> bool {
TransportConfig::is_tool_available(self, tool)
}
fn transport(&self) -> Option<&TransportConfig> {
Some(self)
}
}
pub type TraceContextInjector = fn(&mut JsonObject);
#[async_trait::async_trait]
pub trait ExtensionHost<C: ExtensionSpec>: Send + Sync {
fn get_secret(&self, key: &str) -> anyhow::Result<String> {
anyhow::bail!("secret '{key}' is not available")
}
fn config_generation(&self) -> u64 {
0
}
fn default_timeout(&self) -> u64 {
DEFAULT_EXTENSION_TIMEOUT
}
fn default_working_dir(&self) -> PathBuf {
std::env::current_dir().unwrap_or_default()
}
fn resolve_command(&self, cmd: &str) -> PathBuf {
PathBuf::from(cmd)
}
fn search_path(&self) -> Option<OsString> {
None
}
fn auth(&self) -> Option<Arc<dyn ExtensionAuth>> {
None
}
fn trace_context(&self) -> Option<TraceContextInjector> {
None
}
async fn connect(
&self,
config: &C,
_container: Option<&Container>,
_ctx: ConnectContext,
) -> ExtensionResult<Box<dyn McpClientTrait>> {
Err(ExtensionError::ConfigError(format!(
"no connector for extension {}",
config.name()
)))
}
fn sanitize_result(&self, _result: &mut CallToolResult) {}
}
#[async_trait::async_trait]
pub trait ExtensionAuth: Send + Sync {
fn credential_store(&self, name: &str) -> Box<dyn CredentialStore>;
async fn authorize(
&self,
uri: &str,
name: &str,
static_client: Option<&StaticOAuthClientConfig>,
challenge: Option<String>,
) -> anyhow::Result<AuthorizationManager>;
}
#[derive(Clone, Debug, PartialEq)]
pub struct StaticOAuthClientConfig {
pub client_id: String,
pub client_secret: Option<String>,
pub scopes: Vec<String>,
}
pub(super) struct Secrets<'a, C: ExtensionSpec>(pub(super) &'a dyn ExtensionHost<C>);
impl<C: ExtensionSpec> ExtensionConfigResolver for Secrets<'_, C> {
fn get_secret(&self, key: &str) -> anyhow::Result<String> {
self.0.get_secret(key)
}
}