nexo-core 0.1.13

Agent runtime: event bus, sessions, plugin trait, heartbeat, A2A delegation.
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
//! Phase 12.5 — 2 meta-tools per MCP server that exposes `resources/*`:
//! `mcp_{server}_list_resources` and `mcp_{server}_read_resource`. The LLM
//! uses them to browse and pull data surfaces on demand.
use super::context::AgentContext;
use super::mcp_tool::{sanitize_name_fragment, MCP_NAME_PREFIX};
use super::tool_registry::ToolHandler;
use async_trait::async_trait;
use base64::Engine;
use nexo_llm::ToolDef;
use nexo_mcp::{McpClient, McpResourceContent, ResourceCache};
use serde_json::Value;
use std::sync::Arc;
/// Tail fragment used when composing the list-resources meta-tool name.
/// Kept public so downstream code can pattern-match on suffix if needed.
/// The underscore separator between server name and this fragment is
/// supplied by `ToolDef::fit_name`, not baked in here.
pub const RESOURCE_LIST_SUFFIX: &str = "list_resources";
pub const RESOURCE_READ_SUFFIX: &str = "read_resource";
pub const RESOURCE_TEMPLATES_SUFFIX: &str = "list_resource_templates";
pub struct McpResourceListTool {
    server_name: String,
    client: Arc<dyn McpClient>,
    context_passthrough: bool,
}
pub struct McpResourceReadTool {
    server_name: String,
    client: Arc<dyn McpClient>,
    context_passthrough: bool,
    cache: Option<Arc<ResourceCache>>,
    /// Phase 12.5 follow-up — if non-empty, URIs whose scheme is absent
    /// from the list are rejected before dispatch, logged at `warn`,
    /// and counted via `mcp_resource_uri_allowlist_violations_total`.
    uri_allowlist: Arc<Vec<String>>,
}
pub struct McpResourceListTemplatesTool {
    server_name: String,
    client: Arc<dyn McpClient>,
}
impl McpResourceListTool {
    pub fn new(server_name: impl Into<String>, client: Arc<dyn McpClient>) -> Self {
        Self {
            server_name: server_name.into(),
            client,
            context_passthrough: false,
        }
    }
    /// Phase 12.8 — enable `_meta` propagation on `resources/list`.
    pub fn with_context_passthrough(mut self, enabled: bool) -> Self {
        self.context_passthrough = enabled;
        self
    }
    pub fn server_name(&self) -> &str {
        &self.server_name
    }
    pub fn prefixed_name(server_name: &str) -> String {
        ToolDef::fit_name(
            MCP_NAME_PREFIX,
            &sanitize_name_fragment(server_name),
            RESOURCE_LIST_SUFFIX,
        )
    }
    pub fn tool_def(server_name: &str) -> ToolDef {
        ToolDef {
            name: Self::prefixed_name(server_name),
            description: format!(
                "[mcp:{server_name}] List data resources this server exposes. \
                 No arguments. Returns an array of {{uri, name, description, mime_type}}."
            ),
            parameters: serde_json::json!({
                "type": "object",
                "properties": {},
                "additionalProperties": false
            }),
        }
    }
}
impl McpResourceReadTool {
    pub fn new(server_name: impl Into<String>, client: Arc<dyn McpClient>) -> Self {
        Self {
            server_name: server_name.into(),
            client,
            context_passthrough: false,
            cache: None,
            uri_allowlist: Arc::new(Vec::new()),
        }
    }
    pub fn with_uri_allowlist(mut self, allowlist: Arc<Vec<String>>) -> Self {
        self.uri_allowlist = allowlist;
        self
    }
    /// Phase 12.8 — enable `_meta` propagation on `resources/read`.
    pub fn with_context_passthrough(mut self, enabled: bool) -> Self {
        self.context_passthrough = enabled;
        self
    }
    /// Phase 12.5 follow-up — opt-in LRU+TTL cache shared with the owning
    /// session runtime. Cache is skipped for blob-only responses to keep
    /// memory usage bounded.
    pub fn with_cache(mut self, cache: Arc<ResourceCache>) -> Self {
        self.cache = Some(cache);
        self
    }
    pub fn server_name(&self) -> &str {
        &self.server_name
    }
    pub fn prefixed_name(server_name: &str) -> String {
        ToolDef::fit_name(
            MCP_NAME_PREFIX,
            &sanitize_name_fragment(server_name),
            RESOURCE_READ_SUFFIX,
        )
    }
    pub fn tool_def(server_name: &str) -> ToolDef {
        ToolDef {
            name: Self::prefixed_name(server_name),
            description: format!(
                "[mcp:{server_name}] Read a data resource by uri (obtain candidate uris from \
                 the list_resources tool). Returns the resource text, or a marker for binary \
                 content."
            ),
            parameters: serde_json::json!({
                "type": "object",
                "properties": { "uri": { "type": "string" } },
                "required": ["uri"],
                "additionalProperties": false
            }),
        }
    }
    /// Collapse the raw MCP response into a single LLM-consumable string.
    pub fn flatten_read_result(contents: &[McpResourceContent]) -> Value {
        if contents.is_empty() {
            return Value::String(String::new());
        }
        let parts: Vec<String> = contents
            .iter()
            .map(|c| {
                if let Some(t) = c.text.as_deref() {
                    t.to_string()
                } else if let Some(blob) = c.blob.as_deref() {
                    let byte_len = base64::engine::general_purpose::STANDARD
                        .decode(blob)
                        .map(|b| b.len())
                        .unwrap_or(blob.len());
                    let mime = c.mime_type.as_deref().unwrap_or("?");
                    format!("[blob: {mime}, {byte_len} bytes]")
                } else {
                    "[empty resource content]".to_string()
                }
            })
            .collect();
        Value::String(parts.join("\n\n"))
    }
}

