GeezerVison is showing github.com — feat(server): work a linked environment starts stays within its link … · pingdotgg/t3code@6918c35
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/auth/EnvironmentAuth.ts +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -92,6 +92,8 @@ export interface McpClientSession { | 92 | 92 | readonly sessionId: AuthSessionId; | 93 | 93 | readonly label: string; | 94 | 94 | readonly access: AuthMcpClientAccess; | | 95 | + /** Another T3 Code environment's link, whose work here stays within it. */ | | 96 | + readonly peerLink: boolean; | 95 | 97 | } | 96 | 98 |
| 97 | 99 | export interface AuthenticatedSession { | @@ -555,6 +557,7 @@ export class EnvironmentAuth extends Context.Service< | 555 | 557 | readonly issueMcpClientSession: (input: { | 556 | 558 | readonly label: string; | 557 | 559 | readonly access: AuthMcpClientAccess; | | 560 | + readonly peerLink: boolean; | 558 | 561 | readonly client: AuthClientMetadata; | 559 | 562 | }) => Effect.Effect< | 560 | 563 | { | @@ -1305,6 +1308,7 @@ export const make = Effect.gen(function* () { | 1305 | 1308 | access: session.scopes.includes(AuthOrchestrationOperateScope) | 1306 | 1309 | ? (session.runtimeModeCeiling ?? "approval-required") | 1307 | 1310 | : "read-only", | | 1311 | + peerLink: session.peerLink === true, | 1308 | 1312 | } satisfies McpClientSession) | 1309 | 1313 | : Effect.fail( | 1310 | 1314 | new ServerAuthInvalidCredentialError({ | @@ -1324,6 +1328,7 @@ export const make = Effect.gen(function* () { | 1324 | 1328 | scopes: mcpClientScopes(input.access), | 1325 | 1329 | ttl: MCP_CLIENT_SESSION_TTL, | 1326 | 1330 | ...(input.access === "read-only" ? {} : { runtimeModeCeiling: input.access }), | | 1331 | + peerLink: input.peerLink, | 1327 | 1332 | client: { ...input.client, label: input.label, deviceType: "bot" }, | 1328 | 1333 | }) | 1329 | 1334 | .pipe( | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/auth/McpOAuth.ts +25-4Lines changed: 25 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,6 +13,7 @@ import { | 13 | 13 | type AuthEnvironmentScope, | 14 | 14 | AuthOrchestrationOperateScope, | 15 | 15 | AuthOrchestrationReadScope, | | 16 | + MCP_PEER_LINK_SOFTWARE_ID, | 16 | 17 | } from "@t3tools/contracts"; | 17 | 18 | import { encodeOAuthScope } from "@t3tools/shared/oauthScope"; | 18 | 19 | import * as Clock from "effect/Clock"; | @@ -132,13 +133,16 @@ const ClientIdPayload = Schema.Struct({ | 132 | 133 | v: Schema.Literal(1), | 133 | 134 | n: Schema.String, | 134 | 135 | r: Schema.Array(Schema.String), | | 136 | + /** Registered as another T3 Code environment's peer link. */ | | 137 | + p: Schema.optionalKey(Schema.Literal(1)), | 135 | 138 | }); | 136 | 139 | const decodeClientIdPayload = Schema.decodeUnknownOption(Schema.fromJsonString(ClientIdPayload)); | 137 | 140 |
| 138 | 141 | export interface McpOAuthClient { | 139 | 142 | readonly clientId: string; | 140 | 143 | readonly name: string; | 141 | 144 | readonly redirectUris: ReadonlyArray<string>; | | 145 | + readonly peerLink: boolean; | 142 | 146 | } | 143 | 147 |
| 144 | 148 | export class McpOAuthPageError extends Schema.TaggedError<McpOAuthPageError>()( | @@ -172,6 +176,7 @@ export interface AuthorizationRequest { | 172 | 176 | interface PendingCode { | 173 | 177 | readonly clientId: string; | 174 | 178 | readonly clientName: string; | | 179 | + readonly peerLink: boolean; | 175 | 180 | readonly redirectUri: string; | 176 | 181 | readonly codeChallenge: string; | 177 | 182 | readonly resource: string; | @@ -240,8 +245,10 @@ const make = Effect.gen(function* () { | 240 | 245 |
| 241 | 246 | const sign = (domain: string, payload: string) => signPayload(`${domain}.${payload}`, signingKey); | 242 | 247 |
| 243 | | - const signClientId = (name: string, redirectUris: ReadonlyArray<string>) => { | 244 | | - const body = base64UrlEncode(JSON.stringify({ v: 1, n: name, r: redirectUris })); | | 248 | + const signClientId = (name: string, redirectUris: ReadonlyArray<string>, peerLink: boolean) => { | | 249 | + const body = base64UrlEncode( | | 250 | + JSON.stringify({ v: 1, n: name, r: redirectUris, ...(peerLink ? { p: 1 } : {}) }), | | 251 | + ); | 245 | 252 | return `${body}.${sign("mcp-client-id", body)}`; | 246 | 253 | }; | 247 | 254 |
| @@ -257,7 +264,12 @@ const make = Effect.gen(function* () { | 257 | 264 | } | 258 | 265 | return Option.getOrUndefined( | 259 | 266 | decodeClientIdPayload(json).pipe( | 260 | | - Option.map((payload) => ({ clientId, name: payload.n, redirectUris: payload.r })), | | 267 | + Option.map((payload) => ({ | | 268 | + clientId, | | 269 | + name: payload.n, | | 270 | + redirectUris: payload.r, | | 271 | + peerLink: payload.p === 1, | | 272 | + })), | 261 | 273 | ), | 262 | 274 | ); | 263 | 275 | }; | @@ -303,7 +315,13 @@ const make = Effect.gen(function* () { | 303 | 315 | }); | 304 | 316 | } | 305 | 317 | // RFC 7591 allows ignoring unsupported registration options; all clients use public PKCE. | 306 | | - return { clientId: signClientId(name, redirectUris), name, redirectUris }; | | 318 | + const peerLink = metadata.software_id === MCP_PEER_LINK_SOFTWARE_ID; | | 319 | + return { | | 320 | + clientId: signClientId(name, redirectUris, peerLink), | | 321 | + name, | | 322 | + redirectUris, | | 323 | + peerLink, | | 324 | + }; | 307 | 325 | }, | 308 | 326 | ); | 309 | 327 |
| @@ -385,6 +403,7 @@ const make = Effect.gen(function* () { | 385 | 403 | next.set(code, { | 386 | 404 | clientId: authorization.client.clientId, | 387 | 405 | clientName: authorization.client.name, | | 406 | + peerLink: authorization.client.peerLink, | 388 | 407 | redirectUri: authorization.redirectUri, | 389 | 408 | codeChallenge: authorization.codeChallenge, | 390 | 409 | resource: authorization.resource, | @@ -488,6 +507,7 @@ const make = Effect.gen(function* () { | 488 | 507 | .issueMcpClientSession({ | 489 | 508 | label: pending.clientName, | 490 | 509 | access: pending.access, | | 510 | + peerLink: pending.peerLink, | 491 | 511 | client: deriveAuthClientMetadata({ request }), | 492 | 512 | }) | 493 | 513 | .pipe( | @@ -540,6 +560,7 @@ export const layerMcpClientAuthenticator = Layer.effect( | 540 | 560 | sessionId: client.sessionId, | 541 | 561 | label: client.label, | 542 | 562 | access: client.access, | | 563 | + linked: client.peerLink, | 543 | 564 | }, | 544 | 565 | capabilities: new Set<McpInvocationContext.McpCapability>([ | 545 | 566 | "orchestration", | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/auth/SessionStore.ts +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -60,6 +60,8 @@ export interface VerifiedSession { | 60 | 60 | readonly scopes: ReadonlyArray<AuthEnvironmentScope>; | 61 | 61 | readonly proofKeyThumbprint?: string; | 62 | 62 | readonly runtimeModeCeiling?: RuntimeMode; | | 63 | + /** An MCP client that is another T3 Code environment's peer link. */ | | 64 | + readonly peerLink?: true; | 63 | 65 | } | 64 | 66 |
| 65 | 67 | export interface VerifiedWebSocketTicket extends VerifiedSession { | @@ -381,6 +383,7 @@ export class SessionStore extends Context.Service< | 381 | 383 | readonly client?: AuthClientMetadata; | 382 | 384 | readonly proofKeyThumbprint?: string; | 383 | 385 | readonly runtimeModeCeiling?: RuntimeMode; | | 386 | + readonly peerLink?: boolean; | 384 | 387 | readonly replaceActiveForSubjectAndMethod?: boolean; | 385 | 388 | readonly replaceSessionId?: AuthSessionId; | 386 | 389 | }) => Effect.Effect<IssuedSession, SessionCredentialInternalError>; | @@ -441,6 +444,8 @@ const SessionClaims = Schema.Struct({ | 441 | 444 | method: Schema.Literals(["browser-session-cookie", "bearer-access-token", "dpop-access-token"]), | 442 | 445 | jkt: Schema.optionalKey(Schema.String), | 443 | 446 | rtc: Schema.optionalKey(RuntimeMode), | | 447 | + /** Signed, so a client cannot shed its peer link's limits. */ | | 448 | + plk: Schema.optionalKey(Schema.Literal(1)), | 444 | 449 | iat: Schema.Number, | 445 | 450 | exp: Schema.Number, | 446 | 451 | }); | @@ -679,6 +684,7 @@ const make = Effect.gen(function* () { | 679 | 684 | method: input?.method ?? "browser-session-cookie", | 680 | 685 | ...(input?.proofKeyThumbprint ? { jkt: input.proofKeyThumbprint } : {}), | 681 | 686 | ...(input?.runtimeModeCeiling ? { rtc: input.runtimeModeCeiling } : {}), | | 687 | + ...(input?.peerLink === true ? { plk: 1 as const } : {}), | 682 | 688 | iat: issuedAt.epochMilliseconds, | 683 | 689 | exp: expiresAt.epochMilliseconds, | 684 | 690 | }; | @@ -860,6 +866,7 @@ const make = Effect.gen(function* () { | 860 | 866 | scopes: claims.scopes, | 861 | 867 | ...(claims.jkt ? { proofKeyThumbprint: claims.jkt } : {}), | 862 | 868 | ...(claims.rtc ? { runtimeModeCeiling: claims.rtc } : {}), | | 869 | + ...(claims.plk === 1 ? { peerLink: true as const } : {}), | 863 | 870 | } satisfies VerifiedSession; | 864 | 871 | }, | 865 | 872 | ); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/cli/thread.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -221,6 +221,7 @@ const withThreadTools = Effect.fn("cli.thread.withThreadTools")(function* <A, E, | 221 | 221 | // The CLI runs as the machine's owner. New threads still start at | 222 | 222 | // the project's default mode, never at this ceiling by default. | 223 | 223 | access: "full-access", | | 224 | + peerLink: false, | 224 | 225 | client: { deviceType: "bot" }, | 225 | 226 | }), | 226 | 227 | (session) => | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/environment/ServerEnvironment.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -267,6 +267,7 @@ export const make = Effect.gen(function* () { | 267 | 267 | serverBrowser: true, | 268 | 268 | peerLinks: true, | 269 | 269 | mcpModeLimitHeader: true, | | 270 | + linkFence: true, | 270 | 271 | }, | 271 | 272 | }; | 272 | 273 |
| |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/McpHttpServer.test.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -982,6 +982,7 @@ it.effect( | 982 | 982 | (input) => input, | 983 | 983 | (_, modes) => | 984 | 984 | Effect.succeed({ modes: `${modes.runtimeMode}/${modes.interactionMode}` }), | | 985 | + "stamped", | 985 | 986 | ), | 986 | 987 | }), | 987 | 988 | ).pipe( | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/McpInvocationContext.ts +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,6 +34,11 @@ export interface McpClientCaller { | 34 | 34 | readonly sessionId: string; | 35 | 35 | readonly label: string; | 36 | 36 | readonly access: AuthMcpClientAccess; | | 37 | + /** | | 38 | + * Another T3 Code environment's peer link. What it starts here carries its | | 39 | + * session as `linkOrigin`, and it changes only that work (`linkOrigin.ts`). | | 40 | + */ | | 41 | + readonly linked?: boolean | undefined; | 37 | 42 | /** | 38 | 43 | * Narrower modes the client asked for on this request (`T3-Mode-Limit`): | 39 | 44 | * another environment passing on the limits of the agent it calls for. | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/mcp/McpToolAccess.test.ts +15-1Lines changed: 15 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -144,6 +144,7 @@ const probeHandlers: McpToolAccess.Handlers<typeof ProbeToolkit.tools> = { | 144 | 144 | starts_threads: McpToolAccess.startsThreads( | 145 | 145 | (input) => input, | 146 | 146 | (_, modes) => Effect.succeed({ ran: `${modes.runtimeMode}/${modes.interactionMode}` }), | | 147 | + "stamped", | 147 | 148 | ), | 148 | 149 | writes_environment: McpToolAccess.writesEnvironment(() => ran), | 149 | 150 | }; | @@ -231,6 +232,15 @@ const mismatchedProvider = { | 231 | 232 | const supervisedClient = clientCaller("approval-required"); | 232 | 233 | const fullAccessClient = clientCaller("full-access"); | 233 | 234 | const readOnlyClient = clientCaller("read-only"); | | 235 | +const linkedClient: McpInvocationContext.McpInvocationScope = { | | 236 | + ...supervisedClient, | | 237 | + client: { ...supervisedClient.client!, linked: true }, | | 238 | +}; | | 239 | +// A full-access link's agents can do anything this environment's user can. | | 240 | +const fullAccessLinkedClient: McpInvocationContext.McpInvocationScope = { | | 241 | + ...fullAccessClient, | | 242 | + client: { ...fullAccessClient.client!, linked: true }, | | 243 | +}; | 234 | 244 | const previewOnly = { ...fullAccess, capabilities: new Set(["preview"] as const) }; | 235 | 245 |
| 236 | 246 | it.effect.each([ | @@ -265,6 +275,10 @@ it.effect.each([ | 265 | 275 | ["writes_with_capability", previewOnly, {}, "ran"], | 266 | 276 | ["writes_with_capability", readOnlyClient, {}, "capability_denied"], | 267 | 277 | ["writes_with_capability", ended, {}, "parent_not_active"], | | 278 | + ["writes", linkedClient, {}, "capability_denied"], | | 279 | + ["writes_with_capability", linkedClient, {}, "capability_denied"], | | 280 | + ["writes", fullAccessLinkedClient, {}, "ran"], | | 281 | + ["writes_with_capability", fullAccessLinkedClient, {}, "ran"], | 268 | 282 |
| 269 | 283 | ["writes_threads", supervised, {}, "ran"], | 270 | 284 | ["writes_threads", supervised, { threadId: planThreadId }, "ran"], | @@ -374,7 +388,7 @@ const linkedProbeServer = McpHttpServer.toolkitRegistration( | 374 | 388 | writes: McpToolAccess.writes(ranHere), | 375 | 389 | writes_with_capability: McpToolAccess.writesWithCapability(ranHere), | 376 | 390 | writes_threads: McpToolAccess.writesThreads((input) => [input.threadId], ranHere), | 377 | | - starts_threads: McpToolAccess.startsThreads((input) => input, ranHere), | | 391 | + starts_threads: McpToolAccess.startsThreads((input) => input, ranHere, "stamped"), | 378 | 392 | writes_environment: McpToolAccess.writesEnvironment(ranHere), | 379 | 393 | }), | 380 | 394 | ).pipe( | |
|