@opengeni/network
@opengeni/network is the low-level outbound transport used by Opengeni's
credential-bearing MCP and OAuth paths. It resolves a destination hostname once,
rejects private and special-use answers unless the caller has explicitly enabled
the local/test or private-target escape, and supplies the vetted address through a
per-request Undici 6.x Agent lookup. TLS hostname verification remains enabled:
the URL hostname is still the TLS identity and is never replaced with an IP or
used with rejectUnauthorized: false.
The response body owns the dispatcher lifecycle. The per-request agent is closed
after a body completes and destroyed after cancellation, stream failure, or a
fetch failure. This keeps long-lived MCP SSE streams alive while preventing a
completed or abandoned response from retaining a socket pool.
Callers must put credential-resolution wrappers outside the pinned transport:
const guarded = (input: string | URL, init?: RequestInit) =>
pinnedFetch(input, init, settings, { fetchImpl: undiciFetch });
const withCredentials = resolveHeadersThenCall(guarded);
The wrapper must add Authorization, API-key, or OAuth form credentials before
calling pinnedFetch; the policy resolution and pinned Agent are the final
network boundary. Callers must not preflight with one fetch and then call a
second global fetch.
The pinned transport uses Undici's request() implementation from its explicit
undici/index.js entrypoint and adapts the result to a web Response. This
avoids Bun/Undici fetch() body-stream interoperability stalls while preserving
the vetted per-request Agent. The separately exported undiciFetch remains a
fetch-compatible adapter for callers that do not use pinnedFetch. Bun's bare
undici specifier resolves to a compatibility shim whose Agent does not expose
the required dispatcher methods, and Bun's native fetch does not provide a
portable guarantee that an Undici dispatcher is honored. This package does not
follow redirects; each caller must make a manual redirect decision and call the
transport again so every hop is independently resolved and pinned.
The default pinned request adapter sends User-Agent: Opengeni unless the
caller supplies its own value, including through a Request object. This keeps
OAuth discovery and provider requests compatible with servers that require an
identified HTTP client.
The package also exports readJsonBase64Field for provider APIs that return
large binary artifacts inside JSON. It validates declared and streamed limits,
decodes canonical base64 incrementally, and avoids retaining the JSON envelope
or encoded string. Callers remain responsible for validating the decoded media.
MCP OAuth discovery
resolveMcpOAuthDiscovery provides the shared MCP OAuth discovery state machine
used by runtime connections and catalog diagnostics. It prefers RFC 9728
Protected Resource Metadata and permits the MCP 2025-03-26 compatibility path
only after every PRM candidate is explicitly absent with HTTP 404 or 410. The
legacy path requires a Bearer/OAuth challenge, RFC 8414 metadata on the MCP
origin, an exact issuer match, same-origin resource binding, and PKCE S256.
The resolver is transport-independent. Its fetchMetadata callback must
validate each candidate before I/O, bound response size and duration, disable
automatic redirects, and independently validate and DNS-pin every redirect
hop. It must return status: "absent" only for HTTP 404 or 410 and throw for
network, redirect, destination-policy, HTTP, body, and JSON failures; otherwise
an unsafe or unreachable PRM endpoint could be mistaken for legacy absence.
validateEndpoint and canonicalizeResource provide the caller's deployment
policy and canonical identifier rules.
Successful results include the discovery mode, normalized metadata, structured
classification, and a stable SHA-256 provenance digest. Fail-closed errors carry
classifications for broken discovery, unverified legacy defaults, or a legacy
cross-origin configuration that requires an explicit reviewed profile. These
classifications are diagnostics; callers still decide whether a provider
profile is supported and must perform fresh runtime discovery before granting
credentials.