@@ -79,8 +79,9 @@ function isStringRecord(v: unknown): v is Record<string, string> {
7979 * Fold the resolved instance `auth` into an MCP **http** transport's headers
8080 * (ADR-0024 keeps MCP credentials with the transport). `credentialRef` has
8181 * already been resolved upstream, so this only maps the static credential to the
82- * right header. Not applied to stdio transports — a stdio server receives its
83- * credentials through `transport.env`.
82+ * right header. Not applied to stdio transports — a stdio server receives the
83+ * instance's credential in its environment, under the variable
84+ * `transport.credentialEnv` names (see {@link credentialEnvOf}).
8485 */
8586function applyAuthToHeaders (
8687 auth : ResolvedConnectorAuth | undefined ,
@@ -101,11 +102,85 @@ function applyAuthToHeaders(
101102 }
102103}
103104
104- /** Validate + normalize `providerConfig.transport`, injecting resolved auth for http. */
105+ /** An environment variable name as POSIX shells spell one. */
106+ const ENV_NAME_RE = / ^ [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ ] * $ / ;
107+
108+ /**
109+ * The secret of a resolved static credential — what a stdio server is handed
110+ * in its environment: a `bearer` token, an `api-key` key, a `basic` password.
111+ * An `api-key`'s `headerName` / `paramName` place the key on an http request
112+ * and are not read here; a `basic` username is not a secret, and a server that
113+ * needs it takes it from `transport.env`.
114+ */
115+ function secretOf ( auth : Exclude < ResolvedConnectorAuth , { type : 'none' } > ) : string {
116+ switch ( auth . type ) {
117+ case 'bearer' :
118+ return auth . token ;
119+ case 'api-key' :
120+ return auth . key ;
121+ case 'basic' :
122+ return auth . password ;
123+ }
124+ }
125+
126+ /**
127+ * `transport.credentialEnv` of a stdio transport, judged — the route a stdio
128+ * server's credential takes out of metadata (ADR-0097 §3 at ADR-0024 §3's
129+ * process boundary). The instance's `auth.credentialRef` is resolved by the
130+ * host like any other instance's (the environment, the stored credential, the
131+ * host's resolver; a reference nothing answers degrades the instance naming it
132+ * before this factory runs), and the resolved secret is put into the child's
133+ * environment under the variable `credentialEnv` names. The value is never
134+ * stored, logged, put on the connector `def` or carried in a message: it
135+ * exists only in the environment handed to the spawn.
136+ *
137+ * Refused plain (configuration faults), each naming the key and never a value:
138+ * a `credentialEnv` that is not a variable name; one with no static `auth` to
139+ * place (a grant-bearing kind is refused by the credential-handle check, with
140+ * this route named); one naming a variable `transport.env` also sets, which
141+ * would give that variable two values. `undefined` when the transport names
142+ * none: then `auth` is not applied to the stdio server at all, as before.
143+ */
144+ function credentialEnvOf (
145+ t : Record < string , unknown > ,
146+ env : Record < string , string > | undefined ,
147+ connectorName : string ,
148+ auth : ResolvedConnectorAuth | undefined ,
149+ borrowsCredential : boolean ,
150+ ) : { name : string ; value : string } | undefined {
151+ const name = t . credentialEnv ;
152+ if ( name === undefined ) return undefined ;
153+ if ( typeof name !== 'string' || ! ENV_NAME_RE . test ( name ) ) {
154+ throw new Error (
155+ `connector-mcp provider: connector '${ connectorName } ' providerConfig.transport.credentialEnv must be the name of ` +
156+ `an environment variable (letters, digits and '_', not starting with a digit): the variable the stdio server ` +
157+ `reads the instance's credential from.` ,
158+ ) ;
159+ }
160+ if ( borrowsCredential ) return undefined ;
161+ if ( ! auth || auth . type === 'none' ) {
162+ throw new Error (
163+ `connector-mcp provider: connector '${ connectorName } ' names providerConfig.transport.credentialEnv '${ name } ' but ` +
164+ `declares no credential to place there. Give the entry auth: { type: 'bearer', credentialRef } (or 'api-key' / ` +
165+ `'basic'): the provider passes the resolved credential to the server in '${ name } '.` ,
166+ ) ;
167+ }
168+ if ( env && Object . prototype . hasOwnProperty . call ( env , name ) ) {
169+ throw new Error (
170+ `connector-mcp provider: connector '${ connectorName } ' sets environment variable '${ name } ' twice: in ` +
171+ `providerConfig.transport.env and as providerConfig.transport.credentialEnv. Drop the env entry: the provider ` +
172+ `passes the resolved credential to the server in '${ name } ' itself.` ,
173+ ) ;
174+ }
175+ return { name, value : secretOf ( auth ) } ;
176+ }
177+
178+ /** Validate + normalize `providerConfig.transport`, injecting resolved auth (http headers, stdio `credentialEnv`). */
105179function normalizeTransport (
106180 raw : unknown ,
107181 connectorName : string ,
108182 auth : ResolvedConnectorAuth | undefined ,
183+ borrowsCredential : boolean ,
109184) : McpTransport {
110185 if ( ! raw || typeof raw !== 'object' ) {
111186 throw new Error (
@@ -120,14 +195,23 @@ function normalizeTransport(
120195 `connector-mcp provider: connector '${ connectorName } ' stdio transport requires a 'command' string.` ,
121196 ) ;
122197 }
198+ const env = isStringRecord ( t . env ) ? t . env : undefined ;
199+ const credential = credentialEnvOf ( t , env , connectorName , auth , borrowsCredential ) ;
123200 return {
124201 kind : 'stdio' ,
125202 command : t . command ,
126203 args : Array . isArray ( t . args ) ? t . args . map ( ( a ) => String ( a ) ) : undefined ,
127- env : isStringRecord ( t . env ) ? t . env : undefined ,
204+ env : credential ? { ... env , [ credential . name ] : credential . value } : env ,
128205 } ;
129206 }
130207 if ( t . kind === 'http' ) {
208+ if ( t . credentialEnv !== undefined ) {
209+ throw new Error (
210+ `connector-mcp provider: connector '${ connectorName } ' names providerConfig.transport.credentialEnv on an http ` +
211+ `transport. credentialEnv places the instance's credential in a stdio server's environment; over http the ` +
212+ `provider sends the resolved auth as a request header itself. Delete the key.` ,
213+ ) ;
214+ }
131215 if ( typeof t . url !== 'string' || t . url . length === 0 ) {
132216 throw new Error (
133217 `connector-mcp provider: connector '${ connectorName } ' http transport requires a 'url' string.` ,
@@ -281,6 +365,13 @@ async function credentialRejectionStatus(err: unknown): Promise<number | undefin
281365 * directory. The policy check runs first and is unchanged: it judges the
282366 * `command` string exactly as written.
283367 *
368+ * A declarative stdio server's credential stays out of metadata the way an
369+ * http one's does: the entry declares `auth: { type, credentialRef }`, the host
370+ * resolves the reference, and `providerConfig.transport.credentialEnv` names
371+ * the environment variable the provider hands the resolved secret to the child
372+ * in (see {@link credentialEnvOf}). A stdio transport naming no
373+ * `credentialEnv` does not apply `auth` at all.
374+ *
284375 * The connection is opened at materialization. Faults are classified (#3017):
285376 * an invalid transport shape is a *configuration* fault and throws plain —
286377 * fatal at boot per the ADR-0097 fail-loud contract — while a connect /
@@ -342,7 +433,7 @@ async function credentialRejectionStatus(err: unknown): Promise<number | undefin
342433export function createMcpProviderFactory ( deps : McpProviderDeps = { } ) : ConnectorProviderFactory {
343434 return async ( ctx ) => {
344435 const cfg = ( ctx . providerConfig ?? { } ) as McpProviderConfig ;
345- let transport = normalizeTransport ( cfg . transport , ctx . name , ctx . auth ) ;
436+ let transport = normalizeTransport ( cfg . transport , ctx . name , ctx . auth , ctx . credentialHandle !== undefined ) ;
346437 if ( transport . kind === 'stdio' ) {
347438 assertDeclarativeStdioAllowed ( deps . declarativeStdio , transport . command , ctx . name ) ;
348439 // One anchor for the app's relative paths (#22423). A resolver that
@@ -370,15 +461,17 @@ export function createMcpProviderFactory(deps: McpProviderDeps = {}): ConnectorP
370461 }
371462 // ADR-0143 D4 — a grant-bearing instance (`oauth2`, `app-credential`)
372463 // borrows its credential per request. Only an http transport carries one
373- // (a stdio server takes its credentials in `transport.env`), and a
374- // request is authenticated one way.
464+ // (a stdio server is handed a static credential once, at spawn, in the
465+ // variable `transport.credentialEnv` names), and a request is
466+ // authenticated one way.
375467 const handle = ctx . credentialHandle ;
376468 if ( handle ) {
377469 if ( transport . kind !== 'http' ) {
378470 throw new Error (
379471 `connector-mcp provider: connector '${ ctx . name } ' borrows its credential per request (a grant-bearing auth kind), ` +
380- `which only an http transport can carry: a stdio server takes its credentials in transport.env. Declare an ` +
381- `http transport, or a static credential for the stdio one.` ,
472+ `which only an http transport can carry: a stdio server is handed its credential once, at spawn. Declare an ` +
473+ `http transport, or a static credential for the stdio one (auth: { type: 'bearer', credentialRef }) with ` +
474+ `providerConfig.transport.credentialEnv naming the variable the server reads it from.` ,
382475 ) ;
383476 }
384477 const ways = [
@@ -504,7 +597,9 @@ export function createMcpProviderFactory(deps: McpProviderDeps = {}): ConnectorP
504597 }
505598 // [#27237] The handshake carried the instance's credential only over
506599 // http, and only as a header (an api-key `paramName` is not sent): then
507- // the server's answer was given to that credential.
600+ // the server's answer was given to that credential. A stdio server handed
601+ // the credential in `credentialEnv` is not counted: nothing in a stdio
602+ // handshake says the server used, let alone accepted, its environment.
508603 //
509604 // A grant-bearing instance's borrowed token (`ctx.credentialHandle`)
510605 // reaches the server too, and is deliberately not counted here. A 403 the
0 commit comments