From 66606e5eb0fdfd901fe648266d402716af86cfc4 Mon Sep 17 00:00:00 2001 From: Douwe Osinga Date: Wed, 16 Sep 2026 21:25:28 +0000 Subject: [PATCH] Split extension transports out of ExtensionManager (#12088) Co-authored-by: Claude Opus 5 --- crates/goose-cli/src/session/builder.rs | 14 +- crates/goose/src/agents/extension.rs | 173 +- .../src/agents/extension_malware_check.rs | 15 +- .../src/agents/extension_manager/builtin.rs | 41 + .../mod.rs} | 1852 +---------------- .../src/agents/extension_manager/stdio.rs | 101 + .../extension_manager/streamable_http.rs | 1041 +++++++++ crates/goose/src/agents/mcp_client.rs | 47 +- .../agents/platform_extensions/ext_manager.rs | 78 +- 9 files changed, 1467 insertions(+), 1895 deletions(-) create mode 100644 crates/goose/src/agents/extension_manager/builtin.rs rename crates/goose/src/agents/{extension_manager.rs => extension_manager/mod.rs} (60%) create mode 100644 crates/goose/src/agents/extension_manager/stdio.rs create mode 100644 crates/goose/src/agents/extension_manager/streamable_http.rs diff --git a/crates/goose-cli/src/session/builder.rs b/crates/goose-cli/src/session/builder.rs index 888453677..fb0864d33 100644 --- a/crates/goose-cli/src/session/builder.rs +++ b/crates/goose-cli/src/session/builder.rs @@ -29,15 +29,10 @@ fn truncate_with_ellipsis(s: &str, max_len: usize) -> String { } } -// Plain String rather than `ExtensionError`: the only variant this function -// ever constructs is `ConfigError(String)`, but clippy's `result_large_err` -// sizes an error type by its largest variant, and `ExtensionError` carries a -// `ClientError`/`ClientInitializeError` far past the 128-byte default -// threshold. Callers wrap this back into `ExtensionError::ConfigError`. fn disambiguate_stdio_extension_names( extensions: &mut [(String, ExtensionConfig)], renameable: &HashSet, -) -> Result<(), String> { +) -> Result<(), ExtensionError> { let mut counts: HashMap = HashMap::new(); // Only entries the caller marked fixed (explicitly named, or not a CLI // stdio extension at all) can make this an error — a fixed name colliding @@ -56,10 +51,10 @@ fn disambiguate_stdio_extension_names( !renameable.contains(index) && fixed_counts.get(&config.key()).copied().unwrap_or(0) > 1 }); if let Some((_, (_, config))) = duplicate_fixed_names { - return Err(format!( + return Err(ExtensionError::ConfigError(format!( "extension name '{}' is already in use", config.name() - )); + ))); } let mut taken: HashSet = counts.keys().cloned().collect(); @@ -617,8 +612,7 @@ async fn collect_extension_configs( .into_iter() .map(|(label, config, _)| (label, config)), ); - disambiguate_stdio_extension_names(&mut all, &renameable) - .map_err(ExtensionError::ConfigError)?; + disambiguate_stdio_extension_names(&mut all, &renameable)?; Ok(all.into_iter().map(|(_, config)| config).collect()) } diff --git a/crates/goose/src/agents/extension.rs b/crates/goose/src/agents/extension.rs index 809b470ac..8c4772e39 100644 --- a/crates/goose/src/agents/extension.rs +++ b/crates/goose/src/agents/extension.rs @@ -4,6 +4,7 @@ use crate::config; use crate::config::extensions::name_to_key; use crate::config::permission::PermissionLevel; use crate::config::Config; +use once_cell::sync::Lazy; use rmcp::service::ClientInitializeError; use rmcp::ServiceError as ClientError; use serde::Deserializer; @@ -35,7 +36,6 @@ impl ProcessExit { } } -/// Errors from Extension operation #[derive(Error, Debug)] pub enum ExtensionError { #[error("failed a client call to an MCP server: {0}")] @@ -49,16 +49,27 @@ pub enum ExtensionError { #[error("IO error: {0}")] IoError(#[from] std::io::Error), #[error("failed to initialize MCP client: {0}")] - InitializeError(#[from] ClientInitializeError), + InitializeError(#[source] Box), #[error("{0}")] - ProcessExit(#[from] ProcessExit), + ProcessExit(#[source] Box), +} + +impl From for ExtensionError { + fn from(error: ClientInitializeError) -> Self { + Self::InitializeError(Box::new(error)) + } +} + +impl From for ExtensionError { + fn from(error: ProcessExit) -> Self { + Self::ProcessExit(Box::new(error)) + } } pub type ExtensionResult = Result; #[derive(Debug, Clone, Serialize, Default, PartialEq)] pub struct Envs { - /// A map of environment variables to set, e.g. API_KEY -> some_secret, HOST -> host #[serde(default)] #[serde(flatten)] map: HashMap, @@ -75,7 +86,6 @@ impl<'de> Deserialize<'de> for Envs { } impl Envs { - /// List of sensitive env vars that should not be overridden const DISALLOWED_KEYS: [&'static str; 31] = [ // 🔧 Binary path manipulation "PATH", // Controls executable lookup paths — critical for command hijacking @@ -130,24 +140,10 @@ impl Envs { Self { map: validated } } - /// Returns a copy of the validated env vars pub fn get_env(&self) -> HashMap { self.map.clone() } - /// Returns an error if any disallowed env var is present - pub fn validate(&self) -> Result<(), Box> { - for key in self.map.keys() { - if Self::is_disallowed(key) { - return Err(Box::new(ExtensionError::ConfigError(format!( - "environment variable {} not allowed to be overwritten", - key - )))); - } - } - Ok(()) - } - fn is_disallowed(key: &str) -> bool { Self::DISALLOWED_KEYS .iter() @@ -155,14 +151,11 @@ impl Envs { } } -/// Represents the different types of MCP extensions that can be added to the manager #[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] #[serde(tag = "type")] pub enum ExtensionConfig { - /// Standard I/O client with command and arguments #[serde(rename = "stdio")] Stdio { - /// The name used to identify this extension name: String, #[serde(default)] #[serde(deserialize_with = "deserialize_null_with_default")] @@ -185,7 +178,6 @@ pub enum ExtensionConfig { /// Built-in extension that is part of the bundled goose MCP server #[serde(rename = "builtin")] Builtin { - /// The name used to identify this extension name: String, #[serde(default)] #[serde(deserialize_with = "deserialize_null_with_default")] @@ -201,7 +193,6 @@ pub enum ExtensionConfig { /// Platform extensions that have direct access to the agent etc and run in the agent process #[serde(rename = "platform")] Platform { - /// The name used to identify this extension name: String, #[serde(default)] #[serde(deserialize_with = "deserialize_null_with_default")] @@ -213,10 +204,8 @@ pub enum ExtensionConfig { #[serde(skip_serializing_if = "Vec::is_empty")] available_tools: Vec, }, - /// Streamable HTTP client with a URI endpoint using MCP Streamable HTTP specification #[serde(rename = "streamable_http")] StreamableHttp { - /// The name used to identify this extension name: String, #[serde(default)] #[serde(deserialize_with = "deserialize_null_with_default")] @@ -369,7 +358,6 @@ impl ExtensionConfig { .to_string() } - /// Check if a tool should be available to the LLM pub fn is_tool_available(&self, tool_name: &str) -> bool { let available_tools = match self { Self::StreamableHttp { @@ -386,14 +374,10 @@ impl ExtensionConfig { } => available_tools, }; - // If no tools are specified, all tools are available - // If tools are specified, only those tools are available available_tools.is_empty() || available_tools.contains(&tool_name.to_string()) } pub async fn resolve(self, config: &Config) -> ExtensionResult { - use crate::agents::extension_manager::{merge_environments, substitute_env_vars}; - match self { Self::Stdio { name, @@ -407,7 +391,7 @@ impl ExtensionConfig { bundled, available_tools, } => { - let merged = merge_environments(&envs, &env_keys, &name, config).await?; + let merged = merge_environments(&envs, env_keys.iter(), config).await?; Ok(Self::Stdio { name, description, @@ -439,13 +423,9 @@ impl ExtensionConfig { // Resolve the OAuth client secret alongside env_keys so that // rotating it changes the resolved config, which is what // add_extension compares to decide whether to restart. - let mut secret_keys = env_keys; - if let Some(key) = &client_secret_key { - if !secret_keys.contains(key) { - secret_keys.push(key.clone()); - } - } - let merged = merge_environments(&envs, &secret_keys, &name, config).await?; + let merged = + merge_environments(&envs, env_keys.iter().chain(&client_secret_key), config) + .await?; let headers = headers .into_iter() .map(|(k, v)| { @@ -476,6 +456,66 @@ impl ExtensionConfig { } } +static RE_ENV_BRACES: Lazy = + Lazy::new(|| regex::Regex::new(r"\$\{\s*([A-Za-z_][A-Za-z0-9_]*)\s*\}").expect("valid regex")); + +static RE_ENV_SIMPLE: Lazy = + Lazy::new(|| regex::Regex::new(r"\$([A-Za-z_][A-Za-z0-9_]*)").expect("valid regex")); + +async fn merge_environments( + envs: &Envs, + env_keys: impl Iterator, + config: &Config, +) -> ExtensionResult> { + let mut all_envs = envs.get_env(); + for key in env_keys { + // inline values shadow the secret store + if all_envs.contains_key(key) { + continue; + } + // Config::get_secret parses env values as JSON, so PORT=3000 would come + // back as a number. An env override is a string by definition; only + // the secret store gets the type check. + let value = match std::env::var(key.to_uppercase()) { + Ok(value) => value, + Err(_) => config.get_secret::(key).map_err(|e| { + ExtensionError::ConfigError(format!( + "Failed to fetch secret '{}' from config: {}", + key, e + )) + })?, + }; + all_envs.insert(key.clone(), value); + } + Ok(Envs::new(all_envs).get_env()) +} + +fn substitute_env_vars(value: &str, env_map: &HashMap) -> String { + let mut result = value.to_string(); + + for cap in RE_ENV_BRACES.captures_iter(value) { + if let Some(var_name) = cap.get(1) { + if let Some(env_value) = env_map.get(var_name.as_str()) { + result = result.replace(&cap[0], env_value); + } + } + } + + // Scan the original input for $VAR patterns (not the post-substitution result) + // to avoid recursive expansion when a substituted value contains $OTHER_VAR. + for cap in RE_ENV_SIMPLE.captures_iter(value) { + if let Some(var_name) = cap.get(1) { + if !value.contains(&format!("${{{}}}", var_name.as_str())) { + if let Some(env_value) = env_map.get(var_name.as_str()) { + result = result.replace(&cap[0], env_value); + } + } + } + } + + result +} + impl std::fmt::Display for ExtensionConfig { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -499,7 +539,6 @@ impl std::fmt::Display for ExtensionConfig { } } -/// Information about the extension used for building prompts #[derive(Clone, Debug, Serialize)] pub struct ExtensionInfo { pub name: String, @@ -526,7 +565,6 @@ where Ok(opt.unwrap_or_default()) } -/// Information about the tool used for building prompts #[derive(Clone, Debug, Serialize)] pub struct ToolInfo { pub name: String, @@ -561,6 +599,7 @@ impl ToolInfo { #[cfg(test)] mod tests { + use super::*; use crate::agents::*; use crate::config; use test_case::test_case; @@ -1070,6 +1109,38 @@ timeout: 300", assert_eq!(config.resolve(&cfg).await.unwrap(), expected); } + #[tokio::test] + async fn test_resolve_takes_env_overrides_as_raw_strings() { + let _guard = env_lock::lock_env([("PORT", Some("3000")), ("HEADLESS", Some("true"))]); + let dir = tempfile::tempdir().unwrap(); + let cfg = config::Config::new_with_file_secrets( + dir.path().join("config.yaml"), + dir.path().join("secrets.yaml"), + ) + .unwrap(); + let config = ExtensionConfig::Stdio { + name: "test".to_string(), + description: String::new(), + cmd: "cmd".to_string(), + args: vec![], + envs: Envs::default(), + env_keys: vec!["PORT".to_string(), "HEADLESS".to_string()], + timeout: None, + cwd: None, + bundled: None, + available_tools: vec![], + }; + + let resolved = config.resolve(&cfg).await.unwrap(); + + let ExtensionConfig::Stdio { envs, .. } = resolved else { + panic!("expected stdio config"); + }; + let envs = envs.get_env(); + assert_eq!(envs["PORT"], "3000"); + assert_eq!(envs["HEADLESS"], "true"); + } + #[test] fn test_display_streamable_http_with_socket() { let config = ExtensionConfig::StreamableHttp { @@ -1115,4 +1186,24 @@ timeout: 300", "StreamableHttp(test: http://localhost:8080/mcp)" ); } + + #[test_case("Bearer ${ AUTH_TOKEN }", "Bearer secret123"; "braces_with_spaces")] + #[test_case("Bearer ${AUTH_TOKEN}", "Bearer secret123"; "braces")] + #[test_case("Bearer $AUTH_TOKEN", "Bearer secret123"; "bare")] + #[test_case("Key: $API_KEY, Token: ${AUTH_TOKEN}", "Key: key456, Token: secret123"; "multiple")] + #[test_case("Bearer ${UNKNOWN_VAR}", "Bearer ${UNKNOWN_VAR}"; "unknown_left_alone")] + #[test_case("${TOKEN}", "abc$KEY"; "substituted_value_not_expanded_again_braces")] + #[test_case("$TOKEN", "abc$KEY"; "substituted_value_not_expanded_again_bare")] + fn test_substitute_env_vars(input: &str, expected: &str) { + let env_map = HashMap::from( + [ + ("AUTH_TOKEN", "secret123"), + ("API_KEY", "key456"), + ("TOKEN", "abc$KEY"), + ("KEY", "xyz"), + ] + .map(|(k, v)| (k.to_string(), v.to_string())), + ); + assert_eq!(substitute_env_vars(input, &env_map), expected); + } } diff --git a/crates/goose/src/agents/extension_malware_check.rs b/crates/goose/src/agents/extension_malware_check.rs index 1acad54ab..8cfc73b5b 100644 --- a/crates/goose/src/agents/extension_malware_check.rs +++ b/crates/goose/src/agents/extension_malware_check.rs @@ -13,8 +13,8 @@ pub struct OsvChecker { impl OsvChecker { /// Constructs a checker. Honors OSV_ENDPOINT env var if present. - pub fn new() -> Result> { - let client = http_client().map_err(Box::new)?; + pub fn new() -> Result { + let client = http_client()?; let endpoint = std::env::var("OSV_ENDPOINT") .ok() .and_then(|s| Url::parse(&s).ok()) @@ -23,8 +23,8 @@ impl OsvChecker { } /// Constructs with a custom endpoint (handy for tests). - pub fn with_endpoint(endpoint: Url) -> Result> { - let client = http_client().map_err(Box::new)?; + pub fn with_endpoint(endpoint: Url) -> Result { + let client = http_client()?; Ok(Self { client, endpoint }) } @@ -48,8 +48,7 @@ pub async fn deny_if_malicious_cmd_args(cmd: &str, args: &[String]) -> Result<() }; if let Some((name, version)) = parse_first_package_arg(ecosystem, args) { - OsvChecker::new() - .map_err(|e| *e)? + OsvChecker::new()? .deny_if_malicious(&name, ecosystem, version.as_deref()) .await?; } else { @@ -78,8 +77,7 @@ pub async fn deny_if_malicious( ecosystem: &str, version: Option<&str>, ) -> Result<(), ExtensionError> { - OsvChecker::new() - .map_err(|e| *e)? + OsvChecker::new()? .deny_if_malicious(name, ecosystem, version) .await } @@ -479,7 +477,6 @@ async fn deny_if_malicious_impl( Ok(()) } -#[allow(clippy::result_large_err)] fn http_client() -> Result { let mut headers = HeaderMap::new(); headers.insert( diff --git a/crates/goose/src/agents/extension_manager/builtin.rs b/crates/goose/src/agents/extension_manager/builtin.rs new file mode 100644 index 000000000..b2a6703eb --- /dev/null +++ b/crates/goose/src/agents/extension_manager/builtin.rs @@ -0,0 +1,41 @@ +use std::collections::HashMap; + +use super::super::container::Container; +use super::super::extension::{ExtensionError, ExtensionResult}; +use super::super::mcp_client::{ConnectContext, McpClient, McpClientTrait}; +use super::stdio; +use crate::builtin_extension::get_builtin_extension; +use crate::config::extensions::name_to_key; + +pub(super) async fn connect( + name: &str, + container: Option<&Container>, + mut ctx: ConnectContext, +) -> ExtensionResult> { + let key = name_to_key(name); + let extension_fn = get_builtin_extension(&key) + .ok_or_else(|| ExtensionError::ConfigError(format!("Unknown extension: {}", name)))?; + + if let Some(container) = container { + ctx.docker_container = Some(container.id().to_string()); + tracing::info!( + container = %container.id(), + builtin = %name, + "Starting builtin extension inside Docker container" + ); + let command = stdio::docker_exec( + container, + HashMap::new(), + "goose", + &["mcp".to_string(), key], + ); + return Ok(Box::new(stdio::spawn(command, ctx).await?)); + } + + let (server_read, client_write) = tokio::io::duplex(65536); + let (client_read, server_write) = tokio::io::duplex(65536); + extension_fn(server_read, server_write); + Ok(Box::new( + McpClient::connect((client_read, client_write), ctx).await?, + )) +} diff --git a/crates/goose/src/agents/extension_manager.rs b/crates/goose/src/agents/extension_manager/mod.rs similarity index 60% rename from crates/goose/src/agents/extension_manager.rs rename to crates/goose/src/agents/extension_manager/mod.rs index 4605824c0..0d24cdbbb 100644 --- a/crates/goose/src/agents/extension_manager.rs +++ b/crates/goose/src/agents/extension_manager/mod.rs @@ -1,63 +1,47 @@ use anyhow::Result; -use axum::http::{HeaderMap, HeaderName, HeaderValue}; use chrono::{DateTime, Utc}; use futures::stream::{self, FuturesUnordered, StreamExt}; use futures::Stream; use futures::{future, FutureExt}; -use once_cell::sync::Lazy; -use rmcp::model::ProtocolVersion; -use rmcp::service::{ClientInitializeError, ServiceError}; -use rmcp::transport::streamable_http_client::{ - StreamableHttpClientTransportConfig, StreamableHttpError, -}; -use rmcp::transport::{ConfigureCommandExt, DynamicTransportError, StreamableHttpClientTransport}; +use rmcp::service::ServiceError; use std::collections::HashMap; use std::path::PathBuf; use std::pin::Pin; use std::sync::atomic::{AtomicU64, Ordering}; -use std::sync::{Arc, Weak}; +use std::sync::Arc; use std::task::{Context, Poll}; use std::time::Duration; -#[cfg(test)] -use tempfile::tempdir; -use tokio::io::AsyncReadExt; -use tokio::process::Command; use tokio::sync::{mpsc, Mutex}; use tokio_stream::wrappers::ReceiverStream; use tokio_util::sync::CancellationToken; -use tracing::{error, warn}; +use tracing::warn; use super::container::Container; use super::extension::{ - ExtensionConfig, ExtensionError, ExtensionInfo, ExtensionResult, PlatformExtensionContext, - PLATFORM_EXTENSIONS, + ExtensionConfig, ExtensionInfo, ExtensionResult, PlatformExtensionContext, PLATFORM_EXTENSIONS, }; use super::tool_execution::{ToolCallContext, ToolCallNotificationEmitter, ToolCallResult}; use super::types::SharedProvider; use crate::action_required_manager::ActionRequiredManager; -use crate::agents::extension::{Envs, ProcessExit}; -use crate::agents::extension_malware_check; use crate::agents::mcp_client::{ - GooseMcpClientCapabilities, GooseMcpHostInfo, McpClient, McpClientTrait, + ConnectContext, GooseMcpClientCapabilities, GooseMcpHostInfo, McpClientTrait, }; use crate::agents::reply_parts::is_tool_visible_to_app; -use crate::builtin_extension::get_builtin_extension; use crate::config::extensions::name_to_key; -use crate::config::search_path::SearchPaths; -use crate::config::{get_all_extensions, Config}; -use crate::oauth::{ - oauth_flow, oauth_flow_with_challenge, GooseCredentialStore, StaticOAuthClientConfig, -}; -use crate::subprocess::spawn_long_lived_mcp_subprocess; +use crate::config::Config; +use crate::oauth::GooseCredentialStore; use rmcp::model::{ CallToolRequestParams, CallToolResult, ContentBlock, ErrorCode, ErrorData, GetPromptResult, ListResourcesResult, ListToolsResult, MetaObject, Prompt, Resource, ResourceContents, ServerInfo, ServerNotification, Tool, }; -use rmcp::transport::auth::{AuthClient, CredentialStore}; use schemars::_private::NoSerialize; use serde_json::Value; +mod builtin; +mod stdio; +mod streamable_http; + type McpClientBox = Arc; const TOOL_CALL_NOTIFICATION_CHANNEL_CAPACITY: usize = 32; @@ -109,12 +93,6 @@ impl Drop for ActionRequiredStream { } } -static RE_ENV_BRACES: Lazy = - Lazy::new(|| regex::Regex::new(r"\$\{\s*([A-Za-z_][A-Za-z0-9_]*)\s*\}").expect("valid regex")); - -static RE_ENV_SIMPLE: Lazy = - Lazy::new(|| regex::Regex::new(r"\$([A-Za-z_][A-Za-z0-9_]*)").expect("valid regex")); - fn resolve_timeout(timeout: Option) -> u64 { timeout.unwrap_or_else(|| { Config::global() @@ -234,16 +212,6 @@ impl ResourceItem { } } -fn resolve_command(cmd: &str) -> PathBuf { - SearchPaths::builder() - .with_npm() - .resolve(cmd) - .unwrap_or_else(|_| { - // let the OS raise the error - PathBuf::from(cmd) - }) -} - fn require_str_parameter<'a>(v: &'a serde_json::Value, name: &str) -> Result<&'a str, ErrorData> { let v = v.get(name).ok_or_else(|| { ErrorData::new( @@ -429,1027 +397,6 @@ struct ResolvedTool { resource_uri: Option, } -#[allow(clippy::too_many_arguments)] -async fn child_process_client( - mut command: Command, - timeout: &Option, - provider: SharedProvider, - working_dir: &PathBuf, - docker_container: Option, - client_name: String, - capabilities: GooseMcpClientCapabilities, - action_required: Arc, - extension_manager: Weak, -) -> ExtensionResult { - if let Ok(path) = SearchPaths::builder().path() { - command.env("PATH", path); - } - - if working_dir.exists() && working_dir.is_dir() { - tracing::info!("Setting MCP process working directory: {:?}", working_dir); - command.current_dir(working_dir); - } else { - tracing::warn!( - "Working directory doesn't exist or isn't a directory: {:?}", - working_dir - ); - } - - let (transport, mut stderr) = spawn_long_lived_mcp_subprocess(command).await?; - let mut stderr = stderr.take().ok_or_else(|| { - ExtensionError::SetupError("failed to attach child process stderr".to_owned()) - })?; - - let stderr_task = tokio::spawn(async move { - let mut all_stderr = Vec::new(); - stderr.read_to_end(&mut all_stderr).await?; - Ok::(String::from_utf8_lossy(&all_stderr).into()) - }); - - let client_result = McpClient::connect_with_container( - transport, - Duration::from_secs(resolve_timeout(*timeout)), - provider, - docker_container, - client_name, - capabilities, - working_dir.clone(), - action_required, - extension_manager, - ) - .await; - - match client_result { - Ok(client) => Ok(client), - Err(error) => { - let error_task_out = stderr_task.await?; - Err::(match error_task_out { - Ok(stderr_content) => ProcessExit::new(stderr_content, error).into(), - Err(e) => e.into(), - }) - } - } -} - -/// Retry with OAuth for typed auth challenges and wrapped bare HTTP 401 responses. -fn is_oauth_auth_failure(err: &ClientInitializeError) -> bool { - let ClientInitializeError::TransportError { - error: DynamicTransportError { error, .. }, - .. - } = err - else { - return false; - }; - - if let Some(http_err) = error.downcast_ref::>() { - return match http_err { - StreamableHttpError::AuthRequired(_) | StreamableHttpError::InsufficientScope(_) => { - true - } - StreamableHttpError::UnexpectedServerResponse(body) => { - body.starts_with("HTTP 401") || body.starts_with("HTTP 403") - } - _ => false, - }; - } - - #[cfg(unix)] - if let Some(http_err) = error - .downcast_ref::>( - ) - { - return match http_err { - StreamableHttpError::AuthRequired(_) | StreamableHttpError::InsufficientScope(_) => { - true - } - StreamableHttpError::UnexpectedServerResponse(body) => { - body.starts_with("HTTP 401") || body.starts_with("HTTP 403") - } - _ => false, - }; - } - - let message = error.to_string(); - message.contains("unexpected server response: HTTP 401") - || message.contains("unexpected server response: HTTP 403") - || message.contains("Auth required") - || message.contains("Authorization required") -} - -fn should_attempt_oauth_fallback(res: &Result) -> bool { - res.as_ref().err().is_some_and(is_oauth_auth_failure) -} - -/// Extract the `WWW-Authenticate` challenge from a failed initialization, so -/// OAuth discovery can be seeded from the server's 401/403 response instead of -/// probing well-known locations. -fn auth_challenge_from_error(err: &ClientInitializeError) -> Option { - let ClientInitializeError::TransportError { - error: DynamicTransportError { error, .. }, - .. - } = err - else { - return None; - }; - - if let Some(http_err) = error.downcast_ref::>() { - return http_err.auth_challenge().map(str::to_string); - } - - #[cfg(unix)] - if let Some(http_err) = error - .downcast_ref::>( - ) - { - return http_err.auth_challenge().map(str::to_string); - } - - None -} - -fn auth_challenge_from_result(res: &Result) -> Option { - res.as_ref().err().and_then(auth_challenge_from_error) -} - -/// Extract the `WWW-Authenticate` challenge from a post-initialization request -/// failure (401 auth required or 403 insufficient scope), so a step-up -/// authorization can be started reactively. -fn auth_challenge_from_service_error(err: &ServiceError) -> Option { - let ServiceError::TransportSend(DynamicTransportError { error, .. }) = err else { - return None; - }; - - if let Some(http_err) = error.downcast_ref::>() { - return http_err.auth_challenge().map(str::to_string); - } - - #[cfg(unix)] - if let Some(http_err) = error - .downcast_ref::>( - ) - { - return http_err.auth_challenge().map(str::to_string); - } - - None -} - -async fn clear_credentials_on_post_refresh_auth_failure( - credential_store: &dyn CredentialStore, - name: &str, - error: &ExtensionError, -) -> bool { - let ExtensionError::InitializeError(err) = error else { - return false; - }; - - if !is_oauth_auth_failure(err) - || auth_challenge_from_error(err).is_some_and(|challenge| { - challenge - .to_ascii_lowercase() - .contains("insufficient_scope") - }) - { - return false; - } - - if let Err(e) = credential_store.clear().await { - warn!( - "[OAuth:{}] error clearing rejected credentials: {}", - name, e - ); - } - true -} - -/// Merge environment variables from direct envs and keychain-stored env_keys -pub(crate) async fn merge_environments( - envs: &Envs, - env_keys: &[String], - ext_name: &str, - config: &Config, -) -> Result, ExtensionError> { - let mut all_envs = envs.get_env(); - - for key in env_keys { - if all_envs.contains_key(key) { - continue; - } - - match config.get(key, true) { - Ok(value) => { - if value.is_null() { - warn!( - key = %key, - ext_name = %ext_name, - "Secret key not found in config (returned null)." - ); - continue; - } - - if let Some(str_val) = value.as_str() { - all_envs.insert(key.clone(), str_val.to_string()); - } else { - warn!( - key = %key, - ext_name = %ext_name, - value_type = %value.get("type").and_then(|t| t.as_str()).unwrap_or("unknown"), - "Secret value is not a string; skipping." - ); - } - } - Err(e) => { - error!( - key = %key, - ext_name = %ext_name, - error = %e, - "Failed to fetch secret from config." - ); - return Err(ExtensionError::ConfigError(format!( - "Failed to fetch secret '{}' from config: {}", - key, e - ))); - } - } - } - - Ok(Envs::new(all_envs).get_env()) -} - -/// Build the pre-registered OAuth client config for a streamable_http -/// extension. The secret is referenced by key and resolved from the merged -/// environment or the config secret store, so it is never stored inline in -/// the extension config. -fn resolve_static_oauth_client( - client_id: Option<&str>, - client_secret_key: Option<&str>, - scopes: &[String], - envs: &HashMap, - config: &Config, -) -> Result, Box> { - let Some(client_id) = client_id else { - if client_secret_key.is_some() { - return Err(Box::new(ExtensionError::ConfigError( - "client_secret_key requires client_id".to_string(), - ))); - } - if !scopes.is_empty() { - return Err(Box::new(ExtensionError::ConfigError( - "scopes requires client_id".to_string(), - ))); - } - return Ok(None); - }; - - let client_secret = match client_secret_key { - Some(key) => Some(resolve_secret_value(key, envs, config)?), - None => None, - }; - - Ok(Some(StaticOAuthClientConfig { - client_id: substitute_env_vars(client_id, envs), - client_secret, - scopes: scopes.to_vec(), - })) -} - -fn resolve_secret_value( - key: &str, - envs: &HashMap, - config: &Config, -) -> Result> { - if let Some(value) = envs.get(key) { - return Ok(value.clone()); - } - - let value = config.get(key, true).map_err(|error| { - Box::new(ExtensionError::ConfigError(format!( - "Failed to fetch secret '{}' from config: {}", - key, error - ))) - })?; - - value.as_str().map(str::to_string).ok_or_else(|| { - Box::new(ExtensionError::ConfigError(format!( - "Secret '{}' is not a string", - key - ))) - }) -} - -/// Substitute environment variables in a string. Supports both ${VAR} and $VAR syntax. -pub(crate) fn substitute_env_vars(value: &str, env_map: &HashMap) -> String { - let mut result = value.to_string(); - - for cap in RE_ENV_BRACES.captures_iter(value) { - if let Some(var_name) = cap.get(1) { - if let Some(env_value) = env_map.get(var_name.as_str()) { - result = result.replace(&cap[0], env_value); - } - } - } - - // Scan the original input for $VAR patterns (not the post-substitution result) - // to avoid recursive expansion when a substituted value contains $OTHER_VAR. - for cap in RE_ENV_SIMPLE.captures_iter(value) { - if let Some(var_name) = cap.get(1) { - if !value.contains(&format!("${{{}}}", var_name.as_str())) { - if let Some(env_value) = env_map.get(var_name.as_str()) { - result = result.replace(&cap[0], env_value); - } - } - } - } - - result -} - -const GOOSE_USER_AGENT: reqwest::header::HeaderValue = - reqwest::header::HeaderValue::from_static(concat!("goose/", env!("CARGO_PKG_VERSION"))); - -fn should_retry_legacy_after_empty_discover( - result: &Result, - capabilities: &GooseMcpClientCapabilities, -) -> bool { - capabilities.protocol_version.is_none() - && result.as_ref().is_err_and(|error| { - error.to_string().contains("empty sse stream") - || matches!( - error, - ClientInitializeError::ConnectionClosed(context) - if context == "discover response" - ) - }) -} - -#[allow(clippy::too_many_arguments)] -async fn connect_with_auth( - auth_manager: rmcp::transport::AuthorizationManager, - action_required: Arc, - uri: &str, - timeout: Duration, - headers: &HashMap, - provider: SharedProvider, - client_name: String, - capabilities: GooseMcpClientCapabilities, - roots_dir: &std::path::Path, - extension_manager: Weak, -) -> ExtensionResult { - let mut auth_headers = HeaderMap::new(); - auth_headers.insert(reqwest::header::USER_AGENT, GOOSE_USER_AGENT); - for (key, value) in headers { - auth_headers.insert( - HeaderName::try_from(key) - .map_err(|_| ExtensionError::ConfigError(format!("invalid header: {}", key)))?, - value.parse().map_err(|_| { - ExtensionError::ConfigError(format!("invalid header value: {}", key)) - })?, - ); - } - #[allow(unused_mut)] - let mut auth_client_builder = reqwest::Client::builder().default_headers(auth_headers); - #[cfg(target_os = "linux")] - { - auth_client_builder = auth_client_builder.tcp_user_timeout(Some(timeout)); - } - let auth_http_client = auth_client_builder - .build() - .map_err(|_| ExtensionError::ConfigError("could not construct http client".to_string()))?; - let auth_client = AuthClient::new(auth_http_client, auth_manager); - let transport = StreamableHttpClientTransport::with_client( - auth_client.clone(), - StreamableHttpClientTransportConfig::with_uri(uri), - ); - let mut result = McpClient::connect( - transport, - timeout, - provider.clone(), - client_name.clone(), - capabilities.clone(), - roots_dir.to_path_buf(), - action_required.clone(), - extension_manager.clone(), - ) - .await; - - if should_retry_legacy_after_empty_discover(&result, &capabilities) { - let transport = StreamableHttpClientTransport::with_client( - auth_client, - StreamableHttpClientTransportConfig::with_uri(uri), - ); - let mut legacy_capabilities = capabilities; - legacy_capabilities.protocol_version = Some(ProtocolVersion::V_2025_11_25); - result = McpClient::connect( - transport, - timeout, - provider, - client_name, - legacy_capabilities, - roots_dir.to_path_buf(), - action_required, - extension_manager, - ) - .await; - } - - Ok(result?) -} - -/// Connection parameters needed to re-establish an authorized streamable HTTP -/// client after a post-initialization auth challenge (401/403). -#[derive(Clone)] -struct StreamableHttpConnectParams { - uri: String, - name: String, - timeout: Duration, - headers: HashMap, - provider: SharedProvider, - client_name: String, - capabilities: GooseMcpClientCapabilities, - roots_dir: PathBuf, - action_required: Arc, - extension_manager: Weak, - static_oauth_client: Option, -} - -/// Wraps a streamable HTTP `McpClient` and handles step-up authorization: -/// when a request fails with a 401/403 carrying a `WWW-Authenticate` -/// challenge after initialization succeeded, re-authorize using the challenge -/// (requesting the union of scopes), reconnect, and retry the request once. -struct OAuthStepUpClient { - inner: tokio::sync::RwLock, - server_info: Option, - params: tokio::sync::RwLock, - step_up_lock: tokio::sync::Mutex<()>, - notification_subscribers: Arc>>>, -} - -impl OAuthStepUpClient { - async fn new(inner: McpClient, params: StreamableHttpConnectParams) -> Self { - let server_info = inner.get_info().cloned(); - let notification_subscribers = Arc::new(Mutex::new(Vec::new())); - Self::forward_notifications(&inner, notification_subscribers.clone()).await; - Self { - inner: tokio::sync::RwLock::new(inner), - server_info, - params: tokio::sync::RwLock::new(params), - step_up_lock: tokio::sync::Mutex::new(()), - notification_subscribers, - } - } - - async fn forward_notifications( - client: &McpClient, - subscribers: Arc>>>, - ) { - let mut receiver = client.subscribe().await; - tokio::spawn(async move { - while let Some(notification) = receiver.recv().await { - let mut subscribers = subscribers.lock().await; - subscribers.retain(|subscriber| subscriber.try_send(notification.clone()).is_ok()); - } - }); - } - - async fn step_up_reconnect( - &self, - challenge: String, - ) -> Result<(), crate::agents::mcp_client::Error> { - let params = self.params.read().await; - let auth_manager = oauth_flow_with_challenge( - ¶ms.uri, - ¶ms.name, - params.static_oauth_client.as_ref(), - Some(challenge), - ) - .await - .map_err(|e| { - crate::agents::mcp_client::Error::McpError(ErrorData::new( - ErrorCode::INTERNAL_ERROR, - format!("step-up authorization failed: {e}"), - None, - )) - })?; - let client = connect_with_auth( - auth_manager, - params.action_required.clone(), - ¶ms.uri, - params.timeout, - ¶ms.headers, - params.provider.clone(), - params.client_name.clone(), - params.capabilities.clone(), - ¶ms.roots_dir, - params.extension_manager.clone(), - ) - .await - .map_err(|e| { - crate::agents::mcp_client::Error::McpError(ErrorData::new( - ErrorCode::INTERNAL_ERROR, - format!("reconnect after step-up authorization failed: {e}"), - None, - )) - })?; - Self::forward_notifications(&client, self.notification_subscribers.clone()).await; - *self.inner.write().await = client; - Ok(()) - } - - /// Run `op` against the current client; on an auth challenge, re-authorize - /// and retry once. - async fn with_step_up_retry(&self, op: F) -> Result - where - F: for<'a> Fn( - &'a McpClient, - ) -> Pin< - Box< - dyn std::future::Future> - + Send - + 'a, - >, - >, - { - let first = { - let client = self.inner.read().await; - op(&client).await - }; - match first { - Err(err) => { - if let Some(challenge) = auth_challenge_from_service_error(&err) { - let _step_up_guard = self.step_up_lock.lock().await; - let retry = { - let client = self.inner.read().await; - op(&client).await - }; - match retry { - Ok(value) => Ok(value), - Err(retry_err) - if auth_challenge_from_service_error(&retry_err).is_some() => - { - self.step_up_reconnect(challenge).await?; - let client = self.inner.read().await; - op(&client).await - } - Err(retry_err) => Err(retry_err), - } - } else { - Err(err) - } - } - ok => ok, - } - } -} - -#[async_trait::async_trait] -impl McpClientTrait for OAuthStepUpClient { - async fn list_tools( - &self, - session_id: &str, - next_cursor: Option, - cancel_token: CancellationToken, - ) -> Result { - let session_id = session_id.to_string(); - self.with_step_up_retry(move |client| { - let session_id = session_id.clone(); - let next_cursor = next_cursor.clone(); - let cancel_token = cancel_token.clone(); - Box::pin(async move { - client - .list_tools(&session_id, next_cursor, cancel_token) - .await - }) - }) - .await - } - - async fn call_tool( - &self, - ctx: &ToolCallContext, - name: &str, - arguments: Option, - cancel_token: CancellationToken, - ) -> Result { - let ctx = ctx.clone(); - let name = name.to_string(); - self.with_step_up_retry(move |client| { - let ctx = ctx.clone(); - let name = name.clone(); - let arguments = arguments.clone(); - let cancel_token = cancel_token.clone(); - Box::pin(async move { client.call_tool(&ctx, &name, arguments, cancel_token).await }) - }) - .await - } - - fn get_info(&self) -> Option<&ServerInfo> { - self.server_info.as_ref() - } - - async fn list_resources( - &self, - session_id: &str, - next_cursor: Option, - cancel_token: CancellationToken, - ) -> Result { - let session_id = session_id.to_string(); - self.with_step_up_retry(move |client| { - let session_id = session_id.clone(); - let next_cursor = next_cursor.clone(); - let cancel_token = cancel_token.clone(); - Box::pin(async move { - client - .list_resources(&session_id, next_cursor, cancel_token) - .await - }) - }) - .await - } - - async fn read_resource( - &self, - session_id: &str, - uri: &str, - cancel_token: CancellationToken, - ) -> Result { - let session_id = session_id.to_string(); - let uri = uri.to_string(); - self.with_step_up_retry(move |client| { - let session_id = session_id.clone(); - let uri = uri.clone(); - let cancel_token = cancel_token.clone(); - Box::pin(async move { client.read_resource(&session_id, &uri, cancel_token).await }) - }) - .await - } - - async fn list_prompts( - &self, - session_id: &str, - next_cursor: Option, - cancel_token: CancellationToken, - ) -> Result { - let session_id = session_id.to_string(); - self.with_step_up_retry(move |client| { - let session_id = session_id.clone(); - let next_cursor = next_cursor.clone(); - let cancel_token = cancel_token.clone(); - Box::pin(async move { - client - .list_prompts(&session_id, next_cursor, cancel_token) - .await - }) - }) - .await - } - - async fn get_prompt( - &self, - session_id: &str, - name: &str, - arguments: Value, - cancel_token: CancellationToken, - ) -> Result { - let session_id = session_id.to_string(); - let name = name.to_string(); - self.with_step_up_retry(move |client| { - let session_id = session_id.clone(); - let name = name.clone(); - let arguments = arguments.clone(); - let cancel_token = cancel_token.clone(); - Box::pin(async move { - client - .get_prompt(&session_id, &name, arguments, cancel_token) - .await - }) - }) - .await - } - - async fn subscribe(&self) -> tokio::sync::mpsc::Receiver { - let (sender, receiver) = mpsc::channel(32); - self.notification_subscribers.lock().await.push(sender); - receiver - } - - async fn get_moim(&self, session_id: &str) -> Option { - self.inner.read().await.get_moim(session_id).await - } - - async fn update_working_dir( - &self, - new_dir: PathBuf, - ) -> Result<(), crate::agents::mcp_client::Error> { - self.params.write().await.roots_dir = new_dir.clone(); - self.inner.read().await.update_working_dir(new_dir).await - } -} - -#[allow(clippy::too_many_arguments)] -async fn create_streamable_http_client( - uri: &str, - timeout: Option, - headers: &HashMap, - name: &str, - socket: Option<&str>, - static_oauth_client: Option, - credential_store: Box, - provider: SharedProvider, - client_name: String, - capabilities: GooseMcpClientCapabilities, - roots_dir: &std::path::Path, - action_required: Arc, - extension_manager: Weak, -) -> ExtensionResult> { - #[cfg(unix)] - if let Some(socket_path) = socket { - return create_unix_socket_http_client( - uri, - timeout, - headers, - name, - socket_path, - provider, - client_name, - capabilities, - roots_dir, - action_required, - extension_manager, - ) - .await; - } - #[cfg(not(unix))] - if socket.is_some() { - return Err(ExtensionError::ConfigError( - "Unix domain socket transport is not supported on this platform".to_string(), - )); - } - - let mut default_headers = HeaderMap::new(); - - default_headers.insert(reqwest::header::USER_AGENT, GOOSE_USER_AGENT); - - for (key, value) in headers { - default_headers.insert( - HeaderName::try_from(key) - .map_err(|_| ExtensionError::ConfigError(format!("invalid header: {}", key)))?, - value.parse().map_err(|_| { - ExtensionError::ConfigError(format!("invalid header value: {}", key)) - })?, - ); - } - - let timeout_duration = Duration::from_secs(resolve_timeout(timeout)); - - #[allow(unused_mut)] - let mut http_client_builder = reqwest::Client::builder().default_headers(default_headers); - #[cfg(target_os = "linux")] - { - http_client_builder = http_client_builder.tcp_user_timeout(Some(timeout_duration)); - } - let http_client = http_client_builder - .build() - .map_err(|_| ExtensionError::ConfigError("could not construct http client".to_string()))?; - - let transport = StreamableHttpClientTransport::with_client( - http_client.clone(), - StreamableHttpClientTransportConfig::with_uri(uri), - ); - - let connect_params = StreamableHttpConnectParams { - uri: uri.to_string(), - name: name.to_string(), - timeout: timeout_duration, - headers: headers.clone(), - provider: provider.clone(), - client_name: client_name.clone(), - capabilities: capabilities.clone(), - roots_dir: roots_dir.to_path_buf(), - action_required: action_required.clone(), - extension_manager: extension_manager.clone(), - static_oauth_client: static_oauth_client.clone(), - }; - - // If we have stored OAuth credentials, try refreshing and connecting directly. - // This avoids the unnecessary 401 → browser re-auth cycle on every new session. - if credential_store.load().await.is_ok_and(|c| c.is_some()) { - match oauth_flow( - &uri.to_string(), - &name.to_string(), - static_oauth_client.as_ref(), - ) - .await - { - Ok(auth_manager) => { - let auth_result = connect_with_auth( - auth_manager, - action_required.clone(), - uri, - timeout_duration, - headers, - provider.clone(), - client_name.clone(), - capabilities.clone(), - roots_dir, - extension_manager.clone(), - ) - .await; - - if let Err(error) = &auth_result { - if clear_credentials_on_post_refresh_auth_failure( - credential_store.as_ref(), - name, - error, - ) - .await - { - warn!( - "[OAuth:{}] Refreshed token was rejected, falling back to browser auth", - name - ); - } else { - return Ok(Box::new( - OAuthStepUpClient::new(auth_result?, connect_params).await, - )); - } - } else { - return Ok(Box::new( - OAuthStepUpClient::new(auth_result?, connect_params).await, - )); - } - } - Err(e) => { - warn!( - "[OAuth:{}] Proactive refresh failed: {}, falling back to unauthenticated attempt", - name, e - ); - } - } - } - - let mut client_res = McpClient::connect( - transport, - timeout_duration, - provider.clone(), - client_name.clone(), - capabilities.clone(), - roots_dir.to_path_buf(), - action_required.clone(), - extension_manager.clone(), - ) - .await; - - // TODO: Remove these compatibility retries, including the authenticated path in - // connect_with_auth, once rmcp handles empty discovery SSE responses upstream. - if should_retry_legacy_after_empty_discover(&client_res, &capabilities) { - let transport = StreamableHttpClientTransport::with_client( - http_client, - StreamableHttpClientTransportConfig::with_uri(uri), - ); - let mut legacy_capabilities = capabilities.clone(); - legacy_capabilities.protocol_version = Some(ProtocolVersion::V_2025_11_25); - client_res = McpClient::connect( - transport, - timeout_duration, - provider.clone(), - client_name.clone(), - legacy_capabilities, - roots_dir.to_path_buf(), - action_required.clone(), - extension_manager.clone(), - ) - .await; - } - - if should_attempt_oauth_fallback(&client_res) { - let challenge = auth_challenge_from_result(&client_res); - match oauth_flow_with_challenge( - &uri.to_string(), - &name.to_string(), - static_oauth_client.as_ref(), - challenge, - ) - .await - { - Ok(auth_manager) => { - let client = connect_with_auth( - auth_manager, - action_required, - uri, - timeout_duration, - headers, - provider, - client_name, - capabilities, - roots_dir, - extension_manager, - ) - .await?; - Ok(Box::new( - OAuthStepUpClient::new(client, connect_params).await, - )) - } - Err(e) => { - warn!( - "[OAuth:{}] Browser authorization flow failed: {:#}", - name, e - ); - Ok(Box::new(client_res?)) - } - } - } else { - Ok(Box::new( - OAuthStepUpClient::new(client_res?, connect_params).await, - )) - } -} - -#[cfg(unix)] -#[allow(clippy::too_many_arguments)] -async fn create_unix_socket_http_client( - uri: &str, - timeout: Option, - headers: &HashMap, - name: &str, - socket_path: &str, - provider: SharedProvider, - client_name: String, - capabilities: GooseMcpClientCapabilities, - roots_dir: &std::path::Path, - action_required: Arc, - extension_manager: Weak, -) -> ExtensionResult> { - use rmcp::transport::UnixSocketHttpClient; - - let unix_client = UnixSocketHttpClient::new(socket_path, uri); - - let mut custom_headers = std::collections::HashMap::::new(); - - custom_headers.insert( - HeaderName::from_static("user-agent"), - GOOSE_USER_AGENT - .to_str() - .unwrap_or("goose") - .parse() - .unwrap_or_else(|_| HeaderValue::from_static("goose")), - ); - - for (key, value) in headers { - let header_name = HeaderName::try_from(key) - .map_err(|_| ExtensionError::ConfigError(format!("invalid header: {}", key)))?; - let header_value = value - .parse::() - .map_err(|_| ExtensionError::ConfigError(format!("invalid header value: {}", key)))?; - custom_headers.insert(header_name, header_value); - } - - let config = - StreamableHttpClientTransportConfig::with_uri(uri).custom_headers(custom_headers.clone()); - let transport = StreamableHttpClientTransport::with_client(unix_client.clone(), config); - - let timeout_duration = Duration::from_secs(resolve_timeout(timeout)); - - let mut client_res = McpClient::connect( - transport, - timeout_duration, - provider.clone(), - client_name.clone(), - capabilities.clone(), - roots_dir.to_path_buf(), - action_required.clone(), - extension_manager.clone(), - ) - .await; - - if should_retry_legacy_after_empty_discover(&client_res, &capabilities) { - let config = - StreamableHttpClientTransportConfig::with_uri(uri).custom_headers(custom_headers); - let transport = StreamableHttpClientTransport::with_client(unix_client, config); - let mut legacy_capabilities = capabilities; - legacy_capabilities.protocol_version = Some(ProtocolVersion::V_2025_11_25); - client_res = McpClient::connect( - transport, - timeout_duration, - provider.clone(), - client_name.clone(), - legacy_capabilities, - roots_dir.to_path_buf(), - action_required, - extension_manager, - ) - .await; - } - - if should_attempt_oauth_fallback(&client_res) { - tracing::warn!( - "Extension '{}' returned 401 over Unix domain socket transport; \ - OAuth is not supported for UDS connections", - name, - ); - } - Ok(Box::new(client_res?)) -} - impl ExtensionManager { fn mcp_client_capabilities(&self) -> GooseMcpClientCapabilities { GooseMcpClientCapabilities { @@ -1520,7 +467,6 @@ impl ExtensionManager { /// Add an extension with an optional working directory. /// If working_dir is None, falls back to current_dir. - #[allow(clippy::too_many_lines)] pub async fn add_extension( self: &Arc, config: ExtensionConfig, @@ -1546,203 +492,93 @@ impl ExtensionManager { ); } - let effective_working_dir = working_dir - .clone() + let working_dir = working_dir .or_else(|| std::env::var("GOOSE_WORKING_DIR").ok().map(PathBuf::from)) .unwrap_or_else(|| std::env::current_dir().unwrap_or_default()); + let ctx = |timeout: Option, working_dir: PathBuf| ConnectContext { + timeout: Duration::from_secs(resolve_timeout(timeout)), + provider: self.provider.clone(), + client_name: self.client_name.clone(), + capabilities: self.mcp_client_capabilities(), + working_dir, + docker_container: None, + action_required: self.context.session_manager.action_required(), + extension_manager: Arc::downgrade(self), + }; - let client: Box = match &config { + let client: Box = match &resolved_config { ExtensionConfig::StreamableHttp { uri, timeout, headers, name, envs, - env_keys, socket, client_id, client_secret_key, scopes, .. } => { - let config = Config::global(); - let all_envs = merge_environments(envs, env_keys, &sanitized_name, config).await?; - let resolved_uri = substitute_env_vars(uri, &all_envs); - let resolved_headers = headers - .iter() - .map(|(k, v)| (k.clone(), substitute_env_vars(v, &all_envs))) - .collect(); - let resolved_socket = socket.as_ref().map(|s| substitute_env_vars(s, &all_envs)); - let static_oauth_client = resolve_static_oauth_client( + let static_oauth_client = streamable_http::resolve_static_oauth_client( client_id.as_deref(), client_secret_key.as_deref(), scopes, - &all_envs, - config, - ) - .map_err(|error| *error)?; - create_streamable_http_client( - &resolved_uri, - *timeout, - &resolved_headers, - name, - resolved_socket.as_deref(), + &envs.get_env(), + )?; + let params = streamable_http::ConnectParams { + uri: uri.clone(), + name: name.clone(), + headers: headers.clone(), static_oauth_client, - Box::new(GooseCredentialStore::new(name.to_string())), - self.provider.clone(), - self.client_name.clone(), - self.mcp_client_capabilities(), - &effective_working_dir, - self.context.session_manager.action_required(), - Arc::downgrade(self), + ctx: ctx(*timeout, working_dir), + }; + streamable_http::connect( + params, + socket.as_deref(), + Box::new(GooseCredentialStore::new(name.clone())), ) .await? } - ExtensionConfig::Builtin { ref name, .. } - | ExtensionConfig::Platform { ref name, .. } => { - let timeout = if let ExtensionConfig::Builtin { timeout, .. } = &config { - *timeout - } else { - None - }; - let normalized_name = name_to_key(name); - - if let Some(def) = PLATFORM_EXTENSIONS.get(normalized_name.as_str()) { - // Platform extension: create via in-process client factory - let mut context = self.context.clone(); - context.extension_manager = Some(Arc::downgrade(self)); - if let Some(id) = session_id { - if let Ok(session) = - self.context.session_manager.get_session(id, false).await - { - context.session = Some(Arc::new(session)); - } - } - // A platform extension the host cannot provide (no scheduler - // service, say) declines rather than registering with no tools. - let Some(client) = (def.client_factory)(context) else { - return Ok(()); - }; - client - } else { - // Builtin MCP server extension - let timeout_secs = resolve_timeout(timeout); - let extension_fn = - get_builtin_extension(normalized_name.as_str()).ok_or_else(|| { - ExtensionError::ConfigError(format!("Unknown extension: {}", name)) - })?; - - if let Some(container) = container { - let container_id = container.id(); - tracing::info!( - container = %container_id, - builtin = %name, - "Starting builtin extension inside Docker container" - ); - let command = Command::new("docker").configure(|command| { - command - .arg("exec") - .arg("-i") - .arg(container_id) - .arg("goose") - .arg("mcp") - .arg(&normalized_name); - }); - - let client = child_process_client( - command, - &Some(timeout_secs), - self.provider.clone(), - &effective_working_dir, - Some(container_id.to_string()), - self.client_name.clone(), - self.mcp_client_capabilities(), - self.context.session_manager.action_required(), - Arc::downgrade(self), - ) - .await?; - Box::new(client) - } else { - let (server_read, client_write) = tokio::io::duplex(65536); - let (client_read, server_write) = tokio::io::duplex(65536); - extension_fn(server_read, server_write); - - Box::new( - McpClient::connect( - (client_read, client_write), - Duration::from_secs(timeout_secs), - self.provider.clone(), - self.client_name.clone(), - self.mcp_client_capabilities(), - effective_working_dir.clone(), - self.context.session_manager.action_required(), - Arc::downgrade(self), - ) - .await?, - ) + ExtensionConfig::Builtin { name, .. } | ExtensionConfig::Platform { name, .. } + if PLATFORM_EXTENSIONS.contains_key(name_to_key(name).as_str()) => + { + let def = &PLATFORM_EXTENSIONS[name_to_key(name).as_str()]; + let mut context = self.context.clone(); + context.extension_manager = Some(Arc::downgrade(self)); + if let Some(id) = session_id { + if let Ok(session) = self.context.session_manager.get_session(id, false).await { + context.session = Some(Arc::new(session)); } } + // A platform extension the host cannot provide (no scheduler + // service, say) declines rather than registering with no tools. + let Some(client) = (def.client_factory)(context) else { + return Ok(()); + }; + client + } + ExtensionConfig::Builtin { name, timeout, .. } => { + builtin::connect(name, container, ctx(*timeout, working_dir)).await? + } + ExtensionConfig::Platform { name, .. } => { + builtin::connect(name, container, ctx(None, working_dir)).await? } ExtensionConfig::Stdio { cmd, args, envs, - env_keys, timeout, cwd, .. } => { - let config = Config::global(); - let mut all_envs = - merge_environments(envs, env_keys, &sanitized_name, config).await?; - let process_working_dir = cwd - .as_deref() - .map(PathBuf::from) - .unwrap_or_else(|| effective_working_dir.clone()); - + let mut envs = envs.get_env(); if let Some(sid) = session_id { - all_envs.insert("AGENT_SESSION_ID".to_string(), sid.to_string()); + envs.insert("AGENT_SESSION_ID".to_string(), sid.to_string()); } - - // Check for malicious packages before launching the process - extension_malware_check::deny_if_malicious_cmd_args(cmd, args).await?; - - let command = if let Some(container) = container { - let container_id = container.id(); - tracing::info!( - container = %container_id, - cmd = %cmd, - "Starting stdio extension inside Docker container" - ); - Command::new("docker").configure(|command| { - command.arg("exec").arg("-i"); - for (key, value) in &all_envs { - command.arg("-e").arg(format!("{}={}", key, value)); - } - command.arg(container_id); - command.arg(cmd); - command.args(args); - }) - } else { - let cmd = resolve_command(cmd); - Command::new(cmd).configure(|command| { - command.args(args).envs(all_envs); - }) - }; - - let client = child_process_client( - command, - timeout, - self.provider.clone(), - &process_working_dir, - container.map(|c| c.id().to_string()), - self.client_name.clone(), - self.mcp_client_capabilities(), - self.context.session_manager.action_required(), - Arc::downgrade(self), + let working_dir = cwd.as_deref().map(PathBuf::from).unwrap_or(working_dir); + Box::new( + stdio::connect(cmd, args, envs, container, ctx(*timeout, working_dir)).await?, ) - .await?; - Box::new(client) } }; @@ -2679,70 +1515,6 @@ impl ExtensionManager { .map_err(|e| anyhow::anyhow!("Failed to get prompt: {}", e)) } - pub async fn search_available_extensions(&self) -> Result, ErrorData> { - let mut output_parts = vec![]; - - // First get disabled extensions from current config (skip hidden ones) - let mut disabled_extensions: Vec = vec![]; - for extension in get_all_extensions() { - if !extension.enabled && !is_hidden_extension(&extension.config.name()) { - let config = extension.config.clone(); - let description = match &config { - ExtensionConfig::Builtin { - description, - display_name, - .. - } => { - if description.is_empty() { - display_name.as_deref().unwrap_or("Built-in extension") - } else { - description - } - } - ExtensionConfig::Platform { description, .. } - | ExtensionConfig::StreamableHttp { description, .. } - | ExtensionConfig::Stdio { description, .. } => description, - }; - disabled_extensions.push(format!("- {} - {}", config.name(), description)); - } - } - - // Get currently enabled extensions that can be disabled (skip hidden ones) - let enabled_extensions: Vec = self - .extensions - .lock() - .await - .keys() - .filter(|name| !is_hidden_extension(name)) - .cloned() - .collect(); - - // Build output string - if !disabled_extensions.is_empty() { - output_parts.push(format!( - "Extensions available to enable:\n{}\n", - disabled_extensions.join("\n") - )); - } else { - output_parts.push("No extensions available to enable.\n".to_string()); - } - - if !enabled_extensions.is_empty() { - output_parts.push(format!( - "\n\nExtensions available to disable:\n{}\n", - enabled_extensions - .iter() - .map(|name| format!("- {}", name)) - .collect::>() - .join("\n") - )); - } else { - output_parts.push("No extensions that can be disabled.\n".to_string()); - } - - Ok(vec![ContentBlock::text(output_parts.join("\n"))]) - } - async fn get_server_client(&self, name: impl Into) -> Option { let normalized = name_to_key(&name.into()); self.extensions @@ -2792,160 +1564,6 @@ impl ExtensionManager { mod tests { use super::*; use rmcp::model::CallToolResult; - - mod static_oauth_client { - use super::*; - - fn test_config(dir: &tempfile::TempDir) -> Config { - Config::new_with_file_secrets( - dir.path().join("config.yaml"), - dir.path().join("secrets.yaml"), - ) - .unwrap() - } - - #[test] - fn absent_client_id_yields_no_static_client() { - let dir = tempfile::tempdir().unwrap(); - let config = test_config(&dir); - - let resolved = - resolve_static_oauth_client(None, None, &[], &HashMap::new(), &config).unwrap(); - - assert_eq!(resolved, None); - } - - #[test] - fn client_id_without_secret_resolves_public_client() { - let dir = tempfile::tempdir().unwrap(); - let config = test_config(&dir); - - let resolved = resolve_static_oauth_client( - Some("registered-client"), - None, - &["scope.read".to_string()], - &HashMap::new(), - &config, - ) - .unwrap() - .unwrap(); - - assert_eq!(resolved.client_id, "registered-client"); - assert_eq!(resolved.client_secret, None); - assert_eq!(resolved.scopes, vec!["scope.read"]); - } - - #[test] - fn client_id_supports_env_substitution() { - let dir = tempfile::tempdir().unwrap(); - let config = test_config(&dir); - let envs = HashMap::from([( - "OAUTH_CLIENT_ID".to_string(), - "registered-client".to_string(), - )]); - - let resolved = - resolve_static_oauth_client(Some("${OAUTH_CLIENT_ID}"), None, &[], &envs, &config) - .unwrap() - .unwrap(); - - assert_eq!(resolved.client_id, "registered-client"); - } - - #[test] - fn client_secret_resolves_from_envs() { - let dir = tempfile::tempdir().unwrap(); - let config = test_config(&dir); - let envs = HashMap::from([( - "OAUTH_CLIENT_SECRET".to_string(), - "secret-value".to_string(), - )]); - - let resolved = resolve_static_oauth_client( - Some("registered-client"), - Some("OAUTH_CLIENT_SECRET"), - &[], - &envs, - &config, - ) - .unwrap() - .unwrap(); - - assert_eq!(resolved.client_secret.as_deref(), Some("secret-value")); - } - - #[test] - fn client_secret_falls_back_to_config_secret_store() { - let dir = tempfile::tempdir().unwrap(); - let config = test_config(&dir); - config - .set("OAUTH_CLIENT_SECRET", &"stored-secret", true) - .unwrap(); - - let resolved = resolve_static_oauth_client( - Some("registered-client"), - Some("OAUTH_CLIENT_SECRET"), - &[], - &HashMap::new(), - &config, - ) - .unwrap() - .unwrap(); - - assert_eq!(resolved.client_secret.as_deref(), Some("stored-secret")); - } - - #[test] - fn client_secret_key_without_client_id_is_rejected() { - let dir = tempfile::tempdir().unwrap(); - let config = test_config(&dir); - - let error = resolve_static_oauth_client( - None, - Some("OAUTH_CLIENT_SECRET"), - &[], - &HashMap::new(), - &config, - ) - .unwrap_err(); - - assert!(matches!(*error, ExtensionError::ConfigError(_))); - } - - #[test] - fn scopes_without_client_id_are_rejected() { - let dir = tempfile::tempdir().unwrap(); - let config = test_config(&dir); - - let error = resolve_static_oauth_client( - None, - None, - &["scope.read".to_string()], - &HashMap::new(), - &config, - ) - .unwrap_err(); - - assert!(matches!(*error, ExtensionError::ConfigError(_))); - } - - #[test] - fn missing_client_secret_key_is_an_error() { - let dir = tempfile::tempdir().unwrap(); - let config = test_config(&dir); - - let error = resolve_static_oauth_client( - Some("registered-client"), - Some("MISSING_KEY"), - &[], - &HashMap::new(), - &config, - ) - .unwrap_err(); - - assert!(matches!(*error, ExtensionError::ConfigError(_))); - } - } use rmcp::model::{CustomNotification, InitializeResult, JsonObject}; use rmcp::{object, ServiceError as Error}; @@ -3454,54 +2072,6 @@ mod tests { assert!(result.is_ok()); } - #[tokio::test] - async fn test_streamable_http_header_env_substitution() { - let mut env_map = HashMap::new(); - env_map.insert("AUTH_TOKEN".to_string(), "secret123".to_string()); - env_map.insert("API_KEY".to_string(), "key456".to_string()); - - // Test ${VAR} syntax - let result = substitute_env_vars("Bearer ${ AUTH_TOKEN }", &env_map); - assert_eq!(result, "Bearer secret123"); - - // Test ${VAR} syntax without spaces - let result = substitute_env_vars("Bearer ${AUTH_TOKEN}", &env_map); - assert_eq!(result, "Bearer secret123"); - - // Test $VAR syntax - let result = substitute_env_vars("Bearer $AUTH_TOKEN", &env_map); - assert_eq!(result, "Bearer secret123"); - - // Test multiple substitutions - let result = substitute_env_vars("Key: $API_KEY, Token: ${AUTH_TOKEN}", &env_map); - assert_eq!(result, "Key: key456, Token: secret123"); - - // Test no substitution when variable doesn't exist - let result = substitute_env_vars("Bearer ${UNKNOWN_VAR}", &env_map); - assert_eq!(result, "Bearer ${UNKNOWN_VAR}"); - - // Test mixed content - let result = substitute_env_vars( - "Authorization: Bearer ${AUTH_TOKEN} and API ${API_KEY}", - &env_map, - ); - assert_eq!(result, "Authorization: Bearer secret123 and API key456"); - } - - #[tokio::test] - async fn test_substitute_env_vars_no_recursive_expansion() { - let mut env_map = HashMap::new(); - env_map.insert("TOKEN".to_string(), "abc$KEY".to_string()); - env_map.insert("KEY".to_string(), "xyz".to_string()); - - // A substituted value containing $KEY should NOT be re-expanded - let result = substitute_env_vars("${TOKEN}", &env_map); - assert_eq!(result, "abc$KEY"); - - let result = substitute_env_vars("$TOKEN", &env_map); - assert_eq!(result, "abc$KEY"); - } - #[tokio::test] async fn test_tools_cache_invalidated_on_add_extension() { let temp_dir = tempfile::tempdir().unwrap(); @@ -4210,302 +2780,4 @@ mod tests { "old extension must be preserved when replacement client creation fails" ); } - - fn transport_err(error: Box) -> ClientInitializeError { - ClientInitializeError::TransportError { - error: rmcp::transport::DynamicTransportError::from_parts( - "test", - std::any::TypeId::of::<()>(), - error, - ), - context: "test context".into(), - } - } - - fn streamable_err( - e: rmcp::transport::streamable_http_client::StreamableHttpError, - ) -> ClientInitializeError { - transport_err(Box::new(e)) - } - - #[test] - fn test_oauth_fallback_on_typed_auth_required() { - let err = streamable_err( - rmcp::transport::streamable_http_client::StreamableHttpError::AuthRequired( - rmcp::transport::streamable_http_client::AuthRequiredError::new( - "Bearer realm=\"test\"".to_string(), - ), - ), - ); - assert!(should_attempt_oauth_fallback(&Err(err))); - } - - #[test] - fn test_oauth_fallback_on_unexpected_response_http_401_prefix() { - let err = streamable_err( - rmcp::transport::streamable_http_client::StreamableHttpError::UnexpectedServerResponse( - std::borrow::Cow::Borrowed("HTTP 401 Unauthorized"), - ), - ); - assert!(should_attempt_oauth_fallback(&Err(err))); - } - - #[tokio::test] - async fn test_post_refresh_auth_failure_clears_credentials() { - use rmcp::transport::auth::{ - InMemoryCredentialStore, OAuthTokenResponse, StoredCredentials, - }; - - let token_response: OAuthTokenResponse = serde_json::from_value(serde_json::json!({ - "access_token": "rejected-token", - "token_type": "bearer", - })) - .expect("valid fake token JSON"); - let store = InMemoryCredentialStore::new(); - store - .save(StoredCredentials::new( - "test-client".to_string(), - Some(token_response), - vec![], - None, - )) - .await - .unwrap(); - - let err = streamable_err( - rmcp::transport::streamable_http_client::StreamableHttpError::AuthRequired( - rmcp::transport::streamable_http_client::AuthRequiredError::new( - "Bearer error=\"invalid_token\"".to_string(), - ), - ), - ); - let error = ExtensionError::InitializeError(err); - - assert!(clear_credentials_on_post_refresh_auth_failure(&store, "test-ext", &error).await); - assert!(store.load().await.unwrap().is_none()); - } - - #[tokio::test] - async fn test_invalid_header_name_returns_config_error() { - let mut headers = HashMap::new(); - headers.insert("bad header name".to_string(), "value".to_string()); - - let temp_dir = tempdir().unwrap(); - let provider: SharedProvider = Arc::new(Mutex::new(None)); - let capabilities = GooseMcpClientCapabilities { - mcpui: false, - host_info: None, - elicitation_handler: None, - protocol_version: None, - }; - - let result = create_streamable_http_client( - "http://localhost:1", - None, - &headers, - "test-ext", - None, - None, - Box::new(rmcp::transport::auth::InMemoryCredentialStore::new()), - provider, - "goose-test".to_string(), - capabilities, - temp_dir.path(), - Arc::new(ActionRequiredManager::new()), - Weak::new(), - ) - .await; - - let Err(ExtensionError::ConfigError(msg)) = result else { - panic!("expected ConfigError, got a different result"); - }; - assert!( - msg.contains("invalid header"), - "unexpected error message: {msg}" - ); - } - - #[tokio::test] - async fn test_invalid_header_value_returns_config_error() { - let mut headers = HashMap::new(); - headers.insert("x-valid-name".to_string(), "bad\r\nvalue".to_string()); - - let temp_dir = tempdir().unwrap(); - let provider: SharedProvider = Arc::new(Mutex::new(None)); - let capabilities = GooseMcpClientCapabilities { - mcpui: false, - host_info: None, - elicitation_handler: None, - protocol_version: None, - }; - - let result = create_streamable_http_client( - "http://localhost:1", - None, - &headers, - "test-ext", - None, - None, - Box::new(rmcp::transport::auth::InMemoryCredentialStore::new()), - provider, - "goose-test".to_string(), - capabilities, - temp_dir.path(), - Arc::new(ActionRequiredManager::new()), - Weak::new(), - ) - .await; - - let Err(ExtensionError::ConfigError(msg)) = result else { - panic!("expected ConfigError, got a different result"); - }; - assert!( - msg.contains("invalid header value"), - "unexpected error message: {msg}" - ); - } - - #[tokio::test] - async fn test_custom_headers_forwarded_to_http_extension() { - use wiremock::matchers::any; - use wiremock::{Mock, MockServer, ResponseTemplate}; - - let mock_server = MockServer::start().await; - Mock::given(any()) - .respond_with(ResponseTemplate::new(200)) - .mount(&mock_server) - .await; - - let mut headers = HashMap::new(); - headers.insert("x-api-key".to_string(), "test-secret-123".to_string()); - - let temp_dir = tempdir().unwrap(); - let provider: SharedProvider = Arc::new(Mutex::new(None)); - let capabilities = GooseMcpClientCapabilities { - mcpui: false, - host_info: None, - elicitation_handler: None, - protocol_version: None, - }; - - // The MCP handshake will fail against the stub server. We only care that - // the outgoing HTTP request carried the custom header. - let _ = create_streamable_http_client( - &mock_server.uri(), - None, - &headers, - "test-ext", - None, - None, - Box::new(rmcp::transport::auth::InMemoryCredentialStore::new()), - provider, - "goose-test".to_string(), - capabilities, - temp_dir.path(), - Arc::new(ActionRequiredManager::new()), - Weak::new(), - ) - .await; - - let received = mock_server.received_requests().await.unwrap(); - assert!( - !received.is_empty(), - "expected at least one HTTP request to reach the mock server" - ); - let header_found = received.iter().any(|req| { - req.headers - .get("x-api-key") - .map(|v| v == "test-secret-123") - .unwrap_or(false) - }); - assert!( - header_found, - "custom header x-api-key was not forwarded to the extension server" - ); - } - - /// Directly exercises `connect_with_auth`, which is the code path fixed by - /// the PR (custom headers were dropped when the OAuth connection path was - /// taken). Uses a pre-seeded `InMemoryCredentialStore` with a fake, - /// non-expiring token so `get_access_token()` returns immediately without - /// touching any OAuth endpoints or the system keychain. - #[tokio::test] - async fn test_custom_headers_forwarded_oauth_path() { - use rmcp::transport::auth::{ - InMemoryCredentialStore, OAuthTokenResponse, StoredCredentials, - }; - use wiremock::matchers::any; - use wiremock::{Mock, MockServer, ResponseTemplate}; - - let mock_server = MockServer::start().await; - Mock::given(any()) - .respond_with(ResponseTemplate::new(200)) - .mount(&mock_server) - .await; - - let mut headers = HashMap::new(); - headers.insert("x-api-key".to_string(), "test-secret-oauth".to_string()); - - // Build a fake, non-expiring token. token_received_at=None skips the - // expiry check, so get_access_token() returns without any network call. - let token_response: OAuthTokenResponse = serde_json::from_value(serde_json::json!({ - "access_token": "fake-test-token", - "token_type": "bearer", - })) - .expect("valid fake token JSON"); - let creds = StoredCredentials::new( - "test-client".to_string(), - Some(token_response), - vec![], - None, - ); - let store = InMemoryCredentialStore::new(); - store.save(creds).await.unwrap(); - - let mut auth_manager = rmcp::transport::AuthorizationManager::new(mock_server.uri()) - .await - .expect("AuthorizationManager::new should not make network calls"); - auth_manager.set_credential_store(store); - - let temp_dir = tempdir().unwrap(); - let provider: SharedProvider = Arc::new(Mutex::new(None)); - let capabilities = GooseMcpClientCapabilities { - mcpui: false, - host_info: None, - elicitation_handler: None, - protocol_version: None, - }; - - // connect_with_auth will fail (mock server isn't an MCP server) but we - // only care that the outgoing request carried the custom header. - let _ = connect_with_auth( - auth_manager, - Arc::new(ActionRequiredManager::new()), - &mock_server.uri(), - Duration::from_secs(5), - &headers, - provider, - "goose-test".to_string(), - capabilities, - temp_dir.path(), - Weak::new(), - ) - .await; - - let received = mock_server.received_requests().await.unwrap(); - assert!( - !received.is_empty(), - "expected at least one HTTP request to reach the mock server" - ); - let header_found = received.iter().any(|req| { - req.headers - .get("x-api-key") - .map(|v| v == "test-secret-oauth") - .unwrap_or(false) - }); - assert!( - header_found, - "custom header x-api-key was not forwarded through the OAuth connection path" - ); - } } diff --git a/crates/goose/src/agents/extension_manager/stdio.rs b/crates/goose/src/agents/extension_manager/stdio.rs new file mode 100644 index 000000000..73daaec70 --- /dev/null +++ b/crates/goose/src/agents/extension_manager/stdio.rs @@ -0,0 +1,101 @@ +use std::collections::HashMap; + +use rmcp::transport::ConfigureCommandExt; +use tokio::io::AsyncReadExt; +use tokio::process::Command; + +use super::super::container::Container; +use super::super::extension::{ExtensionError, ExtensionResult, ProcessExit}; +use super::super::extension_malware_check; +use super::super::mcp_client::{ConnectContext, McpClient}; +use crate::config::search_path::SearchPaths; +use crate::subprocess::spawn_long_lived_mcp_subprocess; + +pub(super) async fn connect( + cmd: &str, + args: &[String], + envs: HashMap, + container: Option<&Container>, + mut ctx: ConnectContext, +) -> ExtensionResult { + extension_malware_check::deny_if_malicious_cmd_args(cmd, args).await?; + + let command = match container { + Some(container) => { + ctx.docker_container = Some(container.id().to_string()); + tracing::info!( + container = %container.id(), + cmd = %cmd, + "Starting stdio extension inside Docker container" + ); + docker_exec(container, envs, cmd, args) + } + None => Command::new(resolve_command(cmd)).configure(|command| { + command.args(args).envs(envs); + }), + }; + spawn(command, ctx).await +} + +pub(super) fn docker_exec( + container: &Container, + envs: HashMap, + program: &str, + args: &[String], +) -> Command { + Command::new("docker").configure(|command| { + command.arg("exec").arg("-i"); + for (key, value) in envs { + command.arg("-e").arg(format!("{}={}", key, value)); + } + command.arg(container.id()).arg(program).args(args); + }) +} + +fn resolve_command(cmd: &str) -> std::path::PathBuf { + SearchPaths::builder() + .with_npm() + .resolve(cmd) + .unwrap_or_else(|_| { + // let the OS raise the error + std::path::PathBuf::from(cmd) + }) +} + +pub(super) async fn spawn(mut command: Command, ctx: ConnectContext) -> ExtensionResult { + if let Ok(path) = SearchPaths::builder().path() { + command.env("PATH", path); + } + + if ctx.working_dir.is_dir() { + tracing::info!( + "Setting MCP process working directory: {:?}", + ctx.working_dir + ); + command.current_dir(&ctx.working_dir); + } else { + tracing::warn!( + "Working directory doesn't exist or isn't a directory: {:?}", + ctx.working_dir + ); + } + + let (transport, mut stderr) = spawn_long_lived_mcp_subprocess(command).await?; + let mut stderr = stderr.take().ok_or_else(|| { + ExtensionError::SetupError("failed to attach child process stderr".to_owned()) + })?; + + let stderr_task = tokio::spawn(async move { + let mut all_stderr = Vec::new(); + stderr.read_to_end(&mut all_stderr).await?; + Ok::(String::from_utf8_lossy(&all_stderr).into()) + }); + + match McpClient::connect(transport, ctx).await { + Ok(client) => Ok(client), + Err(error) => Err(match stderr_task.await? { + Ok(stderr_content) => ProcessExit::new(stderr_content, error).into(), + Err(e) => e.into(), + }), + } +} diff --git a/crates/goose/src/agents/extension_manager/streamable_http.rs b/crates/goose/src/agents/extension_manager/streamable_http.rs new file mode 100644 index 000000000..7c98c66f5 --- /dev/null +++ b/crates/goose/src/agents/extension_manager/streamable_http.rs @@ -0,0 +1,1041 @@ +use std::collections::HashMap; +use std::path::PathBuf; +use std::pin::Pin; +use std::sync::Arc; +use std::time::Duration; + +use axum::http::{HeaderMap, HeaderName}; +use rmcp::model::{ + CallToolResult, ErrorCode, ErrorData, GetPromptResult, ProtocolVersion, ServerInfo, + ServerNotification, +}; +use rmcp::service::{ClientInitializeError, ServiceError}; +use rmcp::transport::auth::{AuthClient, CredentialStore}; +use rmcp::transport::streamable_http_client::{ + StreamableHttpClientTransportConfig, StreamableHttpError, +}; +use rmcp::transport::{DynamicTransportError, IntoTransport, StreamableHttpClientTransport}; +use rmcp::RoleClient; +use serde_json::Value; +use tokio::sync::{mpsc, Mutex}; +use tokio_util::sync::CancellationToken; +use tracing::warn; + +use super::super::extension::{ExtensionError, ExtensionResult}; +use super::super::mcp_client::{ConnectContext, McpClient, McpClientTrait}; +use super::super::tool_execution::ToolCallContext; +use crate::oauth::{oauth_flow, oauth_flow_with_challenge, StaticOAuthClientConfig}; + +/// Retry with OAuth for typed auth challenges and wrapped bare HTTP 401 responses. +fn is_oauth_auth_failure(err: &ClientInitializeError) -> bool { + let ClientInitializeError::TransportError { + error: DynamicTransportError { error, .. }, + .. + } = err + else { + return false; + }; + + if let Some(http_err) = error.downcast_ref::>() { + return match http_err { + StreamableHttpError::AuthRequired(_) | StreamableHttpError::InsufficientScope(_) => { + true + } + StreamableHttpError::UnexpectedServerResponse(body) => { + body.starts_with("HTTP 401") || body.starts_with("HTTP 403") + } + _ => false, + }; + } + + #[cfg(unix)] + if let Some(http_err) = error + .downcast_ref::>( + ) + { + return match http_err { + StreamableHttpError::AuthRequired(_) | StreamableHttpError::InsufficientScope(_) => { + true + } + StreamableHttpError::UnexpectedServerResponse(body) => { + body.starts_with("HTTP 401") || body.starts_with("HTTP 403") + } + _ => false, + }; + } + + let message = error.to_string(); + message.contains("unexpected server response: HTTP 401") + || message.contains("unexpected server response: HTTP 403") + || message.contains("Auth required") + || message.contains("Authorization required") +} + +fn should_attempt_oauth_fallback(res: &Result) -> bool { + res.as_ref().err().is_some_and(is_oauth_auth_failure) +} + +/// Extract the `WWW-Authenticate` challenge from a failed initialization, so +/// OAuth discovery can be seeded from the server's 401/403 response instead of +/// probing well-known locations. +fn auth_challenge_from_error(err: &ClientInitializeError) -> Option { + let ClientInitializeError::TransportError { + error: DynamicTransportError { error, .. }, + .. + } = err + else { + return None; + }; + + if let Some(http_err) = error.downcast_ref::>() { + return http_err.auth_challenge().map(str::to_string); + } + + #[cfg(unix)] + if let Some(http_err) = error + .downcast_ref::>( + ) + { + return http_err.auth_challenge().map(str::to_string); + } + + None +} + +fn auth_challenge_from_result(res: &Result) -> Option { + res.as_ref().err().and_then(auth_challenge_from_error) +} + +/// Extract the `WWW-Authenticate` challenge from a post-initialization request +/// failure (401 auth required or 403 insufficient scope), so a step-up +/// authorization can be started reactively. +fn auth_challenge_from_service_error(err: &ServiceError) -> Option { + let ServiceError::TransportSend(DynamicTransportError { error, .. }) = err else { + return None; + }; + + if let Some(http_err) = error.downcast_ref::>() { + return http_err.auth_challenge().map(str::to_string); + } + + #[cfg(unix)] + if let Some(http_err) = error + .downcast_ref::>( + ) + { + return http_err.auth_challenge().map(str::to_string); + } + + None +} + +async fn clear_credentials_on_post_refresh_auth_failure( + credential_store: &dyn CredentialStore, + name: &str, + error: &ExtensionError, +) -> bool { + let ExtensionError::InitializeError(err) = error else { + return false; + }; + + if !is_oauth_auth_failure(err) + || auth_challenge_from_error(err).is_some_and(|challenge| { + challenge + .to_ascii_lowercase() + .contains("insufficient_scope") + }) + { + return false; + } + + if let Err(e) = credential_store.clear().await { + warn!( + "[OAuth:{}] error clearing rejected credentials: {}", + name, e + ); + } + true +} + +pub(super) fn resolve_static_oauth_client( + client_id: Option<&str>, + client_secret_key: Option<&str>, + scopes: &[String], + envs: &HashMap, +) -> ExtensionResult> { + let Some(client_id) = client_id else { + if client_secret_key.is_some() { + return Err(ExtensionError::ConfigError( + "client_secret_key requires client_id".to_string(), + )); + } + if !scopes.is_empty() { + return Err(ExtensionError::ConfigError( + "scopes requires client_id".to_string(), + )); + } + return Ok(None); + }; + + let client_secret = + match client_secret_key { + Some(key) => Some(envs.get(key).cloned().ok_or_else(|| { + ExtensionError::ConfigError(format!("Secret '{}' not found", key)) + })?), + None => None, + }; + + Ok(Some(StaticOAuthClientConfig { + client_id: client_id.to_string(), + client_secret, + scopes: scopes.to_vec(), + })) +} + +const GOOSE_USER_AGENT: reqwest::header::HeaderValue = + reqwest::header::HeaderValue::from_static(concat!("goose/", env!("CARGO_PKG_VERSION"))); + +fn header_map(headers: &HashMap) -> ExtensionResult { + let mut map = HeaderMap::new(); + map.insert(reqwest::header::USER_AGENT, GOOSE_USER_AGENT); + for (key, value) in headers { + map.insert( + HeaderName::try_from(key) + .map_err(|_| ExtensionError::ConfigError(format!("invalid header: {}", key)))?, + value.parse().map_err(|_| { + ExtensionError::ConfigError(format!("invalid header value: {}", key)) + })?, + ); + } + Ok(map) +} + +#[cfg_attr(not(target_os = "linux"), allow(unused_variables))] +fn http_client( + headers: &HashMap, + timeout: Duration, +) -> ExtensionResult { + #[allow(unused_mut)] + let mut builder = reqwest::Client::builder().default_headers(header_map(headers)?); + #[cfg(target_os = "linux")] + { + builder = builder.tcp_user_timeout(Some(timeout)); + } + builder + .build() + .map_err(|_| ExtensionError::ConfigError("could not construct http client".to_string())) +} + +fn should_retry_legacy_after_empty_discover( + result: &Result, + capabilities: &super::super::mcp_client::GooseMcpClientCapabilities, +) -> bool { + capabilities.protocol_version.is_none() + && result.as_ref().is_err_and(|error| { + error.to_string().contains("empty sse stream") + || matches!( + error, + ClientInitializeError::ConnectionClosed(context) + if context == "discover response" + ) + }) +} + +// TODO: Remove this compatibility retry once rmcp handles empty discovery SSE +// responses upstream. +async fn connect_with_legacy_retry( + make_transport: impl Fn() -> T, + ctx: ConnectContext, +) -> Result +where + T: IntoTransport, + E: std::error::Error + From + Send + Sync + 'static, +{ + let result = McpClient::connect(make_transport(), ctx.clone()).await; + if !should_retry_legacy_after_empty_discover(&result, &ctx.capabilities) { + return result; + } + let mut ctx = ctx; + ctx.capabilities.protocol_version = Some(ProtocolVersion::V_2025_11_25); + McpClient::connect(make_transport(), ctx).await +} + +async fn connect_with_auth( + auth_manager: rmcp::transport::AuthorizationManager, + uri: &str, + headers: &HashMap, + ctx: ConnectContext, +) -> ExtensionResult { + let auth_client = AuthClient::new(http_client(headers, ctx.timeout)?, auth_manager); + Ok(connect_with_legacy_retry( + || { + StreamableHttpClientTransport::with_client( + auth_client.clone(), + StreamableHttpClientTransportConfig::with_uri(uri), + ) + }, + ctx, + ) + .await?) +} + +/// Connection parameters needed to re-establish an authorized streamable HTTP +/// client after a post-initialization auth challenge (401/403). +#[derive(Clone)] +pub(super) struct ConnectParams { + pub(super) uri: String, + pub(super) name: String, + pub(super) headers: HashMap, + pub(super) static_oauth_client: Option, + pub(super) ctx: ConnectContext, +} + +/// Wraps a streamable HTTP `McpClient` and handles step-up authorization: +/// when a request fails with a 401/403 carrying a `WWW-Authenticate` +/// challenge after initialization succeeded, re-authorize using the challenge +/// (requesting the union of scopes), reconnect, and retry the request once. +struct OAuthStepUpClient { + inner: tokio::sync::RwLock, + server_info: Option, + params: tokio::sync::RwLock, + step_up_lock: tokio::sync::Mutex<()>, + notification_subscribers: Arc>>>, +} + +impl OAuthStepUpClient { + async fn new(inner: McpClient, params: ConnectParams) -> Self { + let server_info = inner.get_info().cloned(); + let notification_subscribers = Arc::new(Mutex::new(Vec::new())); + Self::forward_notifications(&inner, notification_subscribers.clone()).await; + Self { + inner: tokio::sync::RwLock::new(inner), + server_info, + params: tokio::sync::RwLock::new(params), + step_up_lock: tokio::sync::Mutex::new(()), + notification_subscribers, + } + } + + async fn forward_notifications( + client: &McpClient, + subscribers: Arc>>>, + ) { + let mut receiver = client.subscribe().await; + tokio::spawn(async move { + while let Some(notification) = receiver.recv().await { + let mut subscribers = subscribers.lock().await; + subscribers.retain(|subscriber| subscriber.try_send(notification.clone()).is_ok()); + } + }); + } + + async fn step_up_reconnect( + &self, + challenge: String, + ) -> Result<(), crate::agents::mcp_client::Error> { + let params = self.params.read().await; + let auth_manager = oauth_flow_with_challenge( + ¶ms.uri, + ¶ms.name, + params.static_oauth_client.as_ref(), + Some(challenge), + ) + .await + .map_err(|e| { + crate::agents::mcp_client::Error::McpError(ErrorData::new( + ErrorCode::INTERNAL_ERROR, + format!("step-up authorization failed: {e}"), + None, + )) + })?; + let client = connect_with_auth( + auth_manager, + ¶ms.uri, + ¶ms.headers, + params.ctx.clone(), + ) + .await + .map_err(|e| { + crate::agents::mcp_client::Error::McpError(ErrorData::new( + ErrorCode::INTERNAL_ERROR, + format!("reconnect after step-up authorization failed: {e}"), + None, + )) + })?; + Self::forward_notifications(&client, self.notification_subscribers.clone()).await; + *self.inner.write().await = client; + Ok(()) + } + + /// Run `op` against the current client; on an auth challenge, re-authorize + /// and retry once. + async fn with_step_up_retry(&self, op: F) -> Result + where + F: for<'a> Fn( + &'a McpClient, + ) -> Pin< + Box< + dyn std::future::Future> + + Send + + 'a, + >, + >, + { + let first = { + let client = self.inner.read().await; + op(&client).await + }; + match first { + Err(err) => { + if let Some(challenge) = auth_challenge_from_service_error(&err) { + let _step_up_guard = self.step_up_lock.lock().await; + let retry = { + let client = self.inner.read().await; + op(&client).await + }; + match retry { + Ok(value) => Ok(value), + Err(retry_err) + if auth_challenge_from_service_error(&retry_err).is_some() => + { + self.step_up_reconnect(challenge).await?; + let client = self.inner.read().await; + op(&client).await + } + Err(retry_err) => Err(retry_err), + } + } else { + Err(err) + } + } + ok => ok, + } + } +} + +#[async_trait::async_trait] +impl McpClientTrait for OAuthStepUpClient { + async fn list_tools( + &self, + session_id: &str, + next_cursor: Option, + cancel_token: CancellationToken, + ) -> Result { + let session_id = session_id.to_string(); + self.with_step_up_retry(move |client| { + let session_id = session_id.clone(); + let next_cursor = next_cursor.clone(); + let cancel_token = cancel_token.clone(); + Box::pin(async move { + client + .list_tools(&session_id, next_cursor, cancel_token) + .await + }) + }) + .await + } + + async fn call_tool( + &self, + ctx: &ToolCallContext, + name: &str, + arguments: Option, + cancel_token: CancellationToken, + ) -> Result { + let ctx = ctx.clone(); + let name = name.to_string(); + self.with_step_up_retry(move |client| { + let ctx = ctx.clone(); + let name = name.clone(); + let arguments = arguments.clone(); + let cancel_token = cancel_token.clone(); + Box::pin(async move { client.call_tool(&ctx, &name, arguments, cancel_token).await }) + }) + .await + } + + fn get_info(&self) -> Option<&ServerInfo> { + self.server_info.as_ref() + } + + async fn list_resources( + &self, + session_id: &str, + next_cursor: Option, + cancel_token: CancellationToken, + ) -> Result { + let session_id = session_id.to_string(); + self.with_step_up_retry(move |client| { + let session_id = session_id.clone(); + let next_cursor = next_cursor.clone(); + let cancel_token = cancel_token.clone(); + Box::pin(async move { + client + .list_resources(&session_id, next_cursor, cancel_token) + .await + }) + }) + .await + } + + async fn read_resource( + &self, + session_id: &str, + uri: &str, + cancel_token: CancellationToken, + ) -> Result { + let session_id = session_id.to_string(); + let uri = uri.to_string(); + self.with_step_up_retry(move |client| { + let session_id = session_id.clone(); + let uri = uri.clone(); + let cancel_token = cancel_token.clone(); + Box::pin(async move { client.read_resource(&session_id, &uri, cancel_token).await }) + }) + .await + } + + async fn list_prompts( + &self, + session_id: &str, + next_cursor: Option, + cancel_token: CancellationToken, + ) -> Result { + let session_id = session_id.to_string(); + self.with_step_up_retry(move |client| { + let session_id = session_id.clone(); + let next_cursor = next_cursor.clone(); + let cancel_token = cancel_token.clone(); + Box::pin(async move { + client + .list_prompts(&session_id, next_cursor, cancel_token) + .await + }) + }) + .await + } + + async fn get_prompt( + &self, + session_id: &str, + name: &str, + arguments: Value, + cancel_token: CancellationToken, + ) -> Result { + let session_id = session_id.to_string(); + let name = name.to_string(); + self.with_step_up_retry(move |client| { + let session_id = session_id.clone(); + let name = name.clone(); + let arguments = arguments.clone(); + let cancel_token = cancel_token.clone(); + Box::pin(async move { + client + .get_prompt(&session_id, &name, arguments, cancel_token) + .await + }) + }) + .await + } + + async fn subscribe(&self) -> tokio::sync::mpsc::Receiver { + let (sender, receiver) = mpsc::channel(32); + self.notification_subscribers.lock().await.push(sender); + receiver + } + + async fn get_moim(&self, session_id: &str) -> Option { + self.inner.read().await.get_moim(session_id).await + } + + async fn update_working_dir( + &self, + new_dir: PathBuf, + ) -> Result<(), crate::agents::mcp_client::Error> { + self.params.write().await.ctx.working_dir = new_dir.clone(); + self.inner.read().await.update_working_dir(new_dir).await + } +} + +pub(super) async fn connect( + params: ConnectParams, + socket: Option<&str>, + credential_store: Box, +) -> ExtensionResult> { + #[cfg(unix)] + if let Some(socket_path) = socket { + return connect_over_unix_socket(¶ms, socket_path).await; + } + #[cfg(not(unix))] + if socket.is_some() { + return Err(ExtensionError::ConfigError( + "Unix domain socket transport is not supported on this platform".to_string(), + )); + } + + let ConnectParams { + uri, + name, + headers, + static_oauth_client, + ctx, + } = ¶ms; + let http_client = http_client(headers, ctx.timeout)?; + + // If we have stored OAuth credentials, try refreshing and connecting directly. + // This avoids the unnecessary 401 → browser re-auth cycle on every new session. + if credential_store.load().await.is_ok_and(|c| c.is_some()) { + match oauth_flow(uri, name, static_oauth_client.as_ref()).await { + Ok(auth_manager) => { + match connect_with_auth(auth_manager, uri, headers, ctx.clone()).await { + Ok(client) => { + return Ok(Box::new(OAuthStepUpClient::new(client, params).await)); + } + Err(error) => { + if !clear_credentials_on_post_refresh_auth_failure( + credential_store.as_ref(), + name, + &error, + ) + .await + { + return Err(error); + } + warn!( + "[OAuth:{}] Refreshed token was rejected, falling back to browser auth", + name + ); + } + } + } + Err(e) => { + warn!( + "[OAuth:{}] Proactive refresh failed: {}, falling back to unauthenticated attempt", + name, e + ); + } + } + } + + let client_res = connect_with_legacy_retry( + || { + StreamableHttpClientTransport::with_client( + http_client.clone(), + StreamableHttpClientTransportConfig::with_uri(uri.as_str()), + ) + }, + ctx.clone(), + ) + .await; + + if !should_attempt_oauth_fallback(&client_res) { + return Ok(Box::new(OAuthStepUpClient::new(client_res?, params).await)); + } + + let challenge = auth_challenge_from_result(&client_res); + match oauth_flow_with_challenge(uri, name, static_oauth_client.as_ref(), challenge).await { + Ok(auth_manager) => { + let client = connect_with_auth(auth_manager, uri, headers, ctx.clone()).await?; + Ok(Box::new(OAuthStepUpClient::new(client, params).await)) + } + Err(e) => { + warn!( + "[OAuth:{}] Browser authorization flow failed: {:#}", + name, e + ); + Ok(Box::new(client_res?)) + } + } +} + +#[cfg(unix)] +async fn connect_over_unix_socket( + params: &ConnectParams, + socket_path: &str, +) -> ExtensionResult> { + use rmcp::transport::UnixSocketHttpClient; + + let unix_client = UnixSocketHttpClient::new(socket_path, ¶ms.uri); + let custom_headers: HashMap = header_map(¶ms.headers)? + .into_iter() + .filter_map(|(name, value)| name.map(|name| (name, value))) + .collect(); + + let client_res = connect_with_legacy_retry( + || { + StreamableHttpClientTransport::with_client( + unix_client.clone(), + StreamableHttpClientTransportConfig::with_uri(params.uri.as_str()) + .custom_headers(custom_headers.clone()), + ) + }, + params.ctx.clone(), + ) + .await; + + if should_attempt_oauth_fallback(&client_res) { + warn!( + "Extension '{}' returned 401 over Unix domain socket transport; \ + OAuth is not supported for UDS connections", + params.name, + ); + } + Ok(Box::new(client_res?)) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::action_required_manager::ActionRequiredManager; + use crate::agents::mcp_client::GooseMcpClientCapabilities; + use rmcp::transport::auth::InMemoryCredentialStore; + use std::sync::Weak; + use tempfile::tempdir; + + fn test_ctx(working_dir: &std::path::Path) -> ConnectContext { + ConnectContext { + timeout: Duration::from_secs(5), + provider: Arc::new(Mutex::new(None)), + client_name: "goose-test".to_string(), + capabilities: GooseMcpClientCapabilities { + mcpui: false, + host_info: None, + elicitation_handler: None, + protocol_version: None, + }, + working_dir: working_dir.to_path_buf(), + docker_container: None, + action_required: Arc::new(ActionRequiredManager::new()), + extension_manager: Weak::new(), + } + } + + fn test_params( + uri: &str, + headers: HashMap, + working_dir: &std::path::Path, + ) -> ConnectParams { + ConnectParams { + uri: uri.to_string(), + name: "test-ext".to_string(), + headers, + static_oauth_client: None, + ctx: test_ctx(working_dir), + } + } + + fn transport_err(error: Box) -> ClientInitializeError { + ClientInitializeError::TransportError { + error: rmcp::transport::DynamicTransportError::from_parts( + "test", + std::any::TypeId::of::<()>(), + error, + ), + context: "test context".into(), + } + } + + fn streamable_err( + e: rmcp::transport::streamable_http_client::StreamableHttpError, + ) -> ClientInitializeError { + transport_err(Box::new(e)) + } + + #[test] + fn test_oauth_fallback_on_typed_auth_required() { + let err = streamable_err( + rmcp::transport::streamable_http_client::StreamableHttpError::AuthRequired( + rmcp::transport::streamable_http_client::AuthRequiredError::new( + "Bearer realm=\"test\"".to_string(), + ), + ), + ); + assert!(should_attempt_oauth_fallback(&Err(err))); + } + + #[test] + fn test_oauth_fallback_on_unexpected_response_http_401_prefix() { + let err = streamable_err( + rmcp::transport::streamable_http_client::StreamableHttpError::UnexpectedServerResponse( + std::borrow::Cow::Borrowed("HTTP 401 Unauthorized"), + ), + ); + assert!(should_attempt_oauth_fallback(&Err(err))); + } + + #[tokio::test] + async fn test_post_refresh_auth_failure_clears_credentials() { + use rmcp::transport::auth::{OAuthTokenResponse, StoredCredentials}; + + let token_response: OAuthTokenResponse = serde_json::from_value(serde_json::json!({ + "access_token": "rejected-token", + "token_type": "bearer", + })) + .expect("valid fake token JSON"); + let store = InMemoryCredentialStore::new(); + store + .save(StoredCredentials::new( + "test-client".to_string(), + Some(token_response), + vec![], + None, + )) + .await + .unwrap(); + + let err = streamable_err( + rmcp::transport::streamable_http_client::StreamableHttpError::AuthRequired( + rmcp::transport::streamable_http_client::AuthRequiredError::new( + "Bearer error=\"invalid_token\"".to_string(), + ), + ), + ); + let error = ExtensionError::from(err); + + assert!(clear_credentials_on_post_refresh_auth_failure(&store, "test-ext", &error).await); + assert!(store.load().await.unwrap().is_none()); + } + + #[tokio::test] + async fn test_invalid_header_name_returns_config_error() { + let mut headers = HashMap::new(); + headers.insert("bad header name".to_string(), "value".to_string()); + + let temp_dir = tempdir().unwrap(); + + let result = connect( + test_params("http://localhost:1", headers, temp_dir.path()), + None, + Box::new(InMemoryCredentialStore::new()), + ) + .await; + + let Err(ExtensionError::ConfigError(msg)) = result else { + panic!("expected ConfigError, got a different result"); + }; + assert!( + msg.contains("invalid header"), + "unexpected error message: {msg}" + ); + } + + #[tokio::test] + async fn test_invalid_header_value_returns_config_error() { + let mut headers = HashMap::new(); + headers.insert("x-valid-name".to_string(), "bad\r\nvalue".to_string()); + + let temp_dir = tempdir().unwrap(); + + let result = connect( + test_params("http://localhost:1", headers, temp_dir.path()), + None, + Box::new(InMemoryCredentialStore::new()), + ) + .await; + + let Err(ExtensionError::ConfigError(msg)) = result else { + panic!("expected ConfigError, got a different result"); + }; + assert!( + msg.contains("invalid header value"), + "unexpected error message: {msg}" + ); + } + + #[tokio::test] + async fn test_custom_headers_forwarded_to_http_extension() { + use wiremock::matchers::any; + use wiremock::{Mock, MockServer, ResponseTemplate}; + + let mock_server = MockServer::start().await; + Mock::given(any()) + .respond_with(ResponseTemplate::new(200)) + .mount(&mock_server) + .await; + + let mut headers = HashMap::new(); + headers.insert("x-api-key".to_string(), "test-secret-123".to_string()); + + let temp_dir = tempdir().unwrap(); + + // The MCP handshake will fail against the stub server. We only care that + // the outgoing HTTP request carried the custom header. + let _ = connect( + test_params(&mock_server.uri(), headers, temp_dir.path()), + None, + Box::new(InMemoryCredentialStore::new()), + ) + .await; + + let received = mock_server.received_requests().await.unwrap(); + assert!( + !received.is_empty(), + "expected at least one HTTP request to reach the mock server" + ); + let header_found = received.iter().any(|req| { + req.headers + .get("x-api-key") + .map(|v| v == "test-secret-123") + .unwrap_or(false) + }); + assert!( + header_found, + "custom header x-api-key was not forwarded to the extension server" + ); + } + + /// Directly exercises `connect_with_auth`, which is the code path fixed by + /// the PR (custom headers were dropped when the OAuth connection path was + /// taken). Uses a pre-seeded `InMemoryCredentialStore` with a fake, + /// non-expiring token so `get_access_token()` returns immediately without + /// touching any OAuth endpoints or the system keychain. + #[tokio::test] + async fn test_custom_headers_forwarded_oauth_path() { + use rmcp::transport::auth::{OAuthTokenResponse, StoredCredentials}; + use wiremock::matchers::any; + use wiremock::{Mock, MockServer, ResponseTemplate}; + + let mock_server = MockServer::start().await; + Mock::given(any()) + .respond_with(ResponseTemplate::new(200)) + .mount(&mock_server) + .await; + + let mut headers = HashMap::new(); + headers.insert("x-api-key".to_string(), "test-secret-oauth".to_string()); + + // Build a fake, non-expiring token. token_received_at=None skips the + // expiry check, so get_access_token() returns without any network call. + let token_response: OAuthTokenResponse = serde_json::from_value(serde_json::json!({ + "access_token": "fake-test-token", + "token_type": "bearer", + })) + .expect("valid fake token JSON"); + let creds = StoredCredentials::new( + "test-client".to_string(), + Some(token_response), + vec![], + None, + ); + let store = InMemoryCredentialStore::new(); + store.save(creds).await.unwrap(); + + let mut auth_manager = rmcp::transport::AuthorizationManager::new(mock_server.uri()) + .await + .expect("AuthorizationManager::new should not make network calls"); + auth_manager.set_credential_store(store); + + let temp_dir = tempdir().unwrap(); + + // connect_with_auth will fail (mock server isn't an MCP server) but we + // only care that the outgoing request carried the custom header. + let _ = connect_with_auth( + auth_manager, + &mock_server.uri(), + &headers, + test_ctx(temp_dir.path()), + ) + .await; + + let received = mock_server.received_requests().await.unwrap(); + assert!( + !received.is_empty(), + "expected at least one HTTP request to reach the mock server" + ); + let header_found = received.iter().any(|req| { + req.headers + .get("x-api-key") + .map(|v| v == "test-secret-oauth") + .unwrap_or(false) + }); + assert!( + header_found, + "custom header x-api-key was not forwarded through the OAuth connection path" + ); + } + + mod static_oauth_client { + use super::*; + + #[test] + fn absent_client_id_yields_no_static_client() { + let resolved = resolve_static_oauth_client(None, None, &[], &HashMap::new()).unwrap(); + + assert_eq!(resolved, None); + } + + #[test] + fn client_id_without_secret_resolves_public_client() { + let resolved = resolve_static_oauth_client( + Some("registered-client"), + None, + &["scope.read".to_string()], + &HashMap::new(), + ) + .unwrap() + .unwrap(); + + assert_eq!(resolved.client_id, "registered-client"); + assert_eq!(resolved.client_secret, None); + assert_eq!(resolved.scopes, vec!["scope.read"]); + } + + #[test] + fn client_secret_resolves_from_envs() { + let envs = + HashMap::from([("OAUTH_CLIENT_SECRET".to_string(), "env-secret".to_string())]); + + let resolved = resolve_static_oauth_client( + Some("registered-client"), + Some("OAUTH_CLIENT_SECRET"), + &[], + &envs, + ) + .unwrap() + .unwrap(); + + assert_eq!(resolved.client_secret.as_deref(), Some("env-secret")); + } + + #[test] + fn client_secret_key_without_client_id_is_rejected() { + let error = resolve_static_oauth_client( + None, + Some("OAUTH_CLIENT_SECRET"), + &[], + &HashMap::new(), + ) + .unwrap_err(); + + assert!(error + .to_string() + .contains("client_secret_key requires client_id")); + } + + #[test] + fn scopes_without_client_id_are_rejected() { + let error = resolve_static_oauth_client( + None, + None, + &["scope.read".to_string()], + &HashMap::new(), + ) + .unwrap_err(); + + assert!(error.to_string().contains("scopes requires client_id")); + } + + #[test] + fn missing_client_secret_key_is_an_error() { + let error = resolve_static_oauth_client( + Some("registered-client"), + Some("MISSING_KEY"), + &[], + &HashMap::new(), + ) + .unwrap_err(); + + assert!(error.to_string().contains("MISSING_KEY")); + } + } +} diff --git a/crates/goose/src/agents/mcp_client.rs b/crates/goose/src/agents/mcp_client.rs index f2794ef6f..94dcc6329 100644 --- a/crates/goose/src/agents/mcp_client.rs +++ b/crates/goose/src/agents/mcp_client.rs @@ -635,6 +635,18 @@ impl std::fmt::Debug for GooseMcpClientCapabilities { } } +#[derive(Clone)] +pub(crate) struct ConnectContext { + pub timeout: Duration, + pub provider: SharedProvider, + pub client_name: String, + pub capabilities: GooseMcpClientCapabilities, + pub working_dir: PathBuf, + pub docker_container: Option, + pub action_required: Arc, + pub extension_manager: Weak, +} + /// The MCP client is the interface for MCP operations. pub struct McpClient { client: Mutex>>, @@ -645,51 +657,24 @@ pub struct McpClient { } impl McpClient { - #[allow(clippy::too_many_arguments)] pub(crate) async fn connect( transport: T, - timeout: std::time::Duration, - provider: SharedProvider, - client_name: String, - capabilities: GooseMcpClientCapabilities, - working_dir: PathBuf, - action_required: Arc, - extension_manager: Weak, + ctx: ConnectContext, ) -> Result where T: IntoTransport, E: std::error::Error + From + Send + Sync + 'static, { - Self::connect_with_container( - transport, + let ConnectContext { timeout, provider, - None, client_name, capabilities, working_dir, + docker_container, action_required, extension_manager, - ) - .await - } - - #[allow(clippy::too_many_arguments)] - pub(crate) async fn connect_with_container( - transport: T, - timeout: std::time::Duration, - provider: SharedProvider, - docker_container: Option, - client_name: String, - capabilities: GooseMcpClientCapabilities, - working_dir: PathBuf, - action_required: Arc, - extension_manager: Weak, - ) -> Result - where - T: IntoTransport, - E: std::error::Error + From + Send + Sync + 'static, - { + } = ctx; let notification_subscribers = Arc::new(Mutex::new(Vec::>::new())); diff --git a/crates/goose/src/agents/platform_extensions/ext_manager.rs b/crates/goose/src/agents/platform_extensions/ext_manager.rs index d11464927..1f44390e6 100644 --- a/crates/goose/src/agents/platform_extensions/ext_manager.rs +++ b/crates/goose/src/agents/platform_extensions/ext_manager.rs @@ -1,7 +1,9 @@ +use crate::agents::extension::ExtensionConfig; use crate::agents::extension::PlatformExtensionContext; +use crate::agents::extension_manager::is_hidden_extension; use crate::agents::mcp_client::{Error, McpClientTrait}; use crate::agents::tool_execution::ToolCallContext; -use crate::config::get_extension_by_name; +use crate::config::{get_all_extensions, get_extension_by_name}; use crate::session::SessionType; use anyhow::Result; use async_trait::async_trait; @@ -105,20 +107,20 @@ impl ExtensionManagerClient { async fn handle_search_available_extensions( &self, ) -> Result, ExtensionManagerToolError> { - if let Some(weak_ref) = &self.context.extension_manager { - if let Some(extension_manager) = weak_ref.upgrade() { - match extension_manager.search_available_extensions().await { - Ok(content) => Ok(content), - Err(e) => Err(ExtensionManagerToolError::OperationFailed { - message: format!("Failed to search available extensions: {}", e.message), - }), - } - } else { - Err(ExtensionManagerToolError::ManagerUnavailable) + let extension_manager = self + .context + .extension_manager + .as_ref() + .and_then(|weak| weak.upgrade()) + .ok_or(ExtensionManagerToolError::ManagerUnavailable)?; + let enabled = extension_manager.list_extensions().await.map_err(|e| { + ExtensionManagerToolError::OperationFailed { + message: format!("Failed to search available extensions: {}", e), } - } else { - Err(ExtensionManagerToolError::ManagerUnavailable) - } + })?; + Ok(vec![ContentBlock::text(search_available_extensions( + &enabled, + ))]) } async fn handle_manage_extensions( @@ -505,6 +507,54 @@ impl McpClientTrait for ExtensionManagerClient { } } +fn search_available_extensions(enabled: &[String]) -> String { + let disabled: Vec = get_all_extensions() + .into_iter() + .filter(|extension| !extension.enabled && !is_hidden_extension(&extension.config.name())) + .map(|extension| { + let description = match &extension.config { + ExtensionConfig::Builtin { + description, + display_name, + .. + } if description.is_empty() => display_name + .as_deref() + .unwrap_or("Built-in extension") + .to_string(), + ExtensionConfig::Builtin { description, .. } + | ExtensionConfig::Platform { description, .. } + | ExtensionConfig::StreamableHttp { description, .. } + | ExtensionConfig::Stdio { description, .. } => description.clone(), + }; + format!("- {} - {}", extension.config.name(), description) + }) + .collect(); + let enabled: Vec = enabled + .iter() + .filter(|name| !is_hidden_extension(name)) + .map(|name| format!("- {}", name)) + .collect(); + + let mut output_parts = vec![]; + if disabled.is_empty() { + output_parts.push("No extensions available to enable.\n".to_string()); + } else { + output_parts.push(format!( + "Extensions available to enable:\n{}\n", + disabled.join("\n") + )); + } + if enabled.is_empty() { + output_parts.push("No extensions that can be disabled.\n".to_string()); + } else { + output_parts.push(format!( + "\n\nExtensions available to disable:\n{}\n", + enabled.join("\n") + )); + } + output_parts.join("\n") +} + #[cfg(test)] mod tests { use super::*;