echo_agent 0.1.4

Production-grade AI Agent framework for Rust — ReAct engine, multi-agent, memory, streaming, MCP, IM channels, workflows
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# MCP Integration (Model Context Protocol)

## What It Is

MCP (Model Context Protocol) is an open standard proposed by Anthropic in 2024 that unifies communication between LLM applications and external tool services. An MCP server exposes tools, resources, and prompts; an MCP client (i.e., an Agent) connects and automatically discovers and invokes those capabilities.

echo-agent implements a complete MCP client supporting the latest protocol version (2025-03-26), capable of connecting to any spec-compliant server and seamlessly adapting its tools to the framework's `Tool` trait.

---

## Problem It Solves

### Tool Ecosystem Fragmentation

Traditionally, every AI framework reimplements tools in its own language:

```
┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐
│   LangChain     │     │   AutoGPT       │     │   echo-agent    │
│ (Python tools)  │     │ (Python tools)  │     │  (Rust tools)   │
└─────────────────┘     └─────────────────┘     └─────────────────┘
        ↓                       ↓                       ↓
  Implement once         Implement once          Implement once
```

### MCP's Solution

MCP decouples tool capabilities from the application:

```
                    ┌─────────────────────────────────────┐
                    │          MCP Server                  │
                    │  (Python / Node.js / Java / ...)    │
                    │  exposes tools/resources/prompts    │
                    └──────────────┬──────────────────────┘
                                   │ MCP Protocol
           ┌───────────────────────┼───────────────────────┐
           ↓                       ↓                       ↓
   ┌───────────────┐       ┌───────────────┐       ┌───────────────┐
   │ Claude Desktop │       │    Cursor     │       │  echo-agent   │
   │ (MCP client)  │       │ (MCP client)  │       │ (MCP client)  │
   └───────────────┘       └───────────────┘       └───────────────┘
```

Tool services run independently, and any MCP client can connect and use them—"write once, run anywhere."

---

## MCP Protocol Fundamentals

### Protocol Stack

MCP is built on JSON-RPC 2.0, a request-response protocol:

```
┌─────────────────────────────────────────────────────────────┐
│                   Application Layer (MCP)                   │
│  tools/list, tools/call, resources/list, prompts/get...    │
├─────────────────────────────────────────────────────────────┤
│                   JSON-RPC 2.0 Layer                         │
│  { "jsonrpc": "2.0", "id": 1, "method": "...", ... }       │
├─────────────────────────────────────────────────────────────┤
│                     Transport Layer                          │
│         stdio / HTTP (Streamable HTTP) / SSE                │
└─────────────────────────────────────────────────────────────┘
```

### Connection Lifecycle

```
Client                                        Server
  │                                            │
  │──── initialize(protocolVersion, ──────────>│  1. Handshake
  │          capabilities, clientInfo)         │
  │                                            │
  │<─── InitializeResult(protocolVersion, ─────│
  │          capabilities, serverInfo)         │
  │                                            │
  │──── notifications/initialized ────────────>│  2. Ready notification
  │                                            │
  │──── tools/list ───────────────────────────>│  3. Capability discovery
  │<─── tools: [{name, description, schema}] ──│
  │                                            │
  │──── tools/call(name, arguments) ──────────>│  4. Tool invocation
  │<─── content: [{type: "text", text: ...}] ──│
  │                                            │
  │──── resources/list ───────────────────────>│  5. Resource access
  │<─── resources: [{uri, name, mimeType}] ────│
  │                                            │
  │──── prompts/list ─────────────────────────>│  6. Prompt retrieval
  │<─── prompts: [{name, description}] ────────│
  │                                            │
```

### Three Core Capabilities

| Capability | Description | Typical Use |
|------------|-------------|-------------|
| **Tools** | Executable operations | File read/write, API calls, database queries |
| **Resources** | Readable data | File contents, database records, configuration |
| **Prompts** | Reusable prompt templates | Code review templates, documentation generators |

---

## Transport Layer Details

echo-agent supports three transport modes:

### 1. stdio (subprocess, recommended for local tools)

Agent spawns the tool service as a child process and communicates via stdin/stdout:

```
┌───────────────────┐                    ┌───────────────────┐
│   echo-agent      │                    │   MCP Server      │
│   (parent)        │                    │   (child)         │
│                   │   stdin ────────>  │                   │
│                   │   stdout <───────  │                   │
└───────────────────┘                    └───────────────────┘
```

**Advantages**:
- Zero network overhead, lowest latency
- Lifecycle bound to Agent, automatic cleanup
- Seamless integration with existing Node.js/Python ecosystem

