everruns-core 0.43.0

Transport-neutral agent execution contracts for Everruns
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
// MCP Virtual Capability
//
// Spec: knowledge/integrations/mcp.md (umbrella), knowledge/integrations/mcp-servers.md (capabilities integration)
//
// This module provides a capability wrapper for MCP servers.
// Each active MCP server becomes a virtual capability that contributes
// its tools to the agent's tool set.
//
// Design decisions:
// - Capability ID format: "mcp:{server_id}" using the MCP server's UUID
// - Tool names are prefixed: "mcp_{sanitized_server_name}_{tool_name}"
// - Tool execution is delegated to the MCP server via HTTP
// - Tools are cached and refreshed periodically

use crate::capabilities::Capability;
use crate::capability_types::CapabilityStatus;
use crate::mcp_server::{McpToolDefinition, mcp_tool_name};
use crate::tools::Tool;
use everruns_contracts::CapabilityId;
use everruns_contracts::tool_types::{
    BuiltinTool, DeferrablePolicy, ToolDefinition, ToolHints, ToolPolicy,
};
use uuid::Uuid;

/// MCP Virtual Capability ID prefix
pub const MCP_CAPABILITY_PREFIX: &str = "mcp:";

/// Generate capability ID for an MCP server
pub fn mcp_capability_id(server_id: Uuid) -> String {
    format!("{}{}", MCP_CAPABILITY_PREFIX, server_id)
}

/// Check if a capability ID is an MCP capability
pub fn is_mcp_capability(capability_id: &str) -> bool {
    capability_id.starts_with(MCP_CAPABILITY_PREFIX)
}

/// Parse MCP server ID from capability ID
pub fn parse_mcp_capability_id(capability_id: &str) -> Option<Uuid> {
    if !capability_id.starts_with(MCP_CAPABILITY_PREFIX) {
        return None;
    }
    let uuid_str = &capability_id[MCP_CAPABILITY_PREFIX.len()..];
    Uuid::parse_str(uuid_str).ok()
}

/// MCP Virtual Capability wrapping an MCP server.
///
/// This capability provides tools from a remote MCP server.
/// Tool names are prefixed with "mcp_{server_name}_" to avoid collisions.
#[derive(Debug, Clone)]
pub struct McpCapability {
    /// MCP server UUID
    pub server_id: Uuid,
    /// Server name (used for tool name prefix)
    pub server_name: String,
    /// Server description
    pub description: Option<String>,
    /// Cached tool definitions from the MCP server
    pub tools: Vec<McpToolDefinition>,
}

impl McpCapability {
    /// Create a new MCP capability from server info and cached tools
    pub fn new(
        server_id: Uuid,
        server_name: String,
        description: Option<String>,
        tools: Vec<McpToolDefinition>,
    ) -> Self {
        Self {
            server_id,
            server_name,
            description,
            tools,
        }
    }

    /// Get the capability ID for this MCP server
    pub fn capability_id(&self) -> String {
        mcp_capability_id(self.server_id)
    }

    /// Convert MCP tool definition to our ToolDefinition with prefixed name.
    /// Maps MCP annotations to ToolHints when available.
    fn mcp_tool_to_definition(&self, mcp_tool: &McpToolDefinition) -> ToolDefinition {
        let prefixed_name = mcp_tool_name(&self.server_name, &mcp_tool.name);

        // Map MCP annotations to ToolHints
        let hints = match &mcp_tool.annotations {
            Some(ann) => ToolHints {
                readonly: ann.read_only_hint,
                destructive: ann.destructive_hint,
                idempotent: ann.idempotent_hint,
                // Default open_world to true for MCP tools unless explicitly set to false
                open_world: Some(ann.open_world_hint.unwrap_or(true)),
                // MCP doesn't define requires_secrets or long_running — leave as None
                ..ToolHints::default()
            },
            None => {
                // MCP tools are external by nature
                ToolHints::default().with_open_world(true)
            }
        };

        ToolDefinition::Builtin(BuiltinTool {
            name: prefixed_name,
            display_name: Some(
                mcp_tool
                    .title
                    .as_deref()
                    .filter(|title| !title.trim().is_empty())
                    .map(crate::tool_narration::narration_detail)
                    .unwrap_or_else(|| {
                        format!(
                            "{}: {}",
                            self.server_name,
                            mcp_tool
                                .name
                                .split(['_', '-', '.'])
                                .filter(|part| !part.is_empty())
                                .collect::<Vec<_>>()
                                .join(" ")
                        )
                    }),
            ),
            description: mcp_tool
                .description
                .clone()
                .unwrap_or_else(|| format!("Tool from MCP server: {}", self.server_name)),
            parameters: mcp_tool.input_schema.clone(),
            policy: ToolPolicy::Auto,
            category: self.category().map(|s| s.to_string()),
            deferrable: DeferrablePolicy::default(),
            hints,
            full_parameters: None,
        })
        .with_capability_attribution(self.capability_id(), Some(self.server_name.clone()))
    }
}

