Preserve MCP registration scope across client operations (#61257)

## Why

MCP servers registered under the same name in different environments need distinct inventory, routing, and status reporting.

## What changed

- Keep MCP inventory and runtime status keyed by registration scope so same-named servers remain separate.
- Add optional `environmentId` selectors for status lookup, tool calls, resource reads, and OAuth login. Omitting the selector preserves existing name-only routing.
- Carry environment identity through startup notifications, elicitation requests, and tool-call history, and display it in the TUI.
- Support `/mcp login <name> --environment <id>` and update generated protocol schemas and SDK types.

## Testing

Add integration coverage for two environments with identically named MCP servers, tools, and resource URIs. Verify separate inventory, explicit environment routing, legacy routing when the selector is omitted, and errors for unknown environments. Update protocol and TUI tests for environment identity.

GitOrigin-RevId: 074a3db10549438cb6aff33eb7bed10791adae4d
This commit is contained in:
sayan-oai
2026-11-13 04:51:06 +00:00
committed by copyberry
parent 7eaaee4270
commit 6fcfa75a9f
190 changed files with 1821 additions and 502 deletions
@@ -2586,6 +2586,7 @@ mod tests {
let mcp_oauth = ClientRequest::McpServerOauthLogin {
request_id: request_id(),
params: v2::McpServerOauthLoginParams {
environment_id: None,
name: "server-a".to_string(),
thread_id: None,
client_registration: None,
@@ -2603,6 +2604,7 @@ mod tests {
let mcp_resource_read = ClientRequest::McpResourceRead {
request_id: request_id(),
params: v2::McpResourceReadParams {
environment_id: None,
meta: None,
thread_id: Some("thread-1".to_string()),
origin_call_id: None,
@@ -2799,6 +2801,7 @@ mod tests {
let mcp_resource_read = ClientRequest::McpResourceRead {
request_id: request_id(),
params: v2::McpResourceReadParams {
environment_id: None,
meta: None,
thread_id: None,
origin_call_id: None,
@@ -3193,6 +3196,7 @@ mod tests {
thread_id: "thr_123".to_string(),
turn_id: Some("turn_123".to_string()),
server_name: "codex_apps".to_string(),
environment_id: None,
request: v2::McpServerElicitationRequest::Form {
meta: Some(json!({
"suggestion_id": "request_plugin_install_install-github"
@@ -3214,6 +3218,7 @@ mod tests {
"threadId": "thr_123",
"turnId": "turn_123",
"serverName": "codex_apps",
"environmentId": null,
"mode": "form",
"_meta": {
"suggestion_id": "request_plugin_install_install-github"
@@ -961,6 +961,7 @@ impl ThreadHistoryBuilder {
let item = ThreadItemPayload::McpToolCall {
id: payload.call_id.clone(),
server: payload.invocation.server.clone(),
environment_id: payload.invocation.environment_id.clone(),
tool: payload.invocation.tool.clone(),
status: McpToolCallStatus::InProgress,
arguments: payload
@@ -1019,6 +1020,7 @@ impl ThreadHistoryBuilder {
let item = ThreadItemPayload::McpToolCall {
id: payload.call_id.clone(),
server: payload.invocation.server.clone(),
environment_id: payload.invocation.environment_id.clone(),
tool: payload.invocation.tool.clone(),
status,
arguments: payload
@@ -3590,6 +3592,7 @@ mod tests {
call_id: "mcp-1".into(),
invocation: McpInvocation {
server: "docs".into(),
environment_id: None,
tool: "lookup".into(),
arguments: Some(serde_json::json!({"id":"123"})),
},
@@ -3655,6 +3658,7 @@ mod tests {
ThreadItemPayload::McpToolCall {
id: "mcp-1".into(),
server: "docs".into(),
environment_id: None,
tool: "lookup".into(),
status: McpToolCallStatus::Failed,
arguments: serde_json::json!({"id":"123"}),
@@ -3689,6 +3693,7 @@ mod tests {
call_id: "mcp-1".into(),
invocation: McpInvocation {
server: "docs".into(),
environment_id: None,
tool: "lookup".into(),
arguments: Some(serde_json::json!({"id":"123"})),
},
@@ -3726,6 +3731,7 @@ mod tests {
ThreadItemPayload::McpToolCall {
id: "mcp-1".into(),
server: "docs".into(),
environment_id: None,
tool: "lookup".into(),
status: McpToolCallStatus::Completed,
arguments: serde_json::json!({"id":"123"}),
@@ -4135,6 +4141,7 @@ mod tests {
item: CoreTurnItem::McpToolCall(McpToolCallItem {
id: "mcp-1".into(),
server: "slack".into(),
environment_id: None,
tool: "search".into(),
arguments: serde_json::json!({"query": "incident"}),
connector_id: None,
@@ -422,6 +422,8 @@ pub enum ThreadItemPayload {
McpToolCall {
id: String,
server: String,
/// Registration scope, not necessarily the server's execution environment.
environment_id: Option<String>,
tool: String,
status: McpToolCallStatus,
arguments: JsonValue,
@@ -1129,6 +1131,7 @@ impl From<CoreTurnItem> for ThreadItemPayload {
Self::McpToolCall {
id: mcp.id,
server: mcp.server,
environment_id: mcp.environment_id,
tool: mcp.tool,
status: McpToolCallStatus::from(mcp.status),
arguments: mcp.arguments,
@@ -63,6 +63,10 @@ pub struct ListMcpServerStatusParams {
/// Limit discovery to one server. With a thread ID, reuse that thread's MCP connection.
#[ts(optional = nullable)]
pub server_name: Option<String>,
/// Select this environment's registration. Requires `serverName` and `threadId`.
/// When omitted, a named request uses the same server as older clients.
#[ts(optional = nullable)]
pub environment_id: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, JsonSchema, TS)]
@@ -78,6 +82,10 @@ pub enum McpServerStatusDetail {
#[ts(export_to = "v2/")]
pub struct McpServerStatus {
pub name: String,
/// Environment that contributed this registration; null for a thread-wide server.
/// Older servers omit this field.
#[ts(optional, as = "Option<Option<String>>")]
pub environment_id: Option<String>,
/// Current thread-runtime connection state; null when unavailable or the configuration changed.
pub runtime_status: Option<McpServerConnectionStatus>,
pub plugin_id: Option<String>,
@@ -122,6 +130,9 @@ pub struct McpResourceReadParams {
#[ts(optional = nullable)]
pub origin_call_id: Option<String>,
pub server: String,
/// Exact registration scope; omission preserves legacy name-only selection.
#[ts(optional = nullable)]
pub environment_id: Option<String>,
pub uri: String,
#[ts(optional = nullable)]
pub connector_id: Option<String>,
@@ -207,6 +218,9 @@ pub struct McpServerEventStreamNotification {
pub struct McpServerToolCallParams {
pub thread_id: String,
pub server: String,
/// Exact registration scope; omission preserves legacy name-only selection.
#[ts(optional = nullable)]
pub environment_id: Option<String>,
pub tool: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
@@ -299,6 +313,9 @@ pub struct McpServerRefreshResponse {}
#[ts(export_to = "v2/")]
pub struct McpServerOauthLoginParams {
pub name: String,
/// Exact registration scope; omission preserves legacy name-only selection.
#[ts(optional = nullable)]
pub environment_id: Option<String>,
#[ts(optional = nullable)]
pub thread_id: Option<String>,
/// Registration strategy for this login only; omission selects automatic discovery.
@@ -374,6 +391,8 @@ pub enum McpServerStartupState {
pub struct McpServerStatusUpdatedNotification {
pub thread_id: Option<String>,
pub name: String,
/// Registration scope, not necessarily the server's execution environment.
pub environment_id: Option<String>,
pub status: McpServerStartupState,
pub error: Option<String>,
pub failure_reason: Option<McpServerStartupFailureReason>,
@@ -433,6 +452,8 @@ pub struct McpServerElicitationRequestParams {
/// elicitation itself.
pub turn_id: Option<String>,
pub server_name: String,
/// Registration scope, not necessarily the server's execution environment.
pub environment_id: Option<String>,
#[serde(flatten)]
#[experimental(nested)]
pub request: McpServerElicitationRequest,
@@ -2694,6 +2694,7 @@ fn mcp_server_elicitation_response_serializes_nullable_content() {
fn mcp_server_status_serializes_absent_server_info_as_null() {
let response = ListMcpServerStatusResponse {
data: vec![McpServerStatus {
environment_id: None,
tool_list_meta: None,
server_capabilities: None,
tools_error: None,
@@ -2715,6 +2716,7 @@ fn mcp_server_status_serializes_absent_server_info_as_null() {
json!({
"data": [{
"name": "not-ready",
"environmentId": null,
"runtimeStatus": null,
"pluginId": null,
"httpOrigin": null,
@@ -2747,6 +2749,7 @@ fn mcp_server_status_accepts_older_inventory_without_runtime_status() {
assert_eq!(
status,
McpServerStatus {
environment_id: None,
tool_list_meta: None,
server_capabilities: None,
tools_error: None,
@@ -2775,6 +2778,7 @@ fn mcp_server_status_updated_accepts_missing_thread_id() {
let expected = McpServerStatusUpdatedNotification {
thread_id: None,
name: "optional_broken".to_string(),
environment_id: None,
status: McpServerStartupState::Failed,
error: Some("handshake failed".to_string()),
failure_reason: None,
@@ -2785,6 +2789,7 @@ fn mcp_server_status_updated_accepts_missing_thread_id() {
json!({
"threadId": null,
"name": "optional_broken",
"environmentId": null,
"status": "failed",
"error": "handshake failed",
"failureReason": null,
@@ -2798,6 +2803,7 @@ fn mcp_server_status_updated_serializes_failure_reason() {
ServerNotification::McpServerStatusUpdated(McpServerStatusUpdatedNotification {
thread_id: Some("thread-1".to_string()),
name: "expired-oauth".to_string(),
environment_id: Some("remote".to_string()),
status: McpServerStartupState::Failed,
error: Some("OAuth credentials expired".to_string()),
failure_reason: Some(McpServerStartupFailureReason::ReauthenticationRequired),
@@ -2810,6 +2816,7 @@ fn mcp_server_status_updated_serializes_failure_reason() {
"params": {
"threadId": "thread-1",
"name": "expired-oauth",
"environmentId": "remote",
"status": "failed",
"error": "OAuth credentials expired",
"failureReason": "reauthenticationRequired",
@@ -2822,6 +2829,7 @@ fn mcp_server_status_updated_serializes_failure_reason() {
fn mcp_server_status_serializes_absent_server_info_metadata_as_null() {
let response = ListMcpServerStatusResponse {
data: vec![McpServerStatus {
environment_id: None,
tool_list_meta: None,
server_capabilities: None,
tools_error: None,
@@ -2850,6 +2858,7 @@ fn mcp_server_status_serializes_absent_server_info_metadata_as_null() {
json!({
"data": [{
"name": "initialized",
"environmentId": null,
"runtimeStatus": null,
"pluginId": "lookup@test",
"httpOrigin": null,
@@ -3714,6 +3723,7 @@ fn core_turn_item_into_thread_item_converts_supported_variants() {
let mcp_tool_call_item = TurnItem::McpToolCall(McpToolCallItem {
id: "mcp-1".to_string(),
server: "server".to_string(),
environment_id: Some("remote".to_string()),
tool: "tool".to_string(),
arguments: json!({"arg": "value"}),
connector_id: Some("calendar".to_string()),
@@ -3735,6 +3745,7 @@ fn core_turn_item_into_thread_item_converts_supported_variants() {
ThreadItemPayload::McpToolCall {
id: "mcp-1".to_string(),
server: "server".to_string(),
environment_id: Some("remote".to_string()),
tool: "tool".to_string(),
status: McpToolCallStatus::InProgress,
arguments: json!({"arg": "value"}),
@@ -3758,6 +3769,7 @@ fn core_turn_item_into_thread_item_converts_supported_variants() {
let completed_mcp_tool_call_item = TurnItem::McpToolCall(McpToolCallItem {
id: "mcp-2".to_string(),
server: "server".to_string(),
environment_id: None,
tool: "tool".to_string(),
arguments: JsonValue::Null,
connector_id: None,
@@ -3784,6 +3796,7 @@ fn core_turn_item_into_thread_item_converts_supported_variants() {
ThreadItemPayload::McpToolCall {
id: "mcp-2".to_string(),
server: "server".to_string(),
environment_id: None,
tool: "tool".to_string(),
status: McpToolCallStatus::Completed,
arguments: JsonValue::Null,
@@ -3821,6 +3834,7 @@ fn mcp_tool_call_app_context_serializes_connector_id() {
let item = ThreadItemPayload::McpToolCall {
id: "mcp-1".to_string(),
server: "codex_apps".to_string(),
environment_id: None,
tool: "calendar.create_event".to_string(),
status: McpToolCallStatus::InProgress,
arguments: json!({}),
@@ -3846,6 +3860,7 @@ fn mcp_tool_call_app_context_serializes_connector_id() {
"type": "mcpToolCall",
"id": "mcp-1",
"server": "codex_apps",
"environmentId": null,
"tool": "calendar.create_event",
"status": "inProgress",
"arguments": {},