**Configuration**:
```rust
McpServerConfig::stdio(
    "filesystem",             // server name (any identifier)
    "npx",                    // command
    vec![
        "-y",
        "@modelcontextprotocol/server-filesystem",
        "/tmp"                // root directory the server can access
    ],
)
```

### 2. HTTP (Streamable HTTP, recommended for remote services)

Modern HTTP transport compliant with MCP 2025-03-26 specification:

```
┌───────────────────┐                    ┌───────────────────┐
│   echo-agent      │                    │   MCP Server      │
│                   │   POST /mcp ────>  │                   │
│                   │   <── JSON-RPC ─── │                   │
│                   │                    │                   │
│                   │   GET /mcp (SSE)   │  Optional: push   │
│                   │   <── events ────  │                   │
└───────────────────┘                    └───────────────────┘
```

**Features**:
- Single endpoint POST requests
- Automatic `MCP-Protocol-Version` header
- `MCP-Session-Id` session management support
- Optional GET SSE notification stream

**Configuration**:
```rust
McpServerConfig::http("my-api", "http://localhost:3000/mcp");

// With authentication headers
let mut headers = HashMap::new();
headers.insert("Authorization".to_string(), "Bearer token".to_string());
McpServerConfig::http_with_headers("secure-api", "https://api.example.com/mcp", headers);
```

### 3. SSE (Legacy HTTP+SSE, for older SDKs)

For older MCP SDKs (2024-11-05 protocol):

```
┌───────────────────┐                    ┌───────────────────┐
│   echo-agent      │                    │   MCP Server      │
│                   │   GET /sse ──────> │                   │
│                   │   <── endpoint ─── │  Establish SSE    │
│                   │                    │                   │
│                   │   POST /msg/xxx ─> │  Dynamic endpoint │
│                   │   <── JSON-RPC ─── │                   │
└───────────────────┘                    └───────────────────┘
```

**Configuration**:
```rust
McpServerConfig::sse("legacy-api", "http://localhost:8080");
```

---

## Configuration File Format

echo-agent supports `mcp.json` format compatible with Claude Desktop / Cursor / VS Code:

```json
{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/workspace"],
      "env": {
        "OPTIONAL_VAR": "value"
      }
    },
    "github": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-github"],
      "env": {
        "GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_xxx"
      }
    },
    "remote-api": {
      "url": "http://localhost:8080/mcp",
      "headers": {
        "Authorization": "Bearer token"
      }
    },
    "legacy-sse": {
      "url": "http://localhost:3000",
      "transport": "sse"
    },
    "disabled-server": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-postgres"],
      "disabled": true
    }
  }
}
```

**Loading from config file**:
```rust
let mut agent = ReactAgent::new(config);
let clients = agent.load_mcp_from_file("mcp.json").await?;
println!("Connected {} MCP servers", clients.len());
```

---

## Usage

### Method 1: Direct Connection via Agent

```rust
use echo_agent::prelude::*;

#[tokio::main]
async fn main() -> echo_agent::error::Result<()> {
    let config = AgentConfig::new("qwen3-max", "agent", "You are a filesystem assistant")
        .enable_tool(true);
    let mut agent = ReactAgent::new(config);

    // Connect MCP filesystem server (lifecycle bound to Agent)
    let client = agent.connect_mcp_from_config(McpServerConfig::stdio(
        "filesystem",
        "npx",
        vec!["-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
    )).await?;

    // Execute task
    let answer = agent.execute("List all files in /tmp").await?;
    println!("{}", answer);

    // MCP connection closed automatically when Agent is dropped
    Ok(())
}
```

### Method 2: Manage Connections via McpManager

```rust
use echo_agent::prelude::*;

#[tokio::main]
async fn main() -> echo_agent::error::Result<()> {
    let mut agent = ReactAgentBuilder::new()
        .model("qwen3-max")
        .name("file-agent")
        .system_prompt("You are a file operation assistant")
        .enable_tools()
        .build()?;

    // Application layer manages MCP lifecycle
    let mut mcp = McpManager::new();
    let tools = mcp.connect(McpServerConfig::stdio(
        "filesystem",
        "npx",
        vec!["-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
    )).await?;

    // Register tools
    agent.add_tools(tools);

    // Execute task
    let answer = agent.execute("List all files in /tmp").await?;
    println!("{}", answer);

    // Manually close connections
    mcp.close_all().await;
    Ok(())
}
```

---

## Multiple Server Connections

```rust
let mut mcp = McpManager::new();

// Filesystem tools
let fs_tools = mcp.connect(McpServerConfig::stdio(
    "filesystem",
    "npx",
    vec!["-y", "@modelcontextprotocol/server-filesystem", "/workspace"],
)).await?;

// GitHub tools
let gh_tools = mcp.connect(McpServerConfig::stdio(
    "github",
    "npx",
    vec!["-y", "@modelcontextprotocol/server-github"],
)).await?;

// Remote API tools
let api_tools = mcp.connect(McpServerConfig::http(
    "internal-api",
    "http://api-server:8080/mcp"
)).await?;

// Register all tools
agent.add_tools(fs_tools);
agent.add_tools(gh_tools);
agent.add_tools(api_tools);

// Or register all at once
agent.add_tools(mcp.get_all_tools());
```

