tandem-server 0.6.5

HTTP server for Tandem engine APIs
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
use super::*;

#[tokio::test]
async fn mcp_phase_tool_authority_allows_same_scope_and_records_policy() {
    let state = test_state().await;
    let (endpoint, server) = spawn_fake_notion_oauth_mcp_server().await;
    state
        .mcp
        .add_or_update("notion".to_string(), endpoint, HashMap::new(), true)
        .await;
    let tenant =
        tandem_types::TenantContext::explicit_user_workspace("org-a", "workspace-a", None, "alice");
    state
        .mcp
        .set_bearer_token_for_tenant("notion", "alice-union-token", &tenant)
        .await
        .expect("store tenant token");
    state
        .mcp
        .refresh_for_tenant("notion", &tenant)
        .await
        .expect("refresh tenant tools");
    let verified = verified_mcp_execute_context(
        &tenant,
        tandem_types::PrincipalRef::human_user("alice").with_tenant_actor_id("alice"),
        "assertion-phase-tool-allow",
    );

    let result = crate::http::mcp::call_mcp_tool_for_tenant_with_verified_context(
        &state,
        "notion",
        "alice_search",
        json!({
            "query": "roadmap",
            "__phase_tool_authority": {
                "phase": "research",
                "allowed_tools": ["mcp.notion.alice_search"],
                "run_id": "run-phase-allow",
                "automation_id": "automation-phase",
                "node_id": "node-research",
                "session_id": "session-phase",
                "message_id": "message-phase"
            }
        }),
        &tenant,
        Some(&verified),
    )
    .await
    .expect("allowed phase MCP call");

    assert_eq!(
        result
            .metadata
            .pointer("/phaseToolAuthorityPreflight/phase")
            .and_then(Value::as_str),
        Some("research")
    );
    let decisions = state
        .list_policy_decisions_for_run(&tenant, "run-phase-allow", 50)
        .await;
    let decision = decisions
        .iter()
        .find(|decision| {
            decision.policy_id.as_deref() == Some("workflow_phase_tool_authority")
                && decision.reason_code == "phase_tool_allowed"
        })
        .expect("phase tool allow policy decision");
    assert_eq!(decision.decision, tandem_types::PolicyDecisionEffect::Allow);
    assert_eq!(
        decision
            .metadata
            .pointer("/phase_tool_authority/phase")
            .and_then(Value::as_str),
        Some("research")
    );

    let audit = tokio::fs::read_to_string(&state.protected_audit_path)
        .await
        .expect("protected audit file");
    assert!(audit.contains("\"event_type\":\"mcp.tool.execution\""));
    assert!(audit.contains("workflow_phase_tool_authority"));
    assert!(!audit.contains("alice-union-token"));
    drop(server);
}