impl Capability for McpCapability {
    fn id(&self) -> &str {
        // Return a static reference by leaking the capability ID
        // This is acceptable since capabilities are long-lived
        Box::leak(self.capability_id().into_boxed_str())
    }

    fn name(&self) -> &str {
        Box::leak(self.server_name.clone().into_boxed_str())
    }

    fn description(&self) -> &str {
        let desc = self
            .description
            .clone()
            .unwrap_or_else(|| format!("MCP Server providing {} tool(s)", self.tools.len()));
        Box::leak(desc.into_boxed_str())
    }

    fn status(&self) -> CapabilityStatus {
        CapabilityStatus::Available
    }

    fn icon(&self) -> Option<&str> {
        Some("mcp") // Official MCP logo
    }

    fn category(&self) -> Option<&str> {
        Some("MCP Servers")
    }

    fn system_prompt_addition(&self) -> Option<&str> {
        None // MCP tools are self-documenting
    }

    fn narrate(
        &self,
        _tool_def: Option<&everruns_contracts::tool_types::ToolDefinition>,
        tool_call: &everruns_contracts::tool_types::ToolCall,
        phase: crate::tool_narration::ToolNarrationPhase,
        locale: Option<&str>,
        _ctx: crate::tool_narration::ToolNarrationContext<'_>,
    ) -> Option<String> {
        // Generic search narration for provider/MCP search tools (`*__search`).
        if !self
            .tools
            .iter()
            .any(|tool| mcp_tool_name(&self.server_name, &tool.name) == tool_call.name)
            || !tool_call.name.ends_with("__search")
        {
            return None;
        }
        Some(crate::tool_narration::narrate_provider_search(
            &tool_call.arguments,
            phase,
            locale,
        ))
    }

    fn tools(&self) -> Vec<Box<dyn Tool>> {
        // MCP tools are executed via HTTP, not directly
        // Return empty vec - tool execution is handled specially
        vec![]
    }

    fn tool_definitions(&self) -> Vec<ToolDefinition> {
        // Existing stored configs also pass here: never publish a name that
        // parses as a different server/tool pair.
        if !crate::mcp_server::is_valid_mcp_server_name(&self.server_name) {
            return vec![];
        }
        self.tools
            .iter()
            .map(|t| self.mcp_tool_to_definition(t))
            .collect()
    }
}

/// MCP-namespace helpers for [`CapabilityId`].
///
/// An extension trait because the ID type lives in the neutral
/// `everruns-contracts` contract crate while the `mcp:` namespace is owned
/// by this capability implementation.
pub trait McpCapabilityIdExt: Sized {
    /// Check if this capability ID is for an MCP server
    fn is_mcp(&self) -> bool;
    /// Create a capability ID for an MCP server
    fn mcp(server_id: Uuid) -> Self;
    /// Parse MCP server UUID from this capability ID
    fn mcp_server_id(&self) -> Option<Uuid>;
}

impl McpCapabilityIdExt for CapabilityId {
    fn is_mcp(&self) -> bool {
        is_mcp_capability(self.as_str())
    }

    fn mcp(server_id: Uuid) -> Self {
        Self::new(mcp_capability_id(server_id))
    }

