mirror of
https://github.com/openai/codex.git
synced 2026-11-17 22:10:37 +00:00
## What changed Reuse connected thread tool inventories for paginated `ToolsAndAuthOnly` status requests when registrations, client settings, permissions, environments, and authentication match. Restrict reuse to connections with fixed credentials; discover remaining servers separately and preserve enterprise-managed servers' admitted thread connections. Bind status configuration to the same environment and permission inputs used by the thread runtime, and limit authentication-failure inspection to the requested servers. ## Testing Extend status tests to cover fixed-credential reuse, fresh discovery for stored OAuth authority, full resource discovery, and inventories for dormant threads without starting their runtime connections. Verify that paginated lightweight results keep same-named servers in different environments separate. GitOrigin-RevId: eaca6365fb2a739eaad76eefdc62ec9f796b1024
283 lines
9.3 KiB
Rust
283 lines
9.3 KiB
Rust
use std::collections::HashMap;
|
|
use std::ffi::OsString;
|
|
use std::sync::Arc;
|
|
use std::sync::atomic::AtomicUsize;
|
|
use std::sync::atomic::Ordering;
|
|
use std::time::Duration;
|
|
|
|
use anyhow::Context;
|
|
use codex_exec_server::Environment;
|
|
use codex_rmcp_client::Elicitation;
|
|
use codex_rmcp_client::ElicitationAction;
|
|
use codex_rmcp_client::ElicitationResponse;
|
|
use codex_rmcp_client::ExecutorStdioServerLauncher;
|
|
use codex_rmcp_client::LocalStdioServerLauncher;
|
|
use codex_rmcp_client::McpProtocolMode;
|
|
use codex_rmcp_client::RmcpClient;
|
|
use codex_rmcp_client::StdioServerLauncher;
|
|
use futures::FutureExt;
|
|
use pretty_assertions::assert_eq;
|
|
use rmcp::model::ClientCapabilities;
|
|
use rmcp::model::ElicitRequestParams;
|
|
use rmcp::model::ElicitationCapability;
|
|
use rmcp::model::FormElicitationCapability;
|
|
use rmcp::model::Implementation;
|
|
use rmcp::model::InitializeRequestParams;
|
|
use rmcp::model::ProtocolVersion;
|
|
use serde_json::json;
|
|
|
|
async fn exercise_stdio_server(
|
|
server_mode: &str,
|
|
protocol_mode: McpProtocolMode,
|
|
opt_in: bool,
|
|
use_executor: bool,
|
|
) -> anyhow::Result<()> {
|
|
let server = codex_utils_cargo_bin::cargo_bin("test_mcp_2026_stdio_server")?;
|
|
let launcher: Arc<dyn StdioServerLauncher> = if use_executor {
|
|
Arc::new(ExecutorStdioServerLauncher::new(
|
|
Environment::default_for_tests().get_exec_backend(),
|
|
))
|
|
} else {
|
|
Arc::new(LocalStdioServerLauncher::new(std::env::current_dir()?))
|
|
};
|
|
let mut env = HashMap::new();
|
|
if opt_in {
|
|
env.insert(
|
|
OsString::from("CODEX_MCP_PROTOCOL_VERSION"),
|
|
OsString::from("2026-07-28"),
|
|
);
|
|
}
|
|
let root = tempfile::tempdir()?;
|
|
let cwd = if use_executor {
|
|
std::env::current_dir()?
|
|
} else {
|
|
root.path().to_path_buf()
|
|
};
|
|
#[cfg(unix)]
|
|
let server = if use_executor {
|
|
server
|
|
} else {
|
|
let wrapper = root.path().join("server");
|
|
let script = "#!/bin/sh\nprintf '%s' \"$0\" > argv0\nexec \"$MCP_SERVER\" \"$@\"\n";
|
|
codex_utils_cargo_bin::write_executable(&wrapper, script)?;
|
|
env.insert(OsString::from("MCP_SERVER"), server.into_os_string());
|
|
std::path::PathBuf::from("./server")
|
|
};
|
|
let client = RmcpClient::new_stdio_client_with_protocol_mode(
|
|
server.into(),
|
|
vec![OsString::from(server_mode)],
|
|
Some(env),
|
|
&[],
|
|
Some(cwd.to_string_lossy().into_owned()),
|
|
launcher,
|
|
protocol_mode,
|
|
)
|
|
.await?;
|
|
|
|
let mut capabilities = ClientCapabilities::default();
|
|
capabilities.elicitation =
|
|
Some(ElicitationCapability::new().with_form(FormElicitationCapability::new()));
|
|
let elicitation_count = Arc::new(AtomicUsize::new(0));
|
|
let observed_elicitations = Arc::clone(&elicitation_count);
|
|
let legacy_session = server_mode.starts_with("legacy");
|
|
let handshake_timeout = match protocol_mode {
|
|
// Discovery can wait for a silent probe, then stop and relaunch the helper.
|
|
McpProtocolMode::DiscoverLocalStdio => Duration::from_secs(30),
|
|
McpProtocolMode::Legacy | McpProtocolMode::V20260728 => Duration::from_secs(5),
|
|
};
|
|
let initialized = client
|
|
.initialize(
|
|
InitializeRequestParams::new(
|
|
capabilities,
|
|
Implementation::new("codex-mcp-client", "0.0.0-test"),
|
|
)
|
|
.with_protocol_version(ProtocolVersion::V_2025_06_18),
|
|
Some(handshake_timeout),
|
|
Box::new(move |_request_id, request| {
|
|
let observed_elicitations = Arc::clone(&observed_elicitations);
|
|
async move {
|
|
observed_elicitations.fetch_add(1, Ordering::Relaxed);
|
|
let Elicitation::Mcp(ElicitRequestParams::FormElicitationParams {
|
|
requested_schema,
|
|
..
|
|
}) = request
|
|
else {
|
|
anyhow::bail!("expected a standard MCP form elicitation");
|
|
};
|
|
let content = if legacy_session {
|
|
assert_eq!(
|
|
serde_json::to_value(requested_schema)?,
|
|
json!({
|
|
"type": "object",
|
|
"properties": {
|
|
"name": {"type": "string", "default": "John Doe"},
|
|
"age": {"type": "integer", "default": 30},
|
|
"score": {"type": "number", "default": 95.5},
|
|
"status": {
|
|
"type": "string",
|
|
"enum": ["active", "inactive"],
|
|
"default": "active",
|
|
},
|
|
"verified": {"type": "boolean", "default": true},
|
|
},
|
|
"required": [],
|
|
}),
|
|
);
|
|
json!({
|
|
"name": "John Doe",
|
|
"age": 30,
|
|
"score": 95.5,
|
|
"status": "active",
|
|
"verified": true,
|
|
})
|
|
} else {
|
|
json!({"approved": true})
|
|
};
|
|
Ok(ElicitationResponse {
|
|
action: ElicitationAction::Accept,
|
|
content: Some(content),
|
|
meta: None,
|
|
})
|
|
}
|
|
.boxed()
|
|
}),
|
|
)
|
|
.await?;
|
|
|
|
let expected_version = if legacy_session {
|
|
ProtocolVersion::V_2025_06_18
|
|
} else {
|
|
ProtocolVersion::V_2026_07_28
|
|
};
|
|
assert_eq!(initialized.protocol_version, expected_version);
|
|
assert_eq!(
|
|
initialized
|
|
.server_info
|
|
.as_ref()
|
|
.map(|server_info| server_info.name.as_str()),
|
|
Some(if legacy_session {
|
|
"legacy-stdio-test"
|
|
} else {
|
|
"strict-stdio-test"
|
|
})
|
|
);
|
|
let tools = client
|
|
.list_tools(/*params*/ None, Some(Duration::from_secs(5)))
|
|
.await?;
|
|
assert_eq!(
|
|
tools
|
|
.tools
|
|
.iter()
|
|
.map(|tool| tool.name.as_ref())
|
|
.collect::<Vec<_>>(),
|
|
vec!["echo"]
|
|
);
|
|
let result = client
|
|
.call_tool(
|
|
"echo".to_owned(),
|
|
Some(json!({"message": "hello stdio"})),
|
|
|_| Ok(None),
|
|
Some(Duration::from_secs(5)),
|
|
)
|
|
.await?;
|
|
assert_eq!(
|
|
result.content[0].as_text().map(|text| text.text.as_str()),
|
|
Some(if legacy_session {
|
|
"legacy approved"
|
|
} else {
|
|
"modern approved"
|
|
})
|
|
);
|
|
assert_eq!(elicitation_count.load(Ordering::Relaxed), 1);
|
|
#[cfg(unix)]
|
|
if !use_executor {
|
|
assert_eq!(
|
|
std::fs::read_to_string(root.path().join("argv0"))?,
|
|
"./server"
|
|
);
|
|
}
|
|
client.shutdown().await;
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn modern_local_stdio_discovers_metadata_only_identity_and_drives_mrtr() -> anyhow::Result<()>
|
|
{
|
|
exercise_stdio_server(
|
|
"modern",
|
|
McpProtocolMode::V20260728,
|
|
/*opt_in*/ true,
|
|
/*use_executor*/ false,
|
|
)
|
|
.await
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn modern_executor_stdio_discovers_metadata_only_identity_and_drives_mrtr()
|
|
-> anyhow::Result<()> {
|
|
exercise_stdio_server(
|
|
"modern",
|
|
McpProtocolMode::V20260728,
|
|
/*opt_in*/ true,
|
|
/*use_executor*/ true,
|
|
)
|
|
.await
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn legacy_stdio_supports_sep1034_defaults_without_the_rollout_flag() -> anyhow::Result<()> {
|
|
exercise_stdio_server(
|
|
"legacy",
|
|
McpProtocolMode::Legacy,
|
|
/*opt_in*/ false,
|
|
/*use_executor*/ false,
|
|
)
|
|
.await
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn rollout_flag_alone_preserves_legacy_stdio_and_sep1034_defaults() -> anyhow::Result<()> {
|
|
exercise_stdio_server(
|
|
"legacy",
|
|
McpProtocolMode::V20260728,
|
|
/*opt_in*/ false,
|
|
/*use_executor*/ false,
|
|
)
|
|
.await
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn unmarked_stdio_helpers_recover_from_failed_or_late_discovery() -> anyhow::Result<()> {
|
|
for mode in [
|
|
"legacy",
|
|
"legacy-init-first",
|
|
"legacy-reject-late-initialize",
|
|
"legacy-reject-and-exit",
|
|
"legacy-late",
|
|
"legacy-silent",
|
|
"legacy-versions",
|
|
"modern-late",
|
|
] {
|
|
exercise_stdio_server(
|
|
mode,
|
|
McpProtocolMode::DiscoverLocalStdio,
|
|
/*opt_in*/ false,
|
|
/*use_executor*/ false,
|
|
)
|
|
.await
|
|
.with_context(|| format!("stdio discovery failed in {mode} mode"))?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
|
async fn modern_stdio_safely_falls_back_to_legacy_elicitation() -> anyhow::Result<()> {
|
|
exercise_stdio_server(
|
|
"legacy-fallback",
|
|
McpProtocolMode::V20260728,
|
|
/*opt_in*/ true,
|
|
/*use_executor*/ false,
|
|
)
|
|
.await
|
|
}
|