#[tokio::test]
async fn mcp_phase_tool_authority_enterprise_override_blocks_local_allow() {
    let state = test_state().await;
    let (endpoint, server) = spawn_fake_notion_oauth_mcp_server().await;
    state
        .mcp
        .add_or_update("notion".to_string(), endpoint, HashMap::new(), true)
        .await;
    let tenant =
        tandem_types::TenantContext::explicit_user_workspace("org-a", "workspace-a", None, "alice");
    state
        .mcp
        .set_bearer_token_for_tenant("notion", "alice-union-token", &tenant)
        .await
        .expect("store tenant token");
    state
        .mcp
        .refresh_for_tenant("notion", &tenant)
        .await
        .expect("refresh tenant tools");
    state.enterprise.policy_rules.write().await.insert(
        "enterprise-phase-tool-deny".to_string(),
        tandem_enterprise_contract::EnterprisePolicyRule::new(
            "enterprise-phase-tool-deny",
            "enterprise-phase-tool-floor",
            tandem_enterprise_contract::EnterprisePolicyScopeLevel::Enterprise,
            tandem_enterprise_contract::EnterprisePolicyEffect::Deny,
        )
        .with_tenant_context(tenant.clone())
        .with_workflow_phase("research")
        .with_tool_patterns(vec!["mcp.notion.*".to_string()])
        .with_reason(
            "enterprise_phase_tool_floor",
            "enterprise policy denies phase MCP tools",
        ),
    );
    let verified = verified_mcp_execute_context(
        &tenant,
        tandem_types::PrincipalRef::human_user("alice").with_tenant_actor_id("alice"),
        "assertion-phase-tool-enterprise-deny",
    );

    let err = crate::http::mcp::call_mcp_tool_for_tenant_with_verified_context(
        &state,
        "notion",
        "alice_search",
        json!({
            "query": "roadmap",
            "__phase_tool_authority": {
                "phase": "research",
                "allowed_tools": ["mcp.notion.alice_search"],
                "run_id": "run-phase-enterprise-deny",
                "automation_id": "automation-phase",
                "node_id": "node-research",
                "session_id": "session-phase",
                "message_id": "message-phase"
            }
        }),
        &tenant,
        Some(&verified),
    )
    .await
    .expect_err("enterprise policy must override phase tool allow");

    assert!(err.contains("ToolDenied { reason: PhaseToolAuthority }"));
    assert!(err.contains("enterprise policy denies phase MCP tools"));
    let decisions = state
        .list_policy_decisions_for_run(&tenant, "run-phase-enterprise-deny", 50)
        .await;
    let decision = decisions
        .iter()
        .find(|decision| decision.reason_code == "enterprise_phase_tool_floor")
        .expect("enterprise phase tool decision");
    assert_eq!(decision.decision, tandem_types::PolicyDecisionEffect::Deny);
    drop(server);
}

#[tokio::test]
async fn mcp_bridge_derives_phase_authority_from_dispatch_context() {
    let state = test_state().await;
    let (endpoint, server) = spawn_fake_notion_oauth_mcp_server().await;
    state
        .mcp
        .add_or_update("notion".to_string(), endpoint, HashMap::new(), true)
        .await;
    let tenant =
        tandem_types::TenantContext::explicit_user_workspace("org-a", "workspace-a", None, "alice");
    state
        .mcp
        .set_bearer_token_for_tenant("notion", "alice-union-token", &tenant)
        .await
        .expect("store tenant token");
    state
        .mcp
        .refresh_for_tenant("notion", &tenant)
        .await
        .expect("refresh tenant tools");
    assert_eq!(
        crate::http::mcp::sync_mcp_tools_for_server_for_tenant(&state, "notion", &tenant).await,
        1
    );
    let verified = verified_mcp_execute_context(
        &tenant,
        tandem_types::PrincipalRef::human_user("alice").with_tenant_actor_id("alice"),
        "assertion-dispatch-phase-tool",
    );
    let context = tandem_tools::ToolDispatchContext::for_tenant("test", tenant.clone())
        .with_source(
            tandem_tools::ToolDispatchSource::new("engine_loop")
                .session("session-dispatch")
                .message("message-dispatch")
                .run("run-dispatch")
                .node("node-dispatch"),
        )
        .with_scope_allowlist(vec!["mcp.notion.alice_search".to_string()])
        .with_verified_tenant_context(verified);

    let result = state
        .tool_dispatcher
        .dispatch(
            "mcp.notion.alice_search",
            json!({
                "query": "roadmap",
                "__phase_tool_authority": {
                    "allowed_tools": ["mcp.notion.spoofed"]
                }
            }),
            context,
        )
        .await
        .expect("dispatcher-injected phase authority should allow matching MCP tool");

    assert_eq!(
        result
            .metadata
            .pointer("/phaseToolAuthorityPreflight/runId")
            .and_then(Value::as_str),
        Some("run-dispatch")
    );
    let decisions = state
        .list_policy_decisions_for_run(&tenant, "run-dispatch", 50)
        .await;
    let decision = decisions
        .iter()
        .find(|decision| {
            decision.policy_id.as_deref() == Some("workflow_phase_tool_authority")
                && decision.reason_code == "phase_tool_allowed"
        })
        .expect("phase tool decision from dispatch context");
    assert_eq!(
        decision
            .metadata
            .pointer("/phase_tool_authority/allowed_tools/0")
            .and_then(Value::as_str),
        Some("mcp.notion.alice_search")
    );
    drop(server);
}