---

## Tool Adaptation Mechanism

MCP tools are adapted to the framework's `Tool` trait via `McpToolAdapter`:

```
MCP server declares:
{
  "name": "read_file",
  "description": "Read file contents",
  "inputSchema": {
    "type": "object",
    "properties": {
      "path": { "type": "string", "description": "File path" }
    },
    "required": ["path"]
  }
}

                    ↓ McpToolAdapter adaptation

impl Tool for McpToolAdapter {
    fn name(&self)        -> "read_file"
    fn description(&self) -> "Read file contents"
    fn parameters(&self)  -> original inputSchema (JSON Schema)
    async fn execute(&self, params) -> Result<ToolResult> {
        // 1. Serialize params to JSON
        // 2. Call MCP tools/call method
        // 3. Convert MCP content to ToolResult
    }
}
```

To an Agent, MCP tools are indistinguishable from native Rust tools—both are invoked via `execute()`.

---

## Resource Access

MCP resources are read-only data sources exposed as URI-addressable resources:

```rust
// List resources
if let Some(client) = mcp.get_client("filesystem") {
    if client.supports_resources() {
        for resource in client.resources() {
            println!("Resource: {} ({})", resource.name, resource.uri);
        }

        // Read resource content
        let result = client.read_resource("file:///workspace/README.md").await?;
        for content in result.contents {
            match content {
                McpResourceContents::Text { text, .. } => println!("{}", text),
                McpResourceContents::Blob { blob, .. } => {/* Base64 data */},
            }
        }
    }
}
```

---

## Prompt Usage

MCP prompts are predefined templates that accept parameters:

```rust
if let Some(client) = mcp.get_client("code-review") {
    if client.supports_prompts() {
        // List prompts
        for prompt in client.prompts() {
            println!("Prompt: {} - {}", prompt.name, prompt.description.unwrap_or_default());
        }

        // Get prompt content
        let mut args = HashMap::new();
        args.insert("language".to_string(), "rust".to_string());
        args.insert("file".to_string(), "src/main.rs".to_string());

        let result = client.get_prompt("code_review", Some(args)).await?;
        for msg in result.messages {
            println!("[{}] {:?}", msg.role, msg.content);
        }
    }
}
```

---

## Inspecting Connected Servers

```rust
// List all connected servers
println!("Connected MCP servers: {:?}", mcp.server_names());

// Get specific server client reference
if let Some(client) = mcp.get_client("filesystem") {
    println!("filesystem provides {} tools", client.tools().len());
    println!("Protocol version: {}", client.protocol_version());

    // View server capabilities
    let caps = client.server_capabilities();
    println!("Supports tools: {}", caps.tools.is_some());
    println!("Supports resources: {}", caps.resources.is_some());
    println!("Supports prompts: {}", caps.prompts.is_some());

    // Health check
    client.ping().await?;
}
```

---

## Popular MCP Servers

| Server | Install Command | Capabilities |
|--------|----------------|--------------|
| Filesystem | `npx -y @modelcontextprotocol/server-filesystem <dir>` | File read/write, directory listing |
| GitHub | `npx -y @modelcontextprotocol/server-github` | PRs, Issues, code search |
| Brave Search | `npx -y @modelcontextprotocol/server-brave-search` | Web search |
| PostgreSQL | `npx -y @modelcontextprotocol/server-postgres <url>` | SQL queries |
| Puppeteer | `npx -y @modelcontextprotocol/server-puppeteer` | Browser automation |
| Slack | `npx -y @modelcontextprotocol/server-slack` | Message sending, channel management |
| Google Maps | `npx -y @modelcontextprotocol/server-google-maps` | Geocoding, directions |

> Full list: [MCP Servers Directory]https://github.com/modelcontextprotocol/servers

---

## Error Handling

Potential MCP errors:

| Error Type | Description | Handling Suggestion |
|------------|-------------|---------------------|
| `McpError::ConnectionFailed` | Cannot connect to server | Check command/URL correctness |
| `McpError::InitializationFailed` | Handshake failed | Check protocol version compatibility |
| `McpError::ProtocolError` | Protocol layer error | Check JSON format |
| `McpError::ToolCallFailed` | Tool invocation failed | Check parameter correctness |
| `McpError::TransportClosed` | Transport layer closed | Reconnect to server |

---

See: `examples/demo06_mcp.rs`