Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Set a default user agent for MCP HTTP requests (#34883)

Eric Traut6e0455fdc411
diff --git a/codex-rs/rmcp-client/src/utils.rs b/codex-rs/rmcp-client/src/utils.rs
index 892df544cb..db6170c831 100644
--- a/codex-rs/rmcp-client/src/utils.rs
+++ b/codex-rs/rmcp-client/src/utils.rs
@@ -5,10 +5,13 @@ use reqwest::ClientBuilder;
use reqwest::header::HeaderMap;
use reqwest::header::HeaderName;
use reqwest::header::HeaderValue;
+use reqwest::header::USER_AGENT;
use std::collections::HashMap;
use std::env;
use std::ffi::OsString;
+const MCP_USER_AGENT: &str = concat!("codex-mcp-client/", env!("CARGO_PKG_VERSION"));
+
pub(crate) fn create_env_for_mcp_server(
extra_env: Option<HashMap<OsString, OsString>>,
env_vars: &[McpServerEnvVar],
@@ -62,6 +65,7 @@ pub(crate) fn build_default_headers(
env_http_headers: Option<HashMap<String, String>>,
) -> Result<HeaderMap> {
let mut headers = HeaderMap::new();
+ headers.insert(USER_AGENT, HeaderValue::from_static(MCP_USER_AGENT));
if let Some(static_headers) = http_headers {
for (name, value) in static_headers {
diff --git a/codex-rs/rmcp-client/tests/streamable_http_oauth_startup.rs b/codex-rs/rmcp-client/tests/streamable_http_oauth_startup.rs
index 32f90bc41e..c9320b408e 100644
--- a/codex-rs/rmcp-client/tests/streamable_http_oauth_startup.rs
+++ b/codex-rs/rmcp-client/tests/streamable_http_oauth_startup.rs
@@ -50,6 +50,10 @@ async fn refreshes_expired_persisted_token_before_initialize() -> anyhow::Result
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/.well-known/oauth-authorization-server/mcp"))
+ .and(header(
+ "user-agent",
+ concat!("codex-mcp-client/", env!("CARGO_PKG_VERSION")),
+ ))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"authorization_endpoint": format!("{}/oauth/authorize", server.uri()),
"token_endpoint": format!("{}/oauth/token", server.uri()),
@@ -60,6 +64,10 @@ async fn refreshes_expired_persisted_token_before_initialize() -> anyhow::Result
.await;
Mock::given(method("POST"))
.and(path("/oauth/token"))
+ .and(header(
+ "user-agent",
+ concat!("codex-mcp-client/", env!("CARGO_PKG_VERSION")),
+ ))
.and(body_string_contains("grant_type=refresh_token"))
.and(body_string_contains(format!(
"refresh_token={REFRESH_TOKEN}"
@@ -75,6 +83,10 @@ async fn refreshes_expired_persisted_token_before_initialize() -> anyhow::Result
.await;
Mock::given(method("POST"))
.and(path("/mcp"))
+ .and(header(
+ "user-agent",
+ concat!("codex-mcp-client/", env!("CARGO_PKG_VERSION")),
+ ))
.and(header(
"authorization",
format!("Bearer {REFRESHED_ACCESS_TOKEN}"),
diff --git a/codex-rs/rmcp-client/tests/streamable_http_user_agent.rs b/codex-rs/rmcp-client/tests/streamable_http_user_agent.rs
new file mode 100644
index 0000000000..45b5d6025c
--- /dev/null
+++ b/codex-rs/rmcp-client/tests/streamable_http_user_agent.rs
@@ -0,0 +1,70 @@
+mod streamable_http_test_support;
+
+use std::collections::HashMap;
+
+use codex_config::types::AuthKeyringBackendKind;
+use codex_config::types::OAuthCredentialsStoreMode;
+use codex_exec_server::Environment;
+use codex_rmcp_client::RmcpClient;
+use serde_json::Value;
+use serde_json::json;
+use streamable_http_test_support::initialize_client;
+use wiremock::Mock;
+use wiremock::MockServer;
+use wiremock::Request;
+use wiremock::ResponseTemplate;
+use wiremock::matchers::header;
+use wiremock::matchers::method;
+use wiremock::matchers::path;
+
+#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
+async fn streamable_http_requests_preserve_configured_user_agent() -> anyhow::Result<()> {
+ let custom_user_agent = "custom-agent/9.9";
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .and(path("/mcp"))
+ .and(header("user-agent", custom_user_agent))
+ .respond_with(|request: &Request| {
+ let body: Value = request.body_json().expect("valid JSON-RPC request");
+ match body.get("method").and_then(Value::as_str) {
+ Some("initialize") => ResponseTemplate::new(200).set_body_json(json!({
+ "jsonrpc": "2.0",
+ "id": body.get("id").cloned().unwrap_or(Value::Null),
+ "result": {
+ "protocolVersion": "2025-06-18",
+ "capabilities": {},
+ "serverInfo": {
+ "name": "user-agent-test",
+ "version": "0.0.0-test",
+ },
+ },
+ })),
+ Some("notifications/initialized") => ResponseTemplate::new(202),
+ method => ResponseTemplate::new(400)
+ .set_body_string(format!("unexpected JSON-RPC method: {method:?}")),
+ }
+ })
+ .expect(2)
+ .mount(&server)
+ .await;
+
+ let custom_client = RmcpClient::new_streamable_http_client(
+ "test-streamable-http",
+ &format!("{}/mcp", server.uri()),
+ Some("test-bearer".to_string()),
+ Some(HashMap::from([(
+ "user-agent".to_string(),
+ custom_user_agent.to_string(),
+ )])),
+ /*env_http_headers*/ None,
+ OAuthCredentialsStoreMode::File,
+ AuthKeyringBackendKind::default(),
+ Environment::default_for_tests().get_http_client(),
+ /*auth_provider*/ None,
+ )
+ .await?;
+ initialize_client(&custom_client).await?;
+ server.verify().await;
+
+ Ok(())
+}