#[tokio::test]
async fn mcp_bridge_allows_unscoped_dispatch_context_authority() {
    let state = test_state().await;
    let (endpoint, server) = spawn_fake_notion_oauth_mcp_server().await;
    state
        .mcp
        .add_or_update("notion".to_string(), endpoint, HashMap::new(), true)
        .await;
    let tenant =
        tandem_types::TenantContext::explicit_user_workspace("org-a", "workspace-a", None, "alice");
    state
        .mcp
        .set_bearer_token_for_tenant("notion", "alice-union-token", &tenant)
        .await
        .expect("store tenant token");
    state
        .mcp
        .refresh_for_tenant("notion", &tenant)
        .await
        .expect("refresh tenant tools");
    assert_eq!(
        crate::http::mcp::sync_mcp_tools_for_server_for_tenant(&state, "notion", &tenant).await,
        1
    );
    let verified = verified_mcp_execute_context(
        &tenant,
        tandem_types::PrincipalRef::human_user("alice").with_tenant_actor_id("alice"),
        "assertion-dispatch-unscoped-phase-tool",
    );
    let context = tandem_tools::ToolDispatchContext::for_tenant("test", tenant.clone())
        .with_source(
            tandem_tools::ToolDispatchSource::new("engine_loop")
                .session("session-unscoped")
                .message("message-unscoped"),
        )
        .with_verified_tenant_context(verified);

    let result = state
        .tool_dispatcher
        .dispatch(
            "mcp.notion.alice_search",
            json!({ "query": "roadmap" }),
            context,
        )
        .await
        .expect("unscoped dispatcher authority should not deny MCP tool");

    assert_eq!(
        result
            .metadata
            .pointer("/phaseToolAuthorityPreflight/reasonCode")
            .and_then(Value::as_str),
        Some("phase_tool_unscoped_dispatch_context")
    );
    assert_eq!(
        result
            .metadata
            .pointer("/phaseToolAuthorityPreflight/source")
            .and_then(Value::as_str),
        Some("tool_dispatch_context")
    );
    let decisions = state.list_policy_decisions(&tenant, 50).await;
    let decision = decisions
        .iter()
        .find(|decision| decision.reason_code == "phase_tool_unscoped_dispatch_context")
        .expect("unscoped dispatch context allow decision");
    assert_eq!(decision.decision, tandem_types::PolicyDecisionEffect::Allow);
    assert_eq!(decision.session_id.as_deref(), Some("session-unscoped"));
    drop(server);
}

#[tokio::test]
async fn mcp_phase_tool_authority_denies_wrong_phase_tool_with_audit() {
    let state = test_state().await;
    let tenant =
        tandem_types::TenantContext::explicit_user_workspace("org-a", "workspace-a", None, "alice");
    let verified = verified_mcp_execute_context(
        &tenant,
        tandem_types::PrincipalRef::human_user("alice").with_tenant_actor_id("alice"),
        "assertion-phase-tool-deny",
    );

    let err = crate::http::mcp::call_mcp_tool_for_tenant_with_verified_context(
        &state,
        "notion",
        "alice_search",
        json!({
            "query": "roadmap",
            "__phase_tool_authority": {
                "phase": "publish",
                "allowed_tools": ["mcp.notion.create_page"],
                "run_id": "run-phase-deny",
                "automation_id": "automation-phase",
                "node_id": "node-publish",
                "session_id": "session-phase",
                "message_id": "message-phase"
            }
        }),
        &tenant,
        Some(&verified),
    )
    .await
    .expect_err("wrong phase tool must be denied before remote execution");

    assert!(err.contains("ToolDenied { reason: PhaseToolAuthority }"));
    assert!(err.contains("not allowed during workflow phase `publish`"));
    let decisions = state
        .list_policy_decisions_for_run(&tenant, "run-phase-deny", 50)
        .await;
    let decision = decisions
        .iter()
        .find(|decision| decision.reason_code == "phase_tool_not_allowed")
        .expect("phase tool denial decision");
    assert_eq!(decision.decision, tandem_types::PolicyDecisionEffect::Deny);

    let audit = tokio::fs::read_to_string(&state.protected_audit_path)
        .await
        .expect("protected audit file");
    assert!(audit.contains("\"event_type\":\"mcp.phase_tool.denied\""));
    assert!(audit.contains("phase_tool_not_allowed"));
}

