Files
imeai-aKode/akode-rs/akode-mcp/src/server_tests.rs
andrea.difonzo a005236bcb aKode: integrazione della memoria di imeAI (plugin memoria)
- 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>
2026-11-14 17:55:21 +01:00

218 lines
7.8 KiB
Rust

use super::EffectiveMcpServer;
use super::McpCredentialPolicy;
use super::McpServerConnectionIdentity;
use super::referenced_environment_variables;
use crate::McpProtocolMode;
use crate::runtime::McpRuntimeContext;
use crate::tool_catalog_cache::McpToolCatalogCache;
use akode_config::DEFAULT_MCP_SERVER_ENVIRONMENT_ID;
use akode_config::McpServerConfig;
use akode_config::types::AuthKeyringBackendKind;
use akode_config::types::OAuthCredentialsStoreMode;
use akode_exec_server_test_support::environment_manager_without_environments;
use akode_protocol::mcp::ClientMcpExtensions;
use akode_rmcp_client::McpOAuthRefreshMode;
use pretty_assertions::assert_eq;
use rmcp::model::ElicitationCapability;
use std::sync::Arc;
#[test]
fn remote_http_connections_track_host_headers_but_not_executor_bearer_tokens() {
let mut config: McpServerConfig = serde_json::from_value(serde_json::json!({
"url": "https://example.com/mcp",
"environment_id": "executor-1",
"bearer_token_env_var": "NODE_REPL_AUTH_TOKEN",
"env_http_headers": {"X-Api-Key": "PATH"},
}))
.expect("remote MCP configuration should deserialize");
assert_eq!(
referenced_environment_variables(&config),
vec![("PATH".to_string(), std::env::var_os("PATH"))],
);
let remote_host_bearer: McpServerConfig = serde_json::from_value(serde_json::json!({
"url": "https://example.com/mcp",
"environment_id": "executor-1",
"bearer_token_env_var": "PATH",
}))
.expect("host-resolved remote MCP configuration should deserialize");
assert_eq!(
referenced_environment_variables(&remote_host_bearer),
vec![("PATH".to_string(), std::env::var_os("PATH"))],
);
config.environment_id = DEFAULT_MCP_SERVER_ENVIRONMENT_ID.to_string();
assert_eq!(
referenced_environment_variables(&config),
vec![
(
"NODE_REPL_AUTH_TOKEN".to_string(),
std::env::var_os("NODE_REPL_AUTH_TOKEN"),
),
("PATH".to_string(), std::env::var_os("PATH")),
],
);
}
fn runtime_context() -> McpRuntimeContext {
McpRuntimeContext::new(
Arc::new(environment_manager_without_environments()),
std::env::temp_dir(),
)
}
fn connection_identity(
server: &EffectiveMcpServer,
runtime_context: &McpRuntimeContext,
) -> McpServerConnectionIdentity {
McpServerConnectionIdentity::new(
"docs",
server,
/*host_plugin_root*/ None,
OAuthCredentialsStoreMode::default(),
AuthKeyringBackendKind::default(),
McpOAuthRefreshMode::Legacy,
&Ok(None),
runtime_context,
ElicitationCapability::default(),
ClientMcpExtensions::default(),
/*previous_identity*/ None,
)
}
fn explicit_authorization_server() -> EffectiveMcpServer {
EffectiveMcpServer::from_host_config(
serde_json::from_value(serde_json::json!({
"url": "https://example.com/mcp",
"environment_id": "executor-1",
"http_headers": {"Authorization": "Bearer test-canary"},
}))
.expect("remote MCP configuration should deserialize"),
)
}
#[test]
fn connection_identity_does_not_reuse_connections_across_credential_policies() {
let runtime_context = runtime_context();
let host_server = explicit_authorization_server();
let executor_server = EffectiveMcpServer::from_config_with_policy(
host_server.config().clone(),
McpCredentialPolicy::ExecutorOnly,
);
let host_identity = connection_identity(&host_server, &runtime_context);
let executor_identity = connection_identity(&executor_server, &runtime_context);
assert!(
host_identity
.has_same_connection_config(&connection_identity(&host_server, &runtime_context,))
);
assert!(
executor_identity
.has_same_connection_config(&connection_identity(&executor_server, &runtime_context,))
);
assert!(!host_identity.has_same_connection_config(&executor_identity));
assert!(!executor_identity.has_same_connection_config(&host_identity));
}
#[test]
fn executor_connection_identity_does_not_capture_host_environment_values() {
let runtime_context = runtime_context();
let host_server = EffectiveMcpServer::from_host_config(
serde_json::from_value(serde_json::json!({
"url": "https://example.com/mcp",
"environment_id": "executor-1",
"bearer_token_env_var": "PATH",
}))
.expect("remote MCP configuration should deserialize"),
);
let executor_server = EffectiveMcpServer::from_config_with_policy(
host_server.config().clone(),
McpCredentialPolicy::ExecutorOnly,
);
assert_eq!(
connection_identity(&host_server, &runtime_context).referenced_environment_variables,
vec![("PATH".to_string(), std::env::var_os("PATH"))],
);
assert_eq!(
connection_identity(&executor_server, &runtime_context).referenced_environment_variables,
Vec::new(),
);
}
#[test]
fn tool_catalog_cache_does_not_reuse_catalogs_across_credential_policies() {
let runtime_context = runtime_context();
let host_server = explicit_authorization_server();
let executor_server = EffectiveMcpServer::from_config_with_policy(
host_server.config().clone(),
McpCredentialPolicy::ExecutorOnly,
);
let host_identity = connection_identity(&host_server, &runtime_context);
let executor_identity = connection_identity(&executor_server, &runtime_context);
let cache = McpToolCatalogCache::default();
let context = |identity| {
cache
.context(
"docs",
host_server.config(),
&runtime_context,
/*resolved_environment*/ None,
(
&ElicitationCapability::default(),
&ClientMcpExtensions::default(),
),
Some((identity, McpProtocolMode::Legacy, false)),
)
.expect("explicit authorization permits catalog caching")
};
let host_context = context(&host_identity);
host_context.publish_if_newest(host_context.begin_fetch(), &[], /*meta*/ None);
assert_eq!(context(&host_identity).current_tools(), Some(Vec::new()));
assert_eq!(context(&executor_identity).current_tools(), None);
}
#[test]
fn server_metadata_propagates_pollutes_memory() {
let server = |pollutes_memory: Option<bool>| {
let mut config = serde_json::json!({"url": "https://example.com/mcp"});
if let Some(pollutes_memory) = pollutes_memory {
config["pollutes_memory"] = serde_json::json!(pollutes_memory);
}
EffectiveMcpServer::from_host_config(
serde_json::from_value(config).expect("MCP configuration should deserialize"),
)
};
assert!(super::McpServerMetadata::from(&server(None)).pollutes_memory);
assert!(super::McpServerMetadata::from(&server(Some(true))).pollutes_memory);
assert!(!super::McpServerMetadata::from(&server(Some(false))).pollutes_memory);
}
#[test]
fn project_header_connections_are_not_shared_across_working_directories() {
let server = EffectiveMcpServer::from_host_config(
serde_json::from_value(serde_json::json!({
"url": "https://example.com/mcp",
"project_header": "X-Memoria-Project",
}))
.expect("MCP configuration should deserialize"),
);
let first = runtime_context();
let second = McpRuntimeContext::new(
Arc::new(environment_manager_without_environments()),
std::env::temp_dir().join("another-project"),
);
assert!(
connection_identity(&server, &first)
.has_same_connection_config(&connection_identity(&server, &first))
);
assert!(
!connection_identity(&server, &first)
.has_same_connection_config(&connection_identity(&server, &second))
);
}