agent_client_protocol/mcp_server/
context.rs1use crate::{ConnectionTo, role::Role};
2
3#[cfg(feature = "unstable_mcp_over_acp")]
4use crate::v1::schema::{McpRequestId, McpServerAcpId, SessionId, ToolCallId};
5#[cfg(feature = "unstable_mcp_over_acp")]
6use futures::channel::oneshot;
7#[cfg(feature = "unstable_mcp_over_acp")]
8use std::sync::{Arc, Mutex};
9
10#[derive(Clone, Debug, PartialEq, Eq)]
12#[non_exhaustive]
13pub enum McpConnectionContext {
14 Standalone,
16
17 #[cfg(feature = "unstable_mcp_over_acp")]
19 #[non_exhaustive]
20 Acp {
21 server_id: McpServerAcpId,
23
24 request_id: McpRequestId,
26
27 session_id: Option<SessionId>,
30
31 tool_call_id: Option<ToolCallId>,
34 },
35}
36
37impl McpConnectionContext {
38 #[must_use]
40 pub fn is_standalone(&self) -> bool {
41 matches!(self, Self::Standalone)
42 }
43
44 #[cfg(feature = "unstable_mcp_over_acp")]
48 #[must_use]
49 pub fn server_id(&self) -> Option<&McpServerAcpId> {
50 match self {
51 Self::Standalone => None,
52 Self::Acp { server_id, .. } => Some(server_id),
53 }
54 }
55
56 #[cfg(feature = "unstable_mcp_over_acp")]
60 #[must_use]
61 pub fn request_id(&self) -> Option<&McpRequestId> {
62 match self {
63 Self::Standalone => None,
64 Self::Acp { request_id, .. } => Some(request_id),
65 }
66 }
67
68 #[cfg(feature = "unstable_mcp_over_acp")]
78 #[must_use]
79 pub fn session_id(&self) -> Option<&SessionId> {
80 match self {
81 Self::Standalone => None,
82 Self::Acp { session_id, .. } => session_id.as_ref(),
83 }
84 }
85
86 #[cfg(feature = "unstable_mcp_over_acp")]
93 #[must_use]
94 pub fn tool_call_id(&self) -> Option<&ToolCallId> {
95 match self {
96 Self::Standalone => None,
97 Self::Acp { tool_call_id, .. } => tool_call_id.as_ref(),
98 }
99 }
100}
101
102#[derive(Clone, Debug)]
104pub struct McpConnectionTo<Counterpart: Role> {
105 pub(super) context: McpConnectionContext,
106 pub(super) connection: ConnectionTo<Counterpart>,
107 #[cfg(feature = "unstable_mcp_over_acp")]
108 pub(super) cleanup: Option<Arc<Mutex<Vec<oneshot::Receiver<()>>>>>,
109}
110
111impl<Counterpart: Role> McpConnectionTo<Counterpart> {
112 #[cfg(all(feature = "unstable_mcp_over_acp", feature = "schemars"))]
113 pub(crate) fn register_cleanup(&self, done: oneshot::Receiver<()>) {
114 if let Some(cleanup) = &self.cleanup {
115 cleanup.lock().expect("MCP cleanup poisoned").push(done);
116 }
117 }
118
119 #[cfg(feature = "unstable_mcp_over_acp")]
120 pub(crate) async fn wait_cleanup(&self) {
121 if let Some(cleanup) = &self.cleanup {
122 let pending = std::mem::take(&mut *cleanup.lock().expect("MCP cleanup poisoned"));
123 for done in pending {
124 let _finished = done.await;
125 }
126 }
127 }
128
129 #[must_use]
131 pub fn context(&self) -> &McpConnectionContext {
132 &self.context
133 }
134
135 #[cfg(feature = "unstable_mcp_over_acp")]
139 #[must_use]
140 pub fn server_id(&self) -> Option<&McpServerAcpId> {
141 self.context.server_id()
142 }
143
144 #[cfg(feature = "unstable_mcp_over_acp")]
148 #[must_use]
149 pub fn request_id(&self) -> Option<&McpRequestId> {
150 self.context.request_id()
151 }
152
153 #[cfg(feature = "unstable_mcp_over_acp")]
163 #[must_use]
164 pub fn session_id(&self) -> Option<&SessionId> {
165 self.context.session_id()
166 }
167
168 #[cfg(feature = "unstable_mcp_over_acp")]
175 #[must_use]
176 pub fn tool_call_id(&self) -> Option<&ToolCallId> {
177 self.context.tool_call_id()
178 }
179
180 #[must_use]
185 pub fn connection(&self) -> &ConnectionTo<Counterpart> {
186 &self.connection
187 }
188}
189
190#[cfg(test)]
191mod tests {
192 use super::McpConnectionContext;
193
194 #[test]
195 fn standalone_context_is_explicit() {
196 let context = McpConnectionContext::Standalone;
197
198 assert!(context.is_standalone());
199
200 #[cfg(feature = "unstable_mcp_over_acp")]
201 {
202 assert_eq!(context.server_id(), None);
203 assert_eq!(context.request_id(), None);
204 assert_eq!(context.session_id(), None);
205 assert_eq!(context.tool_call_id(), None);
206 }
207 }
208
209 #[cfg(feature = "unstable_mcp_over_acp")]
210 #[test]
211 fn acp_context_exposes_server_and_request_ids() {
212 use crate::v1::schema::{McpRequestId, McpServerAcpId, SessionId, ToolCallId};
213
214 let server_id = McpServerAcpId::new("server-id");
215 let request_id = McpRequestId::new("request-id");
216 let session_id = SessionId::new("session-id");
217 let tool_call_id = ToolCallId::new("tool-call-id");
218 let context = McpConnectionContext::Acp {
219 server_id: server_id.clone(),
220 request_id: request_id.clone(),
221 session_id: Some(session_id.clone()),
222 tool_call_id: Some(tool_call_id.clone()),
223 };
224
225 assert!(!context.is_standalone());
226 assert_eq!(context.server_id(), Some(&server_id));
227 assert_eq!(context.request_id(), Some(&request_id));
228 assert_eq!(context.session_id(), Some(&session_id));
229 assert_eq!(context.tool_call_id(), Some(&tool_call_id));
230 }
231}