Skip to content

Commit d8cb294

Browse files
committed
wip(connector-mcp): stdio credentialEnv route (#29434)
Claude-Session: https://claude.ai/code/session_01ADHkmRFhrJgbwLP5mvZkeJ Co-authored-by: Claude <noreply@anthropic.com>
1 parent 2420778 commit d8cb294

8 files changed

Lines changed: 511 additions & 32 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'@objectstack/connector-mcp': patch
3+
'@objectstack/lint': patch
4+
---
5+
6+
Clause-②: yes (widening)
7+
Metadata-protocol: none
8+
9+
`provider: 'mcp'`: a stdio MCP server can now take its credential from the secrets layer instead of a literal in `transport.env`. Declare the instance's `auth` with a `credentialRef` as for an http server, and name the environment variable the server reads in the new `providerConfig.transport.credentialEnv` key: the host resolves the reference (environment, Setup → Connector credentials, the host's resolver) and the provider hands the resolved secret (the `bearer` token, the `api-key` key, or the `basic` password) to the child in that variable. The value is never stored, logged, served on a metadata read or put in a message; a reference that resolves to nothing degrades the instance naming the reference and the server is not started.
10+
11+
```ts
12+
providerConfig: { transport: { kind: 'stdio', command: 'repo-mcp-server', credentialEnv: 'REPO_MCP_TOKEN' } },
13+
auth: { type: 'bearer', credentialRef: 'REPO_MCP_TOKEN' },
14+
```
15+
16+
Refused at materialization as configuration faults (a failed boot for the host's own entry): a `credentialEnv` that is not an environment-variable name, one on an `http` transport, one with no `bearer` / `api-key` / `basic` `auth`, and one naming a variable `transport.env` also sets. A stdio transport that names no `credentialEnv` is unchanged: it does not apply `auth`.
17+
18+
`@objectstack/lint`: the `connector-credential-literal` advisory's finding for a credential literal in `transport.env` now steers to this route (`auth.credentialRef` with `transport.credentialEnv`) instead of telling the author to move to an http transport.

‎content/docs/automation/connectors.mdx‎

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,10 @@ allowlist — only the listed tools become actions) and **`toolsSnapshot`**
228228
HTTP. Resolved `auth` is folded into the request headers; a grant-bearing
229229
`auth` (`oauth2`, `app-credential`) is asked for on every request instead
230230
(see *Grant-bearing kinds* below).
231-
- `{ kind: 'stdio', command, args?, env? }` — a **local child process**,
232-
policy-gated (below). Credentials for a stdio server ride `transport.env`,
233-
not `auth`.
231+
- `{ kind: 'stdio', command, args?, env?, credentialEnv? }` — a **local child
232+
process**, policy-gated (below). Resolved `auth` is handed to the server in
233+
its environment, under the variable `credentialEnv` names (see
234+
[*A stdio server's credential*](#a-stdio-servers-credential) below).
234235

235236
{/* os:check */}
236237
```typescript
@@ -346,6 +347,49 @@ characters fails the boot before connecting, and so does a pinned tool that
346347
`include` leaves out — the instance never exposes it, so its pin would never be
347348
checked.
348349

350+
#### A stdio server's credential
351+
352+
A stdio MCP server reads its credential from an environment variable, and the
353+
value stays out of metadata the way an http server's does: declare `auth` with
354+
a `credentialRef`, and name the variable in `transport.credentialEnv`. The
355+
platform resolves the reference like any instance's (the environment, then a
356+
credential set under Setup → Connector credentials, then the host's resolver)
357+
and the provider hands the resolved secret to the child in that variable — the
358+
token of a `bearer`, the key of an `api-key`, the password of a `basic` (whose
359+
username, not a secret, goes in `env` if the server reads it).
360+
361+
{/* os:check */}
362+
```typescript
363+
import { defineConnector } from '@objectstack/spec/integration';
364+
365+
export const RepoToolsConnector = defineConnector({
366+
name: 'repo_tools',
367+
label: 'Repository Tools (MCP, stdio)',
368+
type: 'api',
369+
provider: 'mcp',
370+
providerConfig: {
371+
transport: {
372+
kind: 'stdio',
373+
command: 'repo-mcp-server',
374+
env: { LOG_LEVEL: 'info' },
375+
credentialEnv: 'REPO_MCP_TOKEN',
376+
},
377+
},
378+
auth: { type: 'bearer', credentialRef: 'REPO_MCP_TOKEN' },
379+
});
380+
```
381+
382+
The value is never stored, logged, served on a metadata read (which shows
383+
`credentialRef` and `credentialEnv`, two names) or carried in a message; it
384+
exists only in the environment the server is started with. A reference that
385+
resolves to nothing never starts the server: the instance is degraded naming
386+
the reference, and a credential set later starts it. These fail the boot:
387+
a `credentialEnv` that is not a variable name, one on an `http` transport,
388+
one with no `bearer` / `api-key` / `basic` `auth` to place, and one naming a
389+
variable `env` also sets. A stdio transport that names no `credentialEnv`
390+
does not apply `auth`. A literal credential typed into `env` instead is
391+
reported by the `connector-credential-literal` advisory, which points here.
392+
349393
<Callout type="warn">
350394
**Declarative stdio transports are denied by default.** Materializing one
351395
spawns a local process *from metadata* — which a runtime Studio publish can
@@ -581,8 +625,9 @@ platform's to fill, so it is left out of the action's input schema, as
581625
`Authorization` is.
582626

583627
An **`mcp`** instance carries a grant-bearing credential only over an `http`
584-
transport (a stdio server takes its credentials in `transport.env`; a handle
585-
on a stdio transport, or beside a static credential, fails the boot). Every
628+
transport (a stdio server is handed a static credential once, at spawn, in
629+
`transport.credentialEnv`; a handle on a stdio transport, or beside a static
630+
credential, fails the boot). Every
586631
HTTP request of its MCP session asks the handle. Each organization a dispatch
587632
runs for opens its own session with its own token, so organizations never
588633
share one. The tools are listed at materialization, outside any run, so the

‎packages/connectors/connector-mcp/src/mcp-connector.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import type { Connector } from '@objectstack/spec/integration';
2121
* **Credentials live with the MCP server, not in `ConnectorSchema`** (ADR-0024
2222
* §3). The operator supplies `env` (stdio) / `headers` (http) which we pass
2323
* straight to the transport; they are never copied into the serialized `def`
24-
* (which is exposed via discovery) and must never be logged.
24+
* (which is exposed via discovery) and must never be logged. A declarative
25+
* instance's resolved `auth` reaches them through the `mcp` provider factory:
26+
* as a header over http, and over stdio in the variable its
27+
* `transport.credentialEnv` names.
2528
*
2629
* **Trust:** launching a stdio server runs a local process. Sandboxed,
2730
* multi-tenant execution and managed secrets are the enterprise tier (ADR-0024

‎packages/connectors/connector-mcp/src/mcp-provider.ts‎

Lines changed: 105 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
8586
function 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-Za-z_][A-Za-z0-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`). */
105179
function 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
342433
export 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

Comments
 (0)