roboticus-agent 0.11.4

Agent core with ReAct loop, policy engine, injection defense, memory system, and skill loader
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
//! MCP Server Handler — bridges rmcp's `ServerHandler` to Roboticus's `ToolRegistry`.
//!
//! This module implements the server half of the MCP gateway. External MCP clients
//! (Claude Desktop, Cursor, VS Code, etc.) connect via SSE/HTTP, discover Roboticus's
//! tools through `tools/list`, and invoke them through `tools/call`.
//!
//! All tool calls from MCP clients run with `InputAuthority::External` and pass
//! through the policy engine — Forbidden tools are never exposed, and Dangerous
//! tools require explicit configuration.

use std::borrow::Cow;
use std::sync::Arc;

use rmcp::handler::server::ServerHandler;
use rmcp::model::{
    CallToolRequestParams, CallToolResult, Content, JsonObject, ListToolsResult,
    PaginatedRequestParams, ServerCapabilities, ServerInfo, Tool as McpTool,
};
use rmcp::service::RequestContext;
use rmcp::{ErrorData as McpError, RoleServer};
use serde_json::Value;
use tracing::{debug, info, warn};

use roboticus_core::{InputAuthority, RiskLevel};

use crate::tools::{ToolContext, ToolRegistry, ToolSandboxSnapshot};

// ─── Public types ──────────────────────────────────────────────────────────

/// Bridges Roboticus's `ToolRegistry` to the MCP protocol so external clients
/// can discover and invoke tools over SSE/HTTP.
#[derive(Clone)]
pub struct RoboticusMcpHandler {
    tool_registry: Arc<ToolRegistry>,
    /// Optional allow-list. When `Some`, only these tool names are exposed.
    /// When `None`, all non-Forbidden tools are exposed.
    allow_list: Option<Vec<String>>,
    default_context: McpToolContext,
}

/// Minimal context needed to construct a `ToolContext` for MCP-originated calls.
#[derive(Clone)]
pub struct McpToolContext {
    pub agent_id: String,
    pub agent_name: String,
    pub workspace_root: std::path::PathBuf,
    pub tool_allowed_paths: Vec<std::path::PathBuf>,
    pub sandbox: ToolSandboxSnapshot,
    pub db: Option<roboticus_db::Database>,
}

// ─── Construction ──────────────────────────────────────────────────────────

impl RoboticusMcpHandler {
    pub fn new(tool_registry: Arc<ToolRegistry>, default_context: McpToolContext) -> Self {
        Self {
            tool_registry,
            allow_list: None,
            default_context,
        }
    }

    /// Restrict which tools are visible over MCP. Only names in this list
    /// will appear in `tools/list` (Forbidden tools are *always* hidden
    /// regardless of this list).
    pub fn with_allow_list(mut self, allow_list: Vec<String>) -> Self {
        self.allow_list = Some(allow_list);
        self
    }

    /// Returns true if the tool should be visible to MCP clients.
    fn is_tool_exposed(&self, name: &str, risk: RiskLevel) -> bool {
        if risk == RiskLevel::Forbidden {
            return false;
        }
        if let Some(ref allow_list) = self.allow_list {
            return allow_list.iter().any(|n| n == name);
        }
        true
    }

    /// Convert a serde_json::Value (expected to be an object) into rmcp's JsonObject.
    fn to_json_object(schema: &Value) -> JsonObject {
        match schema.as_object() {
            Some(obj) => obj.iter().map(|(k, v)| (k.clone(), v.clone())).collect(),
            None => {
                let mut obj = JsonObject::new();
                obj.insert("type".to_string(), Value::String("object".to_string()));
                obj
            }
        }
    }

    /// Build a `ToolContext` for an MCP-originated tool call.
    fn build_tool_context(&self, session_id: String) -> ToolContext {
        ToolContext {
            session_id,
            agent_id: self.default_context.agent_id.clone(),
            agent_name: self.default_context.agent_name.clone(),
            authority: InputAuthority::External,
            workspace_root: self.default_context.workspace_root.clone(),
            tool_allowed_paths: self.default_context.tool_allowed_paths.clone(),
            channel: Some("mcp".to_string()),
            db: self.default_context.db.clone(),
            sandbox: self.default_context.sandbox.clone(),
        }
    }
}

// ─── Core logic (testable without RequestContext) ──────────────────────────

