a3s-code-core 8.6.0

A3S Code Core - Embeddable AI agent library with tool execution
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Immutable, generation-exact MCP runtime bindings.
//!
//! Compatibility MCP APIs resolve calls through a mutable multi-server
//! [`McpManager`](super::McpManager). Capability projections cannot use that
//! boundary: a later registration under the same server name could otherwise
//! change an already admitted Run. [`McpBinding`] instead owns one exact
//! initialized client and one canonical frozen tool catalog.

use std::fmt;
use std::sync::Arc;

use anyhow::{anyhow, Result};
use async_trait::async_trait;
use thiserror::Error;

use super::client::McpClient;
use super::protocol::McpTool;
use super::result::project_tool_result;
use super::tools::annotation_requires_confirmation;
use crate::tools::{Tool, ToolContext, ToolOutput};

pub const MAX_MCP_BINDING_TOOLS: usize = 1_024;
pub const MAX_MCP_BINDING_DEFINITION_BYTES: usize = 16 * 1024 * 1024;
const MAX_MCP_NAME_BYTES: usize = 256;
const MAX_MCP_FULL_TOOL_NAME_BYTES: usize = 768;

/// Invalid or no-longer-ready exact MCP binding.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
pub enum McpBindingError {
    #[error("MCP {field} is empty, padded, contains control characters, or exceeds its bound")]
    InvalidName { field: &'static str },
    #[error("MCP binding server name does not match its exact client identity")]
    ClientNameMismatch,
    #[error("MCP binding exceeds the {field} bound of {max}")]
    BoundExceeded { field: &'static str, max: usize },
    #[error("MCP binding repeats tool name '{name}'")]
    DuplicateToolName { name: String },
    #[error("MCP binding tool catalog is not serializable")]
    InvalidToolCatalog,
    #[error("MCP binding client is not initialized and connected")]
    ClientNotReady,
}

/// One exact connected MCP server projected into an immutable Code catalog.
///
/// The binding deliberately contains no manager, package locator, Grant, or
/// mutable discovery handle. Its client and canonical tool definitions are
/// frozen together, while the owning capability transaction retains the
/// client's asynchronous close effect. A Run separately retains the matching
/// non-clone A3S Use generation lease.
pub struct McpBinding {
    server_name: Box<str>,
    client: Arc<McpClient>,
    tools: Arc<[McpTool]>,
}

impl McpBinding {
    pub fn new(
        server_name: impl Into<String>,
        client: Arc<McpClient>,
        tools: impl IntoIterator<Item = McpTool>,
    ) -> std::result::Result<Self, McpBindingError> {
        let server_name = server_name.into();
        validate_name("server name", &server_name)?;
        if client.name != server_name {
            return Err(McpBindingError::ClientNameMismatch);
        }
        if !client.is_ready() {
            return Err(McpBindingError::ClientNotReady);
        }

        let mut tools = tools.into_iter().collect::<Vec<_>>();
        if tools.len() > MAX_MCP_BINDING_TOOLS {
            return Err(McpBindingError::BoundExceeded {
                field: "tool count",
                max: MAX_MCP_BINDING_TOOLS,
            });
        }
        tools.sort_by(|left, right| left.name.cmp(&right.name));

        let mut definition_bytes = 0_usize;
        let mut previous_name: Option<&str> = None;
        for tool in &tools {
            validate_name("tool name", &tool.name)?;
            let full_name_len = "mcp__"
                .len()
                .saturating_add(server_name.len())
                .saturating_add("__".len())
                .saturating_add(tool.name.len());
            if full_name_len > MAX_MCP_FULL_TOOL_NAME_BYTES {
                return Err(McpBindingError::BoundExceeded {
                    field: "fully qualified tool name bytes",
                    max: MAX_MCP_FULL_TOOL_NAME_BYTES,
                });
            }
            if previous_name == Some(tool.name.as_str()) {
                return Err(McpBindingError::DuplicateToolName {
                    name: tool.name.clone(),
                });
            }
            previous_name = Some(&tool.name);
            let encoded =
                serde_json::to_vec(tool).map_err(|_| McpBindingError::InvalidToolCatalog)?;
            definition_bytes = definition_bytes.saturating_add(encoded.len());
            if definition_bytes > MAX_MCP_BINDING_DEFINITION_BYTES {
                return Err(McpBindingError::BoundExceeded {
                    field: "tool definition bytes",
                    max: MAX_MCP_BINDING_DEFINITION_BYTES,
                });
            }
        }

        Ok(Self {
            server_name: server_name.into_boxed_str(),
            client,
            tools: tools.into(),
        })
    }

    pub fn server_name(&self) -> &str {
        &self.server_name
    }

    pub fn tools(&self) -> &[McpTool] {
        &self.tools
    }

    pub fn is_ready(&self) -> bool {
        self.client.is_ready()
    }

    pub fn validate_run_scope(&self) -> std::result::Result<(), McpBindingError> {
        if self.is_ready() {
            Ok(())
        } else {
            Err(McpBindingError::ClientNotReady)
        }
    }

    pub fn contains_public_tool_name(&self, full_name: &str) -> bool {
        self.tools
            .iter()
            .any(|tool| full_name == format!("mcp__{}__{}", self.server_name, tool.name))
    }

    pub(crate) fn projected_tools(self: &Arc<Self>) -> Vec<Arc<dyn Tool>> {
        self.tools
            .iter()
            .enumerate()
            .map(|(tool_index, tool)| {
                Arc::new(ProjectedMcpTool {
                    full_name: format!("mcp__{}__{}", self.server_name, tool.name).into_boxed_str(),
                    tool_index,
                    binding: Arc::clone(self),
                }) as Arc<dyn Tool>
            })
            .collect()
    }

    async fn call_tool(
        &self,
        tool_name: &str,
        arguments: Option<serde_json::Value>,
    ) -> Result<super::protocol::CallToolResult> {
        if self
            .tools
            .binary_search_by(|tool| tool.name.as_str().cmp(tool_name))
            .is_err()
        {
            return Err(anyhow!(
                "MCP tool '{}' is not present in the frozen server binding",
                tool_name
            ));
        }
        self.client.call_tool(tool_name, arguments).await
    }
}

impl fmt::Debug for McpBinding {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("McpBinding")
            .field("server_name", &self.server_name)
            .field("tool_count", &self.tools.len())
            .field("ready", &self.is_ready())
            .finish_non_exhaustive()
    }
}

struct ProjectedMcpTool {
    full_name: Box<str>,
    tool_index: usize,
    binding: Arc<McpBinding>,
}

impl ProjectedMcpTool {
    fn tool(&self) -> &McpTool {
        &self.binding.tools[self.tool_index]
    }
}

#[async_trait]
impl Tool for ProjectedMcpTool {
    fn name(&self) -> &str {
        &self.full_name
    }

