brainwires-agent-network 0.6.0

Agent networking layer — MCP server framework, IPC, remote bridge, and mesh networking for Brainwires
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
# brainwires-agent-network

[![Crates.io](https://img.shields.io/crates/v/brainwires-agent-network.svg)](https://crates.io/crates/brainwires-agent-network)
[![Documentation](https://img.shields.io/docsrs/brainwires-agent-network)](https://docs.rs/brainwires-agent-network)
[![License](https://img.shields.io/crates/l/brainwires-agent-network.svg)](LICENSE)

Agent networking layer for the Brainwires Agent Framework.

## Overview

`brainwires-agent-network` provides the full networking stack for AI agents: an MCP server framework with composable middleware, a 5-layer protocol stack for pluggable agent communication, encrypted IPC, a remote bridge for backend connectivity, agent lifecycle management, and optional distributed mesh networking.

**Design principles:**

- **Trait-driven**`McpHandler`, `Transport`, `Router`, `Discovery`, and friends decouple the framework from any concrete implementation
- **Protocol-agnostic** — agents communicate over IPC, TCP, HTTP, WebSocket, A2A, or Pub/Sub through a uniform `Transport` trait
- **Middleware-composable** — auth, rate limiting, logging, and tool filtering stack via an onion model
- **Encryption-first** — IPC sockets use ChaCha20-Poly1305 authenticated encryption by default
- **Feature-gated** — only compile the transports and discovery mechanisms you need

```text
              ┌───────────────────────────────────────────────────────────┐
              │              brainwires-agent-network                     │
              │                                                           │
              │  ┌─────────────────────────────────────────────────────┐  │
              │  │         5-Layer Protocol Stack                      │  │
              │  │                                                     │  │
              │  │  Layer 5: Application (NetworkManager, Events)      │  │
              │  │  Layer 4: Discovery  (Manual, Registry)             │  │
              │  │  Layer 3: Routing    (Direct, Broadcast, Content)   │  │
              │  │  Layer 2: Transport  (IPC, Remote, TCP, A2A, PubSub)│  │
              │  │  Layer 1: Identity   (AgentIdentity, AgentCard)     │  │
              │  └─────────────────────────────────────────────────────┘  │
              │                                                           │
              │  ┌─────────────────────────────────────────────────────┐  │
              │  │             MCP Server Framework                    │  │
              │  │  McpHandler ──► MiddlewareChain ──► Transport       │  │
              │  │                   (onion model)                     │  │
              │  └─────────────────────────────────────────────────────┘  │
              │                                                           │
              │  ┌──────────────────┐  ┌───────────────────────────┐     │
              │  │  Agent Manager   │  │  Remote Bridge            │     │
              │  │  Spawn / List /  │  │  Supabase Realtime /      │     │
              │  │  Stop / Await    │  │  HTTP Polling Fallback    │     │
              │  └──────────────────┘  └───────────────────────────┘     │
              └───────────────────────────────────────────────────────────┘
```

## Quick Start

```toml
[dependencies]
brainwires-agent-network = "0.6"
```

Minimal MCP server:

```rust
use brainwires_agent_network::prelude::*;

struct MyHandler;

#[async_trait]
impl McpHandler for MyHandler {
    fn server_info(&self) -> ServerInfo {
        ServerInfo { name: "my-server".into(), version: "0.4.0".into() }
    }

    fn capabilities(&self) -> ServerCapabilities {
        ServerCapabilities::default()
    }

    fn list_tools(&self) -> Vec<McpToolDef> {
        vec![]
    }

    async fn call_tool(
        &self,
        name: &str,
        args: Value,
        ctx: &RequestContext,
    ) -> Result<CallToolResult> {
        todo!()
    }
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let server = McpServer::new(MyHandler)
        .with_transport(StdioServerTransport::new())
        .with_middleware(LoggingMiddleware::new());

    server.run().await?;
    Ok(())
}
```

## Features

| Feature | Default | Description |
|---------|---------|-------------|
| `server` | Yes | MCP server framework |
| `client` | Yes | Client for connecting to remote agent network servers |
| `ipc-transport` | Yes | Unix-socket IPC transport with ChaCha20 encryption |
| `remote-transport` | Yes | Supabase Realtime / HTTP polling bridge transport |
| `tcp-transport` | No | Direct TCP peer-to-peer transport |
| `pubsub-transport` | No | In-process pub/sub transport with topic-based messaging |
| `a2a-transport` | No | A2A protocol transport (requires `brainwires-a2a`) |
| `mesh` | No | Distributed mesh networking (includes `tcp-transport`) |
| `registry-discovery` | No | HTTP-based agent registry discovery |
| `auth-keyring` | No | Secure API key storage via system keyring |
| `full` | No | All optional features enabled |

```toml
# With all transports and discovery
brainwires-agent-network = { version = "0.6", features = ["full"] }

# Just TCP and pub/sub
brainwires-agent-network = { version = "0.6", features = ["tcp-transport", "pubsub-transport"] }
```

## Architecture

### Protocol-Layer Stack

The networking layer is organized as a 5-layer protocol stack. Each layer has a well-defined trait, and concrete implementations are feature-gated.

#### Layer 1: Identity

Agent identity, capability advertisement, and cryptographic credentials.

**Key types:**

| Type | Description |
|------|-------------|
| `AgentIdentity` | UUID, name, and `AgentCard` |
| `AgentCard` | Capabilities, supported protocols, metadata, endpoint, compute capacity |
| `ProtocolId` | Protocol identifier string |
| `SigningKey` / `VerifyingKey` | ChaCha20-Poly1305 signing with SHA-256 key derivation |

```rust
use brainwires_agent_network::identity::{AgentIdentity, AgentCard};

let identity = AgentIdentity::new("my-agent")
    .with_capability("inference")
    .with_protocol("mcp")
    .with_endpoint("tcp://10.0.0.1:8080");
```

#### Layer 2: Transport

How bytes move between agents. Every transport implements the `Transport` trait.

```rust
#[async_trait]
pub trait Transport: Send + Sync {
    async fn connect(&mut self, target: &TransportAddress) -> Result<()>;
    async fn disconnect(&mut self) -> Result<()>;
    async fn send(&self, envelope: &MessageEnvelope) -> Result<()>;
    async fn receive(&self) -> Result<Option<MessageEnvelope>>;
    fn transport_type(&self) -> TransportType;
    fn is_connected(&self) -> bool;
}
```

**Provided transports:**

| Transport | Feature flag | Wire format | Use case |
|-----------|-------------|-------------|----------|
| `IpcTransport` | `ipc-transport` | Length-prefixed, ChaCha20-encrypted JSON | Same-machine agents |
| `RemoteTransport` | `remote-transport` | HTTP POST with broadcast channel | Backend connectivity |
| `TcpTransport` | `tcp-transport` | Length-prefixed JSON over TCP (Nagle disabled) | Peer-to-peer mesh |
| `PubSubTransport` | `pubsub-transport` | In-process `tokio::broadcast` channels | Same-process topic messaging |
| `A2aTransport` | `a2a-transport` | A2A JSON-RPC over HTTP/WebSocket | Cross-framework interop |

**Addressing:**

```rust
pub enum TransportAddress {
    Unix(PathBuf),     // unix:///tmp/agent.sock
    Tcp(SocketAddr),   // tcp://127.0.0.1:9090
    Url(String),       // https://example.com/a2a
    Channel(String),   // channel://status-updates
}
```

#### Layer 3: Routing

Where messages go. Routers decide which transport addresses to deliver to.

```rust
#[async_trait]
pub trait Router: Send + Sync {
    async fn route(
        &self,
        envelope: &MessageEnvelope,
        peers: &PeerTable,
    ) -> Result<Vec<TransportAddress>>;
    fn strategy(&self) -> RoutingStrategy;
}
```

**Provided routers:**

| Router | Strategy | Description |
|--------|----------|-------------|
| `DirectRouter` | `Direct` | Point-to-point delivery to a single peer |
| `BroadcastRouter` | `Broadcast` | Deliver to all known peers (except sender) |
| `ContentRouter` | `ContentBased` | Route to peers subscribed to matching topics |

**`PeerTable`** tracks known peers and their transport addresses, with optional topic subscriptions for content-based routing.

#### Layer 4: Discovery

How agents find each other on the network.

```rust
#[async_trait]
pub trait Discovery: Send + Sync {
    async fn register(&self, identity: &AgentIdentity) -> Result<()>;
    async fn deregister(&self, id: &Uuid) -> Result<()>;
    async fn discover(&self) -> Result<Vec<AgentIdentity>>;
    async fn lookup(&self, id: &Uuid) -> Result<Option<AgentIdentity>>;
    fn protocol(&self) -> DiscoveryProtocol;
}
```

**Provided implementations:**

| Implementation | Feature flag | Description |
|----------------|-------------|-------------|
| `ManualDiscovery` | Always | In-memory peer list, configured programmatically |
| `RegistryDiscovery` | `registry-discovery` | HTTP REST-based agent registry |

#### Layer 5: Application (NetworkManager)

The user-facing API that ties all layers together.

```rust
use brainwires_agent_network::{
    NetworkManagerBuilder, AgentIdentity, Payload, NetworkEvent,
};
use brainwires_agent_network::transport::TcpTransport;
use brainwires_agent_network::routing::DirectRouter;
use brainwires_agent_network::discovery::ManualDiscovery;

let manager = NetworkManagerBuilder::new(identity)
    .add_transport(Box::new(TcpTransport::new()))
    .with_router(Box::new(DirectRouter))
    .add_discovery(Box::new(ManualDiscovery::new()))
    .build()
    .await?;

// Send a message
manager.send(peer_id, Payload::Text("hello".into())).await?;

// Broadcast to all peers
manager.broadcast(Payload::Json(json!({"status": "ready"}))).await?;

// Subscribe to network events
let mut events = manager.subscribe();
while let Ok(event) = events.recv().await {
    match event {
        NetworkEvent::PeerJoined(peer) => println!("New peer: {}", peer.name),
        NetworkEvent::MessageReceived(env) => println!("Got: {:?}", env.payload),
        _ => {}
    }
}
```

### MCP Server Framework

The framework follows the Model Context Protocol specification for tool-based AI server integration.

**Key types:**

- `McpHandler` — defines server identity, capabilities, and tool dispatch
- `McpServer<H>` — wires handler, middleware chain, and transport together
- `McpToolRegistry` — declarative tool registration with automatic dispatch
- `ServerTransport` / `StdioServerTransport` — request/response I/O

### Middleware

Middleware follows an **onion model**: requests flow forward through layers, responses flow back.

| Layer | Description |
|-------|-------------|
| `AuthMiddleware` | Bearer token validation |
| `RateLimitMiddleware` | Token-bucket rate limiter with per-tool limits |
| `LoggingMiddleware` | Structured request/response logging via `tracing` |
| `ToolFilterMiddleware` | Allow-list or deny-list for tool access |

### IPC (Inter-Process Communication)

Local agent-to-agent communication over Unix domain sockets with authenticated encryption.

**Connection lifecycle:**

1. `IpcConnection::connect(socket_path)` -- plain-text connection
2. `Handshake` exchange -- session ID, token, model, working directory
3. `connection.upgrade_to_encrypted(session_token)` -- ChaCha20-Poly1305 from this point

**Encryption (`IpcCipher`):** Derives a 256-bit key from the session token via SHA-256 with domain separator `brainwires-ipc-v1:`. All post-handshake messages use ChaCha20-Poly1305 authenticated encryption. Wire format: `[nonce 12B][ciphertext + auth tag 16B]`.

### Authentication

Session-based authentication with optional keyring storage.

**`AuthClient`** — HTTP client for authenticating against the Brainwires Studio backend.

**`SessionManager`** — Persists sessions to disk as JSON with `0600` permissions. API keys are stored separately via the `KeyStore` trait (system keyring preferred).

### Agent Manager

Trait-based agent lifecycle for MCP server mode.

```rust
#[async_trait]
pub trait AgentManager: Send + Sync {
    async fn spawn_agent(&self, config: SpawnConfig) -> Result<String>;
    async fn list_agents(&self) -> Result<Vec<AgentInfo>>;
    async fn agent_status(&self, agent_id: &str) -> Result<AgentInfo>;
    async fn stop_agent(&self, agent_id: &str) -> Result<()>;
    async fn await_agent(&self, agent_id: &str, timeout_secs: Option<u64>) -> Result<AgentResult>;
    async fn pool_stats(&self) -> Result<Value>;
    async fn file_locks(&self) -> Result<Value>;
}
```

### Remote Bridge

Backend connectivity with protocol negotiation, heartbeats, and priority command queuing.

Dual-mode transport: Supabase Realtime WebSocket (preferred) with HTTP polling fallback for restricted environments.

### Mesh Networking (feature: `mesh`)

Distributed agent mesh networking for multi-node coordination. Includes topology management (star, ring, full mesh, hierarchical), message routing strategies, peer discovery protocols, and federation gateways for cross-mesh bridging.

> **Note:** The mesh module provides trait definitions and types. The protocol-layer stack (transport, routing, discovery) provides the concrete implementations that power mesh networking.

### Message Types

**`MessageEnvelope`** — the universal message container:

| Field | Type | Description |
|-------|------|-------------|
| `id` | `Uuid` | Unique message ID |
| `sender` | `Uuid` | Sender agent ID |
| `recipient` | `MessageTarget` | Direct(Uuid), Broadcast, or Topic(String) |
| `payload` | `Payload` | Json(Value), Binary(Bytes), or Text(String) |
| `timestamp` | `DateTime<Utc>` | When the message was created |
| `ttl` | `Option<u32>` | Remaining hops before discard |
| `correlation_id` | `Option<Uuid>` | Links replies to requests |
| `transport_type` | `TransportType` | Which transport originated this message |

## Usage Examples

### MCP Server with Auth and Rate Limiting

```rust
use brainwires_agent_network::prelude::*;
use brainwires_agent_network::middleware::{
    AuthMiddleware, RateLimitMiddleware, ToolFilterMiddleware,
};

let server = McpServer::new(MyHandler)
    .with_transport(StdioServerTransport::new())
    .with_middleware(AuthMiddleware::bearer("my-secret-token"))
    .with_middleware(RateLimitMiddleware::new(100)) // 100 req/min
    .with_middleware(ToolFilterMiddleware::deny(["bash"]));

server.run().await?;
```

### TCP Transport Peer-to-Peer

```rust
use brainwires_agent_network::transport::{TcpTransport, TransportAddress, Transport};
use brainwires_agent_network::network::{MessageEnvelope, Payload};

let mut client = TcpTransport::new();
client.connect(&TransportAddress::Tcp("127.0.0.1:9090".parse()?)).await?;

let envelope = MessageEnvelope::direct(sender_id, peer_id, Payload::Text("ping".into()));
client.send(&envelope).await?;

let reply = client.receive().await?;
client.disconnect().await?;
```

### Pub/Sub Topic Messaging

```rust
use brainwires_agent_network::transport::{PubSubTransport, TransportAddress, Transport};
use brainwires_agent_network::network::{MessageEnvelope, Payload};

let mut transport = PubSubTransport::new();
transport.connect(&TransportAddress::Channel("events".into())).await?;

// Subscribe to a topic
let mut rx = transport.subscribe_topic("status-updates").await;

// Send a topic message
let envelope = MessageEnvelope::topic(sender_id, "status-updates", Payload::Text("ready".into()));
transport.send(&envelope).await?;
```

### A2A Transport (Cross-Framework)

```rust
use brainwires_agent_network::transport::{A2aTransport, TransportAddress, Transport};

let mut transport = A2aTransport::from_url("https://other-agent.example.com/a2a")?;
transport.connect(&TransportAddress::Url("https://other-agent.example.com/a2a".into())).await?;

transport.send(&envelope).await?;
```

### Encrypted IPC Connection

```rust
use brainwires_agent_network::ipc::{IpcConnection, ViewerMessage, AgentMessage};

let conn = IpcConnection::connect(socket_path).await?;
let (mut reader, mut writer) = conn.upgrade_to_encrypted(session_token).split();

writer.write(&ViewerMessage::UserInput {
    content: "Hello".into(),
    context_files: vec![],
}).await?;

let response: Option<AgentMessage> = reader.read().await?;
```

### Agent Discovery

```rust
use brainwires_agent_network::ipc::{
    list_agent_sessions_with_metadata, cleanup_stale_sockets, format_agent_tree,
};

cleanup_stale_sockets(sessions_dir).await?;
let agents = list_agent_sessions_with_metadata(sessions_dir)?;
let tree = format_agent_tree(sessions_dir, Some("current-session-id"))?;
println!("{}", tree);
```

## Integration with Brainwires

Use via the `brainwires` facade crate:

```toml
[dependencies]
brainwires = { version = "0.6", features = ["agent-network"] }
```

Or use standalone — `brainwires-agent-network` depends only on `brainwires-core` and `brainwires-mcp`.

## License

Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or [MIT License](LICENSE-MIT) at your option.