impl RoboticusMcpHandler {
    /// List all tools that should be visible to MCP clients.
    pub async fn list_exposed_tools(&self) -> Vec<McpTool> {
        let registry = &self.tool_registry;
        registry
            .list()
            .into_iter()
            .filter(|t| self.is_tool_exposed(t.name(), t.risk_level()))
            .map(|t| {
                let schema = Self::to_json_object(&t.parameters_schema());
                McpTool {
                    name: Cow::Owned(t.name().to_string()),
                    title: None,
                    description: Some(Cow::Owned(t.description().to_string())),
                    input_schema: Arc::new(schema),
                    output_schema: None,
                    annotations: None,
                    execution: None,
                    icons: None,
                    meta: None,
                }
            })
            .collect()
    }

    /// Execute a tool call and return an MCP-formatted result.
    pub async fn execute_tool_call(
        &self,
        tool_name: &str,
        arguments: JsonObject,
    ) -> CallToolResult {
        let registry = &self.tool_registry;

        let tool = match registry.get(tool_name) {
            Some(t) => t,
            None => {
                warn!(tool = tool_name, "MCP client requested unknown tool");
                return CallToolResult::error(vec![Content::text(format!(
                    "Unknown tool: {tool_name}"
                ))]);
            }
        };

        // Never execute Forbidden tools, even if somehow requested directly.
        if tool.risk_level() == RiskLevel::Forbidden {
            warn!(
                tool = tool_name,
                "MCP client attempted to call Forbidden tool"
            );
            return CallToolResult::error(vec![Content::text(
                "This tool is not available via MCP".to_string(),
            )]);
        }

        // Check allow-list
        if !self.is_tool_exposed(tool_name, tool.risk_level()) {
            warn!(
                tool = tool_name,
                "MCP client attempted to call tool not in allow-list"
            );
            return CallToolResult::error(vec![Content::text(
                "This tool is not available via MCP".to_string(),
            )]);
        }

        let params: Value = Value::Object(
            arguments
                .into_iter()
                .collect::<serde_json::Map<String, Value>>(),
        );

        let session_id = uuid::Uuid::new_v4().to_string();
        let ctx = self.build_tool_context(session_id);

        debug!(tool = tool_name, "Executing MCP tool call");

        match tool.execute(params, &ctx).await {
            Ok(result) => {
                let mut content = vec![Content::text(result.output)];
                if let Some(meta) = result.metadata {
                    content.push(Content::text(format!(
                        "\n---\nMetadata: {}",
                        serde_json::to_string_pretty(&meta).unwrap_or_default()
                    )));
                }
                CallToolResult::success(content)
            }
            Err(e) => {
                warn!(tool = tool_name, error = %e, "MCP tool call failed");
                CallToolResult::error(vec![Content::text(e.message)])
            }
        }
    }
}

// ─── ServerHandler trait impl (thin delegation) ────────────────────────────

impl ServerHandler for RoboticusMcpHandler {
    fn get_info(&self) -> ServerInfo {
        ServerInfo {
            instructions: Some(
                "Roboticus agent runtime — tools are filtered by policy engine".into(),
            ),
            capabilities: ServerCapabilities::builder().enable_tools().build(),
            ..Default::default()
        }
    }

    #[allow(unused_variables)]
    async fn list_tools(
        &self,
        request: Option<PaginatedRequestParams>,
        context: RequestContext<RoleServer>,
    ) -> Result<ListToolsResult, McpError> {
        let tools = self.list_exposed_tools().await;
        info!(count = tools.len(), "MCP tools/list");
        Ok(ListToolsResult {
            tools,
            next_cursor: None,
            meta: None,
        })
    }

    #[allow(unused_variables)]
    async fn call_tool(
        &self,
        request: CallToolRequestParams,
        context: RequestContext<RoleServer>,
    ) -> Result<CallToolResult, McpError> {
        info!(tool = %request.name, "MCP tools/call");
        Ok(self
            .execute_tool_call(&request.name, request.arguments.unwrap_or_default())
            .await)
    }
}

