From 7fc742ba86e4d7175659d4a5c5fade792c175d96 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Fri, 23 Oct 2026 15:05:55 -0700 Subject: [PATCH] Keep MCP access tokens valid while agents use them Knife passes each agent session a fixed bearer header, so tokens that expired an hour after issue cut long-running agents off from the Computer MCP. Access tokens now expire after a week without use; clients are still told to renew hourly, which keeps refresh tokens rotating. Assisted-by: claude-opus-5.5 --- dashboard/src/mcp.rs | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/dashboard/src/mcp.rs b/dashboard/src/mcp.rs index d3e88c0586ca3251d62482b85d9473df7a9abfc4..65f042c521bdab1b64f1347fd7c9b76e1c9d3915 100644 --- a/dashboard/src/mcp.rs +++ b/dashboard/src/mcp.rs @@ -21,6 +21,10 @@ const CATALOGS: &[(&str, &str, &[&str])] = &[ ), ]; +/// Agents keep the access token their session started with, so it lives while used rather +/// than for the hour clients are told to renew within. +const ACCESS_IDLE: i64 = 7 * 86400; + pub struct Store { pub(crate) db: Mutex, pub origin: url::Url, @@ -211,7 +215,7 @@ impl Store { db, &format!("access:{}", hash(&access)), &json!({"grant":grant["id"],"resource":grant["resource"]}), - 3600, + ACCESS_IDLE, )?; let mut response = json!({"access_token":access,"token_type":"Bearer","expires_in":3600,"scope":array(&grant["scopes"]).iter().map(string).collect::>().join(" ")}); if array(&grant["scopes"]) @@ -237,11 +241,17 @@ impl Store { .filter(|s| !s.is_empty() && s.len() <= 256) .ok_or_else(|| Error::new(401, "invalid_token"))?; let db = self.db.lock().unwrap(); - let access = get(&db, &format!("access:{}", hash(token)))?; + let key = format!("access:{}", hash(token)); + let access = get(&db, &key)?; let grant = get(&db, &format!("grant:{}", string(&access["grant"])))?; if grant.is_null() || access["resource"] != resource || grant["resource"] != resource { return Err(Error::new(401, "invalid_token")); } + let expires = now() as i64 + ACCESS_IDLE; + db.execute( + "UPDATE records SET expires=? WHERE key=? AND expires i64 { + db.query_row("SELECT expires FROM records WHERE key=?", [&key], |r| r.get(0)).unwrap() + }; + let set = |db: &Connection, at: i64| { + db.execute("UPDATE records SET expires=? WHERE key=?", rusqlite::params![at, key]).unwrap(); + }; + set(&db, now() as i64 + 60); + drop(db); + let headers = bearer(string(&tokens["access_token"])); + assert!(fixture.store.authenticate(&headers, &resource).is_ok()); + assert!(expires(&fixture.store.db.lock().unwrap()) >= now() as i64 + ACCESS_IDLE - 5); + set(&fixture.store.db.lock().unwrap(), now() as i64 - 1); + assert!(fixture.store.authenticate(&headers, &resource).is_err()); + } #[tokio::test] async fn authorization_restricts_issuer_scopes_to_the_requested_resource() { let fixture = Fixture::new(); -- 2.54.1