pub(crate) fn ensure_uri_scheme_allowed(
    server_name: &str,
    uri: &str,
    allowlist: &[String],
) -> anyhow::Result<()> {
    if allowlist.is_empty() {
        return Ok(());
    }
    let scheme = uri.split(':').next().unwrap_or("");
    if allowlist.iter().any(|s| s == scheme) {
        return Ok(());
    }
    tracing::warn!(
        mcp = %server_name,
        uri = %uri,
        scheme = %scheme,
        allowed = ?allowlist,
        "mcp read_resource: URI scheme outside allowlist"
    );
    nexo_mcp::telemetry::inc_resource_uri_allowlist_violation(server_name);
    Err(anyhow::anyhow!(
        "mcp '{server_name}' read_resource: URI scheme '{scheme}' is not allowed by resource_uri_allowlist"
    ))
}
impl McpResourceListTemplatesTool {
    pub fn new(server_name: impl Into<String>, client: Arc<dyn McpClient>) -> Self {
        Self {
            server_name: server_name.into(),
            client,
        }
    }
    pub fn server_name(&self) -> &str {
        &self.server_name
    }
    pub fn prefixed_name(server_name: &str) -> String {
        ToolDef::fit_name(
            MCP_NAME_PREFIX,
            &sanitize_name_fragment(server_name),
            RESOURCE_TEMPLATES_SUFFIX,
        )
    }
    pub fn tool_def(server_name: &str) -> ToolDef {
        ToolDef {
            name: Self::prefixed_name(server_name),
            description: format!(
                "[mcp:{server_name}] List RFC-6570 URI templates this server exposes. \
                 No arguments. Returns an array of {{uri_template, name, description, mime_type}}. \
                 Use the templates to construct concrete URIs for read_resource."
            ),
            parameters: serde_json::json!({
                "type": "object",
                "properties": {},
                "additionalProperties": false
            }),
        }
    }
}
#[async_trait]
impl ToolHandler for McpResourceListTemplatesTool {
    async fn call(&self, _ctx: &AgentContext, _args: Value) -> anyhow::Result<Value> {
        let templates = self.client.list_resource_templates().await.map_err(|e| {
            anyhow::anyhow!(
                "mcp '{}' list_resource_templates error: {e}",
                self.server_name
            )
        })?;
        let list: Vec<Value> = templates
            .into_iter()
            .map(|t| {
                let mut obj = serde_json::Map::new();
                obj.insert("uri_template".into(), Value::String(t.uri_template));
                if !t.name.is_empty() {
                    obj.insert("name".into(), Value::String(t.name));
                }
                if let Some(d) = t.description {
                    obj.insert("description".into(), Value::String(d));
                }
                if let Some(m) = t.mime_type {
                    obj.insert("mime_type".into(), Value::String(m));
                }
                Value::Object(obj)
            })
            .collect();
        Ok(Value::Array(list))
    }
}
fn make_meta(ctx: &AgentContext) -> Value {
    serde_json::json!({
        "agent_id": ctx.agent_id,
        "session_id": ctx.session_id.map(|u| u.to_string()),
    })
}
#[async_trait]
impl ToolHandler for McpResourceListTool {
    async fn call(&self, ctx: &AgentContext, _args: Value) -> anyhow::Result<Value> {
        let meta = if self.context_passthrough {
            Some(make_meta(ctx))
        } else {
            None
        };
        let resources = self
            .client
            .list_resources_with_meta(meta)
            .await
            .map_err(|e| anyhow::anyhow!("mcp '{}' list_resources error: {e}", self.server_name))?;
        let list: Vec<Value> = resources
            .into_iter()
            .map(|r| {
                let mut obj = serde_json::Map::new();
                obj.insert("uri".into(), Value::String(r.uri));
                if !r.name.is_empty() {
                    obj.insert("name".into(), Value::String(r.name));
                }
                if let Some(d) = r.description {
                    obj.insert("description".into(), Value::String(d));
                }
                if let Some(m) = r.mime_type {
                    obj.insert("mime_type".into(), Value::String(m));
                }
                if let Some(ann) = r.annotations {
                    let mut ann_obj = serde_json::Map::new();
                    if !ann.audience.is_empty() {
                        ann_obj.insert(
                            "audience".into(),
                            Value::Array(ann.audience.into_iter().map(Value::String).collect()),
                        );
                    }
                    if let Some(p) = ann.priority {
                        if let Some(n) = serde_json::Number::from_f64(p as f64) {
                            ann_obj.insert("priority".into(), Value::Number(n));
                        }
                    }
                    if !ann_obj.is_empty() {
                        obj.insert("annotations".into(), Value::Object(ann_obj));
                    }
                }
                Value::Object(obj)
            })
            .collect();
        Ok(Value::Array(list))
    }
}
#[async_trait]
impl ToolHandler for McpResourceReadTool {
    async fn call(&self, ctx: &AgentContext, args: Value) -> anyhow::Result<Value> {
        let uri = args.get("uri").and_then(|v| v.as_str()).ok_or_else(|| {
            anyhow::anyhow!(
                "mcp '{}' read_resource: missing required 'uri' argument",
                self.server_name
            )
        })?;
        ensure_uri_scheme_allowed(&self.server_name, uri, self.uri_allowlist.as_ref())?;
        // Cache is bypassed when `_meta` is propagated: the server may
        // produce user-specific content so cross-agent reuse is unsafe.
        let use_cache = self.cache.is_some() && !self.context_passthrough;
        if use_cache {
            if let Some(cached) = self
                .cache
                .as_ref()
                .and_then(|c| c.get(&self.server_name, uri))
            {
                nexo_mcp::telemetry::inc_resource_cache_hit(&self.server_name);
                return Ok(McpResourceReadTool::flatten_read_result(&cached));
            }
            nexo_mcp::telemetry::inc_resource_cache_miss(&self.server_name);
        }
        let meta = if self.context_passthrough {
            Some(make_meta(ctx))
        } else {
            None
        };
        let contents = self
            .client
            .read_resource_with_meta(uri, meta)
            .await
            .map_err(|e| anyhow::anyhow!("mcp '{}' read_resource error: {e}", self.server_name))?;
        if use_cache && contents.iter().any(|c| c.text.is_some()) {
            if let Some(cache) = self.cache.as_ref() {
                cache.put(&self.server_name, uri, contents.clone());
            }
        }
        Ok(McpResourceReadTool::flatten_read_result(&contents))
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn prefixed_name_list_includes_suffix() {
        assert_eq!(
            McpResourceListTool::prefixed_name("fs"),
            "mcp_fs_list_resources"
        );
    }
    #[test]
    fn prefixed_name_read_includes_suffix() {
        assert_eq!(
            McpResourceReadTool::prefixed_name("fs@v1"),
            "mcp_fs_v1_read_resource"
        );
    }
    #[test]
    fn long_server_name_is_hashed_into_limit() {
        let server = "a".repeat(60);
        let name = McpResourceListTool::prefixed_name(&server);
        assert_eq!(name.len(), ToolDef::MAX_NAME_LEN);
        assert!(name.starts_with("mcp_"));
    }
    #[test]
    fn tool_def_description_attributed() {
        let d = McpResourceListTool::tool_def("fs");
        assert!(d.description.starts_with("[mcp:fs]"));
        assert_eq!(d.name, "mcp_fs_list_resources");
    }
    #[test]
    fn flatten_text_resource() {
        use nexo_mcp::McpResourceContent;
        let c = vec![McpResourceContent {
            uri: "file:///x".into(),
            mime_type: Some("text/plain".into()),
            text: Some("hello".into()),
            blob: None,
        }];
        let v = McpResourceReadTool::flatten_read_result(&c);
        assert_eq!(v, Value::String("hello".into()));
    }
    #[test]
    fn flatten_blob_shows_mime_and_byte_count() {
        use nexo_mcp::McpResourceContent;
        let c = vec![McpResourceContent {
            uri: "file:///x".into(),
            mime_type: Some("image/png".into()),
            text: None,
            blob: Some("aGVsbG8=".into()),
        }];
        let v = McpResourceReadTool::flatten_read_result(&c);
        assert_eq!(v, Value::String("[blob: image/png, 5 bytes]".into()));
    }
    #[test]
    fn flatten_empty_returns_empty_string() {
        let v = McpResourceReadTool::flatten_read_result(&[]);
        assert_eq!(v, Value::String(String::new()));
    }
    #[test]
    fn flatten_mixed_text_and_blob() {
        use nexo_mcp::McpResourceContent;
        let c = vec![
            McpResourceContent {
                uri: "a".into(),
                mime_type: None,
                text: Some("summary".into()),
                blob: None,
            },
            McpResourceContent {
                uri: "b".into(),
                mime_type: Some("image/png".into()),
                text: None,
                blob: Some("aGVsbG8=".into()),
            },
        ];
        let v = McpResourceReadTool::flatten_read_result(&c);
        assert_eq!(
            v,
            Value::String("summary\n\n[blob: image/png, 5 bytes]".into())
        );
    }
}