    fn mcp_server_id(&self) -> Option<Uuid> {
        parse_mcp_capability_id(self.as_str())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn capability_id_helpers_preserve_wire_identity_and_reject_invalid_namespaces() {
        let id = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
        let wire = "mcp:550e8400-e29b-41d4-a716-446655440000";
        assert_eq!(mcp_capability_id(id), wire);
        assert!(is_mcp_capability(wire));
        assert_eq!(parse_mcp_capability_id(wire), Some(id));
        let capability_id = CapabilityId::mcp(id);
        assert_eq!(capability_id.as_str(), wire);
        assert!(capability_id.is_mcp());
        assert_eq!(capability_id.mcp_server_id(), Some(id));
        for invalid in ["current_time", "mcp_something", "mcp:invalid", "mcp:"] {
            assert_eq!(parse_mcp_capability_id(invalid), None);
            assert_eq!(CapabilityId::new(invalid).mcp_server_id(), None);
        }
        assert!(!is_mcp_capability("current_time"));
        assert!(!is_mcp_capability("mcp_something"));
        assert!(!CapabilityId::new("current_time").is_mcp());
        assert!(is_mcp_capability("mcp:invalid"));
    }

    #[test]
    fn capability_definitions_preserve_schema_attribution_and_annotation_overrides() {
        let id = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
        let schema = json!({"type":"object","properties":{"query":{"type":"string","minLength":3}},"required":["query"]});
        for (
            annotations,
            expected_readonly,
            expected_destructive,
            expected_idempotent,
            expected_open_world,
        ) in [
            (None, None, None, None, true),
            (
                Some(crate::McpToolAnnotations {
                    read_only_hint: Some(true),
                    destructive_hint: Some(false),
                    idempotent_hint: Some(true),
                    open_world_hint: Some(false),
                }),
                Some(true),
                Some(false),
                Some(true),
                false,
            ),
        ] {
            let capability = McpCapability::new(
                id,
                "microsoft-learn".into(),
                Some("Microsoft Learn MCP".into()),
                vec![McpToolDefinition {
                    name: "search".into(),
                    title: None,
                    description: Some("Search documentation".into()),
                    input_schema: schema.clone(),
                    annotations,
                }],
            );
            let defs = capability.tool_definitions();
            assert_eq!(defs.len(), 1);
            let ToolDefinition::Builtin(builtin) = &defs[0] else {
                panic!("expected builtin")
            };
            assert_eq!(builtin.name, "mcp_microsoft_learn__search");
            assert_eq!(builtin.description, "Search documentation");
            assert_eq!(builtin.parameters, schema);
            assert_eq!(builtin.hints.readonly, expected_readonly);
            assert_eq!(builtin.hints.destructive, expected_destructive);
            assert_eq!(builtin.hints.idempotent, expected_idempotent);
            assert_eq!(builtin.hints.open_world, Some(expected_open_world));
            assert_eq!(
                defs[0].capability_attribution(),
                Some((
                    "mcp:550e8400-e29b-41d4-a716-446655440000",
                    Some("microsoft-learn")
                ))
            );
        }
    }

    #[test]
    fn ambiguous_server_names_do_not_publish_misrouted_tool_definitions() {
        for name in ["docs_", "docs-", "docs__private", "docs..private", "_", ""] {
            let capability = McpCapability::new(
                Uuid::nil(),
                name.into(),
                None,
                vec![McpToolDefinition {
                    name: "search".into(),
                    title: None,
                    description: None,
                    input_schema: json!({"type":"object"}),
                    annotations: None,
                }],
            );
            assert!(
                capability.tool_definitions().is_empty(),
                "ambiguous server {name:?} published tools"
            );
        }
        let capability = McpCapability::new(
            Uuid::nil(),
            "docs_api".into(),
            None,
            vec![McpToolDefinition {
                name: "read__file".into(),
                title: None,
                description: None,
                input_schema: json!({"type":"object"}),
                annotations: None,
            }],
        );
        let definitions = capability.tool_definitions();
        assert_eq!(definitions.len(), 1);
        assert_eq!(
            crate::parse_mcp_tool_name(definitions[0].name()),
            Some(("docs_api".into(), "read__file".into()))
        );
    }
}

#[cfg(test)]
mod narration_tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn remote_titles_survive_discovery_and_generic_narration() {
        for (title, expected) in [
            (Some("Read project notes"), "Read project notes"),
            (None, "notes: read file"),
            (Some("  "), "notes: read file"),
        ] {
            let tool: McpToolDefinition = serde_json::from_value(
                json!({"name":"read_file", "title":title, "inputSchema":{"type":"object"}}),
            )
            .unwrap();
            let capability = McpCapability::new(Uuid::nil(), "notes".into(), None, vec![tool]);
            let definitions = capability.tool_definitions();
            assert_eq!(definitions[0].display_name(), Some(expected));
            let call = everruns_contracts::tool_types::ToolCall {
                id: "call".into(),
                name: definitions[0].name().into(),
                arguments: json!({}),
            };
            assert_eq!(
                crate::tool_narration::render_tool_narration(
                    Some(&definitions[0]),
                    &call,
                    crate::tool_narration::ToolNarrationPhase::Completed
                ),
                format!("Ran {expected}")
            );
        }
    }

    #[test]
    fn capability_only_narrates_search_tools_it_owns() {
        let capability = McpCapability::new(
            Uuid::nil(),
            "notes".into(),
            None,
            vec![
                serde_json::from_value(json!({"name":"search", "inputSchema":{"type":"object"}}))
                    .unwrap(),
            ],
        );
        for (name, owned) in [("mcp_notes__search", true), ("mcp_other__search", false)] {
            let call = everruns_contracts::tool_types::ToolCall {
                id: "call".into(),
                name: name.into(),
                arguments: json!({"query":"release notes"}),
            };
            assert_eq!(
                capability
                    .narrate(
                        None,
                        &call,
                        crate::tool_narration::ToolNarrationPhase::Completed,
                        None,
                        crate::tool_narration::ToolNarrationContext::default()
                    )
                    .is_some(),
                owned
            );
        }
    }
}