    fn description(&self) -> &str {
        self.tool().description.as_deref().unwrap_or("MCP tool")
    }

    fn parameters(&self) -> serde_json::Value {
        self.tool().input_schema.clone()
    }

    fn requires_confirmation(&self, _args: &serde_json::Value) -> bool {
        annotation_requires_confirmation(self.tool())
    }

    async fn execute(&self, args: &serde_json::Value, context: &ToolContext) -> Result<ToolOutput> {
        if context.is_cancelled() {
            return Ok(ToolOutput::error(format!(
                "MCP tool '{}' cancelled by caller",
                self.full_name
            )));
        }

        let cancellation = context.cancellation_token();
        let call = self
            .binding
            .call_tool(&self.tool().name, Some(args.clone()));
        let result = tokio::select! {
            _ = cancellation.cancelled() => {
                return Ok(ToolOutput::error(format!(
                    "MCP tool '{}' cancelled by caller",
                    self.full_name
                )));
            }
            result = call => result,
        };
        match result {
            Ok(result) => project_tool_result(&self.full_name, &result, context).await,
            Err(error) => Ok(ToolOutput::error(format!("MCP tool error: {error}"))),
        }
    }
}

fn validate_name(field: &'static str, value: &str) -> std::result::Result<(), McpBindingError> {
    if value.is_empty()
        || value.trim() != value
        || value.len() > MAX_MCP_NAME_BYTES
        || value.chars().any(char::is_control)
    {
        return Err(McpBindingError::InvalidName { field });
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use super::*;
    use crate::mcp::test_support::{mcp_tool, ready_binding, RecordingMcpTransport};
    use crate::mcp::transport::McpTransport;
    use crate::mcp::McpProjectionAdapter;

    #[tokio::test]
    async fn binding_rejects_clients_that_are_not_ready_or_have_another_identity() {
        let transport = RecordingMcpTransport::new("uninitialized", Vec::new());
        let client = Arc::new(McpClient::new(
            "catalog".to_string(),
            Arc::clone(&transport) as Arc<dyn McpTransport>,
        ));
        assert_eq!(
            McpBinding::new("catalog", Arc::clone(&client), Vec::new()).unwrap_err(),
            McpBindingError::ClientNotReady
        );

        client.initialize().await.unwrap();
        assert_eq!(
            McpBinding::new("another", Arc::clone(&client), Vec::new()).unwrap_err(),
            McpBindingError::ClientNameMismatch
        );

        transport.disconnect();
        assert_eq!(
            McpBinding::new("catalog", client, Vec::new()).unwrap_err(),
            McpBindingError::ClientNotReady
        );
    }

    #[tokio::test]
    async fn binding_canonicalizes_tools_and_rejects_duplicate_or_oversized_catalogs() {
        let (_, _transport, client) = ready_binding("catalog", "one", Vec::new()).await;
        let binding = McpBinding::new(
            "catalog",
            Arc::clone(&client),
            [mcp_tool("zeta", "last"), mcp_tool("alpha", "first")],
        )
        .unwrap();
        assert_eq!(
            binding
                .tools()
                .iter()
                .map(|tool| tool.name.as_str())
                .collect::<Vec<_>>(),
            ["alpha", "zeta"]
        );

        assert_eq!(
            McpBinding::new(
                "catalog",
                Arc::clone(&client),
                [mcp_tool("same", "one"), mcp_tool("same", "two")],
            )
            .unwrap_err(),
            McpBindingError::DuplicateToolName {
                name: "same".to_string()
            }
        );

        let too_many = (0..=MAX_MCP_BINDING_TOOLS)
            .map(|index| mcp_tool(&format!("tool-{index:04}"), "bounded"))
            .collect::<Vec<_>>();
        assert_eq!(
            McpBinding::new("catalog", Arc::clone(&client), too_many).unwrap_err(),
            McpBindingError::BoundExceeded {
                field: "tool count",
                max: MAX_MCP_BINDING_TOOLS,
            }
        );

        let oversized = mcp_tool("oversized", &"x".repeat(MAX_MCP_BINDING_DEFINITION_BYTES));
        assert_eq!(
            McpBinding::new("catalog", client, [oversized]).unwrap_err(),
            McpBindingError::BoundExceeded {
                field: "tool definition bytes",
                max: MAX_MCP_BINDING_DEFINITION_BYTES,
            }
        );
    }

    #[tokio::test]
    async fn projected_wrapper_calls_the_raw_tool_on_the_exact_client() {
        let (binding, transport, _) = ready_binding(
            "catalog",
            "generation-one",
            vec![mcp_tool("lookup", "generation-one")],
        )
        .await;
        let wrappers = binding.projected_tools();
        assert_eq!(wrappers.len(), 1);
        assert_eq!(wrappers[0].name(), "mcp__catalog__lookup");
        assert_eq!(wrappers[0].description(), "generation-one");

        let arguments = serde_json::json!({"generation": "one"});
        let output = wrappers[0]
            .execute(&arguments, &ToolContext::new(PathBuf::from("/tmp")))
            .await
            .unwrap();
        assert!(output.success);
        assert_eq!(output.content, "generation-one");
        assert_eq!(
            transport.calls(),
            [crate::mcp::test_support::RecordedMcpCall {
                name: "lookup".to_string(),
                arguments: Some(arguments),
            }]
        );
    }

    #[tokio::test]
    async fn projected_wrapper_returns_promptly_when_caller_cancels() {
        let (binding, transport, _) = ready_binding(
            "catalog",
            "generation-one",
            vec![mcp_tool("lookup", "generation-one")],
        )
        .await;
        let wrappers = binding.projected_tools();
        let cancel = tokio_util::sync::CancellationToken::new();
        cancel.cancel();
        let output = wrappers[0]
            .execute(
                &serde_json::json!({}),
                &ToolContext::new(PathBuf::from("/tmp")).with_cancellation(cancel),
            )
            .await
            .unwrap();
        assert!(!output.success);
        assert!(output.content.contains("cancelled by caller"));
        assert!(
            transport.calls().is_empty(),
            "cancelled calls must not reach the MCP client"
        );
    }

    #[test]
    fn projected_mcp_types_are_send_and_sync() {
        fn assert_send_sync<T: Send + Sync>() {}

        assert_send_sync::<McpBinding>();
        assert_send_sync::<McpProjectionAdapter>();
    }
}