// ─── Tests ─────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tools::{Tool, ToolError, ToolResult};
    use async_trait::async_trait;
    use serde_json::json;

    // --- Dummy tools for testing ---

    struct EchoTool;

    #[async_trait]
    impl Tool for EchoTool {
        fn name(&self) -> &str {
            "echo"
        }
        fn description(&self) -> &str {
            "Echoes input back"
        }
        fn risk_level(&self) -> RiskLevel {
            RiskLevel::Safe
        }
        fn parameters_schema(&self) -> Value {
            json!({"type": "object", "properties": {"message": {"type": "string"}}})
        }
        async fn execute(
            &self,
            params: Value,
            _ctx: &ToolContext,
        ) -> Result<ToolResult, ToolError> {
            let msg = params
                .get("message")
                .and_then(|v| v.as_str())
                .unwrap_or("(empty)");
            Ok(ToolResult {
                output: format!("Echo: {msg}"),
                metadata: None,
            })
        }
    }

    struct ForbiddenTool;

    #[async_trait]
    impl Tool for ForbiddenTool {
        fn name(&self) -> &str {
            "nuke"
        }
        fn description(&self) -> &str {
            "Forbidden operation"
        }
        fn risk_level(&self) -> RiskLevel {
            RiskLevel::Forbidden
        }
        fn parameters_schema(&self) -> Value {
            json!({"type": "object"})
        }
        async fn execute(
            &self,
            _params: Value,
            _ctx: &ToolContext,
        ) -> Result<ToolResult, ToolError> {
            panic!("should never execute");
        }
    }

    struct FailingTool;

    #[async_trait]
    impl Tool for FailingTool {
        fn name(&self) -> &str {
            "fail"
        }
        fn description(&self) -> &str {
            "Always fails"
        }
        fn risk_level(&self) -> RiskLevel {
            RiskLevel::Safe
        }
        fn parameters_schema(&self) -> Value {
            json!({"type": "object"})
        }
        async fn execute(
            &self,
            _params: Value,
            _ctx: &ToolContext,
        ) -> Result<ToolResult, ToolError> {
            Err(ToolError {
                message: "Intentional failure".to_string(),
            })
        }
    }

    fn make_handler(tools: Vec<Box<dyn Tool>>) -> RoboticusMcpHandler {
        let mut registry = ToolRegistry::new();
        for tool in tools {
            registry.register(tool);
        }
        let ctx = McpToolContext {
            agent_id: "test-agent".to_string(),
            agent_name: "test-agent".to_string(),
            workspace_root: std::path::PathBuf::from("/tmp"),
            tool_allowed_paths: vec![],
            sandbox: ToolSandboxSnapshot::default(),
            db: None,
        };
        RoboticusMcpHandler::new(Arc::new(registry), ctx)
    }

    #[tokio::test]
    async fn list_tools_excludes_forbidden() {
        let handler = make_handler(vec![Box::new(EchoTool), Box::new(ForbiddenTool)]);
        let tools = handler.list_exposed_tools().await;
        assert_eq!(tools.len(), 1);
        assert_eq!(tools[0].name.as_ref(), "echo");
    }

    #[tokio::test]
    async fn list_tools_respects_allow_list() {
        let handler = make_handler(vec![Box::new(EchoTool), Box::new(FailingTool)])
            .with_allow_list(vec!["echo".to_string()]);
        let tools = handler.list_exposed_tools().await;
        assert_eq!(tools.len(), 1);
        assert_eq!(tools[0].name.as_ref(), "echo");
    }

    #[tokio::test]
    async fn execute_tool_success() {
        let handler = make_handler(vec![Box::new(EchoTool)]);
        let mut args = JsonObject::new();
        args.insert("message".to_string(), Value::String("hello".to_string()));
        let result = handler.execute_tool_call("echo", args).await;
        assert!(!result.is_error.unwrap_or(false));
        let text = result
            .content
            .first()
            .and_then(|c| c.as_text())
            .map(|t| t.text.as_str())
            .unwrap_or("");
        assert_eq!(text, "Echo: hello");
    }

    #[tokio::test]
    async fn execute_unknown_tool_returns_error() {
        let handler = make_handler(vec![Box::new(EchoTool)]);
        let result = handler
            .execute_tool_call("nonexistent", JsonObject::new())
            .await;
        assert!(result.is_error.unwrap_or(false));
    }

    #[tokio::test]
    async fn execute_forbidden_tool_returns_error() {
        let handler = make_handler(vec![Box::new(ForbiddenTool)]);
        let result = handler.execute_tool_call("nuke", JsonObject::new()).await;
        assert!(result.is_error.unwrap_or(false));
    }

    #[tokio::test]
    async fn execute_failing_tool_returns_error() {
        let handler = make_handler(vec![Box::new(FailingTool)]);
        let result = handler.execute_tool_call("fail", JsonObject::new()).await;
        assert!(result.is_error.unwrap_or(false));
        let text = result
            .content
            .first()
            .and_then(|c| c.as_text())
            .map(|t| t.text.as_str())
            .unwrap_or("");
        assert!(text.contains("Intentional failure"));
    }

    #[tokio::test]
    async fn tool_context_uses_external_authority() {
        let handler = make_handler(vec![]);
        let ctx = handler.build_tool_context("test-session".to_string());
        assert_eq!(ctx.authority, InputAuthority::External);
        assert_eq!(ctx.channel.as_deref(), Some("mcp"));
    }
}