@0xmaxma/claude-gateway 2.1.4 → 2.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/runner.d.ts +2 -0
- package/dist/agent/runner.d.ts.map +1 -1
- package/dist/agent/runner.js +4 -0
- package/dist/agent/runner.js.map +1 -1
- package/dist/api/connectors-router.d.ts +4 -0
- package/dist/api/connectors-router.d.ts.map +1 -1
- package/dist/api/connectors-router.js +182 -2
- package/dist/api/connectors-router.js.map +1 -1
- package/dist/api/gateway-router.d.ts.map +1 -1
- package/dist/api/gateway-router.js +8 -0
- package/dist/api/gateway-router.js.map +1 -1
- package/dist/api/router.d.ts.map +1 -1
- package/dist/api/router.js +5 -3
- package/dist/api/router.js.map +1 -1
- package/dist/build-provenance.json +4 -4
- package/dist/connectors/custom-connectors-store.d.ts +23 -0
- package/dist/connectors/custom-connectors-store.d.ts.map +1 -1
- package/dist/connectors/custom-connectors-store.js +23 -1
- package/dist/connectors/custom-connectors-store.js.map +1 -1
- package/dist/connectors/custom.d.ts +31 -0
- package/dist/connectors/custom.d.ts.map +1 -1
- package/dist/connectors/custom.js +37 -0
- package/dist/connectors/custom.js.map +1 -1
- package/dist/connectors/exposure.d.ts +23 -0
- package/dist/connectors/exposure.d.ts.map +1 -0
- package/dist/connectors/exposure.js +37 -0
- package/dist/connectors/exposure.js.map +1 -0
- package/dist/connectors/proxy.d.ts +84 -0
- package/dist/connectors/proxy.d.ts.map +1 -0
- package/dist/connectors/proxy.js +652 -0
- package/dist/connectors/proxy.js.map +1 -0
- package/dist/connectors/redirect.d.ts +12 -0
- package/dist/connectors/redirect.d.ts.map +1 -0
- package/dist/connectors/redirect.js +25 -0
- package/dist/connectors/redirect.js.map +1 -0
- package/dist/connectors/resolve.d.ts +11 -0
- package/dist/connectors/resolve.d.ts.map +1 -1
- package/dist/connectors/resolve.js +23 -2
- package/dist/connectors/resolve.js.map +1 -1
- package/dist/connectors/types.d.ts +18 -0
- package/dist/connectors/types.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/orchestration/capabilities.d.ts.map +1 -1
- package/dist/orchestration/capabilities.js +7 -1
- package/dist/orchestration/capabilities.js.map +1 -1
- package/dist/session/codex-process.d.ts +2 -0
- package/dist/session/codex-process.d.ts.map +1 -1
- package/dist/session/codex-process.js +5 -1
- package/dist/session/codex-process.js.map +1 -1
- package/dist/session/container-connectors.d.ts +14 -1
- package/dist/session/container-connectors.d.ts.map +1 -1
- package/dist/session/container-connectors.js +36 -0
- package/dist/session/container-connectors.js.map +1 -1
- package/dist/session/managed-connectors.d.ts +9 -7
- package/dist/session/managed-connectors.d.ts.map +1 -1
- package/dist/session/managed-connectors.js +23 -9
- package/dist/session/managed-connectors.js.map +1 -1
- package/dist/session/process.d.ts +4 -0
- package/dist/session/process.d.ts.map +1 -1
- package/dist/session/process.js +32 -4
- package/dist/session/process.js.map +1 -1
- package/mcp/connector-bridge.ts +161 -0
- package/mcp/connector-launcher.mjs +44 -0
- package/package.json +1 -1
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway-side half of a launched connector (#635). The gateway spawns this per
|
|
3
|
+
* launcher connection and writes one JSON line first: the resolved mcpServers entry
|
|
4
|
+
* plus the launcher's cwd and environment. Everything after it is the agent's MCP
|
|
5
|
+
* stdio stream. The secret therefore travels over a pipe and never reaches a file,
|
|
6
|
+
* this process's argv or its environment.
|
|
7
|
+
*
|
|
8
|
+
* stdio entry spawn the server exactly as Claude Code would (launcher env + entry
|
|
9
|
+
* env, launcher cwd) and hand it the stream byte for byte
|
|
10
|
+
* sse / http forward each JSON-RPC message to the upstream with the entry's headers,
|
|
11
|
+
* following same-origin redirects only (src/connectors/redirect.ts),
|
|
12
|
+
* the same policy as the gateway's HTTP proxy
|
|
13
|
+
*
|
|
14
|
+
* Bundled for Node when it has to run inside an app-agent container.
|
|
15
|
+
*/
|
|
16
|
+
import { spawn } from 'child_process';
|
|
17
|
+
import * as http from 'node:http';
|
|
18
|
+
import { createInterface } from 'readline';
|
|
19
|
+
import { PassThrough } from 'stream';
|
|
20
|
+
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
21
|
+
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
22
|
+
import { MAX_REDIRECTS, sameOriginRedirect } from '../dist/connectors/redirect.js';
|
|
23
|
+
|
|
24
|
+
const MAX_SPEC_BYTES = 4 * 1024 * 1024;
|
|
25
|
+
let pending = Buffer.alloc(0);
|
|
26
|
+
|
|
27
|
+
function onData(chunk: Buffer): void {
|
|
28
|
+
pending = Buffer.concat([pending, chunk]);
|
|
29
|
+
const end = pending.indexOf(10);
|
|
30
|
+
if (end < 0) {
|
|
31
|
+
if (pending.length > MAX_SPEC_BYTES) fail('connector spec too large');
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
process.stdin.off('data', onData);
|
|
35
|
+
process.stdin.pause();
|
|
36
|
+
let spec: { config: Record<string, any>; cwd?: string; env?: Record<string, string>; authorization?: { url?: string; socketPath?: string; path?: string; grant: string } };
|
|
37
|
+
try { spec = JSON.parse(pending.subarray(0, end).toString('utf8')); } catch { return fail('invalid connector spec'); }
|
|
38
|
+
// The rest of the stream, starting with whatever arrived alongside the spec.
|
|
39
|
+
const rest = new PassThrough();
|
|
40
|
+
rest.write(pending.subarray(end + 1));
|
|
41
|
+
process.stdin.pipe(rest);
|
|
42
|
+
pending = Buffer.alloc(0);
|
|
43
|
+
if (typeof spec.config?.command === 'string') runStdio(spec as any, rest);
|
|
44
|
+
else runRemote(spec, rest).catch(err => fail(err instanceof Error ? err.message : String(err)));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function fail(message: string): never {
|
|
48
|
+
process.stderr.write(`connector-bridge: ${message}\n`);
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function authorize(authorization: { url?: string; socketPath?: string; path?: string; grant: string } | undefined): Promise<boolean> {
|
|
53
|
+
if (!authorization?.grant || (!authorization.url && (!authorization.socketPath || !authorization.path))) return Promise.resolve(false);
|
|
54
|
+
return new Promise(resolve => {
|
|
55
|
+
const request = authorization.url
|
|
56
|
+
? http.request(authorization.url, { method: 'POST', headers: { authorization: `Bearer ${authorization.grant}` } })
|
|
57
|
+
: http.request({ socketPath: authorization.socketPath, path: authorization.path, method: 'POST', headers: { authorization: `Bearer ${authorization.grant}` } });
|
|
58
|
+
request.setTimeout(5000, () => request.destroy(new Error('authorization check timed out')));
|
|
59
|
+
request.on('response', response => { response.resume(); resolve(response.statusCode === 204); });
|
|
60
|
+
request.on('error', () => resolve(false));
|
|
61
|
+
request.end();
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function denied(message: any): void {
|
|
66
|
+
if (message.id !== undefined && message.method) {
|
|
67
|
+
process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, error: { code: -32003, message: 'Connector is not available' } }) + '\n');
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function runStdio(spec: { config: { command: string; args?: unknown; env?: Record<string, string>; cwd?: string }; cwd?: string; env?: Record<string, string>; authorization?: { url?: string; socketPath?: string; path?: string; grant: string } }, input: PassThrough): void {
|
|
72
|
+
const { config } = spec;
|
|
73
|
+
const child = spawn(config.command, Array.isArray(config.args) ? config.args.map(String) : [], {
|
|
74
|
+
cwd: typeof config.cwd === 'string' ? config.cwd : spec.cwd,
|
|
75
|
+
env: { ...spec.env, ...config.env },
|
|
76
|
+
stdio: ['pipe', 'inherit', 'inherit'],
|
|
77
|
+
});
|
|
78
|
+
child.stdin.on('error', () => {});
|
|
79
|
+
const lines = createInterface({ input, crlfDelay: Infinity });
|
|
80
|
+
let pending = Promise.resolve();
|
|
81
|
+
lines.on('line', line => {
|
|
82
|
+
if (!line.trim()) return;
|
|
83
|
+
let message: any;
|
|
84
|
+
try { message = JSON.parse(line); } catch { return; }
|
|
85
|
+
pending = pending.then(async () => {
|
|
86
|
+
if (message.method && !(await authorize(spec.authorization))) { denied(message); return; }
|
|
87
|
+
child.stdin.write(line + '\n');
|
|
88
|
+
}).catch(err => fail(err instanceof Error ? err.message : String(err)));
|
|
89
|
+
});
|
|
90
|
+
lines.on('close', () => child.stdin.end());
|
|
91
|
+
for (const signal of ['SIGTERM', 'SIGINT'] as const) process.on(signal, () => child.kill(signal));
|
|
92
|
+
child.on('error', err => fail(`cannot start ${config.command}: ${err.message}`));
|
|
93
|
+
child.on('exit', (code, signal) => process.exit(code ?? (signal ? 1 : 0)));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* fetch with the connector's redirect policy. Same-origin hops keep fetch's own
|
|
98
|
+
* method rules (303, and 301/302 after a POST, continue as a bodyless GET); a hop to
|
|
99
|
+
* another origin fails the request instead of carrying the headers there.
|
|
100
|
+
*/
|
|
101
|
+
async function sameOriginFetch(input: string | URL | Request, init: RequestInit = {}): Promise<Response> {
|
|
102
|
+
let url = new URL(input instanceof Request ? input.url : String(input));
|
|
103
|
+
let current: RequestInit = { ...init, redirect: 'manual' };
|
|
104
|
+
for (let hop = 0; ; hop++) {
|
|
105
|
+
const res = await fetch(url, current);
|
|
106
|
+
const location = res.headers.get('location');
|
|
107
|
+
if (res.status < 300 || res.status >= 400 || location === null) return res;
|
|
108
|
+
const next = sameOriginRedirect(url, location);
|
|
109
|
+
await res.body?.cancel();
|
|
110
|
+
if (!next) throw new Error(`upstream redirect to another origin refused (${res.status})`);
|
|
111
|
+
if (hop >= MAX_REDIRECTS) throw new Error('too many upstream redirects');
|
|
112
|
+
if (res.status === 303 || ((res.status === 301 || res.status === 302) && current.method === 'POST')) {
|
|
113
|
+
current = { ...current, method: 'GET', body: undefined };
|
|
114
|
+
}
|
|
115
|
+
url = next;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function runRemote(spec: { config: Record<string, any>; authorization?: { url?: string; socketPath?: string; path?: string; grant: string } }, input: PassThrough): Promise<void> {
|
|
120
|
+
const config = spec.config;
|
|
121
|
+
const url = new URL(config.url);
|
|
122
|
+
const requestInit = { headers: config.headers ?? {} };
|
|
123
|
+
const transport = config.type === 'sse'
|
|
124
|
+
? new SSEClientTransport(url, { requestInit, fetch: sameOriginFetch })
|
|
125
|
+
: new StreamableHTTPClientTransport(url, { requestInit, fetch: sameOriginFetch });
|
|
126
|
+
const initializing = new Set<unknown>();
|
|
127
|
+
let pending = Promise.resolve();
|
|
128
|
+
transport.onmessage = (message: any) => {
|
|
129
|
+
// Claude Code's own transport sends the negotiated version on later requests.
|
|
130
|
+
if (initializing.delete(message.id) && message.result?.protocolVersion && 'setProtocolVersion' in transport) {
|
|
131
|
+
(transport as StreamableHTTPClientTransport).setProtocolVersion(message.result.protocolVersion);
|
|
132
|
+
}
|
|
133
|
+
process.stdout.write(JSON.stringify(message) + '\n');
|
|
134
|
+
};
|
|
135
|
+
transport.onerror = err => process.stderr.write(`connector-bridge: ${err.message}\n`);
|
|
136
|
+
transport.onclose = () => process.exit(0);
|
|
137
|
+
await transport.start();
|
|
138
|
+
const lines = createInterface({ input, crlfDelay: Infinity });
|
|
139
|
+
lines.on('line', line => {
|
|
140
|
+
if (!line.trim()) return;
|
|
141
|
+
let message: any;
|
|
142
|
+
try { message = JSON.parse(line); } catch { return; }
|
|
143
|
+
if (message.method === 'initialize') initializing.add(message.id);
|
|
144
|
+
pending = pending.then(async () => {
|
|
145
|
+
// Responses to an upstream-initiated request finish work the connector
|
|
146
|
+
// already started; gate new operations while preserving those responses.
|
|
147
|
+
if (message.method && !(await authorize(spec.authorization))) { denied(message); return; }
|
|
148
|
+
await transport.send(message);
|
|
149
|
+
}).catch((err: Error) => {
|
|
150
|
+
// A request that never reached the upstream still needs an answer, or the call hangs.
|
|
151
|
+
if (message.id !== undefined && message.method) {
|
|
152
|
+
process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: message.id, error: { code: -32000, message: err.message } }) + '\n');
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
lines.on('close', () => { void transport.close(); });
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// resume(): a loader that read this module's code off stdin first leaves it paused.
|
|
160
|
+
process.stdin.on('data', onData).resume();
|
|
161
|
+
process.stdin.on('end', () => { if (pending.length) fail('connector spec incomplete'); });
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Agent-side half of a launched connector (#635). Claude Code (or lazy-connector.ts)
|
|
4
|
+
* starts this as the connector's stdio MCP server. It holds no secret: it opens a
|
|
5
|
+
* grant-authenticated upgrade to the gateway, sends the cwd and environment it was
|
|
6
|
+
* started with, then passes stdin/stdout through unchanged. The gateway runs the
|
|
7
|
+
* real server (or SSE/HTTP client) with the resolved entry it never writes to disk.
|
|
8
|
+
*
|
|
9
|
+
* Plain Node with no dependencies, so the same file runs inside an app-agent container.
|
|
10
|
+
*/
|
|
11
|
+
import * as http from 'node:http';
|
|
12
|
+
|
|
13
|
+
const PROTOCOL = 'gateway-connector-stdio';
|
|
14
|
+
const { GATEWAY_CONNECTOR_URL: url, GATEWAY_CONNECTOR_SOCKET: socketPath, GATEWAY_CONNECTOR_PATH: path, GATEWAY_CONNECTOR_GRANT: grant } = process.env;
|
|
15
|
+
const env = Object.fromEntries(Object.entries(process.env).filter(([name]) => !name.startsWith('GATEWAY_CONNECTOR_')));
|
|
16
|
+
|
|
17
|
+
function fail(message) {
|
|
18
|
+
process.stderr.write(`connector-launcher: ${message}\n`);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (!grant || !(url || (socketPath && path))) fail('missing gateway connector address');
|
|
23
|
+
|
|
24
|
+
// A container's socket is bound as its session starts; give it a moment to appear.
|
|
25
|
+
const deadline = Date.now() + 5000;
|
|
26
|
+
function open() {
|
|
27
|
+
const headers = { connection: 'Upgrade', upgrade: PROTOCOL, authorization: `Bearer ${grant}` };
|
|
28
|
+
const req = socketPath ? http.request({ socketPath, path, headers }) : http.request(new URL(url), { headers });
|
|
29
|
+
req.on('upgrade', (_res, socket, head) => {
|
|
30
|
+
socket.write(JSON.stringify({ cwd: process.cwd(), env }) + '\n');
|
|
31
|
+
if (head.length) process.stdout.write(head);
|
|
32
|
+
socket.pipe(process.stdout);
|
|
33
|
+
process.stdin.pipe(socket);
|
|
34
|
+
socket.on('end', () => process.exit(0));
|
|
35
|
+
socket.on('error', err => fail(err.message));
|
|
36
|
+
});
|
|
37
|
+
req.on('response', res => fail(`gateway refused the connector (${res.statusCode})`));
|
|
38
|
+
req.on('error', err => {
|
|
39
|
+
if ((err.code === 'ENOENT' || err.code === 'ECONNREFUSED') && Date.now() < deadline) setTimeout(open, 100);
|
|
40
|
+
else fail(err.message);
|
|
41
|
+
});
|
|
42
|
+
req.end();
|
|
43
|
+
}
|
|
44
|
+
open();
|