Compare commits
5 Commits
fix/inject
...
c79bdab51b
| Author | SHA1 | Date | |
|---|---|---|---|
| c79bdab51b | |||
|
|
913c0fbdc6 | ||
| beb57baf58 | |||
|
|
0a83f71648 | ||
| f25616f720 |
@@ -62,6 +62,59 @@ function parseStreamableResponse(body: string): McpProxyResponse {
|
||||
return JSON.parse(body) as McpProxyResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide how mcpd opens a STDIO session against a running container.
|
||||
*
|
||||
* attach → connect to PID 1's stdin/stdout
|
||||
* exec → spawn a NEW process inside the container
|
||||
*
|
||||
* Pure and exported so the choice is testable: it is subtle, and getting it
|
||||
* wrong fails silently rather than loudly (see the injector case below).
|
||||
*/
|
||||
export function chooseStdioMode(server: {
|
||||
name: string;
|
||||
id: string;
|
||||
secretDelivery?: string | null;
|
||||
command?: string[] | null;
|
||||
packageName?: string | null;
|
||||
dockerImage?: string | null;
|
||||
runtime?: string | null;
|
||||
}): StdioMode {
|
||||
// Injected delivery MUST attach, whatever the server type.
|
||||
//
|
||||
// The secrets exist only in PID 1's environment: the container command is a
|
||||
// shell that sources /vault/secrets/<name> and execs the real server, so the
|
||||
// values live in that process and nowhere else — not in the pod spec, which
|
||||
// is the entire point of the feature.
|
||||
//
|
||||
// `exec` spawns a NEW process, which never sourced the file and so starts
|
||||
// with empty credentials. The server comes up, answers tools/list, and fails
|
||||
// every authenticated call — the exact silent-empty-token failure this
|
||||
// feature exists to prevent. Observed as
|
||||
// `Readiness check (list_datasources) failed: process exited 1` on a pod
|
||||
// whose PID 1 demonstrably held the token.
|
||||
//
|
||||
// Safe because wrapCommandForInjector execs rather than forks, so PID 1 IS
|
||||
// the server process.
|
||||
if (server.secretDelivery === 'injector') return { kind: 'attach' };
|
||||
|
||||
if (server.command !== null && server.command !== undefined && server.command.length > 0) {
|
||||
return { kind: 'exec', command: server.command };
|
||||
}
|
||||
if (server.packageName !== null && server.packageName !== undefined && server.packageName !== '') {
|
||||
return { kind: 'exec', command: buildRuntimeSpawnCmd(server.runtime ?? 'node', server.packageName) };
|
||||
}
|
||||
// Image entrypoint IS the MCP server.
|
||||
if (server.dockerImage !== null && server.dockerImage !== undefined && server.dockerImage !== '') {
|
||||
return { kind: 'attach' };
|
||||
}
|
||||
|
||||
throw new InvalidStateError(
|
||||
`Server '${server.name}' (${server.id}) uses STDIO transport but has no ` +
|
||||
`packageName, command, or dockerImage. Configure one of these.`,
|
||||
);
|
||||
}
|
||||
|
||||
export class McpProxyService {
|
||||
/** Session IDs per server for streamable-http protocol */
|
||||
private sessions = new Map<string, string>();
|
||||
@@ -159,20 +212,15 @@ export class McpProxyService {
|
||||
// - command set → exec the given command in the container.
|
||||
// - dockerImage only → attach to PID 1 (image entrypoint IS the MCP server).
|
||||
// - nothing → unreachable, reject.
|
||||
const runtime = (server.runtime as string | null) ?? 'node';
|
||||
let mode: StdioMode;
|
||||
if (command && command.length > 0) {
|
||||
mode = { kind: 'exec', command };
|
||||
} else if (packageName) {
|
||||
mode = { kind: 'exec', command: buildRuntimeSpawnCmd(runtime, packageName) };
|
||||
} else if (dockerImage) {
|
||||
mode = { kind: 'attach' };
|
||||
} else {
|
||||
throw new InvalidStateError(
|
||||
`Server '${server.name}' (${server.id}) uses STDIO transport but has no ` +
|
||||
`packageName, command, or dockerImage. Configure one of these.`,
|
||||
);
|
||||
}
|
||||
const mode = chooseStdioMode({
|
||||
name: server.name as string,
|
||||
id: server.id as string,
|
||||
secretDelivery: server.secretDelivery as string | null,
|
||||
command,
|
||||
packageName,
|
||||
dockerImage,
|
||||
runtime: server.runtime as string | null,
|
||||
});
|
||||
|
||||
// Try persistent connection first
|
||||
try {
|
||||
@@ -181,7 +229,7 @@ export class McpProxyService {
|
||||
this.removeClient(instance.containerId);
|
||||
// Fall back to one-shot exec when we have a command to run.
|
||||
if (mode.kind === 'exec') {
|
||||
return sendViaStdio(this.orchestrator, instance.containerId, packageName, method, params, 120_000, command, runtime);
|
||||
return sendViaStdio(this.orchestrator, instance.containerId, packageName, method, params, 120_000, command, (server.runtime as string | null) ?? 'node');
|
||||
}
|
||||
// Attach mode has no one-shot equivalent, but the failure is usually
|
||||
// a stale pipe from an in-place container restart — retry once
|
||||
|
||||
@@ -135,17 +135,29 @@ export class ServerIdentityService {
|
||||
const secretNames = this.secretNamesFor(server);
|
||||
if (secretNames.length === 0) return undefined;
|
||||
|
||||
// mcpd owns the runner images, so their entrypoints are known. A
|
||||
// dockerImage server's is not introspectable — hence `entrypoint` being
|
||||
// required on the row at validation time.
|
||||
const imageEntrypoint = server.packageName
|
||||
? server.runtime === 'python'
|
||||
? ['uvx']
|
||||
: ['npx', '-y']
|
||||
// Build the COMPLETE argv the container should run. It differs by shape:
|
||||
//
|
||||
// package server — mcpd owns the runner image, whose ENTRYPOINT
|
||||
// (`npx -y` / `uvx`) is lost once we take over
|
||||
// `command`, so it must be prepended here.
|
||||
// image + command — `command` is already a full command line; mcpd would
|
||||
// have run exactly it. Prepending anything breaks it.
|
||||
// image only — the image's own ENTRYPOINT would run and mcpd cannot
|
||||
// introspect it, so the row must declare `entrypoint`.
|
||||
//
|
||||
// Getting this wrong returns undefined and SILENTLY skips the wrapper: the
|
||||
// agent still renders the file, nothing sources it, and the server starts
|
||||
// with empty credentials. Observed on docmost and my-home-assistant, which
|
||||
// carry a `command` but no `entrypoint`.
|
||||
const hasPackage = server.packageName !== null && server.packageName !== undefined && server.packageName !== '';
|
||||
const argv = hasPackage
|
||||
? [...(server.runtime === 'python' ? ['uvx'] : ['npx', '-y']), ...(command ?? [server.packageName as string])]
|
||||
: command !== undefined && command.length > 0
|
||||
? command
|
||||
: ((server.entrypoint as string[] | null) ?? undefined);
|
||||
if (imageEntrypoint === undefined || imageEntrypoint.length === 0) return undefined;
|
||||
|
||||
return wrapCommandForInjector([...imageEntrypoint, ...(command ?? [])], secretNames);
|
||||
if (argv === undefined || argv.length === 0) return undefined;
|
||||
return wrapCommandForInjector(argv, secretNames);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
58
src/mcpd/tests/injector-argv.test.ts
Normal file
58
src/mcpd/tests/injector-argv.test.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Which argv the injector wrapper wraps, by server shape.
|
||||
*
|
||||
* Getting this wrong is SILENT: wrapCommand returns undefined, the wrapper is
|
||||
* skipped, the agent still renders /vault/secrets/<name>, nothing sources it,
|
||||
* and the server starts with empty credentials. Observed live on docmost and
|
||||
* my-home-assistant, which carry a `command` but no `entrypoint`.
|
||||
*/
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { ServerIdentityService } from '../src/services/server-identity.service.js';
|
||||
import type { SecretBackendService } from '../src/services/secret-backend.service.js';
|
||||
|
||||
const svc = new ServerIdentityService(
|
||||
{} as unknown as SecretBackendService,
|
||||
{ namespace: 'mcpctl-servers', ensure: async () => undefined, remove: async () => undefined },
|
||||
);
|
||||
|
||||
const withSecret = { env: [{ name: 'T', valueFrom: { secretRef: { name: 'creds', key: 'K' } } }] };
|
||||
/** The wrapper is `sh -c <script> arg0 arg1...`; argv starts at index 3. */
|
||||
const argvOf = (r: string[] | undefined): string[] | undefined => r?.slice(3);
|
||||
|
||||
describe('wrapCommand argv by server shape', () => {
|
||||
it('prepends the node runner entrypoint for a package server', () => {
|
||||
const r = svc.wrapCommand({ ...withSecret, packageName: '@leval/mcp-grafana', runtime: 'node', entrypoint: null } as never, ['@leval/mcp-grafana']);
|
||||
expect(argvOf(r)).toEqual(['npx', '-y', '@leval/mcp-grafana']);
|
||||
});
|
||||
|
||||
it('prepends uvx for a python package server', () => {
|
||||
const r = svc.wrapCommand({ ...withSecret, packageName: 'mcp-searxng', runtime: 'python', entrypoint: null } as never, ['mcp-searxng']);
|
||||
expect(argvOf(r)).toEqual(['uvx', 'mcp-searxng']);
|
||||
});
|
||||
|
||||
it('uses an image server\'s command verbatim — prepending anything breaks it', () => {
|
||||
// The docmost/home-assistant regression: this used to return undefined.
|
||||
const r = svc.wrapCommand({ ...withSecret, packageName: null, entrypoint: null } as never, ['node', 'build/index.js']);
|
||||
expect(argvOf(r)).toEqual(['node', 'build/index.js']);
|
||||
});
|
||||
|
||||
it('falls back to the declared entrypoint for an image server with no command', () => {
|
||||
const r = svc.wrapCommand({ ...withSecret, packageName: null, entrypoint: ['/usr/local/bin/gitea-mcp'] } as never, undefined);
|
||||
expect(argvOf(r)).toEqual(['/usr/local/bin/gitea-mcp']);
|
||||
});
|
||||
|
||||
it('returns undefined when there is genuinely nothing to run', () => {
|
||||
expect(svc.wrapCommand({ ...withSecret, packageName: null, entrypoint: null } as never, undefined)).toBeUndefined();
|
||||
});
|
||||
|
||||
it('returns undefined for a server with no secret refs', () => {
|
||||
expect(svc.wrapCommand({ env: [], packageName: 'p', runtime: 'node', entrypoint: null } as never, ['p'])).toBeUndefined();
|
||||
});
|
||||
|
||||
it('always sources before exec, whatever the shape', () => {
|
||||
const r = svc.wrapCommand({ ...withSecret, packageName: null, entrypoint: null } as never, ['node', 'x.js']);
|
||||
expect(r?.[0]).toBe('/bin/sh');
|
||||
expect(r?.[2]).toContain('. /vault/secrets/creds');
|
||||
expect(r?.[2]).toContain('exec "$0" "$@"');
|
||||
});
|
||||
});
|
||||
44
src/mcpd/tests/stdio-mode.test.ts
Normal file
44
src/mcpd/tests/stdio-mode.test.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* How mcpd opens a STDIO session: attach to PID 1, or exec a new process.
|
||||
*
|
||||
* Subtle and silent when wrong. With injected secret delivery the credentials
|
||||
* exist ONLY in PID 1's environment (a shell sourced /vault/secrets/<name> and
|
||||
* exec'd the server), so an `exec` starts a process with empty credentials —
|
||||
* the server comes up, answers tools/list, and fails every authenticated call.
|
||||
*/
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { chooseStdioMode } from '../src/services/mcp-proxy-service.js';
|
||||
|
||||
const base = { name: 's', id: 'id1' };
|
||||
|
||||
describe('chooseStdioMode', () => {
|
||||
it('attaches for an injector server even though it has a packageName', () => {
|
||||
// The regression: packageName would otherwise select exec, and exec loses
|
||||
// the secrets entirely.
|
||||
expect(chooseStdioMode({ ...base, secretDelivery: 'injector', packageName: '@leval/mcp-grafana' }))
|
||||
.toEqual({ kind: 'attach' });
|
||||
});
|
||||
|
||||
it('attaches for an injector server even though it has an explicit command', () => {
|
||||
expect(chooseStdioMode({ ...base, secretDelivery: 'injector', command: ['node', 'x.js'] }))
|
||||
.toEqual({ kind: 'attach' });
|
||||
});
|
||||
|
||||
it('still execs a package server on the default env delivery', () => {
|
||||
const m = chooseStdioMode({ ...base, secretDelivery: 'env', packageName: '@leval/mcp-grafana', runtime: 'node' });
|
||||
expect(m.kind).toBe('exec');
|
||||
});
|
||||
|
||||
it('still prefers an explicit command over packageName on env delivery', () => {
|
||||
expect(chooseStdioMode({ ...base, secretDelivery: 'env', command: ['node', 'x.js'], packageName: 'p' }))
|
||||
.toEqual({ kind: 'exec', command: ['node', 'x.js'] });
|
||||
});
|
||||
|
||||
it('attaches for an image-entrypoint server, as before', () => {
|
||||
expect(chooseStdioMode({ ...base, dockerImage: 'gitea/mcp:latest' })).toEqual({ kind: 'attach' });
|
||||
});
|
||||
|
||||
it('rejects a server with no way to start', () => {
|
||||
expect(() => chooseStdioMode({ ...base })).toThrow(/packageName, command, or dockerImage/);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user