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
+4
View File
@@ -476,6 +476,10 @@ pub struct ElicitationRequestEvent {
#[ts(optional)]
pub turn_id: Option<String>,
pub server_name: String,
/// Registration scope, not necessarily the server's execution environment.
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
pub environment_id: Option<String>,
#[ts(type = "string | number")]
pub id: RequestId,
pub request: ElicitationRequest,
+4
View File
@@ -481,6 +481,10 @@ pub enum McpAppDisplayMode {
pub struct McpToolCallItem {
pub id: String,
pub server: String,
/// Registration scope, not necessarily the server's execution environment.
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
pub environment_id: Option<String>,
pub tool: String,
pub arguments: serde_json::Value,
#[serde(default, skip_serializing_if = "Option::is_none")]
+2
View File
@@ -522,6 +522,7 @@ impl McpToolCallItem {
turn_id,
invocation: McpInvocation {
server: self.server.clone(),
environment_id: self.environment_id.clone(),
tool: self.tool.clone(),
arguments: (!self.arguments.is_null()).then(|| self.arguments.clone()),
},
@@ -548,6 +549,7 @@ impl McpToolCallItem {
turn_id,
invocation: McpInvocation {
server: self.server.clone(),
environment_id: self.environment_id.clone(),
tool: self.tool.clone(),
arguments: (!self.arguments.is_null()).then(|| self.arguments.clone()),
},
+11
View File
@@ -3034,6 +3034,10 @@ pub struct AgentReasoningSectionBreakEvent {
pub struct McpInvocation {
/// Name of the MCP server as defined in the config.
pub server: String,
/// Registration scope, not necessarily the server's execution environment.
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
pub environment_id: Option<String>,
/// Name of the tool as given by the MCP server.
pub tool: String,
/// Arguments to the tool call.
@@ -4199,6 +4203,10 @@ pub struct TurnDiffEvent {
pub struct McpStartupUpdateEvent {
/// Server name being started.
pub server: String,
/// Registration scope, not necessarily the server's execution environment.
#[serde(default, skip_serializing_if = "Option::is_none")]
#[ts(optional)]
pub environment_id: Option<String>,
/// Current startup status.
pub status: McpStartupStatus,
}
@@ -5844,6 +5852,7 @@ mod tests {
item: TurnItem::McpToolCall(McpToolCallItem {
id: "mcp-1".into(),
server: "server".into(),
environment_id: None,
tool: "tool".into(),
arguments: json!({"arg": "value"}),
connector_id: Some("connector".into()),
@@ -5974,6 +5983,7 @@ mod tests {
item: TurnItem::McpToolCall(McpToolCallItem {
id: "mcp-1".into(),
server: "server".into(),
environment_id: None,
tool: "tool".into(),
arguments: json!({"arg": "value"}),
connector_id: Some("connector".into()),
@@ -6853,6 +6863,7 @@ mod tests {
id: "init".to_string(),
msg: EventMsg::McpStartupUpdate(McpStartupUpdateEvent {
server: "srv".to_string(),
environment_id: None,
status: McpStartupStatus::Failed {
error: "boom".to_string(),
reason: Some(McpStartupFailureReason::ReauthenticationRequired),