#[tokio::test]
async fn mcp_phase_tool_authority_is_required_for_explicit_tenant_calls() {
    let state = test_state().await;
    let tenant =
        tandem_types::TenantContext::explicit_user_workspace("org-a", "workspace-a", None, "alice");
    let verified = verified_mcp_execute_context(
        &tenant,
        tandem_types::PrincipalRef::human_user("alice").with_tenant_actor_id("alice"),
        "assertion-phase-tool-missing",
    );

    let err = crate::http::mcp::call_mcp_tool_for_tenant_with_verified_context(
        &state,
        "notion",
        "alice_search",
        json!({ "query": "roadmap" }),
        &tenant,
        Some(&verified),
    )
    .await
    .expect_err("explicit tenant MCP calls must include trusted phase authority");

    assert!(err.contains("ToolDenied { reason: PhaseToolAuthority }"));
    assert!(err.contains("phase tool authority is missing"));
    let decisions = state.list_policy_decisions(&tenant, 50).await;
    let decision = decisions
        .iter()
        .find(|decision| decision.reason_code == "phase_tool_authority_missing")
        .expect("missing phase authority decision");
    assert_eq!(decision.decision, tandem_types::PolicyDecisionEffect::Deny);

    let audit = tokio::fs::read_to_string(&state.protected_audit_path)
        .await
        .expect("protected audit file");
    assert!(audit.contains("\"event_type\":\"mcp.phase_tool.denied\""));
    assert!(audit.contains("phase_tool_authority_missing"));
}

#[tokio::test]
async fn mcp_phase_tool_authority_empty_allowlist_denies_all_tools() {
    let state = test_state().await;
    let tenant =
        tandem_types::TenantContext::explicit_user_workspace("org-a", "workspace-a", None, "alice");
    let verified = verified_mcp_execute_context(
        &tenant,
        tandem_types::PrincipalRef::human_user("alice").with_tenant_actor_id("alice"),
        "assertion-phase-tool-empty",
    );

    let err = crate::http::mcp::call_mcp_tool_for_tenant_with_verified_context(
        &state,
        "notion",
        "alice_search",
        json!({
            "query": "roadmap",
            "__phase_tool_authority": {
                "phase": "research",
                "allowed_tools": [],
                "run_id": "run-phase-empty"
            }
        }),
        &tenant,
        Some(&verified),
    )
    .await
    .expect_err("empty phase allowlist must deny all tools");

    assert!(err.contains("ToolDenied { reason: PhaseToolAuthority }"));
    assert!(err.contains("has no allowed tools"));
    let decisions = state
        .list_policy_decisions_for_run(&tenant, "run-phase-empty", 50)
        .await;
    let decision = decisions
        .iter()
        .find(|decision| decision.reason_code == "phase_tool_authority_empty_allowlist")
        .expect("empty allowlist denial decision");
    assert_eq!(decision.decision, tandem_types::PolicyDecisionEffect::Deny);
}

#[tokio::test]
async fn mcp_phase_tool_authority_does_not_match_bare_tool_aliases() {
    let state = test_state().await;
    let tenant =
        tandem_types::TenantContext::explicit_user_workspace("org-a", "workspace-a", None, "alice");
    let verified = verified_mcp_execute_context(
        &tenant,
        tandem_types::PrincipalRef::human_user("alice").with_tenant_actor_id("alice"),
        "assertion-phase-tool-bare",
    );

    let err = crate::http::mcp::call_mcp_tool_for_tenant_with_verified_context(
        &state,
        "notion",
        "alice_search",
        json!({
            "query": "roadmap",
            "__phase_tool_authority": {
                "phase": "research",
                "allowed_tools": ["alice_search"],
                "run_id": "run-phase-bare"
            }
        }),
        &tenant,
        Some(&verified),
    )
    .await
    .expect_err("bare tool names must not match cross-server MCP tools");

    assert!(err.contains("ToolDenied { reason: PhaseToolAuthority }"));
    let decisions = state
        .list_policy_decisions_for_run(&tenant, "run-phase-bare", 50)
        .await;
    let decision = decisions
        .iter()
        .find(|decision| decision.reason_code == "phase_tool_not_allowed")
        .expect("bare alias denial decision");
    assert_eq!(decision.decision, tandem_types::PolicyDecisionEffect::Deny);
}

