|
| 1 | +//! Client for the task-demo server in `examples/servers/src/task_stdio.rs`. |
| 2 | +//! |
| 3 | +//! Walks through the task lifecycle (SEP-1319): |
| 4 | +//! 1. Call a regular tool (`quick_echo`) — synchronous response. |
| 5 | +//! 2. Call a task-required tool (`slow_sum`) by attaching `task: {}` to |
| 6 | +//! the `tools/call` request. The server returns a `Task` with a `task_id`. |
| 7 | +//! 3. Poll `tasks/get` until status becomes `Completed`. |
| 8 | +//! 4. Fetch the underlying `CallToolResult` via `tasks/result`. |
| 9 | +
|
| 10 | +use anyhow::{Result, anyhow}; |
| 11 | +use rmcp::{ |
| 12 | + ServiceExt, |
| 13 | + model::{ |
| 14 | + CallToolRequestParams, CallToolResult, ClientRequest, GetTaskInfoParams, |
| 15 | + GetTaskResultParams, JsonObject, Request, ServerResult, TaskStatus, |
| 16 | + }, |
| 17 | + object, |
| 18 | + transport::{ConfigureCommandExt, TokioChildProcess}, |
| 19 | +}; |
| 20 | +use tokio::process::Command; |
| 21 | +use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; |
| 22 | + |
| 23 | +#[tokio::main] |
| 24 | +async fn main() -> Result<()> { |
| 25 | + tracing_subscriber::registry() |
| 26 | + .with( |
| 27 | + tracing_subscriber::EnvFilter::try_from_default_env() |
| 28 | + .unwrap_or_else(|_| format!("info,{}=debug", env!("CARGO_CRATE_NAME")).into()), |
| 29 | + ) |
| 30 | + .with(tracing_subscriber::fmt::layer()) |
| 31 | + .init(); |
| 32 | + |
| 33 | + // Spawn the task-demo server as a child process over stdio. |
| 34 | + let client = () |
| 35 | + .serve(TokioChildProcess::new(Command::new("cargo").configure( |
| 36 | + |cmd| { |
| 37 | + cmd.arg("run") |
| 38 | + .arg("-q") |
| 39 | + .arg("-p") |
| 40 | + .arg("mcp-server-examples") |
| 41 | + .arg("--example") |
| 42 | + .arg("servers_task_stdio"); |
| 43 | + }, |
| 44 | + ))?) |
| 45 | + .await?; |
| 46 | + |
| 47 | + // 1) Synchronous call. `quick_echo` has the default task_support = forbidden. |
| 48 | + let echo = client |
| 49 | + .call_tool( |
| 50 | + CallToolRequestParams::new("quick_echo") |
| 51 | + .with_arguments(object!({ "message": "hi from rmcp" })), |
| 52 | + ) |
| 53 | + .await?; |
| 54 | + tracing::info!("quick_echo -> {echo:#?}"); |
| 55 | + |
| 56 | + // 2) Task call. `slow_sum` is task_support = required, so we MUST attach a |
| 57 | + // `task` object. An empty object is fine — clients can stash arbitrary |
| 58 | + // metadata here that the server-side `OperationDescriptor` will keep. |
| 59 | + let create = client |
| 60 | + .send_request(ClientRequest::CallToolRequest(Request::new( |
| 61 | + CallToolRequestParams::new("slow_sum") |
| 62 | + .with_arguments(object!({ "a": 40, "b": 2 })) |
| 63 | + .with_task(JsonObject::new()), |
| 64 | + ))) |
| 65 | + .await?; |
| 66 | + let ServerResult::CreateTaskResult(create) = create else { |
| 67 | + return Err(anyhow!("expected CreateTaskResult, got {create:?}")); |
| 68 | + }; |
| 69 | + let task_id = create.task.task_id.clone(); |
| 70 | + tracing::info!( |
| 71 | + "slow_sum enqueued as task {task_id} (status = {:?})", |
| 72 | + create.task.status |
| 73 | + ); |
| 74 | + |
| 75 | + // 3) Poll `tasks/get` until the server reports a terminal status. |
| 76 | + let final_status = loop { |
| 77 | + tokio::time::sleep(std::time::Duration::from_millis(250)).await; |
| 78 | + |
| 79 | + let info = client |
| 80 | + .send_request(ClientRequest::GetTaskInfoRequest(Request::new( |
| 81 | + GetTaskInfoParams { |
| 82 | + meta: None, |
| 83 | + task_id: task_id.clone(), |
| 84 | + }, |
| 85 | + ))) |
| 86 | + .await?; |
| 87 | + let ServerResult::GetTaskResult(info) = info else { |
| 88 | + return Err(anyhow!("expected GetTaskResult, got {info:?}")); |
| 89 | + }; |
| 90 | + tracing::info!("status = {:?}", info.task.status); |
| 91 | + |
| 92 | + match info.task.status { |
| 93 | + TaskStatus::Completed | TaskStatus::Failed | TaskStatus::Cancelled => { |
| 94 | + break info.task.status; |
| 95 | + } |
| 96 | + _ => {} |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + if final_status != TaskStatus::Completed { |
| 101 | + return Err(anyhow!("task ended in {final_status:?}")); |
| 102 | + } |
| 103 | + |
| 104 | + // 4) Fetch the payload. The server-side handler returns a serialized |
| 105 | + // `CallToolResult`. On the wire the response is just a JSON value, and |
| 106 | + // `ServerResult` is `#[serde(untagged)]`, so the client decodes it as |
| 107 | + // whichever variant the JSON shape matches first — a `CallToolResult` |
| 108 | + // here. (For a non-tool task the same value would surface as |
| 109 | + // `ServerResult::CustomResult` and need manual `serde_json::from_value`.) |
| 110 | + let payload = client |
| 111 | + .send_request(ClientRequest::GetTaskResultRequest(Request::new( |
| 112 | + GetTaskResultParams { |
| 113 | + meta: None, |
| 114 | + task_id: task_id.clone(), |
| 115 | + }, |
| 116 | + ))) |
| 117 | + .await?; |
| 118 | + let call_result: CallToolResult = match payload { |
| 119 | + ServerResult::CallToolResult(r) => r, |
| 120 | + ServerResult::CustomResult(c) => serde_json::from_value(c.0)?, |
| 121 | + other => return Err(anyhow!("unexpected task result: {other:?}")), |
| 122 | + }; |
| 123 | + tracing::info!("slow_sum result -> {call_result:#?}"); |
| 124 | + |
| 125 | + client.cancel().await?; |
| 126 | + Ok(()) |
| 127 | +} |
0 commit comments