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
//! Tasks-as-extension (MCP 2026-07-28) end-to-end checks.
//!
//! Under MCP 2026-07-28 the tasks capability is advertised through the extensions
//! map (`capabilities.extensions["io.modelcontextprotocol/tasks"]`) instead of
//! the former top-level `capabilities.tasks` field, and the method surface is
//! `tasks/get` (polling, carrying the terminal result inline), `tasks/update`
//! (answering input requests) and `tasks/cancel` -- with `tasks/list` and
//! `tasks/result` gone. Exercised over the stateless POST-only path.
#![cfg(all(
    not(feature = "legacy-spec"),
    feature = "tasks",
    feature = "http-server-volga",
    feature = "http-client"
))]

use neva::{App, Context, error::Error, types::elicitation::ElicitRequestParams};
use std::sync::atomic::{AtomicUsize, Ordering};

static TASK_COMMITS: AtomicUsize = AtomicUsize::new(0);

#[tokio::test(flavor = "multi_thread")]
async fn tasks_capability_is_advertised_as_extension() {
    let port = pick_free_port();
    let addr = format!("127.0.0.1:{port}");
    let mut app = App::new().with_options(|opt| {
        opt.with_http(|http| http.bind(&addr).with_endpoint("/mcp"))
            .with_tasks()
    });
    app.map_tool("ping", || async move { "pong".to_string() });
    let handle = tokio::spawn(async move { app.run().await });
    tokio::time::sleep(std::time::Duration::from_millis(300)).await;

    let client = reqwest::Client::builder()
        .no_proxy()
        .build()
        .expect("test client");
    let url = format!("http://{addr}/mcp");

    // (a) discover advertises tasks under the extensions map, not top-level.
    let discover = serde_json::json!({
        "jsonrpc": "2.0", "id": 1, "method": "server/discover", "params": { "_meta": meta() }
    });
    let resp = routed(client.post(&url), &discover)
        .json(&discover)
        .send()
        .await
        .expect("discover failed");
    assert!(resp.status().is_success());
    let body: serde_json::Value = resp.json().await.unwrap();
    let caps = &body["result"]["capabilities"];

    assert!(
        caps["extensions"]["io.modelcontextprotocol/tasks"].is_object(),
        "tasks must be advertised under capabilities.extensions, got: {caps}"
    );
    assert!(
        caps.get("tasks").is_none(),
        "no top-level capabilities.tasks under MCP 2026-07-28, got: {caps}"
    );

    // (b) the removed methods no longer dispatch.
    for gone in ["tasks/list", "tasks/result"] {
        let req = serde_json::json!({
            "jsonrpc": "2.0", "id": 2, "method": gone, "params": { "_meta": meta() }
        });
        let resp = routed(client.post(&url), &req)
            .json(&req)
            .send()
            .await
            .expect("send failed");
        let body: serde_json::Value = resp.json().await.unwrap();
        assert_eq!(
            body["error"]["code"], -32601,
            "{gone} must be gone under MCP 2026-07-28, got: {body}"
        );
    }

    // (c) `tasks/update` is routable (an unknown task is a params error, not an
    // unknown method).
    let update = serde_json::json!({
        "jsonrpc": "2.0", "id": 3, "method": "tasks/update",
        "params": { "taskId": "nope", "inputResponses": {}, "_meta": meta() }
    });
    let resp = routed(client.post(&url), &update)
        .json(&update)
        .send()
        .await
        .expect("send failed");
    let body: serde_json::Value = resp.json().await.unwrap();
    assert_eq!(
        body["error"]["code"], -32602,
        "tasks/update must dispatch, got: {body}"
    );

    handle.abort();
}

