Rinomina: crate akode-*, cartelle akode-rs/ e akode-cli/, binario akode, ~/.akode, AKODE_* (A)
Riapplica la fase 5 di aKode (86e7c72e) sulla base ripulita:
- crate codex-* -> akode-*, codex-rs -> akode-rs, codex-cli -> akode-cli, binario e ausiliari akode;
- ~/.codex -> ~/.akode, CODEX_HOME -> AKODE_HOME, variabili CODEX_* -> AKODE_*, .codex/ -> .akode/ di progetto;
nessun fallback a ~/.codex o a CODEX_*;
- testi per l'utente "Codex" -> "aKode"; testi verso il modello e istruzioni di sistema invariati;
- snapshot insta rinominati (non ancora rigenerati).
I conflitti con la base ripulita sono stati risolti riapplicando la rinomina ai file senza le
funzioni di imeAI (script di mappa dei token per file).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
161
akode-rs/akode-mcp/src/rmcp_client/managed_client.rs
Normal file
161
akode-rs/akode-mcp/src/rmcp_client/managed_client.rs
Normal file
@@ -0,0 +1,161 @@
|
||||
//! A chat's access to an initialized MCP client and its tool catalog.
|
||||
//! A pooled connection honors this chat's lease and routes input requests back to it.
|
||||
|
||||
use std::future::Future;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::client_tool_catalog::ClientToolCatalog;
|
||||
use crate::connection_pool::ConnectionSharing;
|
||||
use crate::tools::ToolInfo;
|
||||
use anyhow::Result;
|
||||
use anyhow::anyhow;
|
||||
use akode_protocol::mcp::McpServerInfo;
|
||||
use akode_rmcp_client::RmcpClient;
|
||||
use akode_rmcp_client::SendElicitation;
|
||||
use rmcp::model::ListResourceTemplatesResult;
|
||||
use rmcp::model::ListResourcesResult;
|
||||
use rmcp::model::PaginatedRequestParams;
|
||||
use rmcp::model::ServerCapabilities;
|
||||
use serde_json::Value;
|
||||
use tokio_util::task::AbortOnDropHandle;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct ManagedClient {
|
||||
pub(crate) connection_sharing: ConnectionSharing,
|
||||
/// The calling chat's input handler, separate from the shared transport's startup handler.
|
||||
pub(crate) shared_elicitation: Option<Arc<SendElicitation>>,
|
||||
pub(crate) _auth_change_notifications: Option<Arc<AbortOnDropHandle<()>>>,
|
||||
pub(crate) client: Arc<RmcpClient>,
|
||||
pub(crate) server_info: McpServerInfo,
|
||||
pub(crate) tool_catalog: Arc<ClientToolCatalog>,
|
||||
pub(crate) tool_timeout: Option<Duration>,
|
||||
pub(crate) server_instructions: Option<String>,
|
||||
pub(crate) server_supports_sandbox_state_meta_capability: bool,
|
||||
pub(crate) client_supports_network_policy_meta_capability: bool,
|
||||
}
|
||||
|
||||
impl ManagedClient {
|
||||
pub(crate) async fn with_active_shared_lease<T>(
|
||||
&self,
|
||||
operation: impl Future<Output = T>,
|
||||
) -> Result<T> {
|
||||
match &self.connection_sharing {
|
||||
ConnectionSharing::Shared(lease) => tokio::select! {
|
||||
biased;
|
||||
() = lease.cancellation().cancelled() => {
|
||||
Err(anyhow!("MCP connection lease has been released"))
|
||||
}
|
||||
result = operation => Ok(result),
|
||||
},
|
||||
ConnectionSharing::Private
|
||||
| ConnectionSharing::Negotiated
|
||||
| ConnectionSharing::Owned(_) => Ok(operation.await),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn with_lease_request<T>(
|
||||
&self,
|
||||
operation: impl Future<Output = Result<T>>,
|
||||
) -> Result<T> {
|
||||
// Keep one copy of the request future as it passes through both lease scopes.
|
||||
let operation = std::pin::pin!(operation);
|
||||
self.with_active_shared_lease(async {
|
||||
match self.connection_sharing.server_lease() {
|
||||
Some(lease) => lease.requests.scope(operation).await,
|
||||
None => operation.await,
|
||||
}
|
||||
})
|
||||
.await?
|
||||
}
|
||||
|
||||
pub(crate) async fn call_tool(
|
||||
&self,
|
||||
name: String,
|
||||
arguments: Option<Value>,
|
||||
meta_for_session: impl Fn(&ServerCapabilities) -> Result<Option<Value>> + Send + Sync,
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<rmcp::model::CallToolResult> {
|
||||
let meta_for_session = |caps: &ServerCapabilities| {
|
||||
let mut meta = meta_for_session(caps)?;
|
||||
if let Some(lease) = self.connection_sharing.server_lease() {
|
||||
meta.get_or_insert_with(|| serde_json::json!({}))
|
||||
.as_object_mut()
|
||||
.ok_or_else(|| anyhow!("MCP tool metadata must be an object"))?
|
||||
.insert(
|
||||
crate::connection_pool::LEASE_META_KEY.to_owned(),
|
||||
serde_json::json!(lease.id),
|
||||
);
|
||||
}
|
||||
Ok(meta)
|
||||
};
|
||||
self.with_lease_request(async {
|
||||
if let Some(send_elicitation) = &self.shared_elicitation {
|
||||
self.client
|
||||
.call_tool_with_elicitation(
|
||||
name,
|
||||
arguments,
|
||||
meta_for_session,
|
||||
timeout,
|
||||
Arc::clone(send_elicitation),
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
self.client
|
||||
.call_tool(name, arguments, meta_for_session, timeout)
|
||||
.await
|
||||
}
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// Lists resources within this chat's lease lifetime.
|
||||
pub(crate) async fn list_resources(
|
||||
&self,
|
||||
params: Option<PaginatedRequestParams>,
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<ListResourcesResult> {
|
||||
self.with_active_shared_lease(self.client.list_resources(params, timeout))
|
||||
.await?
|
||||
}
|
||||
|
||||
/// Lists resource templates within this chat's lease lifetime.
|
||||
pub(crate) async fn list_resource_templates(
|
||||
&self,
|
||||
params: Option<PaginatedRequestParams>,
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<ListResourceTemplatesResult> {
|
||||
self.with_active_shared_lease(self.client.list_resource_templates(params, timeout))
|
||||
.await?
|
||||
}
|
||||
|
||||
/// Reads a resource using this chat's input authority and lease lifetime.
|
||||
pub(crate) async fn read_resource(
|
||||
&self,
|
||||
mut params: rmcp::model::ReadResourceRequestParams,
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<rmcp::model::ReadResourceResult> {
|
||||
if let Some(lease) = self.connection_sharing.server_lease() {
|
||||
params.meta.get_or_insert_default().insert(
|
||||
crate::connection_pool::LEASE_META_KEY.to_owned(),
|
||||
serde_json::json!(lease.id),
|
||||
);
|
||||
}
|
||||
self.with_lease_request(async {
|
||||
if let Some(send_elicitation) = &self.shared_elicitation {
|
||||
self.client
|
||||
.read_resource_with_elicitation(params, timeout, Arc::clone(send_elicitation))
|
||||
.await
|
||||
} else {
|
||||
self.client.read_resource(params, timeout).await
|
||||
}
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub(crate) async fn listed_catalog(&self) -> (Vec<ToolInfo>, Option<Arc<serde_json::Value>>) {
|
||||
self.tool_catalog
|
||||
.read(|catalog| (catalog.tools.to_vec(), catalog.meta))
|
||||
.await
|
||||
}
|
||||
}
|
||||
83
akode-rs/akode-mcp/src/rmcp_client/provenance_tests.rs
Normal file
83
akode-rs/akode-mcp/src/rmcp_client/provenance_tests.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
//! Plugin attribution stays bounded without changing tool definitions or membership.
|
||||
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
fn app_tool(description: &str) -> ToolInfo {
|
||||
regular_mcp_tool_info_from_listed_tool(
|
||||
"docs",
|
||||
/*server_instructions*/ None,
|
||||
ToolWithConnectorId {
|
||||
tool: RmcpTool::new("search", description.to_string(), Arc::default()),
|
||||
connector_id: None,
|
||||
connector_name: None,
|
||||
connector_description: None,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provenance_preserves_short_descriptions_and_membership() {
|
||||
for (description, names, expected_description) in [
|
||||
("Search", vec![], "Search"),
|
||||
("", vec!["Alpha"], "This tool is part of plugin `Alpha`."),
|
||||
(
|
||||
"Search",
|
||||
vec!["Alpha"],
|
||||
"Search. This tool is part of plugin `Alpha`.",
|
||||
),
|
||||
(
|
||||
"Search!",
|
||||
vec!["Alpha", "Beta"],
|
||||
"Search! This tool is part of plugins `Alpha`, `Beta`.",
|
||||
),
|
||||
] {
|
||||
let names = names.into_iter().map(str::to_string).collect::<Vec<_>>();
|
||||
let mut tool = app_tool(description);
|
||||
let mut expected = tool.clone();
|
||||
expected.tool.description = Some(expected_description.to_string().into());
|
||||
expected.plugin_display_names = names.clone();
|
||||
add_plugin_provenance_to_tool(&mut tool, &names);
|
||||
assert_eq!(tool, expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provenance_bounds_aggregate_names_and_retains_all_associations() {
|
||||
let exact_name =
|
||||
"a".repeat(MAX_PLUGIN_PROVENANCE_BYTES - "This tool is part of plugin ``.".len());
|
||||
for names in [
|
||||
vec![exact_name.clone()],
|
||||
vec![format!("{exact_name}a")],
|
||||
vec!["界".repeat(MAX_PLUGIN_PROVENANCE_BYTES)],
|
||||
(0..512)
|
||||
.map(|index| format!("{index:03}{}", "界".repeat(61)))
|
||||
.collect(),
|
||||
] {
|
||||
let mut tool = app_tool("Search.");
|
||||
let original = tool.clone();
|
||||
add_plugin_provenance_to_tool(&mut tool, &names);
|
||||
let note = tool
|
||||
.tool
|
||||
.description
|
||||
.as_deref()
|
||||
.unwrap()
|
||||
.strip_prefix("Search. ")
|
||||
.unwrap();
|
||||
assert!(note.len() <= MAX_PLUGIN_PROVENANCE_BYTES);
|
||||
if names[0] == exact_name {
|
||||
assert_eq!(note.len(), MAX_PLUGIN_PROVENANCE_BYTES);
|
||||
} else if names.len() == 1 {
|
||||
assert_eq!(note, "This tool is part of 1 plugin.");
|
||||
} else {
|
||||
let shown = note.matches('`').count() / 2;
|
||||
assert!(shown > 0 && shown < names.len());
|
||||
assert!(note.starts_with(&format!("This tool is part of plugins `{}`", names[0])));
|
||||
assert!(note.ends_with(&format!(", and {} more.", names.len() - shown)));
|
||||
}
|
||||
let mut expected = original;
|
||||
expected.tool.description = tool.tool.description.clone();
|
||||
expected.plugin_display_names = names;
|
||||
assert_eq!(tool, expected);
|
||||
}
|
||||
}
|
||||
30
akode-rs/akode-mcp/src/rmcp_client/status.rs
Normal file
30
akode-rs/akode-mcp/src/rmcp_client/status.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
//! Observe the latest startup attempt without polling it or initiating a retry.
|
||||
|
||||
use akode_protocol::mcp::McpServerConnectionStatus as Status;
|
||||
|
||||
use super::AsyncManagedClient;
|
||||
use super::StartupOutcomeError;
|
||||
|
||||
impl AsyncManagedClient {
|
||||
pub(crate) async fn connection_status(&self) -> Status {
|
||||
if self.cancel_token.is_cancelled() {
|
||||
return Status::Cancelled;
|
||||
}
|
||||
let outcome = self.client.peek().cloned();
|
||||
match outcome {
|
||||
Some(Ok(client)) => {
|
||||
if client.client.is_closed().await {
|
||||
Status::Failed
|
||||
} else {
|
||||
Status::Connected
|
||||
}
|
||||
}
|
||||
Some(Err(error)) if error.is_authentication_required() => {
|
||||
Status::AuthenticationRequired
|
||||
}
|
||||
Some(Err(StartupOutcomeError::Failed { .. })) => Status::Failed,
|
||||
Some(Err(StartupOutcomeError::Cancelled)) => Status::Cancelled,
|
||||
None => Status::Starting,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user