|
| 1 | +import { IncomingMessage } from "node:http"; |
| 2 | +import { AuthProvider, AuthResult, DEFAULT_AUTH_ERROR } from "../types.js"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Trust verification result from SATP/AgentFolio |
| 6 | + */ |
| 7 | +export interface AgentTrustResult { |
| 8 | + /** Agent identifier */ |
| 9 | + agentId: string; |
| 10 | + /** Trust score (0-100) */ |
| 11 | + trustScore: number; |
| 12 | + /** Whether the agent is verified on-chain */ |
| 13 | + verified: boolean; |
| 14 | + /** Agent display name */ |
| 15 | + name?: string; |
| 16 | + /** Capabilities tags */ |
| 17 | + capabilities?: string[]; |
| 18 | + /** Last verification timestamp */ |
| 19 | + lastVerified?: string; |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Configuration for SATP agent trust verification |
| 24 | + */ |
| 25 | +export interface SATPConfig { |
| 26 | + /** |
| 27 | + * AgentFolio API base URL |
| 28 | + * @default "https://api.agentfolio.bot" |
| 29 | + */ |
| 30 | + apiUrl?: string; |
| 31 | + |
| 32 | + /** |
| 33 | + * Minimum trust score required (0-100) |
| 34 | + * Set to 0 to allow all agents but still annotate requests with trust data |
| 35 | + * @default 0 |
| 36 | + */ |
| 37 | + minTrustScore?: number; |
| 38 | + |
| 39 | + /** |
| 40 | + * Require on-chain verification |
| 41 | + * @default false |
| 42 | + */ |
| 43 | + requireVerified?: boolean; |
| 44 | + |
| 45 | + /** |
| 46 | + * Header name for agent identity |
| 47 | + * @default "x-agent-id" |
| 48 | + */ |
| 49 | + agentIdHeader?: string; |
| 50 | + |
| 51 | + /** |
| 52 | + * Behavior when agent identity is missing from request |
| 53 | + * - "reject": Return 401 |
| 54 | + * - "allow": Continue without trust data |
| 55 | + * @default "allow" |
| 56 | + */ |
| 57 | + onMissing?: "reject" | "allow"; |
| 58 | + |
| 59 | + /** |
| 60 | + * Cache TTL in milliseconds for trust score lookups |
| 61 | + * @default 300000 (5 minutes) |
| 62 | + */ |
| 63 | + cacheTtlMs?: number; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * SATP Agent Trust Provider |
| 68 | + * |
| 69 | + * Verifies agent identity and trust scores via AgentFolio/SATP. |
| 70 | + * Can be used standalone or composed with other auth providers. |
| 71 | + * |
| 72 | + * @example |
| 73 | + * ```typescript |
| 74 | + * import { MCPServer, SATPProvider } from "mcp-framework"; |
| 75 | + * |
| 76 | + * const server = new MCPServer({ |
| 77 | + * auth: { |
| 78 | + * provider: new SATPProvider({ |
| 79 | + * minTrustScore: 50, |
| 80 | + * requireVerified: true, |
| 81 | + * }), |
| 82 | + * }, |
| 83 | + * }); |
| 84 | + * ``` |
| 85 | + */ |
| 86 | +export class SATPProvider implements AuthProvider { |
| 87 | + private config: Required<SATPConfig>; |
| 88 | + private cache: Map<string, { result: AgentTrustResult; expiry: number }> = |
| 89 | + new Map(); |
| 90 | + |
| 91 | + constructor(config: SATPConfig = {}) { |
| 92 | + this.config = { |
| 93 | + apiUrl: config.apiUrl ?? "https://api.agentfolio.bot", |
| 94 | + minTrustScore: config.minTrustScore ?? 0, |
| 95 | + requireVerified: config.requireVerified ?? false, |
| 96 | + agentIdHeader: config.agentIdHeader ?? "x-agent-id", |
| 97 | + onMissing: config.onMissing ?? "allow", |
| 98 | + cacheTtlMs: config.cacheTtlMs ?? 300_000, |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + async authenticate(req: IncomingMessage): Promise<boolean | AuthResult> { |
| 103 | + const agentId = this.extractAgentId(req); |
| 104 | + |
| 105 | + if (!agentId) { |
| 106 | + return this.config.onMissing === "allow" ? { data: { agentTrust: null } } : false; |
| 107 | + } |
| 108 | + |
| 109 | + const trust = await this.queryTrust(agentId); |
| 110 | + |
| 111 | + if (!trust) { |
| 112 | + return this.config.onMissing === "allow" ? { data: { agentTrust: null } } : false; |
| 113 | + } |
| 114 | + |
| 115 | + // Check minimum trust score |
| 116 | + if (trust.trustScore < this.config.minTrustScore) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + // Check verification requirement |
| 121 | + if (this.config.requireVerified && !trust.verified) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + return { |
| 126 | + data: { |
| 127 | + agentTrust: trust, |
| 128 | + }, |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + getAuthError(): { status: number; message: string; headers?: Record<string, string> } { |
| 133 | + return { |
| 134 | + status: 403, |
| 135 | + message: "Agent trust verification failed", |
| 136 | + headers: { |
| 137 | + "X-Trust-Required": `min-score=${this.config.minTrustScore}`, |
| 138 | + }, |
| 139 | + }; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Extract agent ID from request headers or MCP metadata |
| 144 | + */ |
| 145 | + private extractAgentId(req: IncomingMessage): string | null { |
| 146 | + // Check custom header first |
| 147 | + const headerValue = req.headers[this.config.agentIdHeader]; |
| 148 | + if (headerValue) { |
| 149 | + return Array.isArray(headerValue) ? headerValue[0] : headerValue; |
| 150 | + } |
| 151 | + |
| 152 | + // Check Authorization header for agent token |
| 153 | + const auth = req.headers.authorization; |
| 154 | + if (auth?.startsWith("Agent ")) { |
| 155 | + return auth.slice(6).trim(); |
| 156 | + } |
| 157 | + |
| 158 | + return null; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Query AgentFolio API for agent trust data with caching |
| 163 | + */ |
| 164 | + private async queryTrust(agentId: string): Promise<AgentTrustResult | null> { |
| 165 | + // Check cache |
| 166 | + const cached = this.cache.get(agentId); |
| 167 | + if (cached && cached.expiry > Date.now()) { |
| 168 | + return cached.result; |
| 169 | + } |
| 170 | + |
| 171 | + try { |
| 172 | + const response = await fetch( |
| 173 | + `${this.config.apiUrl}/v1/agents/${encodeURIComponent(agentId)}/trust`, |
| 174 | + { |
| 175 | + method: "GET", |
| 176 | + headers: { |
| 177 | + Accept: "application/json", |
| 178 | + "User-Agent": "mcp-framework-satp/1.0", |
| 179 | + }, |
| 180 | + signal: AbortSignal.timeout(5000), |
| 181 | + } |
| 182 | + ); |
| 183 | + |
| 184 | + if (!response.ok) { |
| 185 | + return null; |
| 186 | + } |
| 187 | + |
| 188 | + const data = (await response.json()) as AgentTrustResult; |
| 189 | + const result: AgentTrustResult = { |
| 190 | + agentId: data.agentId ?? agentId, |
| 191 | + trustScore: data.trustScore ?? 0, |
| 192 | + verified: data.verified ?? false, |
| 193 | + name: data.name, |
| 194 | + capabilities: data.capabilities, |
| 195 | + lastVerified: data.lastVerified, |
| 196 | + }; |
| 197 | + |
| 198 | + // Cache result |
| 199 | + this.cache.set(agentId, { |
| 200 | + result, |
| 201 | + expiry: Date.now() + this.config.cacheTtlMs, |
| 202 | + }); |
| 203 | + |
| 204 | + return result; |
| 205 | + } catch { |
| 206 | + return null; |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * Clear the trust score cache |
| 212 | + */ |
| 213 | + clearCache(): void { |
| 214 | + this.cache.clear(); |
| 215 | + } |
| 216 | +} |
0 commit comments