Skip to main content

dasein_agentic_mcp/
lib.rs

1//! # agentic-mcp
2//!
3//! MCP Code Mode implementation for agentic-rs.
4//!
5//! Instead of calling MCP tools directly (which consumes lots of tokens),
6//! agents write code that calls MCP tools. The code runs in our sandbox,
7//! and only final results return to the agent context.
8//!
9//! # The Problem (Traditional MCP)
10//!
11//! ```text
12//! Agent → call tool1 → 10,000 tokens in context
13//!       → call tool2 → 10,000 tokens in context
14//!       → call tool3 → 10,000 tokens in context
15//!
16//! Total: 150,000+ tokens per workflow
17//! ```
18//!
19//! # The Solution (Code Mode)
20//!
21//! ```text
22//! Agent writes TypeScript:
23//!
24//!   import * as gdrive from './servers/google-drive';
25//!   import * as salesforce from './servers/salesforce';
26//!
27//!   const doc = await gdrive.getDocument({ id: 'abc' });
28//!   await salesforce.update({ data: doc.content });
29//!   console.log('Done');
30//!
31//! Code executes in sandbox → Only "Done" returns to context
32//!
33//! Total: 2,000 tokens (98.7% reduction!)
34//! ```
35//!
36//! # Architecture
37//!
38//! ```text
39//! ┌─────────────────────────────────────────────────────────────────┐
40//! │                        Agent (Executor)                          │
41//! │                              │                                   │
42//! │                      writes TypeScript                           │
43//! │                              ▼                                   │
44//! │              ┌──────────────────────────────┐                   │
45//! │              │      Sandbox (Firecracker)    │                   │
46//! │              │                              │                   │
47//! │              │  ./servers/                  │                   │
48//! │              │    ├── context7/             │                   │
49//! │              │    │   └── queryDocs.ts      │                   │
50//! │              │    ├── github/               │                   │
51//! │              │    │   └── createPR.ts       │                   │
52//! │              │    └── ...                   │                   │
53//! │              │                              │                   │
54//! │              │  Code imports and calls      │                   │
55//! │              │  these generated modules     │                   │
56//! │              └──────────────┬───────────────┘                   │
57//! │                             │                                    │
58//! └─────────────────────────────┼────────────────────────────────────┘
59//!                               │ MCP calls (from inside sandbox)
60//!          ┌────────────────────┼────────────────────┐
61//!          ▼                    ▼                    ▼
62//!   ┌──────────────┐   ┌──────────────┐   ┌──────────────┐
63//!   │  Context7    │   │   GitHub     │   │   Postgres   │
64//!   └──────────────┘   └──────────────┘   └──────────────┘
65//! ```
66//!
67//! # Usage
68//!
69//! ```rust,ignore
70//! use dasein_agentic_mcp::{MCPConfig, CodeModeRuntime};
71//! use dasein_agentic_sandbox::FirecrackerSandbox;
72//!
73//! // 1. Load MCP server configurations
74//! let config = MCPConfig::from_json(r#"{
75//!     "mcpServers": {
76//!         "context7": {
77//!             "url": "https://mcp.context7.com/mcp",
78//!             "headers": { "CONTEXT7_API_KEY": "..." }
79//!         }
80//!     }
81//! }"#)?;
82//!
83//! // 2. Create runtime with sandbox
84//! let sandbox = FirecrackerSandbox::builder().build();
85//! let runtime = CodeModeRuntime::new(sandbox, config).await?;
86//!
87//! // 3. Agent writes code that uses MCP tools
88//! let code = r#"
89//!     import { queryDocs } from './servers/context7';
90//!
91//!     const docs = await queryDocs({
92//!         libraryId: '/vercel/next.js',
93//!         query: 'app router'
94//!     });
95//!     console.log(docs.summary);
96//! "#;
97//!
98//! // 4. Execute - only console output returns to agent
99//! let result = runtime.execute(code).await?;
100//! println!("Agent sees: {}", result.stdout);  // Just the summary
101//! ```
102
103pub mod client;
104mod error;
105pub mod server;
106
107pub use client::{MCPClientPool, MCPConfig, MCPServerConfig, TransportType};
108pub use error::MCPError;
109pub use server::ValidatorMCPServer;