#[tokio::test(flavor = "multi_thread")]
async fn task_augmented_tool_elicits_via_suspend_resume() {
    // A task-augmented tool that elicits runs on the *stateful* task substrate,
    // not MRTR: it uses the explicit `ctx.task().elicit(...)` builder, the
    // background future suspends (task -> input_required), the client posts the
    // answer as a Response keyed by the task id (session-independent), the future
    // resumes in place, and the final result carries the elicited value. Side
    // effects are just run inline (no MRTR `on_commit` needed -- there is no
    // re-run); the counter below proves the resumed body ran to completion.
    TASK_COMMITS.store(0, Ordering::SeqCst);

    let port = pick_free_port();
    let addr = format!("127.0.0.1:{port}");
    let mut app = App::new().with_options(|opt| {
        opt.with_http(|http| http.bind(&addr).with_endpoint("/mcp"))
            .with_tasks()
    });
    app.map_tool("greet_task", |mut ctx: Context| async move {
        let params: ElicitRequestParams = ElicitRequestParams::form("Your name?")
            .with_required("name", "string")
            .into();
        let res = ctx.task().elicit(params).await?;
        let name = res
            .content
            .and_then(|c| c.get("name").and_then(|v| v.as_str().map(str::to_owned)))
            .unwrap_or_else(|| "stranger".into());
        // Inline side effect after the elicit resumes (the task runs once).
        TASK_COMMITS.fetch_add(1, Ordering::SeqCst);
        Ok::<String, Error>(format!("hello {name}"))
    })
    .with_task_support("optional");

    let handle = tokio::spawn(async move { app.run().await });
    tokio::time::sleep(std::time::Duration::from_millis(300)).await;

    let client = reqwest::Client::builder()
        .no_proxy()
        .build()
        .expect("test client");
    let url = format!("http://{addr}/mcp");

    let post = |body: serde_json::Value| {
        let client = client.clone();
        let url = url.clone();
        async move {
            routed(client.post(&url), &body)
                .json(&body)
                .send()
                .await
                .expect("send")
                .json::<serde_json::Value>()
                .await
                .expect("json")
        }
    };

    let wait_status = |target: &'static str, task_id: String| {
        let post = &post;
        async move {
            for _ in 0..100 {
                let g = post(serde_json::json!({
                    "jsonrpc": "2.0", "id": 2, "method": "tasks/get",
                    "params": { "taskId": task_id, "_meta": meta() }
                }))
                .await;
                if g["result"]["status"].as_str() == Some(target) {
                    return true;
                }
                tokio::time::sleep(std::time::Duration::from_millis(20)).await;
            }
            false
        }
    };

    // 1. Task-augmented call -> CreateTaskResult carrying a task id.
    let r1 = post(serde_json::json!({
        "jsonrpc": "2.0", "id": 1, "method": "tools/call",
        "params": {
            "name": "greet_task", "arguments": {},
            "task": { "ttl": 60000 },
            "_meta": { "io.modelcontextprotocol/protocolVersion": "2026-07-28", "io.modelcontextprotocol/clientCapabilities": { "elicitation": true } }
        }
    }))
    .await;
    assert_eq!(
        r1["result"]["resultType"], "task",
        "a deferred result is tagged `task`, got: {r1}"
    );
    let task_id = r1["result"]["taskId"]
        .as_str()
        .unwrap_or_else(|| panic!("task id present, got: {r1}"))
        .to_string();

    // 2. The tool elicits -> the task suspends into input_required.
    assert!(
        wait_status("input_required", task_id.clone()).await,
        "task must enter input_required when the tool elicits"
    );

    // 3. `tasks/get` surfaces the outstanding ask; answer it with `tasks/update`
    //    under the same key.
    let g = post(serde_json::json!({
        "jsonrpc": "2.0", "id": 3, "method": "tasks/get",
        "params": { "taskId": task_id, "_meta": meta() }
    }))
    .await;
    let key = g["result"]["inputRequests"]
        .as_object()
        .unwrap_or_else(|| panic!("inputRequests present, got: {g}"))
        .keys()
        .next()
        .expect("one outstanding ask")
        .clone();
    assert_eq!(
        g["result"]["inputRequests"][&key]["method"], "elicitation/create",
        "the ask is surfaced as a {{method, params}} envelope, got: {g}"
    );

    post(serde_json::json!({
        "jsonrpc": "2.0", "id": 4, "method": "tasks/update",
        "params": {
            "taskId": task_id,
            "_meta": meta(),
            "inputResponses": {
                key: { "action": "accept", "content": { "name": "octocat" } }
            }
        }
    }))
    .await;

    // 4. The future resumes and runs to completion.
    assert!(
        wait_status("completed", task_id.clone()).await,
        "task must complete after the answer is delivered"
    );

    // 5. `tasks/get` carries the final result inline -- there is no
    //    `tasks/result` to follow up with.
    let r = post(serde_json::json!({
        "jsonrpc": "2.0", "id": 99, "method": "tasks/get",
        "params": { "taskId": task_id, "_meta": meta() }
    }))
    .await;
    assert_eq!(
        r.pointer("/result/result/content/0/text")
            .and_then(|v| v.as_str()),
        Some("hello octocat"),
        "a completed task carries its result inline, got: {r}"
    );
    assert_eq!(
        r["result"]["resultType"], "complete",
        "the `tasks/get` result itself is complete, got: {r}"
    );
    assert_eq!(
        TASK_COMMITS.load(Ordering::SeqCst),
        1,
        "the resumed task body must run to completion exactly once"
    );

    handle.abort();
}

