- nuovo crate ext/memoria: aggiunge le istruzioni del plugin (instructions.md) come developer policy solo se il plugin memoria è abilitato e il suo server MCP è pronto; nessuna modifica alle istruzioni di base, [features] memories resta spento - pollutes_memory configurabile per server MCP (predefinito true) - project_header per i server HTTP: progetto ricavato dalla cartella della sessione (origin come host/owner/repo, altrimenti la cartella), connessioni separate per progetto - token Bearer dei server MCP anche dal portachiavi di aKode, solo sotto mcp:<server> (mai le chiavi dei provider dei modelli) - tool namespace (MCP, multi-agente) appiattiti in <namespace>__<tool> per i server compatibili OpenAI che accettano solo function tool (llama.cpp, ik_llama.cpp, vLLM li scartavano); capacità del provider tool_namespaces - prove: parsing e propagazione, intestazione del progetto, iniezione solo se attivo e connesso, appiattimento e mappatura delle chiamate, chiavi dei provider mai consegnate ai server MCP Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
730 lines
28 KiB
Rust
730 lines
28 KiB
Rust
pub use auth::McpAuthStatusEntry;
|
|
pub use auth::McpOAuthLoginConfig;
|
|
pub use auth::McpOAuthLoginSupport;
|
|
pub use auth::McpOAuthScopesSource;
|
|
pub use auth::ResolvedMcpOAuthScopes;
|
|
pub use auth::compute_auth_statuses;
|
|
pub use auth::discover_supported_scopes;
|
|
pub use auth::oauth_login_support;
|
|
pub use auth::resolve_oauth_callback;
|
|
pub use auth::resolve_oauth_scopes;
|
|
pub use auth::should_retry_without_scopes;
|
|
|
|
pub(crate) mod auth;
|
|
|
|
use std::collections::HashMap;
|
|
use std::collections::HashSet;
|
|
use std::path::PathBuf;
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use akode_config::ConfigLayerStack;
|
|
use akode_config::Constrained;
|
|
use akode_config::McpServerAuth;
|
|
use akode_config::McpServerConfig;
|
|
use akode_config::types::AppToolApproval;
|
|
use akode_config::types::ApprovalsReviewer;
|
|
use akode_config::types::AuthKeyringBackendKind;
|
|
use akode_config::types::OAuthCredentialsStoreMode;
|
|
use akode_exec_server::FileSystemSandboxContext;
|
|
use akode_login::CodexAuth;
|
|
use akode_protocol::mcp::ClientMcpExtensions;
|
|
use akode_protocol::mcp::McpServerInfo;
|
|
use akode_protocol::mcp::Resource;
|
|
use akode_protocol::mcp::ResourceTemplate;
|
|
use akode_protocol::mcp::Tool;
|
|
use akode_protocol::models::PermissionProfile;
|
|
use akode_protocol::protocol::AskForApproval;
|
|
use akode_protocol::protocol::McpAuthStatus;
|
|
use akode_rmcp_client::McpOAuthRefreshMode;
|
|
use akode_utils_absolute_path::AbsolutePathBuf;
|
|
use akode_utils_path_uri::PathUri;
|
|
use rmcp::model::ElicitationCapability;
|
|
use rmcp::model::ReadResourceRequestParams;
|
|
use rmcp::model::ReadResourceResult;
|
|
use serde_json::Value;
|
|
use tokio_util::sync::CancellationToken;
|
|
|
|
use crate::McpProtocolMode;
|
|
use crate::McpServerSource;
|
|
use crate::ResolvedMcpCatalog;
|
|
use crate::connection_manager::McpConnectionSet;
|
|
use crate::runtime::McpPublicationGate;
|
|
use crate::runtime::McpRuntimeContext;
|
|
use crate::runtime::McpRuntimeInput;
|
|
use crate::runtime::McpStartupPolicy;
|
|
use crate::server::EffectiveMcpServer;
|
|
|
|
/// Product SKU reported in MCP catalog telemetry.
|
|
pub(crate) const MCP_CATALOG_PRODUCT_SKU: &str = "codex";
|
|
const MCP_TOOL_NAME_PREFIX: &str = "mcp";
|
|
const MCP_TOOL_NAME_DELIMITER: &str = "__";
|
|
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
|
pub enum McpSnapshotDetail {
|
|
#[default]
|
|
Full,
|
|
ToolsAndAuthOnly,
|
|
}
|
|
|
|
impl McpSnapshotDetail {
|
|
fn include_resources(self) -> bool {
|
|
matches!(self, Self::Full)
|
|
}
|
|
}
|
|
|
|
pub fn qualified_mcp_tool_name_prefix(server_name: &str) -> String {
|
|
sanitize_responses_api_tool_name(&format!(
|
|
"{MCP_TOOL_NAME_PREFIX}{MCP_TOOL_NAME_DELIMITER}{server_name}{MCP_TOOL_NAME_DELIMITER}"
|
|
))
|
|
}
|
|
|
|
/// Returns true when MCP permission prompts should resolve as approved instead
|
|
/// of being shown to the user.
|
|
pub fn mcp_permission_prompt_is_auto_approved(
|
|
approval_policy: AskForApproval,
|
|
permission_profile: &PermissionProfile,
|
|
context: McpPermissionPromptAutoApproveContext,
|
|
) -> bool {
|
|
if context.tool_approval_mode == Some(AppToolApproval::Approve) {
|
|
return true;
|
|
}
|
|
|
|
if approval_policy != AskForApproval::Never {
|
|
return false;
|
|
}
|
|
|
|
match permission_profile {
|
|
PermissionProfile::Disabled | PermissionProfile::External { .. } => true,
|
|
PermissionProfile::Managed { file_system, .. } => {
|
|
file_system.to_sandbox_policy().has_full_disk_write_access()
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
|
pub struct McpPermissionPromptAutoApproveContext {
|
|
pub tool_approval_mode: Option<AppToolApproval>,
|
|
}
|
|
|
|
/// Which rules a registered MCP server uses.
|
|
#[derive(Debug, Clone)]
|
|
pub enum McpServerPermissions {
|
|
Thread,
|
|
Environment(Arc<FileSystemSandboxContext>),
|
|
}
|
|
|
|
/// MCP runtime settings derived from `akode_core::config::Config`.
|
|
///
|
|
/// Each published runtime and prepared call owns one immutable copy of these
|
|
/// settings, so its connection, approval policy, and sandbox authority cannot
|
|
/// change independently. Auth remains separate and is supplied explicitly to
|
|
/// runtime entry points such as [`effective_mcp_servers`].
|
|
#[derive(Debug, Clone)]
|
|
pub struct McpConfig {
|
|
/// Requests server-side read-only filtering and invocation checks for MCP tools.
|
|
pub requires_read_only_mcp_tools: bool,
|
|
/// Codex home directory used for MCP OAuth state and app-tool cache files.
|
|
pub codex_home: PathBuf,
|
|
/// Preferred credential store for MCP OAuth tokens.
|
|
pub mcp_oauth_credentials_store_mode: OAuthCredentialsStoreMode,
|
|
/// OAuth refresh ownership selected for new MCP connections.
|
|
pub oauth_refresh_mode: McpOAuthRefreshMode,
|
|
/// Backend used when MCP OAuth storage is configured for keyring-backed persistence.
|
|
pub auth_keyring_backend_kind: AuthKeyringBackendKind,
|
|
/// Optional fixed localhost callback port for MCP OAuth login.
|
|
pub mcp_oauth_callback_port: Option<u16>,
|
|
/// Optional OAuth redirect URI override for MCP login.
|
|
pub mcp_oauth_callback_url: Option<String>,
|
|
/// How long a tool catalog capture waits for optional MCP servers to initialize.
|
|
///
|
|
/// A zero duration disables the shared grace and waits for each server's
|
|
/// configured startup timeout instead.
|
|
pub optional_mcp_startup_grace: Duration,
|
|
/// Whether skill MCP dependency installation prompts are enabled.
|
|
pub skill_mcp_dependency_install_enabled: bool,
|
|
/// Approval policy used for MCP tool calls and MCP elicitation requests.
|
|
pub approval_policy: Constrained<AskForApproval>,
|
|
/// Permission profile captured with the connections and approval policy.
|
|
pub permission_profile: PermissionProfile,
|
|
/// Host working directory used to interpret thread permissions.
|
|
pub host_cwd: AbsolutePathBuf,
|
|
/// Configuration layers used to evaluate Apps tool policy and reviewer selection.
|
|
pub config_layer_stack: ConfigLayerStack,
|
|
pub plugins: akode_config::types::PluginsConfigToml,
|
|
/// Default reviewer used when an Apps tool has no reviewer override.
|
|
pub approvals_reviewer: ApprovalsReviewer,
|
|
/// Working directories for the exact environment handles used by this runtime.
|
|
pub environment_cwds: HashMap<String, PathUri>,
|
|
/// Effective backends captured with the environment handles, including local fallback.
|
|
pub environment_use_mxc: HashMap<String, bool>,
|
|
/// Permissions for registered servers. Unavailable servers have no entry.
|
|
pub server_permissions: HashMap<String, McpServerPermissions>,
|
|
/// Host-supplied re-exec binary. Probe its sandbox capability before advertising it.
|
|
/// Never infer this from PATH or an MCP server's configuration.
|
|
pub codex_self_exe: Option<PathBuf>,
|
|
/// Optional path to `akode-linux-sandbox` for sandboxed MCP tool execution.
|
|
pub codex_linux_sandbox_exe: Option<PathBuf>,
|
|
/// Whether to use legacy Landlock behavior in the MCP sandbox state.
|
|
// TODO(anp): Reconcile this runtime-wide copy with TurnEnvironment::sandbox_context
|
|
// for the environment that owns each MCP server.
|
|
pub use_legacy_landlock: bool,
|
|
/// Whether model-visible MCP tool namespaces should keep the legacy
|
|
/// `mcp__` prefix.
|
|
pub prefix_mcp_tool_names: bool,
|
|
/// MCP servers whose model-visible tool namespaces omit the `mcp__` prefix.
|
|
pub non_prefixed_mcp_tool_servers: Vec<String>,
|
|
/// Protocol mode for MCP servers.
|
|
pub protocol_mode: McpProtocolMode,
|
|
/// Try protocol discovery before initializing eligible local stdio servers.
|
|
pub discover_local_stdio: bool,
|
|
/// Refresh ordinary server catalogs after tools/list_changed notifications.
|
|
pub mcp_tool_list_changed: bool,
|
|
/// Client-side elicitation capabilities advertised during MCP initialization.
|
|
pub client_elicitation_capability: ElicitationCapability,
|
|
/// Resolved MCP registrations keyed by logical server name.
|
|
pub mcp_server_catalog: ResolvedMcpCatalog,
|
|
}
|
|
|
|
/// Default amount of time a tool catalog capture waits for optional MCP servers.
|
|
pub const DEFAULT_OPTIONAL_MCP_STARTUP_GRACE: Duration = Duration::from_secs(1);
|
|
|
|
impl McpConfig {
|
|
/// Resolves enabled runtime servers against the exact attachment permissions being published.
|
|
pub fn set_server_permissions(
|
|
&mut self,
|
|
servers: &HashMap<String, EffectiveMcpServer>,
|
|
environment_sandboxes: impl IntoIterator<Item = (String, Arc<FileSystemSandboxContext>)>,
|
|
) {
|
|
let environment_sandboxes = environment_sandboxes.into_iter().collect::<HashMap<_, _>>();
|
|
self.server_permissions = servers
|
|
.iter()
|
|
.filter(|(_, server)| server.enabled())
|
|
.filter_map(|(server_name, _)| {
|
|
let server = self.mcp_server_catalog.server(server_name)?;
|
|
let permissions = if let Some(sandbox) =
|
|
environment_sandboxes.get(&server.config().environment_id)
|
|
{
|
|
McpServerPermissions::Environment(Arc::clone(sandbox))
|
|
} else if server.config().is_local_environment()
|
|
|| matches!(server.source(), McpServerSource::SelectedPlugin(_))
|
|
{
|
|
McpServerPermissions::Thread
|
|
} else {
|
|
return None;
|
|
};
|
|
Some((server_name.clone(), permissions))
|
|
})
|
|
.collect();
|
|
}
|
|
|
|
/// Returns this server's published permission authority.
|
|
pub fn permission_profile_for_server(&self, server_name: &str) -> Option<&PermissionProfile> {
|
|
Some(match self.server_permissions.get(server_name)? {
|
|
McpServerPermissions::Thread => &self.permission_profile,
|
|
McpServerPermissions::Environment(sandbox) => &sandbox.permissions,
|
|
})
|
|
}
|
|
|
|
/// Standalone discovery and resource reads must not inherit thread execution authority.
|
|
pub fn for_threadless_operations(&self, servers: &HashMap<String, EffectiveMcpServer>) -> Self {
|
|
let mut config = self.clone();
|
|
config.permission_profile = PermissionProfile::default();
|
|
config.server_permissions = servers
|
|
.iter()
|
|
.filter(|(_, server)| server.enabled())
|
|
.map(|(name, _)| (name.clone(), McpServerPermissions::Thread))
|
|
.collect();
|
|
config
|
|
}
|
|
}
|
|
|
|
/// Plugin attribution and selection data derived from the current MCP configuration.
|
|
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
|
pub struct ToolPluginContext {
|
|
plugin_display_names_by_mcp_server_name: HashMap<String, Vec<String>>,
|
|
plugin_ids_by_mcp_server_name: HashMap<String, String>,
|
|
selected_plugin_mcp_server_names: HashSet<String>,
|
|
}
|
|
|
|
impl ToolPluginContext {
|
|
pub fn plugin_display_names_for_mcp_server_name(&self, server_name: &str) -> &[String] {
|
|
self.plugin_display_names_by_mcp_server_name
|
|
.get(server_name)
|
|
.map(Vec::as_slice)
|
|
.unwrap_or(&[])
|
|
}
|
|
|
|
pub fn plugin_id_for_mcp_server_name(&self, server_name: &str) -> Option<&str> {
|
|
self.plugin_ids_by_mcp_server_name
|
|
.get(server_name)
|
|
.map(String::as_str)
|
|
}
|
|
|
|
pub(crate) fn is_selected_plugin_mcp_server(&self, server_name: &str) -> bool {
|
|
self.selected_plugin_mcp_server_names.contains(server_name)
|
|
}
|
|
|
|
fn from_config(config: &McpConfig) -> Self {
|
|
let mut tool_plugin_context = Self::default();
|
|
|
|
for (server_name, attribution) in config
|
|
.mcp_server_catalog
|
|
.plugin_attributions_by_server_name()
|
|
{
|
|
tool_plugin_context
|
|
.plugin_display_names_by_mcp_server_name
|
|
.insert(
|
|
server_name.clone(),
|
|
vec![attribution.display_name().to_string()],
|
|
);
|
|
tool_plugin_context
|
|
.plugin_ids_by_mcp_server_name
|
|
.insert(server_name, attribution.plugin_id().to_string());
|
|
}
|
|
tool_plugin_context.selected_plugin_mcp_server_names.extend(
|
|
config
|
|
.mcp_server_catalog
|
|
.selected_plugin_server_names()
|
|
.map(str::to_string),
|
|
);
|
|
|
|
for plugin_names in tool_plugin_context
|
|
.plugin_display_names_by_mcp_server_name
|
|
.values_mut()
|
|
{
|
|
plugin_names.sort_unstable();
|
|
plugin_names.dedup();
|
|
}
|
|
tool_plugin_context
|
|
}
|
|
}
|
|
|
|
pub fn configured_mcp_servers(config: &McpConfig) -> HashMap<String, McpServerConfig> {
|
|
config.mcp_server_catalog.configured_servers()
|
|
}
|
|
|
|
pub fn effective_mcp_servers(
|
|
config: &McpConfig,
|
|
auth: Option<&CodexAuth>,
|
|
) -> HashMap<String, EffectiveMcpServer> {
|
|
effective_mcp_servers_from_configured(configured_mcp_servers(config), config, auth)
|
|
}
|
|
|
|
/// Converts a materialized server map to its auth-gated runtime view.
|
|
///
|
|
/// Compatibility built-ins and extension overlays must already be reflected in
|
|
/// `configured_servers`; this function does not synthesize missing servers.
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// Panics if a materialized server is missing from the catalog.
|
|
pub fn effective_mcp_servers_from_configured(
|
|
configured_servers: HashMap<String, McpServerConfig>,
|
|
config: &McpConfig,
|
|
_auth: Option<&CodexAuth>,
|
|
) -> HashMap<String, EffectiveMcpServer> {
|
|
configured_servers
|
|
.into_iter()
|
|
.map(|(name, mut server)| {
|
|
#[expect(
|
|
clippy::expect_used,
|
|
reason = "materialized servers must have catalog registrations"
|
|
)]
|
|
let registration = config
|
|
.mcp_server_catalog
|
|
.server(&name)
|
|
.expect("materialized MCP server must have a catalog registration");
|
|
// ChatGPT credentials are never available, so local servers that ask for them use
|
|
// their own OAuth flow. Executor-owned servers keep `ChatGpt` so they never consult
|
|
// orchestrator OAuth stores and are rejected unless they carry explicit authorization.
|
|
if matches!(server.auth, McpServerAuth::ChatGpt) && server.is_local_environment() {
|
|
server.auth = McpServerAuth::OAuth;
|
|
}
|
|
(
|
|
name,
|
|
EffectiveMcpServer::from_config_with_policy(
|
|
server,
|
|
registration.credential_policy(),
|
|
)
|
|
.with_agent_plugin(registration.source().is_agent_plugin()),
|
|
)
|
|
})
|
|
.collect::<HashMap<_, _>>()
|
|
}
|
|
|
|
pub fn tool_plugin_context(config: &McpConfig) -> ToolPluginContext {
|
|
ToolPluginContext::from_config(config)
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub async fn read_mcp_resource(
|
|
config: &McpConfig,
|
|
auth: Option<&CodexAuth>,
|
|
runtime_context: McpRuntimeContext,
|
|
tool_catalog_cache: crate::McpToolCatalogCache,
|
|
server: &str,
|
|
params: ReadResourceRequestParams,
|
|
) -> anyhow::Result<ReadResourceResult> {
|
|
let mut mcp_servers = effective_mcp_servers(config, auth);
|
|
mcp_servers.retain(|name, _| name == server);
|
|
let cancel_token = CancellationToken::new();
|
|
let runtime_config = config.for_threadless_operations(&mcp_servers);
|
|
let manager = McpConnectionSet::new(
|
|
/*predecessor*/ None,
|
|
/*previous*/ None,
|
|
McpPublicationGate::already_published(),
|
|
McpRuntimeInput {
|
|
network_policy_metadata: None,
|
|
startup_policy: McpStartupPolicy::Eager,
|
|
config: Arc::new(runtime_config),
|
|
plugins_available: false,
|
|
ready_selected_capability_roots: Vec::new(),
|
|
mcp_servers,
|
|
submit_id: String::new(),
|
|
tx_event: None,
|
|
startup_cancellation_token: cancel_token.clone(),
|
|
runtime_context,
|
|
tool_catalog_cache,
|
|
client_mcp_extensions: ClientMcpExtensions::default(),
|
|
auth: auth.cloned(),
|
|
auth_manager: None,
|
|
elicitation_reviewer: None,
|
|
elicitation_lifecycle: None,
|
|
},
|
|
crate::elicitation::ElicitationRequestRouter::default(),
|
|
)
|
|
.await;
|
|
|
|
let result = manager.read_resource(server, params).await;
|
|
cancel_token.cancel();
|
|
result
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct McpServerStatusSnapshot {
|
|
pub server_infos: HashMap<String, McpServerInfo>,
|
|
pub server_capabilities: HashMap<String, serde_json::Value>,
|
|
pub tool_list_meta: HashMap<String, Arc<serde_json::Value>>,
|
|
pub tools_by_server: HashMap<String, HashMap<String, Tool>>,
|
|
pub tools_errors: HashMap<String, String>,
|
|
pub resources: HashMap<String, Vec<Resource>>,
|
|
pub resource_templates: HashMap<String, Vec<ResourceTemplate>>,
|
|
pub auth_statuses: HashMap<String, McpAuthStatus>,
|
|
pub server_names: Vec<String>,
|
|
}
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
pub async fn collect_mcp_server_status_snapshot_with_detail(
|
|
config: &McpConfig,
|
|
auth: Option<&CodexAuth>,
|
|
runtime_context: McpRuntimeContext,
|
|
tool_catalog_cache: crate::McpToolCatalogCache,
|
|
detail: McpSnapshotDetail,
|
|
server_name: Option<&str>,
|
|
) -> McpServerStatusSnapshot {
|
|
let mut mcp_servers = effective_mcp_servers(config, auth);
|
|
mcp_servers.retain(|name, _| server_name.is_none_or(|selected| name == selected));
|
|
if mcp_servers.is_empty() {
|
|
return McpServerStatusSnapshot {
|
|
server_infos: HashMap::new(),
|
|
server_capabilities: HashMap::new(),
|
|
tool_list_meta: HashMap::new(),
|
|
tools_by_server: HashMap::new(),
|
|
tools_errors: HashMap::new(),
|
|
resources: HashMap::new(),
|
|
resource_templates: HashMap::new(),
|
|
auth_statuses: HashMap::new(),
|
|
server_names: Vec::new(),
|
|
};
|
|
}
|
|
|
|
let auth_status_entries = compute_auth_statuses(
|
|
mcp_servers.iter(),
|
|
config.mcp_oauth_credentials_store_mode,
|
|
config.auth_keyring_backend_kind,
|
|
auth,
|
|
&runtime_context,
|
|
)
|
|
.await;
|
|
|
|
let server_names = mcp_servers.keys().cloned().collect();
|
|
|
|
let cancel_token = CancellationToken::new();
|
|
let runtime_config = config.for_threadless_operations(&mcp_servers);
|
|
let mcp_connection_manager = McpConnectionSet::new(
|
|
/*predecessor*/ None,
|
|
/*previous*/ None,
|
|
McpPublicationGate::already_published(),
|
|
McpRuntimeInput {
|
|
network_policy_metadata: None,
|
|
startup_policy: McpStartupPolicy::Eager,
|
|
config: Arc::new(runtime_config),
|
|
plugins_available: false,
|
|
ready_selected_capability_roots: Vec::new(),
|
|
mcp_servers,
|
|
submit_id: String::new(),
|
|
tx_event: None,
|
|
startup_cancellation_token: cancel_token.clone(),
|
|
runtime_context,
|
|
tool_catalog_cache,
|
|
client_mcp_extensions: ClientMcpExtensions::default(),
|
|
auth: auth.cloned(),
|
|
auth_manager: None,
|
|
elicitation_reviewer: None,
|
|
elicitation_lifecycle: None,
|
|
},
|
|
crate::elicitation::ElicitationRequestRouter::default(),
|
|
)
|
|
.await;
|
|
|
|
let snapshot = collect_mcp_server_status_snapshot_from_manager(
|
|
&mcp_connection_manager,
|
|
auth_status_entries,
|
|
server_names,
|
|
detail,
|
|
)
|
|
.await;
|
|
|
|
cancel_token.cancel();
|
|
|
|
snapshot
|
|
}
|
|
|
|
/// The Responses API requires tool names to match `^[a-zA-Z0-9_-]+$`.
|
|
/// MCP server/tool names are user-controlled, so sanitize the fully-qualified
|
|
/// name we expose to the model by replacing any disallowed character with `_`.
|
|
pub(crate) fn sanitize_responses_api_tool_name(name: &str) -> String {
|
|
let mut sanitized = String::with_capacity(name.len());
|
|
for c in name.chars() {
|
|
if c.is_ascii_alphanumeric() || c == '_' {
|
|
sanitized.push(c);
|
|
} else {
|
|
sanitized.push('_');
|
|
}
|
|
}
|
|
|
|
if sanitized.is_empty() {
|
|
"_".to_string()
|
|
} else {
|
|
sanitized
|
|
}
|
|
}
|
|
|
|
fn protocol_tool_from_rmcp_tool(name: &str, tool: &rmcp::model::Tool) -> Option<Tool> {
|
|
match serde_json::to_value(tool) {
|
|
Ok(value) => match Tool::from_mcp_value(value) {
|
|
Ok(tool) => Some(tool),
|
|
Err(err) => {
|
|
tracing::warn!("Failed to convert MCP tool '{name}': {err}");
|
|
None
|
|
}
|
|
},
|
|
Err(err) => {
|
|
tracing::warn!("Failed to serialize MCP tool '{name}': {err}");
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
fn auth_statuses_from_entries(
|
|
auth_status_entries: &HashMap<String, crate::mcp::auth::McpAuthStatusEntry>,
|
|
) -> HashMap<String, McpAuthStatus> {
|
|
auth_status_entries
|
|
.iter()
|
|
.map(|(name, entry)| (name.clone(), McpAuthStatus::from(entry.auth_state)))
|
|
.collect::<HashMap<_, _>>()
|
|
}
|
|
|
|
fn convert_mcp_resources(
|
|
resources: HashMap<String, Vec<rmcp::model::Resource>>,
|
|
) -> HashMap<String, Vec<Resource>> {
|
|
resources
|
|
.into_iter()
|
|
.map(|(name, resources)| {
|
|
let resources = resources
|
|
.into_iter()
|
|
.filter_map(|resource| match serde_json::to_value(resource) {
|
|
Ok(value) => match Resource::from_mcp_value(value.clone()) {
|
|
Ok(resource) => Some(resource),
|
|
Err(err) => {
|
|
let (uri, resource_name) = match value {
|
|
Value::Object(obj) => (
|
|
obj.get("uri")
|
|
.and_then(|v| v.as_str().map(ToString::to_string)),
|
|
obj.get("name")
|
|
.and_then(|v| v.as_str().map(ToString::to_string)),
|
|
),
|
|
_ => (None, None),
|
|
};
|
|
|
|
tracing::warn!(
|
|
"Failed to convert MCP resource (uri={uri:?}, name={resource_name:?}): {err}"
|
|
);
|
|
None
|
|
}
|
|
},
|
|
Err(err) => {
|
|
tracing::warn!("Failed to serialize MCP resource: {err}");
|
|
None
|
|
}
|
|
})
|
|
.collect::<Vec<_>>();
|
|
(name, resources)
|
|
})
|
|
.collect::<HashMap<_, _>>()
|
|
}
|
|
|
|
fn convert_mcp_resource_templates(
|
|
resource_templates: HashMap<String, Vec<rmcp::model::ResourceTemplate>>,
|
|
) -> HashMap<String, Vec<ResourceTemplate>> {
|
|
resource_templates
|
|
.into_iter()
|
|
.map(|(name, templates)| {
|
|
let templates = templates
|
|
.into_iter()
|
|
.filter_map(|template| match serde_json::to_value(template) {
|
|
Ok(value) => match ResourceTemplate::from_mcp_value(value.clone()) {
|
|
Ok(template) => Some(template),
|
|
Err(err) => {
|
|
let (uri_template, template_name) = match value {
|
|
Value::Object(obj) => (
|
|
obj.get("uriTemplate")
|
|
.or_else(|| obj.get("uri_template"))
|
|
.and_then(|v| v.as_str().map(ToString::to_string)),
|
|
obj.get("name")
|
|
.and_then(|v| v.as_str().map(ToString::to_string)),
|
|
),
|
|
_ => (None, None),
|
|
};
|
|
|
|
tracing::warn!(
|
|
"Failed to convert MCP resource template (uri_template={uri_template:?}, name={template_name:?}): {err}"
|
|
);
|
|
None
|
|
}
|
|
},
|
|
Err(err) => {
|
|
tracing::warn!("Failed to serialize MCP resource template: {err}");
|
|
None
|
|
}
|
|
})
|
|
.collect::<Vec<_>>();
|
|
(name, templates)
|
|
})
|
|
.collect::<HashMap<_, _>>()
|
|
}
|
|
|
|
pub(crate) async fn collect_mcp_server_status_snapshot_from_manager(
|
|
mcp_connection_manager: &McpConnectionSet,
|
|
auth_status_entries: HashMap<String, crate::mcp::auth::McpAuthStatusEntry>,
|
|
server_names: Vec<String>,
|
|
detail: McpSnapshotDetail,
|
|
) -> McpServerStatusSnapshot {
|
|
let selected_servers: HashSet<&str> = server_names.iter().map(String::as_str).collect();
|
|
let include_server = |name: &str| selected_servers.contains(name);
|
|
let ((server_infos, (tools, tools_errors, tool_list_meta)), resources, resource_templates) = tokio::join!(
|
|
async {
|
|
let server_infos = mcp_connection_manager
|
|
.list_available_server_infos(include_server)
|
|
.await;
|
|
let tools = mcp_connection_manager
|
|
.list_catalogs_with_errors(include_server)
|
|
.await;
|
|
(server_infos, tools)
|
|
},
|
|
async {
|
|
if detail.include_resources() {
|
|
mcp_connection_manager
|
|
.list_all_resources(include_server)
|
|
.await
|
|
} else {
|
|
HashMap::new()
|
|
}
|
|
},
|
|
async {
|
|
if detail.include_resources() {
|
|
mcp_connection_manager
|
|
.list_all_resource_templates(include_server)
|
|
.await
|
|
} else {
|
|
HashMap::new()
|
|
}
|
|
},
|
|
);
|
|
|
|
let server_capabilities =
|
|
mcp_connection_manager.list_available_server_capabilities(include_server);
|
|
let mut tools_by_server = HashMap::<String, HashMap<String, Tool>>::new();
|
|
for tool_info in tools {
|
|
let raw_tool_name = tool_info.tool.name.to_string();
|
|
let Some(tool) = protocol_tool_from_rmcp_tool(&raw_tool_name, &tool_info.tool) else {
|
|
continue;
|
|
};
|
|
let tool_name = tool.name.clone();
|
|
tools_by_server
|
|
.entry(tool_info.server_name)
|
|
.or_default()
|
|
.insert(tool_name, tool);
|
|
}
|
|
|
|
// Status-only discovery has no event channel. Report OAuth failures from the completed
|
|
// connection attempt instead of retaining the credential-presence status read beforehand.
|
|
let mut auth_statuses = auth_statuses_from_entries(&auth_status_entries);
|
|
for server_name in mcp_connection_manager.authentication_failed_servers().await {
|
|
if auth_statuses.get(&server_name) == Some(&McpAuthStatus::OAuth) {
|
|
auth_statuses.insert(server_name, McpAuthStatus::NotLoggedIn);
|
|
}
|
|
}
|
|
|
|
McpServerStatusSnapshot {
|
|
server_capabilities,
|
|
tool_list_meta,
|
|
server_infos,
|
|
tools_by_server,
|
|
tools_errors,
|
|
resources: convert_mcp_resources(resources),
|
|
resource_templates: convert_mcp_resource_templates(resource_templates),
|
|
auth_statuses,
|
|
server_names,
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "mod_tests.rs"]
|
|
pub(crate) mod tests;
|
|
|
|
/// Test fixture: a generic user-configured streamable HTTP MCP server at `url`.
|
|
#[cfg(test)]
|
|
pub(crate) fn test_http_mcp_server_config(url: &str) -> McpServerConfig {
|
|
McpServerConfig {
|
|
pollutes_memory: true,
|
|
project_header: None,
|
|
transport: akode_config::McpServerTransportConfig::StreamableHttp {
|
|
url: url.to_string(),
|
|
bearer_token_env_var: None,
|
|
http_headers: None,
|
|
env_http_headers: None,
|
|
http_headers_helper: None,
|
|
},
|
|
auth: McpServerAuth::default(),
|
|
environment_id: akode_config::DEFAULT_MCP_SERVER_ENVIRONMENT_ID.to_string(),
|
|
enabled: true,
|
|
required: false,
|
|
startup_readiness: Default::default(),
|
|
supports_parallel_tool_calls: false,
|
|
tool_input_schema_max_bytes: None,
|
|
omit_tools_from: None,
|
|
disabled_reason: None,
|
|
startup_timeout_sec: Some(Duration::from_secs(30)),
|
|
tool_timeout_sec: None,
|
|
default_tools_approval_mode: None,
|
|
enabled_tools: None,
|
|
disabled_tools: None,
|
|
scopes: None,
|
|
oauth: None,
|
|
oauth_resource: None,
|
|
tools: HashMap::new(),
|
|
}
|
|
}
|