use crate::{ConnectionTo, role::Role};
#[cfg(feature = "unstable_mcp_over_acp")]
use crate::schema::v1::{McpConnectionId, McpServerAcpId};
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum McpConnectionContext {
Standalone,
#[cfg(feature = "unstable_mcp_over_acp")]
#[cfg_attr(docsrs, doc(cfg(feature = "unstable_mcp_over_acp")))]
Acp {
server_id: McpServerAcpId,
connection_id: McpConnectionId,
},
}
impl McpConnectionContext {
#[must_use]
pub fn is_standalone(&self) -> bool {
matches!(self, Self::Standalone)
}
#[cfg(feature = "unstable_mcp_over_acp")]
#[cfg_attr(docsrs, doc(cfg(feature = "unstable_mcp_over_acp")))]
#[must_use]
pub fn server_id(&self) -> Option<&McpServerAcpId> {
match self {
Self::Standalone => None,
Self::Acp { server_id, .. } => Some(server_id),
}
}
#[cfg(feature = "unstable_mcp_over_acp")]
#[cfg_attr(docsrs, doc(cfg(feature = "unstable_mcp_over_acp")))]
#[must_use]
pub fn connection_id(&self) -> Option<&McpConnectionId> {
match self {
Self::Standalone => None,
Self::Acp { connection_id, .. } => Some(connection_id),
}
}
}
#[derive(Clone, Debug)]
pub struct McpConnectionTo<Counterpart: Role> {
pub(super) context: McpConnectionContext,
pub(super) connection: ConnectionTo<Counterpart>,
}
impl<Counterpart: Role> McpConnectionTo<Counterpart> {
#[must_use]
pub fn context(&self) -> &McpConnectionContext {
&self.context
}
#[cfg(feature = "unstable_mcp_over_acp")]
#[cfg_attr(docsrs, doc(cfg(feature = "unstable_mcp_over_acp")))]
#[must_use]
pub fn server_id(&self) -> Option<&McpServerAcpId> {
self.context.server_id()
}
#[cfg(feature = "unstable_mcp_over_acp")]
#[cfg_attr(docsrs, doc(cfg(feature = "unstable_mcp_over_acp")))]
#[must_use]
pub fn connection_id(&self) -> Option<&McpConnectionId> {
self.context.connection_id()
}
#[must_use]
pub fn connection(&self) -> &ConnectionTo<Counterpart> {
&self.connection
}
}
#[cfg(test)]
mod tests {
use super::McpConnectionContext;
#[test]
fn standalone_context_is_explicit() {
let context = McpConnectionContext::Standalone;
assert!(context.is_standalone());
#[cfg(feature = "unstable_mcp_over_acp")]
{
assert_eq!(context.server_id(), None);
assert_eq!(context.connection_id(), None);
}
}
#[cfg(feature = "unstable_mcp_over_acp")]
#[test]
fn acp_context_exposes_server_and_connection_ids() {
use crate::schema::v1::{McpConnectionId, McpServerAcpId};
let server_id = McpServerAcpId::new("server-id");
let connection_id = McpConnectionId::new("connection-id");
let context = McpConnectionContext::Acp {
server_id: server_id.clone(),
connection_id: connection_id.clone(),
};
assert!(!context.is_standalone());
assert_eq!(context.server_id(), Some(&server_id));
assert_eq!(context.connection_id(), Some(&connection_id));
}
}