#[tokio::test(flavor = "multi_thread")]
async fn mrtr_elicit_inside_a_task_is_rejected_with_guidance() {
    // The MRTR `ctx.elicit` is not valid on the task substrate -- it must guide
    // the author to `ctx.task().elicit(...)` rather than silently misbehave.
    let port = pick_free_port();
    let addr = format!("127.0.0.1:{port}");
    let mut app = App::new().with_options(|opt| {
        opt.with_http(|http| http.bind(&addr).with_endpoint("/mcp"))
            .with_tasks()
    });
    app.map_tool("bad_elicit", |mut ctx: Context| async move {
        let params: ElicitRequestParams = ElicitRequestParams::form("Your name?")
            .with_required("name", "string")
            .into();
        // Wrong API for a task: this must error, not suspend.
        let res = ctx.elicit("name", params).await?;
        Ok::<String, Error>(format!("{:?}", res.content))
    })
    .with_task_support("required");

    let handle = tokio::spawn(async move { app.run().await });
    tokio::time::sleep(std::time::Duration::from_millis(300)).await;

    let client = reqwest::Client::builder()
        .no_proxy()
        .build()
        .expect("test client");
    let url = format!("http://{addr}/mcp");
    let post = |body: serde_json::Value| {
        let client = client.clone();
        let url = url.clone();
        async move {
            routed(client.post(&url), &body)
                .json(&body)
                .send()
                .await
                .expect("send")
                .json::<serde_json::Value>()
                .await
                .expect("json")
        }
    };

    let r1 = post(serde_json::json!({
        "jsonrpc": "2.0", "id": 1, "method": "tools/call",
        "params": {
            "name": "bad_elicit", "arguments": {},
            "task": { "ttl": 60000 },
            "_meta": { "io.modelcontextprotocol/protocolVersion": "2026-07-28", "io.modelcontextprotocol/clientCapabilities": { "elicitation": true } }
        }
    }))
    .await;
    assert_eq!(
        r1["result"]["resultType"], "task",
        "a deferred result is tagged `task`, got: {r1}"
    );
    let task_id = r1["result"]["taskId"]
        .as_str()
        .unwrap_or_else(|| panic!("task id present, got: {r1}"))
        .to_string();

    // The tool errors immediately; poll for a terminal state then read the error.
    let mut text = String::new();
    for _ in 0..100 {
        let r = post(serde_json::json!({
            "jsonrpc": "2.0", "id": 2, "method": "tasks/get",
            "params": { "taskId": task_id, "_meta": meta() }
        }))
        .await;
        // A *tool* error is a successful `tools/call` result carrying
        // `isError`, not a JSON-RPC failure -- so it rides in the task's
        // `result`, and the task itself completes. (`error` is reserved for a
        // protocol-level failure during execution.)
        if let Some(t) = r
            .pointer("/result/result/content/0/text")
            .and_then(|v| v.as_str())
        {
            text = t.to_string();
            break;
        }
        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
    }
    assert!(
        text.contains("ctx.task().elicit"),
        "MRTR elicit in a task must guide to ctx.task().elicit, got: {text:?}"
    );

    handle.abort();
}

