|
| 1 | +PRAGMA foreign_keys = ON; |
| 2 | + |
| 3 | +CREATE TABLE mcp_authorization_grants ( |
| 4 | + id TEXT PRIMARY KEY, |
| 5 | + account_id TEXT NOT NULL REFERENCES accounts(id), |
| 6 | + agent_id TEXT NOT NULL REFERENCES agents(id), |
| 7 | + scopes TEXT NOT NULL CHECK (scopes = 'feed:read'), |
| 8 | + oauth_client_id TEXT NOT NULL CHECK ( |
| 9 | + length(trim(oauth_client_id)) BETWEEN 1 AND 255 |
| 10 | + ), |
| 11 | + oauth_client_label TEXT NOT NULL CHECK ( |
| 12 | + length(trim(oauth_client_label)) BETWEEN 1 AND 120 |
| 13 | + ), |
| 14 | + created_at INTEGER NOT NULL, |
| 15 | + last_used_at INTEGER, |
| 16 | + expires_at INTEGER, |
| 17 | + revoked_at INTEGER, |
| 18 | + revoked_reason TEXT, |
| 19 | + CHECK (last_used_at IS NULL OR last_used_at >= created_at), |
| 20 | + CHECK (expires_at IS NULL OR expires_at > created_at), |
| 21 | + CHECK (revoked_at IS NULL OR revoked_at >= created_at), |
| 22 | + CHECK ( |
| 23 | + (revoked_at IS NULL AND revoked_reason IS NULL) |
| 24 | + OR |
| 25 | + ( |
| 26 | + revoked_at IS NOT NULL |
| 27 | + AND revoked_reason IS NOT NULL |
| 28 | + AND length(trim(revoked_reason)) BETWEEN 1 AND 120 |
| 29 | + ) |
| 30 | + ) |
| 31 | +); |
| 32 | + |
| 33 | +CREATE INDEX mcp_authorization_grants_account_state_idx |
| 34 | + ON mcp_authorization_grants (account_id, revoked_at, expires_at, created_at DESC); |
| 35 | + |
| 36 | +CREATE INDEX mcp_authorization_grants_agent_state_idx |
| 37 | + ON mcp_authorization_grants (agent_id, revoked_at, expires_at, created_at DESC); |
| 38 | + |
| 39 | +CREATE INDEX mcp_authorization_grants_client_idx |
| 40 | + ON mcp_authorization_grants (oauth_client_id, created_at DESC); |
| 41 | + |
| 42 | +CREATE TRIGGER mcp_authorization_grants_validate |
| 43 | +BEFORE INSERT ON mcp_authorization_grants |
| 44 | +BEGIN |
| 45 | + SELECT RAISE(ABORT, 'mcp_authorization_account_inactive') |
| 46 | + WHERE NOT EXISTS ( |
| 47 | + SELECT 1 |
| 48 | + FROM accounts account |
| 49 | + WHERE account.id = NEW.account_id |
| 50 | + AND account.status = 'active' |
| 51 | + ); |
| 52 | + |
| 53 | + SELECT RAISE(ABORT, 'mcp_authorization_agent_not_manageable') |
| 54 | + WHERE NOT EXISTS ( |
| 55 | + SELECT 1 |
| 56 | + FROM agents agent |
| 57 | + WHERE agent.id = NEW.agent_id |
| 58 | + AND ( |
| 59 | + EXISTS ( |
| 60 | + SELECT 1 |
| 61 | + FROM agent_memberships membership |
| 62 | + WHERE membership.agent_id = agent.id |
| 63 | + AND membership.account_id = NEW.account_id |
| 64 | + AND membership.role = 'primary_sponsor' |
| 65 | + AND membership.revoked_at IS NULL |
| 66 | + ) |
| 67 | + OR EXISTS ( |
| 68 | + SELECT 1 |
| 69 | + FROM account_roles role |
| 70 | + WHERE role.account_id = NEW.account_id |
| 71 | + AND role.role = 'platform_owner' |
| 72 | + AND role.revoked_at IS NULL |
| 73 | + ) |
| 74 | + ) |
| 75 | + ); |
| 76 | +END; |
| 77 | + |
| 78 | +CREATE TRIGGER mcp_authorization_grants_identity_immutable |
| 79 | +BEFORE UPDATE OF account_id, agent_id, scopes, oauth_client_id, oauth_client_label, created_at, expires_at |
| 80 | +ON mcp_authorization_grants |
| 81 | +BEGIN |
| 82 | + SELECT RAISE(ABORT, 'mcp_authorization_grant_identity_immutable'); |
| 83 | +END; |
| 84 | + |
| 85 | +CREATE TABLE mcp_delegation_codes ( |
| 86 | + id TEXT PRIMARY KEY, |
| 87 | + secret_digest TEXT NOT NULL UNIQUE, |
| 88 | + hash_version INTEGER NOT NULL CHECK (hash_version > 0), |
| 89 | + grant_id TEXT NOT NULL REFERENCES mcp_authorization_grants(id), |
| 90 | + authorization_request_id TEXT NOT NULL UNIQUE CHECK ( |
| 91 | + length(trim(authorization_request_id)) BETWEEN 1 AND 200 |
| 92 | + ), |
| 93 | + created_at INTEGER NOT NULL, |
| 94 | + expires_at INTEGER NOT NULL, |
| 95 | + consumed_at INTEGER, |
| 96 | + CHECK (expires_at > created_at), |
| 97 | + CHECK (expires_at <= created_at + 600000), |
| 98 | + CHECK (consumed_at IS NULL OR consumed_at >= created_at) |
| 99 | +); |
| 100 | + |
| 101 | +CREATE INDEX mcp_delegation_codes_grant_state_idx |
| 102 | + ON mcp_delegation_codes (grant_id, consumed_at, expires_at); |
| 103 | + |
| 104 | +CREATE TRIGGER mcp_delegation_codes_validate |
| 105 | +BEFORE INSERT ON mcp_delegation_codes |
| 106 | +BEGIN |
| 107 | + SELECT RAISE(ABORT, 'mcp_authorization_grant_unavailable') |
| 108 | + WHERE NOT EXISTS ( |
| 109 | + SELECT 1 |
| 110 | + FROM mcp_authorization_grants grant_row |
| 111 | + WHERE grant_row.id = NEW.grant_id |
| 112 | + AND grant_row.revoked_at IS NULL |
| 113 | + AND (grant_row.expires_at IS NULL OR grant_row.expires_at > NEW.created_at) |
| 114 | + ); |
| 115 | +END; |
| 116 | + |
| 117 | +CREATE TRIGGER mcp_delegation_codes_identity_immutable |
| 118 | +BEFORE UPDATE OF secret_digest, hash_version, grant_id, authorization_request_id, created_at, expires_at |
| 119 | +ON mcp_delegation_codes |
| 120 | +BEGIN |
| 121 | + SELECT RAISE(ABORT, 'mcp_delegation_code_identity_immutable'); |
| 122 | +END; |
| 123 | + |
| 124 | +CREATE TABLE mcp_delegation_redemptions ( |
| 125 | + code_id TEXT PRIMARY KEY REFERENCES mcp_delegation_codes(id), |
| 126 | + grant_id TEXT NOT NULL REFERENCES mcp_authorization_grants(id), |
| 127 | + authorization_request_id TEXT NOT NULL UNIQUE, |
| 128 | + redeemed_at INTEGER NOT NULL |
| 129 | +); |
| 130 | + |
| 131 | +CREATE TRIGGER mcp_delegation_redemptions_validate |
| 132 | +BEFORE INSERT ON mcp_delegation_redemptions |
| 133 | +BEGIN |
| 134 | + SELECT RAISE(ABORT, 'invalid_mcp_delegation_code') |
| 135 | + WHERE NOT EXISTS ( |
| 136 | + SELECT 1 |
| 137 | + FROM mcp_delegation_codes code |
| 138 | + JOIN mcp_authorization_grants grant_row |
| 139 | + ON grant_row.id = code.grant_id |
| 140 | + WHERE code.id = NEW.code_id |
| 141 | + AND code.grant_id = NEW.grant_id |
| 142 | + AND code.authorization_request_id = NEW.authorization_request_id |
| 143 | + AND code.consumed_at IS NULL |
| 144 | + AND code.expires_at > NEW.redeemed_at |
| 145 | + AND grant_row.revoked_at IS NULL |
| 146 | + AND (grant_row.expires_at IS NULL OR grant_row.expires_at > NEW.redeemed_at) |
| 147 | + ); |
| 148 | +END; |
| 149 | + |
| 150 | +CREATE TRIGGER mcp_delegation_redemptions_consume |
| 151 | +AFTER INSERT ON mcp_delegation_redemptions |
| 152 | +BEGIN |
| 153 | + UPDATE mcp_delegation_codes |
| 154 | + SET consumed_at = NEW.redeemed_at |
| 155 | + WHERE id = NEW.code_id; |
| 156 | +END; |
| 157 | + |
| 158 | +CREATE TRIGGER mcp_delegation_redemptions_no_update |
| 159 | +BEFORE UPDATE ON mcp_delegation_redemptions |
| 160 | +BEGIN |
| 161 | + SELECT RAISE(ABORT, 'mcp_delegation_redemptions_are_append_only'); |
| 162 | +END; |
| 163 | + |
| 164 | +CREATE TRIGGER mcp_delegation_redemptions_no_delete |
| 165 | +BEFORE DELETE ON mcp_delegation_redemptions |
| 166 | +BEGIN |
| 167 | + SELECT RAISE(ABORT, 'mcp_delegation_redemptions_are_append_only'); |
| 168 | +END; |
| 169 | + |
| 170 | +CREATE TABLE mcp_authorization_revocations ( |
| 171 | + grant_id TEXT PRIMARY KEY REFERENCES mcp_authorization_grants(id), |
| 172 | + actor_account_id TEXT NOT NULL REFERENCES accounts(id), |
| 173 | + reason TEXT NOT NULL CHECK (length(trim(reason)) BETWEEN 1 AND 120), |
| 174 | + revoked_at INTEGER NOT NULL |
| 175 | +); |
| 176 | + |
| 177 | +CREATE TRIGGER mcp_authorization_revocations_validate |
| 178 | +BEFORE INSERT ON mcp_authorization_revocations |
| 179 | +BEGIN |
| 180 | + SELECT RAISE(ABORT, 'mcp_authorization_grant_unavailable') |
| 181 | + WHERE NOT EXISTS ( |
| 182 | + SELECT 1 |
| 183 | + FROM mcp_authorization_grants grant_row |
| 184 | + WHERE grant_row.id = NEW.grant_id |
| 185 | + AND grant_row.revoked_at IS NULL |
| 186 | + ); |
| 187 | + |
| 188 | + SELECT RAISE(ABORT, 'mcp_authorization_revoke_forbidden') |
| 189 | + WHERE NOT EXISTS ( |
| 190 | + SELECT 1 |
| 191 | + FROM mcp_authorization_grants grant_row |
| 192 | + WHERE grant_row.id = NEW.grant_id |
| 193 | + AND ( |
| 194 | + grant_row.account_id = NEW.actor_account_id |
| 195 | + OR EXISTS ( |
| 196 | + SELECT 1 |
| 197 | + FROM agent_memberships membership |
| 198 | + WHERE membership.agent_id = grant_row.agent_id |
| 199 | + AND membership.account_id = NEW.actor_account_id |
| 200 | + AND membership.role = 'primary_sponsor' |
| 201 | + AND membership.revoked_at IS NULL |
| 202 | + ) |
| 203 | + OR EXISTS ( |
| 204 | + SELECT 1 |
| 205 | + FROM account_roles role |
| 206 | + WHERE role.account_id = NEW.actor_account_id |
| 207 | + AND role.role = 'platform_owner' |
| 208 | + AND role.revoked_at IS NULL |
| 209 | + ) |
| 210 | + ) |
| 211 | + ); |
| 212 | +END; |
| 213 | + |
| 214 | +CREATE TRIGGER mcp_authorization_revocations_apply |
| 215 | +AFTER INSERT ON mcp_authorization_revocations |
| 216 | +BEGIN |
| 217 | + UPDATE mcp_authorization_grants |
| 218 | + SET revoked_at = NEW.revoked_at, |
| 219 | + revoked_reason = trim(NEW.reason) |
| 220 | + WHERE id = NEW.grant_id; |
| 221 | +END; |
| 222 | + |
| 223 | +CREATE TRIGGER mcp_authorization_revocations_no_update |
| 224 | +BEFORE UPDATE ON mcp_authorization_revocations |
| 225 | +BEGIN |
| 226 | + SELECT RAISE(ABORT, 'mcp_authorization_revocations_are_append_only'); |
| 227 | +END; |
| 228 | + |
| 229 | +CREATE TRIGGER mcp_authorization_revocations_no_delete |
| 230 | +BEFORE DELETE ON mcp_authorization_revocations |
| 231 | +BEGIN |
| 232 | + SELECT RAISE(ABORT, 'mcp_authorization_revocations_are_append_only'); |
| 233 | +END; |
0 commit comments