mentra 0.13.0

An agent runtime for tool-using LLM applications
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Tests for MCP registration through the runtime builder.

use serde_json::json;

use crate::mcp::sse::testing::SseTestServer;
use crate::mcp::{McpManager, McpSseServerConfig, mcp_tool_name};

/// Scripts a fixture through the handshake, advertising the given tools.
///
/// Returns the manager alongside the bridged tools: the manager owns the
/// connection those tools call through, so dropping it would close the stream.
async fn connect_sse(
    server: &SseTestServer,
    tools: serde_json::Value,
) -> (Vec<crate::mcp::McpBridgedTool>, McpManager) {
    let config = McpSseServerConfig::new("obs", server.sse_url());
    let connecting = tokio::spawn(async move {
        let mut manager = McpManager::new();
        let bridged = manager.connect_sse(&config).await;
        (bridged, manager)
    });

    server.wait_for_stream();
    server.send_endpoint("/messages/?session_id=abc");

    server.wait_for_posts(1);
    server.send_message(&json!({
        "jsonrpc": "2.0",
        "id": 1,
        "result": {
            "protocolVersion": "2024-11-05",
            "capabilities": {"tools": {}},
            "serverInfo": {"name": "fixture", "version": "4.5.6"}
        }
    }));

    server.wait_for_posts(3);
    server.send_message(&json!({
        "jsonrpc": "2.0",
        "id": 2,
        "result": {"tools": tools}
    }));

    let (bridged, manager) = connecting.await.expect("no panic");
    (bridged.expect("the handshake should succeed"), manager)
}

