authoravatarclover caruso<git@paperclover.net>2026-10-23 15:05:55-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-24 19:01:23-07:00
log7fc742ba86e4d7175659d4a5c5fade792c175d96
treeee11ef8abfc4863ee498dbc4bcd250f9d7121dce
parent78daa503d015fc5d1b70bd8a1927000f47ceb64b
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

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

1 files changed, 36 insertions(+), 2 deletions(-)

dashboard/src/mcp.rs+36-2
......@@ -21,6 +21,10 @@ const CATALOGS: &[(&str, &str, &[&str])] = &[
2121 ),
2222];
2323
24/// Agents keep the access token their session started with, so it lives while used rather
25/// than for the hour clients are told to renew within.
26const ACCESS_IDLE: i64 = 7 * 86400;
27
2428pub struct Store {
2529 pub(crate) db: Mutex<Connection>,
2630 pub origin: url::Url,
......@@ -211,7 +215,7 @@ impl Store {
211215 db,
212216 &format!("access:{}", hash(&access)),
213217 &json!({"grant":grant["id"],"resource":grant["resource"]}),
214 3600,
218 ACCESS_IDLE,
215219 )?;
216220 let mut response = json!({"access_token":access,"token_type":"Bearer","expires_in":3600,"scope":array(&grant["scopes"]).iter().map(string).collect::<Vec<_>>().join(" ")});
217221 if array(&grant["scopes"])
......@@ -237,11 +241,17 @@ impl Store {
237241 .filter(|s| !s.is_empty() && s.len() <= 256)
238242 .ok_or_else(|| Error::new(401, "invalid_token"))?;
239243 let db = self.db.lock().unwrap();
240 let access = get(&db, &format!("access:{}", hash(token)))?;
244 let key = format!("access:{}", hash(token));
245 let access = get(&db, &key)?;
241246 let grant = get(&db, &format!("grant:{}", string(&access["grant"])))?;
242247 if grant.is_null() || access["resource"] != resource || grant["resource"] != resource {
243248 return Err(Error::new(401, "invalid_token"));
244249 }
250 let expires = now() as i64 + ACCESS_IDLE;
251 db.execute(
252 "UPDATE records SET expires=? WHERE key=? AND expires<?",
253 rusqlite::params![expires, key, expires - 3600],
254 )?;
245255 Ok(grant)
246256 }
247257 fn exchange(
......@@ -1030,6 +1040,30 @@ mod tests {
10301040 assert!(!get(&db, &format!("{kind}:other")).unwrap().is_null());
10311041 }
10321042 }
1043 #[test]
1044 fn access_tokens_live_while_used_and_lapse_when_idle() {
1045 let fixture = Fixture::new();
1046 let resource = fixture.store.resource("computer");
1047 let grant = json!({"id":"live","resource":resource,"scopes":["computer:read"]});
1048 let db = fixture.store.db.lock().unwrap();
1049 put(&db, "grant:live", &grant, 0).unwrap();
1050 let tokens = fixture.store.issue(&db, &grant).unwrap();
1051 assert_eq!(tokens["expires_in"], 3600);
1052 let key = format!("access:{}", hash(string(&tokens["access_token"])));
1053 let expires = |db: &Connection| -> i64 {
1054 db.query_row("SELECT expires FROM records WHERE key=?", [&key], |r| r.get(0)).unwrap()
1055 };
1056 let set = |db: &Connection, at: i64| {
1057 db.execute("UPDATE records SET expires=? WHERE key=?", rusqlite::params![at, key]).unwrap();
1058 };
1059 set(&db, now() as i64 + 60);
1060 drop(db);
1061 let headers = bearer(string(&tokens["access_token"]));
1062 assert!(fixture.store.authenticate(&headers, &resource).is_ok());
1063 assert!(expires(&fixture.store.db.lock().unwrap()) >= now() as i64 + ACCESS_IDLE - 5);
1064 set(&fixture.store.db.lock().unwrap(), now() as i64 - 1);
1065 assert!(fixture.store.authenticate(&headers, &resource).is_err());
1066 }
10331067 #[tokio::test]
10341068 async fn authorization_restricts_issuer_scopes_to_the_requested_resource() {
10351069 let fixture = Fixture::new();