-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathall_providers.rs
More file actions
529 lines (497 loc) · 16.4 KB
/
all_providers.rs
File metadata and controls
529 lines (497 loc) · 16.4 KB
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! Showcase for every provider/transport. Mirrors go-utcp example set.
//! Each section is opt-in via environment variables so you can run what you have
//! endpoints for without the others failing.
//!
//! Example:
//! DEMO_HTTP_URL=https://httpbin.org/post \
//! DEMO_WS_URL=wss://echo.websocket.events \
//! cargo run --example all_providers
use anyhow::Result;
use rs_utcp::UtcpClientInterface;
use serde_json::json;
use std::collections::HashMap;
use std::path::PathBuf;
use tokio::time::{timeout, Duration};
#[path = "common/mod.rs"]
mod common;
fn env(key: &str) -> Option<String> {
std::env::var(key).ok()
}
#[tokio::main]
async fn main() -> Result<()> {
println!("Running provider demos (set DEMO_* env vars to enable a block):");
if let Err(e) = demo_http().await {
eprintln!("HTTP demo error: {e}");
}
if let Err(e) = demo_cli().await {
eprintln!("CLI demo error: {e}");
}
if let Err(e) = demo_websocket().await {
eprintln!("WebSocket demo error: {e}");
}
if let Err(e) = demo_graphql().await {
eprintln!("GraphQL demo error: {e}");
}
if let Err(e) = demo_grpc().await {
eprintln!("gRPC demo error: {e}");
}
if let Err(e) = demo_tcp().await {
eprintln!("TCP demo error: {e}");
}
if let Err(e) = demo_udp().await {
eprintln!("UDP demo error: {e}");
}
if let Err(e) = demo_sse().await {
eprintln!("SSE demo error: {e}");
}
if let Err(e) = demo_http_stream().await {
eprintln!("HTTP stream demo error: {e}");
}
if let Err(e) = demo_mcp().await {
eprintln!("MCP demo error: {e}");
}
if let Err(e) = demo_text().await {
eprintln!("Text demo error: {e}");
}
if let Err(e) = demo_webrtc().await {
eprintln!("WebRTC demo error: {e}");
}
Ok(())
}
async fn demo_http() -> Result<()> {
let Some(url) = env("DEMO_HTTP_URL") else {
println!(" ▫️ HTTP: set DEMO_HTTP_URL to run");
return Ok(());
};
println!(" ▶️ HTTP -> {url}");
let client = common::client_from_providers(json!({
"manual_version": "1.0.0",
"utcp_version": "0.3.0",
"allowed_communication_protocols": ["http"],
"info": {
"title": "HTTP Demo",
"version": "1.0.0",
"description": "HTTP Demo Manual"
},
"tools": [{
"name": "echo",
"description": "HTTP echo tool",
"inputs": { "type": "object" },
"outputs": { "type": "object" },
"tool_call_template": {
"call_template_type": "http",
"name": "http_demo",
"url": url,
"http_method": "POST"
}
}]
}))
.await?;
let mut args = HashMap::new();
args.insert("echo".into(), json!("hello from rust-utcp"));
let value = client.call_tool("http_demo.echo", args).await?;
println!(" response: {value}");
Ok(())
}
async fn demo_cli() -> Result<()> {
let Some(cmd) = env("DEMO_CLI_CMD") else {
println!(" ▫️ CLI: set DEMO_CLI_CMD (e.g., echo '{{\"tools\":[]}}') to run");
return Ok(());
};
println!(" ▶️ CLI -> {cmd}");
let client = common::client_from_providers(json!({
"manual_version": "1.0.0",
"utcp_version": "0.3.0",
"allowed_communication_protocols": ["cli"],
"info": {
"title": "CLI Demo",
"version": "1.0.0",
"description": "CLI Demo Manual"
},
"tools": [{
"name": "echo",
"description": "CLI echo tool",
"inputs": { "type": "object" },
"outputs": { "type": "object" },
"tool_call_template": {
"call_template_type": "cli",
"name": "cli_demo",
"command": cmd
}
}]
}))
.await?;
let mut args = HashMap::new();
args.insert("msg".into(), json!("hello cli"));
let value = client.call_tool("cli_demo.echo", args).await;
println!(" call result: {:?}", value);
Ok(())
}
async fn demo_websocket() -> Result<()> {
let Some(url) = env("DEMO_WS_URL") else {
println!(" ▫️ WebSocket: set DEMO_WS_URL to run (e.g., wss://echo.websocket.events)");
return Ok(());
};
println!(" ▶️ WebSocket -> {url}");
let client = common::client_from_providers(json!({
"manual_version": "1.0.0",
"utcp_version": "0.3.0",
"allowed_communication_protocols": ["websocket"],
"info": {
"title": "WebSocket Demo",
"version": "1.0.0",
"description": "WebSocket Demo Manual"
},
"tools": [{
"name": "echo",
"description": "WebSocket echo tool",
"inputs": { "type": "object" },
"outputs": { "type": "object" },
"tool_call_template": {
"call_template_type": "websocket",
"name": "ws_demo",
"url": url
}
}]
}))
.await?;
let mut args = HashMap::new();
args.insert("text".into(), json!("hello websocket"));
let value = client.call_tool("ws_demo.echo", args).await;
println!(" call result: {:?}", value);
Ok(())
}
async fn demo_graphql() -> Result<()> {
let Some(url) = env("DEMO_GRAPHQL_URL") else {
println!(" ▫️ GraphQL: set DEMO_GRAPHQL_URL to run");
return Ok(());
};
println!(" ▶️ GraphQL -> {url}");
let client = common::client_from_providers(json!({
"manual_version": "1.0.0",
"utcp_version": "0.3.0",
"allowed_communication_protocols": ["graphql"],
"info": {
"title": "GraphQL Demo",
"version": "1.0.0",
"description": "GraphQL Demo Manual"
},
"tools": [{
"name": "hello",
"description": "GraphQL hello tool",
"inputs": { "type": "object" },
"outputs": { "type": "object" },
"tool_call_template": {
"call_template_type": "graphql",
"name": "graphql_demo",
"url": url
}
}]
}))
.await?;
let mut args = HashMap::new();
args.insert("name".into(), json!("rust-utcp"));
let value = client.call_tool("graphql_demo.hello", args).await;
println!(" call result: {:?}", value);
Ok(())
}
async fn demo_grpc() -> Result<()> {
let (Some(host), Some(port)) = (env("DEMO_GRPC_HOST"), env("DEMO_GRPC_PORT")) else {
println!(" ▫️ gRPC: set DEMO_GRPC_HOST and DEMO_GRPC_PORT to run");
return Ok(());
};
let port: u16 = port.parse().unwrap_or(50051);
println!(" ▶️ gRPC -> {host}:{port}");
let client = common::client_from_providers(json!({
"manual_version": "1.0.0",
"utcp_version": "0.3.0",
"allowed_communication_protocols": ["grpc"],
"info": {
"title": "gRPC Demo",
"version": "1.0.0",
"description": "gRPC Demo Manual"
},
"tools": [{
"name": "echo",
"description": "gRPC echo tool",
"inputs": { "type": "object" },
"outputs": { "type": "object" },
"tool_call_template": {
"call_template_type": "grpc",
"name": "grpc_demo",
"host": host,
"port": port
}
}]
}))
.await?;
let mut args = HashMap::new();
args.insert("echo".into(), json!("hello grpc"));
let value = client.call_tool("grpc_demo.echo", args).await;
println!(" call result: {:?}", value);
Ok(())
}
async fn demo_tcp() -> Result<()> {
let Some(addr) = env("DEMO_TCP_ADDR") else {
println!(" ▫️ TCP: set DEMO_TCP_ADDR (host:port) to run");
return Ok(());
};
let (host, port) = split_host_port(&addr)?;
println!(" ▶️ TCP -> {addr}");
let client = common::client_from_providers(json!({
"manual_version": "1.0.0",
"utcp_version": "0.3.0",
"allowed_communication_protocols": ["tcp"],
"info": {
"title": "TCP Demo",
"version": "1.0.0",
"description": "TCP Demo Manual"
},
"tools": [{
"name": "echo",
"description": "TCP echo tool",
"inputs": { "type": "object" },
"outputs": { "type": "object" },
"tool_call_template": {
"call_template_type": "tcp",
"name": "tcp_demo",
"host": host,
"port": port
}
}]
}))
.await?;
let mut args = HashMap::new();
args.insert("ping".into(), json!("pong"));
let value = client.call_tool("tcp_demo.echo", args).await;
println!(" call result: {:?}", value);
Ok(())
}
async fn demo_udp() -> Result<()> {
let Some(addr) = env("DEMO_UDP_ADDR") else {
println!(" ▫️ UDP: set DEMO_UDP_ADDR (host:port) to run");
return Ok(());
};
let (host, port) = split_host_port(&addr)?;
println!(" ▶️ UDP -> {addr}");
let client = common::client_from_providers(json!({
"manual_version": "1.0.0",
"utcp_version": "0.3.0",
"allowed_communication_protocols": ["udp"],
"info": {
"title": "UDP Demo",
"version": "1.0.0",
"description": "UDP Demo Manual"
},
"tools": [{
"name": "echo",
"description": "UDP echo tool",
"inputs": { "type": "object" },
"outputs": { "type": "object" },
"tool_call_template": {
"call_template_type": "udp",
"name": "udp_demo",
"host": host,
"port": port
}
}]
}))
.await?;
let mut args = HashMap::new();
args.insert("ping".into(), json!("pong"));
let value = client.call_tool("udp_demo.echo", args).await;
println!(" call result: {:?}", value);
Ok(())
}
async fn demo_sse() -> Result<()> {
let Some(url) = env("DEMO_SSE_URL") else {
println!(" ▫️ SSE: set DEMO_SSE_URL to run");
return Ok(());
};
println!(" ▶️ SSE -> {url}");
let client = common::client_from_providers(json!({
"manual_version": "1.0.0",
"utcp_version": "0.3.0",
"allowed_communication_protocols": ["sse"],
"info": {
"title": "SSE Demo",
"version": "1.0.0",
"description": "SSE Demo Manual"
},
"tools": [{
"name": "stream",
"description": "SSE stream tool",
"inputs": { "type": "object" },
"outputs": { "type": "object" },
"tool_call_template": {
"call_template_type": "sse",
"name": "sse_demo",
"url": url
}
}]
}))
.await?;
let mut args = HashMap::new();
args.insert("topic".into(), json!("demo"));
let mut stream = client.call_tool_stream("sse_demo.stream", args).await?;
let item = timeout(Duration::from_secs(5), stream.next()).await;
println!(" first event: {:?}", item);
let _ = stream.close().await;
Ok(())
}
async fn demo_http_stream() -> Result<()> {
let Some(url) = env("DEMO_HTTP_STREAM_URL") else {
println!(" ▫️ HTTP Stream: set DEMO_HTTP_STREAM_URL to run");
return Ok(());
};
println!(" ▶️ HTTP Stream -> {url}");
let client = common::client_from_providers(json!({
"manual_version": "1.0.0",
"utcp_version": "0.3.0",
"allowed_communication_protocols": ["http_stream"],
"info": {
"title": "HTTP Stream Demo",
"version": "1.0.0",
"description": "HTTP Stream Demo Manual"
},
"tools": [{
"name": "stream",
"description": "HTTP stream tool",
"inputs": { "type": "object" },
"outputs": { "type": "object" },
"tool_call_template": {
"call_template_type": "http_stream",
"name": "http_stream_demo",
"url": url
}
}]
}))
.await?;
let mut args = HashMap::new();
args.insert("query".into(), json!("stream me"));
let mut stream = client
.call_tool_stream("http_stream_demo.stream", args)
.await?;
let item = timeout(Duration::from_secs(5), stream.next()).await;
println!(" first chunk: {:?}", item);
let _ = stream.close().await;
Ok(())
}
async fn demo_mcp() -> Result<()> {
let Some(url) = env("DEMO_MCP_URL") else {
println!(" ▫️ MCP: set DEMO_MCP_URL to run");
return Ok(());
};
println!(" ▶️ MCP -> {url}");
let client = common::client_from_providers(json!({
"manual_version": "1.0.0",
"utcp_version": "0.3.0",
"allowed_communication_protocols": ["mcp"],
"info": {
"title": "MCP Demo",
"version": "1.0.0",
"description": "MCP Demo Manual"
},
"tools": [{
"name": "echo",
"description": "MCP echo tool",
"inputs": { "type": "object" },
"outputs": { "type": "object" },
"tool_call_template": {
"call_template_type": "mcp",
"name": "mcp_demo",
"url": url
}
}]
}))
.await?;
let tools = client.search_tools("", 10).await?;
println!(" tools: {}", tools.len());
let mut args = HashMap::new();
args.insert("name".into(), json!("echo"));
let value = client.call_tool("mcp_demo.echo", args).await;
println!(" call result: {:?}", value);
Ok(())
}
async fn demo_text() -> Result<()> {
let Some(path) = env("DEMO_TEXT_PATH") else {
println!(
" ▫️ Text: set DEMO_TEXT_PATH to a folder containing tools.json and scripts to run"
);
return Ok(());
};
let base_path = PathBuf::from(path);
println!(" ▶️ Text -> {}", base_path.display());
let client = common::client_from_providers(json!({
"manual_version": "1.0.0",
"utcp_version": "0.3.0",
"allowed_communication_protocols": ["text"],
"info": {
"title": "Text Demo",
"version": "1.0.0",
"description": "Text Demo Manual"
},
"tools": [{
"name": "hello",
"description": "Text hello tool",
"inputs": { "type": "object" },
"outputs": { "type": "object" },
"tool_call_template": {
"call_template_type": "text",
"name": "text_demo",
"base_path": base_path
}
}]
}))
.await?;
let tools = client.search_tools("", 10).await?;
println!(" tools: {}", tools.len());
let mut args = HashMap::new();
args.insert("name".into(), json!("world"));
let value = client.call_tool("text_demo.hello", args).await;
println!(" call result: {:?}", value);
Ok(())
}
async fn demo_webrtc() -> Result<()> {
let Some(sig) = env("DEMO_WEBRTC_SIGNALING") else {
println!(" ▫️ WebRTC: set DEMO_WEBRTC_SIGNALING to run (transport currently a stub)");
return Ok(());
};
println!(" ▶️ WebRTC -> {sig}");
let client = common::client_from_providers(json!({
"manual_version": "1.0.0",
"utcp_version": "0.3.0",
"allowed_communication_protocols": ["webrtc"],
"info": {
"title": "WebRTC Demo",
"version": "1.0.0",
"description": "WebRTC Demo Manual"
},
"tools": [{
"name": "echo",
"description": "WebRTC echo tool",
"inputs": { "type": "object" },
"outputs": { "type": "object" },
"tool_call_template": {
"call_template_type": "webrtc",
"name": "webrtc_demo",
"signaling_server": sig
}
}]
}))
.await?;
let mut args = HashMap::new();
args.insert("message".into(), json!("hello p2p"));
let value = client.call_tool("webrtc_demo.echo", args).await;
println!(" call result: {:?}", value);
Ok(())
}
fn split_host_port(addr: &str) -> Result<(String, u16)> {
let mut parts = addr.split(':');
let host = parts.next().unwrap_or_default().to_string();
let port = parts
.next()
.ok_or_else(|| anyhow::anyhow!("Missing port in {addr}"))?
.parse()?;
Ok((host, port))
}