MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / refreshConnectionToken

Function refreshConnectionToken

packages/core/sdk/src/executor.ts:2938–2984  ·  view source on GitHub ↗
(
      row: ConnectionRow,
      provider: CredentialProvider,
      trigger: RefreshTrigger = "proactive",
    )

Source from the content-addressed store, hash-verified

2936 );
2937
2938 const refreshConnectionToken = (
2939 row: ConnectionRow,
2940 provider: CredentialProvider,
2941 trigger: RefreshTrigger = "proactive",
2942 ): Effect.Effect<string | null, StorageFailure | CredentialResolutionError> =>
2943 // Share a single refresh per connection so concurrent resolves of the same
2944 // connection all await one refresh-token grant (the AS rotates the refresh
2945 // token; parallel grants would race on a consumed token — v1's refresh
2946 // deferred-map). The gate is cleared once the refresh settles so a later
2947 // expiry can refresh again.
2948 Effect.suspend(() => {
2949 const key = connectionKey(row);
2950 // Joining an in-flight grant is correct for BOTH triggers: whatever
2951 // that peer mints is newer than the token this fiber just saw rejected,
2952 // which is exactly what a reactive retry wants. The gate is cleared on
2953 // settle, so a 401 arriving after a refresh completed starts a fresh
2954 // grant rather than replaying a stale result.
2955 const existing = refreshInFlight.get(key);
2956 if (existing) return Deferred.await(existing);
2957
2958 // The grant runs on a DETACHED fiber and every caller — including this
2959 // one — only awaits its deferred. The entry is shared across execution
2960 // stacks, so the fiber that registers it is merely the first arrival,
2961 // not an owner. Running the grant ON that fiber would hand it that
2962 // caller's interruption: a disconnected MCP client, an execution
2963 // deadline or a cancelled tool call would fail every peer awaiting the
2964 // same entry with an interrupt none of them caused and none can act on.
2965 // Awaiting is per-caller, so a cancelled peer detaches without touching
2966 // the grant or its siblings, and a grant nobody is left waiting on
2967 // still settles and still persists the rotated token — which is what
2968 // keeps the next caller off a consumed one. Token requests are bounded
2969 // by `AbortSignal.timeout`, so the detached fiber cannot outlive its
2970 // request.
2971 const deferred = Deferred.makeUnsafe<
2972 string | null,
2973 StorageFailure | CredentialResolutionError
2974 >();
2975 // Nothing suspends between the lookup above and this registration, so
2976 // check-and-set is atomic against peer fibers and cannot double-fire.
2977 refreshInFlight.set(key, deferred);
2978 const run = performTokenRefresh(row, provider, trigger).pipe(
2979 Effect.exit,
2980 Effect.flatMap((exit) => Deferred.done(deferred, exit)),
2981 Effect.ensuring(Effect.sync(() => void refreshInFlight.delete(key))),
2982 );
2983 return Effect.forkDetach(run).pipe(Effect.andThen(Deferred.await(deferred)));
2984 });
2985
2986 // Resolve every named input of a connection (`variable → value`). A
2987 // single-secret connection yields `{ token: <value> }`; an apiKey method with

Callers 2

resolveConnectionValuesFunction · 0.85

Calls 6

connectionKeyFunction · 0.85
performTokenRefreshFunction · 0.85
getMethod · 0.65
setMethod · 0.65
syncMethod · 0.65
deleteMethod · 0.65

Tested by

no test coverage detected