Fix chat_api companion config issues

This commit is contained in:
Coffee
2026-03-16 17:46:44 +08:00
parent 42b66b33b2
commit ce46e75dec
4 changed files with 74 additions and 7 deletions
+7 -1
View File
@@ -481,7 +481,13 @@ impl AppBuilder {
match servers_result {
Ok(mut servers) => {
if let Some(companion) = companion_mcp_server {
servers.upsert(companion);
let companion_name = companion.name.clone();
if !servers.insert_if_absent(companion) {
tracing::debug!(
"Skipping derived MCP companion '{}': an existing config with that name is already present",
companion_name
);
}
}
let enabled: Vec<_> = servers.enabled_servers().cloned().collect();
if !enabled.is_empty() {
+11
View File
@@ -410,6 +410,17 @@ async fn auth_server(name: String, user_id: String) -> anyhow::Result<()> {
.cloned()
.ok_or_else(|| anyhow::anyhow!("Server '{}' not found", name))?;
if server.uses_runtime_auth_source() {
println!();
println!(
" Server '{}' reuses your active NEAR AI authentication and does not support separate MCP OAuth.",
name
);
println!(" Configure NEAR AI auth (API key or session login) instead.");
println!();
return Ok(());
}
// Initialize secrets store
let secrets = get_secrets_store().await?;
+44 -4
View File
@@ -614,8 +614,11 @@ impl ExtensionManager {
match self.load_mcp_servers().await {
Ok(servers) => {
for server in &servers.servers {
let authenticated =
is_authenticated(server, &self.secrets, &self.user_id).await;
let authenticated = if server.uses_runtime_auth_source() {
self.is_runtime_authenticated(server).await
} else {
is_authenticated(server, &self.secrets, &self.user_id).await
};
let clients = self.mcp_clients.read().await;
let active = clients.contains_key(&server.name);
@@ -646,7 +649,7 @@ impl ExtensionManager {
active,
tools,
needs_setup: false,
has_auth: false,
has_auth: server.requires_auth(),
installed: true,
activation_error: None,
version: None,
@@ -1292,12 +1295,35 @@ impl ExtensionManager {
};
if let Some(ref companion) = self.companion_mcp_server {
servers.upsert(companion.clone());
servers.insert_if_absent(companion.clone());
}
Ok(servers)
}
async fn is_runtime_authenticated(&self, server: &McpServerConfig) -> bool {
match server.auth_source {
Some(crate::tools::mcp::config::McpAuthSource::NearAi) => {
if self.nearai_api_key.is_some() {
return true;
}
if let Ok(key) = std::env::var("NEARAI_API_KEY")
&& !key.trim().is_empty()
{
return true;
}
if let Some(ref session) = self.nearai_session_manager {
return session.has_token().await;
}
false
}
None => false,
}
}
async fn get_mcp_server(
&self,
name: &str,
@@ -1850,6 +1876,20 @@ impl ExtensionManager {
.await
.map_err(|e| ExtensionError::NotInstalled(e.to_string()))?;
if server.uses_runtime_auth_source() {
if self.is_runtime_authenticated(&server).await {
return Ok(AuthResult::authenticated(name, ExtensionKind::McpServer));
}
return Ok(AuthResult::needs_setup(
name,
ExtensionKind::McpServer,
"This MCP server reuses your active NEAR AI authentication. Configure a NEAR AI API key or sign in to NEAR AI first, then try again."
.to_string(),
None,
));
}
// Check if already authenticated
if is_authenticated(&server, &self.secrets, &self.user_id).await {
return Ok(AuthResult::authenticated(name, ExtensionKind::McpServer));
+12 -2
View File
@@ -289,8 +289,8 @@ impl McpServerConfig {
}
}
/// Default name used for the companion chat-api MCP server derived from NEAR AI config.
pub const NEARAI_COMPANION_MCP_NAME: &str = "chat_api";
/// Reserved name used for the companion chat-api MCP server derived from NEAR AI config.
pub const NEARAI_COMPANION_MCP_NAME: &str = "_nearai_companion_mcp";
/// Build the companion chat-api MCP server from the active NearAI config.
///
@@ -414,6 +414,16 @@ impl McpServersFile {
}
}
/// Insert a server only if no server with the same name already exists.
pub fn insert_if_absent(&mut self, config: McpServerConfig) -> bool {
if self.get(&config.name).is_some() {
false
} else {
self.servers.push(config);
true
}
}
/// Remove a server by name.
pub fn remove(&mut self, name: &str) -> bool {
let len_before = self.servers.len();