Expose the WebUI's read views through MCP: order list, plan preview, hypervisors and catalog
Rust development slice / linux (push) Failing after 7s

New tools operations.list, operations.get_plan, inventory.list_hypervisors,
catalog.list_package_recipes, catalog.list_windows_msi_recipes and
catalog.get_artifact reuse the WebUI handlers and their tenant and scope
checks. Interactive-only actions (approvals, device management, customer
transfer, enrollment, emergency stop, imports, tokens) stay out of MCP and
are listed as such in the access guide and product contract.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 11:35:08 +01:00
co-authored by Claude Opus 5.5
parent 19163a2efe
commit 49af111db0
7 changed files with 330 additions and 26 deletions
+87
View File
@@ -600,6 +600,93 @@ mod tests {
std::fs::remove_dir_all(key_dir).unwrap();
}
#[tokio::test]
#[ignore = "requires isolated PostgreSQL with current migrations"]
async fn mcp_read_tools_follow_customer_scope_and_revocation() {
let admin = PgPool::connect(&std::env::var("RMM_TEST_REPAIR_ADMIN_URL").unwrap())
.await
.unwrap();
let pool = sqlx::postgres::PgPoolOptions::new()
.max_connections(2)
.acquire_timeout(std::time::Duration::from_secs(3))
.connect(&std::env::var("RMM_TEST_REPAIR_APP_URL").unwrap())
.await
.unwrap();
let a = Uuid::parse_str(rmm_domain::TENANT_A).unwrap();
let b = Uuid::parse_str(rmm_domain::TENANT_B).unwrap();
let issuer = format!("local:{}", Uuid::new_v4());
sqlx::query("INSERT INTO memberships(tenant_id,principal,kind,permissions) VALUES($1,$2,'human',ARRAY['inventory:read','operations:read','tokens:manage'])")
.bind(a).bind(&issuer).execute(&admin).await.unwrap();
let reader = format!("rmm_mcp_{}A", "Q".repeat(42));
let inventory_only = format!("rmm_mcp_{}A", "g".repeat(42));
let reader_id = Uuid::new_v4();
for (id, token, scopes) in [
(reader_id, &reader, vec!["inventory:read", "operations:read"]),
(Uuid::new_v4(), &inventory_only, vec!["inventory:read"]),
] {
let mut tx = rmm_persistence::tenant_tx(&pool, a).await.unwrap();
sqlx::query("SELECT issue_customer_token($1,$2,'MCP read fixture',$3,$4,now()+interval '1 hour',false,$5)")
.bind(&issuer).bind(id).bind(hash(token.as_bytes())).bind(&scopes).bind(vec![a])
.execute(&mut *tx).await.unwrap();
tx.commit().await.unwrap();
}
let config = sample(&issuer, a, &hex::encode(derive_password(b"unused", &[7; 32]).unwrap()));
let mut auth = crate::auth::Auth::from_env("development").await.unwrap();
auth.local = Some(configured(&config));
let key_dir = std::env::temp_dir().join(reader_id.to_string());
let state = crate::State {
pool: pool.clone(),
signer: None,
viewer_a: [0; 32],
viewer_b: [1; 32],
auth: Arc::new(auth),
job_signer: Arc::new(
crate::job_signer::JobSigner::load_or_create(&key_dir.join("mcp-read.key")).unwrap(),
),
};
let code = |result: Result<serde_json::Value, crate::jobs::Error>| match result {
Ok(_) => "OK",
Err(e) => e.1,
};
let page = |value: serde_json::Value| {
assert!(value["items"].is_array(), "{value}");
value
};
page(crate::mcp::call(&state, &reader, "operations.list", json!({"tenant_id":a})).await.unwrap());
page(crate::mcp::call(&state, &reader, "operations.list",
json!({"tenant_id":a,"status":"pending_approval","q":"fixture","sort":"recent","limit":5})).await.unwrap());
page(crate::mcp::call(&state, &reader, "inventory.list_hypervisors", json!({"tenant_id":a,"q":"pve"})).await.unwrap());
page(crate::mcp::call(&state, &reader, "catalog.list_package_recipes", json!({"tenant_id":a})).await.unwrap());
page(crate::mcp::call(&state, &reader, "catalog.list_windows_msi_recipes", json!({"tenant_id":a})).await.unwrap());
let unknown = Uuid::new_v4();
assert_eq!(code(crate::mcp::call(&state, &reader, "operations.get_plan", json!({"tenant_id":a,"plan_id":unknown})).await), "NOT_FOUND");
assert_eq!(code(crate::mcp::call(&state, &reader, "catalog.get_artifact", json!({"tenant_id":a,"artifact_id":unknown})).await), "NOT_FOUND");
assert_eq!(code(crate::mcp::call(&state, &reader, "operations.list", json!({"tenant_id":a,"status":"approve"})).await), "VALIDATION_ERROR");
assert_eq!(code(crate::mcp::call(&state, &reader, "operations.list", json!({"tenant_id":a,"unexpected":true})).await), "VALIDATION_ERROR");
// A foreign customer is not visible, whatever tool reads it.
for (tool, args) in [
("operations.list", json!({"tenant_id":b})),
("operations.get_plan", json!({"tenant_id":b,"plan_id":unknown})),
("inventory.list_hypervisors", json!({"tenant_id":b})),
("catalog.list_package_recipes", json!({"tenant_id":b})),
] {
assert_eq!(code(crate::mcp::call(&state, &reader, tool, args).await), "NOT_FOUND", "{tool}");
}
// Without operations:read the order and catalog views stay closed.
for tool in ["operations.list", "catalog.list_package_recipes", "catalog.list_windows_msi_recipes"] {
assert_eq!(code(crate::mcp::call(&state, &inventory_only, tool, json!({"tenant_id":a})).await), "FORBIDDEN", "{tool}");
}
assert_eq!(code(crate::mcp::call(&state, &inventory_only, "operations.get_plan", json!({"tenant_id":a,"plan_id":unknown})).await), "NOT_FOUND");
page(crate::mcp::call(&state, &inventory_only, "inventory.list_hypervisors", json!({"tenant_id":a})).await.unwrap());
// A revoked token loses every read on its next call.
sqlx::query("UPDATE mcp_tokens SET revoked_at=now() WHERE token_id=$1")
.bind(reader_id).execute(&admin).await.unwrap();
for tool in ["operations.list", "inventory.list_hypervisors", "catalog.list_package_recipes"] {
assert_eq!(code(crate::mcp::call(&state, &reader, tool, json!({"tenant_id":a})).await), "UNAUTHENTICATED", "{tool}");
}
std::fs::remove_dir_all(key_dir).unwrap();
}
#[tokio::test]
#[ignore = "requires isolated PostgreSQL with current migrations"]
async fn local_password_totp_database() {