#[tokio::test(flavor = "multi_thread")]
async fn mrtr_once_in_a_required_task_is_rejected() {
    // `once` is an MRTR helper; in a required-task tool (which never re-runs) it
    // must error rather than silently masquerade as a dedup.
    let port = pick_free_port();
    let addr = format!("127.0.0.1:{port}");
    let mut app = App::new().with_options(|opt| {
        opt.with_http(|http| http.bind(&addr).with_endpoint("/mcp"))
            .with_tasks()
    });
    app.map_tool("bad_once", |ctx: Context| async move {
        ctx.once("x", async { Ok(()) }).await?;
        Ok::<String, Error>("unreachable".into())
    })
    .with_task_support("required");

    let handle = tokio::spawn(async move { app.run().await });
    tokio::time::sleep(std::time::Duration::from_millis(300)).await;

    let client = reqwest::Client::builder()
        .no_proxy()
        .build()
        .expect("test client");
    let url = format!("http://{addr}/mcp");
    let post = |body: serde_json::Value| {
        let client = client.clone();
        let url = url.clone();
        async move {
            routed(client.post(&url), &body)
                .json(&body)
                .send()
                .await
                .expect("send")
                .json::<serde_json::Value>()
                .await
                .expect("json")
        }
    };

    let r1 = post(serde_json::json!({
        "jsonrpc": "2.0", "id": 1, "method": "tools/call",
        "params": {
            "name": "bad_once", "arguments": {},
            "task": { "ttl": 60000 },
            "_meta": meta()
        }
    }))
    .await;
    assert_eq!(
        r1["result"]["resultType"], "task",
        "a deferred result is tagged `task`, got: {r1}"
    );
    let task_id = r1["result"]["taskId"]
        .as_str()
        .unwrap_or_else(|| panic!("task id present, got: {r1}"))
        .to_string();

    let mut text = String::new();
    for _ in 0..100 {
        let r = post(serde_json::json!({
            "jsonrpc": "2.0", "id": 2, "method": "tasks/get",
            "params": { "taskId": task_id, "_meta": meta() }
        }))
        .await;
        // A *tool* error is a successful `tools/call` result carrying
        // `isError`, not a JSON-RPC failure -- so it rides in the task's
        // `result`, and the task itself completes. (`error` is reserved for a
        // protocol-level failure during execution.)
        if let Some(t) = r
            .pointer("/result/result/content/0/text")
            .and_then(|v| v.as_str())
        {
            text = t.to_string();
            break;
        }
        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
    }
    assert!(
        text.contains("MRTR helper") && text.contains("required-task"),
        "once in a required task must error, got: {text:?}"
    );

    handle.abort();
}

fn pick_free_port() -> u16 {
    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let port = listener.local_addr().unwrap().port();
    drop(listener);
    port
}

/// The `_meta` MCP 2026-07-28 requires on every request.
fn meta() -> serde_json::Value {
    serde_json::json!({
        "io.modelcontextprotocol/protocolVersion": "2026-07-28",
        "io.modelcontextprotocol/clientCapabilities": {}
    })
}

/// Attaches the routing headers MCP 2026-07-28 requires on every request, the
/// way a conforming client derives them: from the body it is about to send.
fn routed(req: reqwest::RequestBuilder, body: &serde_json::Value) -> reqwest::RequestBuilder {
    let method = body["method"].as_str().unwrap_or_default();
    let req = req
        .header("MCP-Protocol-Version", "2026-07-28")
        .header("Mcp-Method", method);
    let name = match method {
        "tools/call" | "prompts/get" => body.pointer("/params/name"),
        "resources/read" => body.pointer("/params/uri"),
        "tasks/get" | "tasks/update" | "tasks/cancel" => body.pointer("/params/taskId"),
        _ => None,
    };
    match name.and_then(|v| v.as_str()) {
        Some(name) => req.header("Mcp-Name", name),
        None => req,
    }
}