#[tokio::test(flavor = "multi_thread")]
async fn the_manager_bridges_sse_tools_under_a_namespaced_name() {
    let server = SseTestServer::start();
    let (bridged, _manager) = connect_sse(
        &server,
        json!([
            {"name": "search_logs", "description": "Search logs", "inputSchema": {"type": "object"}},
            {"name": "list_alerts", "inputSchema": {"type": "object"}}
        ]),
    )
    .await;

    use crate::tool::ToolDefinition;
    let names: Vec<String> = bridged
        .iter()
        .map(|tool| tool.descriptor().name.to_string())
        .collect();

    assert_eq!(
        names,
        vec![
            mcp_tool_name("obs", "search_logs"),
            mcp_tool_name("obs", "list_alerts"),
        ],
        "SSE tools must be namespaced exactly like stdio tools"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn a_bridged_sse_tool_carries_its_description_and_schema() {
    let server = SseTestServer::start();
    let (bridged, _manager) = connect_sse(
        &server,
        json!([{
            "name": "search_logs",
            "description": "Search the log corpus",
            "inputSchema": {
                "type": "object",
                "properties": {"query": {"type": "string"}},
                "required": ["query"]
            }
        }]),
    )
    .await;

    use crate::tool::ToolDefinition;
    let descriptor = bridged[0].descriptor();
    assert_eq!(
        descriptor.description.as_deref(),
        Some("Search the log corpus")
    );
    assert_eq!(
        descriptor.input_schema["properties"]["query"]["type"], "string",
        "the server's schema must reach the model unchanged"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn the_manager_reports_a_connected_sse_server() {
    let server = SseTestServer::start();
    let config = McpSseServerConfig::new("obs", server.sse_url());
    let connecting = tokio::spawn(async move {
        let mut manager = McpManager::new();
        let result = manager.connect_sse(&config).await;
        (result.map(|tools| tools.len()), manager)
    });

    server.wait_for_stream();
    server.send_endpoint("/messages/?session_id=abc");
    server.wait_for_posts(1);
    server.send_message(&json!({
        "jsonrpc": "2.0",
        "id": 1,
        "result": {
            "protocolVersion": "2024-11-05",
            "capabilities": {},
            "serverInfo": {"name": "fixture", "version": "4.5.6"}
        }
    }));
    server.wait_for_posts(3);
    server.send_message(&json!({
        "jsonrpc": "2.0",
        "id": 2,
        "result": {"tools": [{"name": "search", "inputSchema": {"type": "object"}}]}
    }));

    let (count, manager) = connecting.await.expect("no panic");
    assert_eq!(count.expect("the handshake should succeed"), 1);

    assert!(manager.is_connected("obs"));
    assert_eq!(manager.connected_count(), 1);
    assert_eq!(
        manager.all_tool_names(),
        vec![mcp_tool_name("obs", "search")]
    );

    let summary = manager
        .list_servers()
        .into_iter()
        .find(|summary| summary.name == "obs")
        .expect("the server should be listed");
    assert_eq!(summary.status, crate::mcp::McpServerStatus::Connected);
    assert_eq!(summary.server_version.as_deref(), Some("4.5.6"));
    assert_eq!(summary.tool_count, 1);
}

#[tokio::test(flavor = "multi_thread")]
async fn a_failed_sse_connection_is_recorded_as_an_error() {
    let server = SseTestServer::with_opening(crate::mcp::sse::testing::StreamOpening::Status {
        code: 404,
        body: "no such stream".to_string(),
    });

    let mut manager = McpManager::new();
    let config = McpSseServerConfig::new("obs", server.sse_url());
    manager
        .connect_sse(&config)
        .await
        .expect_err("a 404 must not connect");

    assert!(!manager.is_connected("obs"));
    let summary = manager
        .list_servers()
        .into_iter()
        .find(|summary| summary.name == "obs")
        .expect("an errored server should still be listed");
    assert_eq!(summary.status, crate::mcp::McpServerStatus::Error);
    assert!(summary.error.is_some());
}

#[tokio::test(flavor = "multi_thread")]
async fn a_misconfigured_sse_server_fails_before_any_connection_is_opened() {
    let mut manager = McpManager::new();
    // A token over plaintext to a non-loopback host is refused at validation.
    let config =
        McpSseServerConfig::new("obs", "http://internal.corp/sse").with_bearer_token("secret");

    let error = manager
        .connect_sse(&config)
        .await
        .expect_err("validation must reject this before dialing");
    assert!(
        !error.to_string().contains("secret"),
        "the error must not echo the credential: {error}"
    );
}

/// `build_async` must actually register the MCP tools it connects, and a
/// runtime built without any MCP server must not gain namespaced tools.
///
/// Without this, disabling the whole registration arm in `build_async` leaves
/// the suite green: the runtime still builds, it just silently advertises
/// nothing. That failure is invisible until an agent cannot find its tools.
#[tokio::test(flavor = "multi_thread")]
async fn build_async_registers_the_tools_of_a_connected_sse_server() {
    use crate::Runtime;

    let server = SseTestServer::start();
    let config = McpSseServerConfig::new("obs", server.sse_url());

    let building = tokio::spawn(async move {
        Runtime::empty_builder()
            .with_provider_instance(StubProvider)
            .with_mcp_sse_server(config)
            .build_async()
            .await
    });

    server.wait_for_stream();
    server.send_endpoint("/messages/?session_id=abc");
    server.wait_for_posts(1);
    server.send_message(&json!({
        "jsonrpc": "2.0",
        "id": 1,
        "result": {
            "protocolVersion": "2024-11-05",
            "capabilities": {},
            "serverInfo": {"name": "fixture", "version": "4.5.6"}
        }
    }));
    server.wait_for_posts(3);
    server.send_message(&json!({
        "jsonrpc": "2.0",
        "id": 2,
        "result": {"tools": [
            {"name": "search_logs", "inputSchema": {"type": "object"}},
            {"name": "list_alerts", "inputSchema": {"type": "object"}}
        ]}
    }));

    let runtime = building
        .await
        .expect("no panic")
        .expect("the runtime should build");

    let registered: Vec<String> = runtime
        .tools()
        .into_iter()
        .map(|tool| tool.name.to_string())
        .filter(|name| name.starts_with("mcp__"))
        .collect();

    assert_eq!(
        registered.len(),
        2,
        "build_async must register every tool the server advertised, got {registered:?}"
    );
    assert!(registered.contains(&mcp_tool_name("obs", "search_logs")));
    assert!(registered.contains(&mcp_tool_name("obs", "list_alerts")));

    assert!(
        runtime
            .tool_descriptor(&mcp_tool_name("obs", "search_logs"))
            .is_some(),
        "a registered MCP tool must be resolvable by name"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn build_async_registers_no_mcp_tools_without_a_configured_server() {
    use crate::Runtime;

    let runtime = Runtime::empty_builder()
        .with_provider_instance(StubProvider)
        .build_async()
        .await
        .expect("the runtime should build");

    let registered: Vec<String> = runtime
        .tools()
        .into_iter()
        .map(|tool| tool.name.to_string())
        .filter(|name| name.starts_with("mcp__"))
        .collect();

    assert!(
        registered.is_empty(),
        "no MCP server was configured, got {registered:?}"
    );
}

/// A provider that satisfies the builder's "at least one provider" check.
///
/// The registration tests never send a request, so it only needs to exist.
#[derive(Clone)]
struct StubProvider;

#[async_trait::async_trait]
impl crate::provider::Provider for StubProvider {
    fn descriptor(&self) -> crate::provider::ProviderDescriptor {
        crate::provider::ProviderDescriptor::new(crate::BuiltinProvider::Anthropic)
    }

    async fn list_models(&self) -> Result<Vec<crate::ModelInfo>, crate::provider::ProviderError> {
        Ok(vec![crate::ModelInfo::new(
            "stub-model",
            crate::BuiltinProvider::Anthropic,
        )])
    }

    async fn stream(
        &self,
        _request: crate::provider::Request<'_>,
    ) -> Result<crate::provider::ProviderEventStream, crate::provider::ProviderError> {
        let (_tx, rx) = tokio::sync::mpsc::unbounded_channel();
        Ok(rx)
    }
}

/// An SSE-backed tool must reach the model through exactly the same result
/// limiter and paging path as a stdio tool or a custom tool. This runs a real
/// fixture server behind a bridged tool inside a scripted runtime and compares
/// its transcript entry byte for byte against a custom tool returning the same
/// text.
#[tokio::test(flavor = "multi_thread")]
async fn sse_tool_output_is_limited_exactly_like_a_custom_tool() {
    use std::collections::BTreeMap;

    use crate::{
        ContentBlock,
        runtime::RuntimePolicy,
        test::{MockRuntime, MockToolCall},
        tool::ToolDefinition,
    };

    let full_output = "one\ntwo\nthree";
    let server = SseTestServer::start();
    let (bridged, _manager) = connect_sse(
        &server,
        json!([{"name": "large_output", "inputSchema": {"type": "object"}}]),
    )
    .await;
    let bridged_name = bridged[0].descriptor().name.to_string();

    let mock = MockRuntime::builder()
        .with_policy(
            RuntimePolicy::permissive()
                .with_max_tool_result_bytes(8)
                .with_max_tool_result_lines(1)
                .spill_full_tool_output(false),
        )
        .tool_calls([
            MockToolCall::new(&bridged_name, json!({})).with_id("sse-call"),
            MockToolCall::new("matching_custom_output", json!({})).with_id("custom-call"),
        ])
        .text("done")
        .build()
        .expect("build mock runtime");

    for tool in bridged {
        mock.runtime().register_tool(tool);
    }
    mock.runtime().register_tool(EchoTool {
        output: full_output.to_string(),
    });

    let mut agent = mock
        .runtime()
        .spawn("mcp-sse-truncation-test", mock.model())
        .expect("spawn agent");

    let running = tokio::spawn(async move {
        let response = agent
            .send(vec![ContentBlock::text("run both tools")])
            .await
            .expect("run agent");
        (response, agent)
    });

    // Answer the bridged tool call once the runtime has posted it.
    server.wait_for_posts(4);
    server.send_message(&json!({
        "jsonrpc": "2.0",
        "id": 3,
        "result": {
            "content": [{"type": "text", "text": full_output}],
            "isError": false
        }
    }));

    let (response, _agent) = running.await.expect("no panic");
    assert_eq!(response.text(), "done");

    let requests = mock.recorded_requests().await;
    let provider_results = requests[1]
        .messages
        .iter()
        .flat_map(|message| &message.content)
        .filter_map(|block| match block {
            ContentBlock::ToolResult {
                tool_use_id,
                content,
                is_error,
            } => Some((tool_use_id.as_str(), (content.as_str(), *is_error))),
            _ => None,
        })
        .collect::<BTreeMap<_, _>>();

    let sse_result = provider_results
        .get("sse-call")
        .expect("the SSE tool result should reach the provider");
    let custom_result = provider_results
        .get("custom-call")
        .expect("the custom tool result should reach the provider");

    assert_eq!(
        sse_result, custom_result,
        "an SSE tool must be limited identically to any other tool"
    );
    assert_eq!(
        *sse_result,
        (
            "one\n[truncated: showing 1 of 3 lines; full output was not saved because spill-to-file is disabled by runtime policy]",
            false,
        )
    );
}

/// A custom tool returning a fixed string, used as the limiter baseline.
struct EchoTool {
    output: String,
}

impl crate::tool::ToolDefinition for EchoTool {
    fn descriptor(&self) -> crate::tool::ToolSpec {
        crate::tool::ToolSpec::builder("matching_custom_output")
            .description("Return the same output as the MCP test tool")
            .input_schema(json!({ "type": "object", "properties": {} }))
            .side_effect_level(crate::tool::ToolSideEffectLevel::External)
            .build()
    }
}

#[async_trait::async_trait]
impl crate::tool::ToolExecutor for EchoTool {
    async fn execute(
        &self,
        _ctx: crate::tool::ParallelToolContext,
        _input: serde_json::Value,
    ) -> crate::tool::ToolResult {
        Ok(self.output.clone())
    }
}