#[tokio::test]
async fn mcp_secret_tenant_mismatch_records_scope_policy_and_redacts_secret_material() {
    let state = test_state().await;
    let tenant_a = tandem_types::TenantContext::explicit_user_workspace(
        "org-a",
        "workspace-a",
        Some("deployment-a".to_string()),
        "user-a",
    );
    let tenant_b = tandem_types::TenantContext::explicit_user_workspace(
        "org-b",
        "workspace-b",
        Some("deployment-b".to_string()),
        "user-b",
    );
    state
        .mcp
        .add_or_update_with_secret_refs(
            "tenant-server".to_string(),
            "http://127.0.0.1:9/mcp".to_string(),
            HashMap::new(),
            HashMap::from([(
                "Authorization".to_string(),
                tandem_runtime::McpSecretRef::Store {
                    secret_id: "super-secret-canary".to_string(),
                    tenant_context: tenant_a,
                },
            )]),
            &tenant_b,
            true,
        )
        .await;
    let verified = verified_mcp_execute_context(
        &tenant_b,
        tandem_types::PrincipalRef::human_user("user-b").with_tenant_actor_id("user-b"),
        "assertion-secret-scope",
    );

    let err = crate::http::mcp::call_mcp_tool_for_tenant_with_verified_context(
        &state,
        "tenant-server",
        "get_me",
        json!({
            "__phase_tool_authority": {
                "phase": "credential_use",
                "allowed_tools": ["mcp.tenant_server.get_me"],
                "run_id": "run-secret-scope",
                "automation_id": "automation-secret",
                "node_id": "node-secret"
            }
        }),
        &tenant_b,
        Some(&verified),
    )
    .await
    .expect_err("tenant B cannot use tenant A's store-backed secret");

    assert!(err.contains("ToolDenied { reason: TenantScope }"));
    let events = crate::audit::load_protected_audit_events_for_tenant(&state, &tenant_b).await;
    let event = events
        .iter()
        .find(|event| event.event_type == "mcp.secret_tenant_mismatch")
        .expect("mcp secret tenant mismatch audit event");
    assert_eq!(
        event.payload["policy_id"].as_str(),
        Some("mcp_secret_scope")
    );
    assert_eq!(
        event
            .payload
            .pointer("/phase_tool_authority/phase")
            .and_then(Value::as_str),
        Some("credential_use")
    );
    assert_eq!(
        event.payload["secret_material_redacted"].as_bool(),
        Some(true)
    );
    assert!(event.payload["run_as"].is_object());

    let protected_audit = tokio::fs::read_to_string(&state.protected_audit_path)
        .await
        .expect("protected audit file");
    assert!(
        !protected_audit.contains("super-secret-canary"),
        "secret identifiers must not be written to protected audit payloads"
    );
    let decisions = state
        .list_policy_decisions_for_run(&tenant_b, "run-secret-scope", 50)
        .await;
    let secret_decision = decisions
        .iter()
        .find(|decision| decision.policy_id.as_deref() == Some("mcp_secret_scope"))
        .expect("secret scope policy decision");
    assert_eq!(
        secret_decision.decision,
        tandem_types::PolicyDecisionEffect::Deny
    );
    assert!(secret_decision
        .data_classes
        .contains(&tandem_types::DataClass::Credential));
    assert_eq!(
        secret_decision
            .metadata
            .get("secret_material_redacted")
            .and_then(Value::as_bool),
        Some(true)
    );
}