@anyshift/mcp-proxy 0.6.10 → 0.6.11
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/index.js +79 -77
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -363,94 +363,96 @@ async function main() {
|
|
|
363
363
|
// ------------------------------------------------------------------------
|
|
364
364
|
let childClient = null;
|
|
365
365
|
let childToolsResponse = { tools: [] };
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
366
|
+
const connectChild = async () => {
|
|
367
|
+
if (REMOTE_URL) {
|
|
368
|
+
console.debug(`[mcp-proxy] Connecting to remote MCP: ${REMOTE_URL}`);
|
|
369
|
+
let headers = {};
|
|
370
|
+
if (REMOTE_HEADERS) {
|
|
371
|
+
try {
|
|
372
|
+
headers = JSON.parse(REMOTE_HEADERS);
|
|
373
|
+
}
|
|
374
|
+
catch (e) {
|
|
375
|
+
console.error('ERROR: MCP_PROXY_REMOTE_HEADERS contains invalid JSON');
|
|
376
|
+
console.error(` Value: ${REMOTE_HEADERS.substring(0, 100)}${REMOTE_HEADERS.length > 100 ? '...' : ''}`);
|
|
377
|
+
console.error(` Parse error: ${e instanceof Error ? e.message : String(e)}`);
|
|
378
|
+
process.exit(1);
|
|
379
|
+
}
|
|
373
380
|
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
381
|
+
if (REMOTE_CREDENTIALS) {
|
|
382
|
+
try {
|
|
383
|
+
JSON.parse(REMOTE_CREDENTIALS); // Validate JSON before sending
|
|
384
|
+
}
|
|
385
|
+
catch (e) {
|
|
386
|
+
console.error('ERROR: MCP_PROXY_REMOTE_CREDENTIALS contains invalid JSON');
|
|
387
|
+
console.error(` Value: ${REMOTE_CREDENTIALS.substring(0, 100)}${REMOTE_CREDENTIALS.length > 100 ? '...' : ''}`);
|
|
388
|
+
console.error(` Parse error: ${e instanceof Error ? e.message : String(e)}`);
|
|
389
|
+
process.exit(1);
|
|
390
|
+
}
|
|
391
|
+
headers['X-Client-Credentials'] = REMOTE_CREDENTIALS;
|
|
379
392
|
}
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
393
|
+
let childTransport;
|
|
394
|
+
if (REMOTE_TRANSPORT === 'streamable-http') {
|
|
395
|
+
childTransport = new StreamableHTTPClientTransport(new URL(REMOTE_URL), {
|
|
396
|
+
requestInit: { headers },
|
|
397
|
+
});
|
|
384
398
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
399
|
+
else {
|
|
400
|
+
childTransport = new SSEClientTransport(new URL(REMOTE_URL), {
|
|
401
|
+
eventSourceInit: {
|
|
402
|
+
fetch: (url, init) => {
|
|
403
|
+
const merged = new Headers(init?.headers);
|
|
404
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
405
|
+
merged.set(key, value);
|
|
406
|
+
}
|
|
407
|
+
return fetch(url, { ...init, headers: merged });
|
|
408
|
+
},
|
|
409
|
+
},
|
|
410
|
+
requestInit: { headers },
|
|
411
|
+
});
|
|
390
412
|
}
|
|
391
|
-
|
|
413
|
+
childClient = new Client({
|
|
414
|
+
name: 'mcp-proxy-client',
|
|
415
|
+
version: '1.0.0'
|
|
416
|
+
}, {
|
|
417
|
+
capabilities: {}
|
|
418
|
+
});
|
|
419
|
+
await childClient.connect(childTransport);
|
|
420
|
+
console.debug('[mcp-proxy] Connected to remote MCP');
|
|
421
|
+
childToolsResponse = await childClient.listTools();
|
|
422
|
+
console.debug(`[mcp-proxy] Discovered ${childToolsResponse.tools.length} tools from remote MCP`);
|
|
392
423
|
}
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
childTransport = new
|
|
396
|
-
|
|
424
|
+
else if (CHILD_COMMAND) {
|
|
425
|
+
console.debug(`[mcp-proxy] Spawning child MCP: ${CHILD_COMMAND}`);
|
|
426
|
+
const childTransport = new StdioClientTransport({
|
|
427
|
+
command: CHILD_COMMAND,
|
|
428
|
+
args: CHILD_ARGS,
|
|
429
|
+
env: childEnv // All values are defined strings (filtered in extraction loop)
|
|
397
430
|
});
|
|
431
|
+
childTransport.onerror = (error) => {
|
|
432
|
+
console.error('[mcp-proxy] Child error:', error.message);
|
|
433
|
+
};
|
|
434
|
+
childClient = new Client({
|
|
435
|
+
name: 'mcp-proxy-client',
|
|
436
|
+
version: '1.0.0'
|
|
437
|
+
}, {
|
|
438
|
+
capabilities: {}
|
|
439
|
+
});
|
|
440
|
+
await childClient.connect(childTransport);
|
|
441
|
+
console.debug('[mcp-proxy] Connected to child MCP');
|
|
442
|
+
await new Promise(resolve => setTimeout(resolve, 50));
|
|
443
|
+
childToolsResponse = await childClient.listTools();
|
|
444
|
+
console.debug(`[mcp-proxy] Discovered ${childToolsResponse.tools.length} tools from child MCP`);
|
|
398
445
|
}
|
|
399
446
|
else {
|
|
400
|
-
|
|
401
|
-
eventSourceInit: {
|
|
402
|
-
fetch: (url, init) => {
|
|
403
|
-
const merged = new Headers(init?.headers);
|
|
404
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
405
|
-
merged.set(key, value);
|
|
406
|
-
}
|
|
407
|
-
return fetch(url, { ...init, headers: merged });
|
|
408
|
-
},
|
|
409
|
-
},
|
|
410
|
-
requestInit: { headers },
|
|
411
|
-
});
|
|
447
|
+
console.debug('[mcp-proxy] Standalone mode - no child MCP');
|
|
412
448
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
capabilities: {}
|
|
418
|
-
});
|
|
419
|
-
await childClient.connect(childTransport);
|
|
420
|
-
console.debug('[mcp-proxy] Connected to remote MCP');
|
|
421
|
-
// Discover tools from remote MCP
|
|
422
|
-
childToolsResponse = await childClient.listTools();
|
|
423
|
-
console.debug(`[mcp-proxy] Discovered ${childToolsResponse.tools.length} tools from remote MCP`);
|
|
424
|
-
}
|
|
425
|
-
else if (CHILD_COMMAND) {
|
|
426
|
-
console.debug(`[mcp-proxy] Spawning child MCP: ${CHILD_COMMAND}`);
|
|
427
|
-
const childTransport = new StdioClientTransport({
|
|
428
|
-
command: CHILD_COMMAND,
|
|
429
|
-
args: CHILD_ARGS,
|
|
430
|
-
env: childEnv // All values are defined strings (filtered in extraction loop)
|
|
431
|
-
});
|
|
432
|
-
// Log errors instead of crashing silently
|
|
433
|
-
childTransport.onerror = (error) => {
|
|
434
|
-
console.error('[mcp-proxy] Child error:', error.message);
|
|
435
|
-
};
|
|
436
|
-
childClient = new Client({
|
|
437
|
-
name: 'mcp-proxy-client',
|
|
438
|
-
version: '1.0.0'
|
|
439
|
-
}, {
|
|
440
|
-
capabilities: {}
|
|
441
|
-
});
|
|
442
|
-
await childClient.connect(childTransport);
|
|
443
|
-
console.debug('[mcp-proxy] Connected to child MCP');
|
|
444
|
-
// Give child 50ms to stabilize before making requests
|
|
445
|
-
await new Promise(resolve => setTimeout(resolve, 50));
|
|
446
|
-
// ------------------------------------------------------------------------
|
|
447
|
-
// 2. DISCOVER TOOLS FROM CHILD MCP
|
|
448
|
-
// ------------------------------------------------------------------------
|
|
449
|
-
childToolsResponse = await childClient.listTools();
|
|
450
|
-
console.debug(`[mcp-proxy] Discovered ${childToolsResponse.tools.length} tools from child MCP`);
|
|
449
|
+
};
|
|
450
|
+
if (SENTRY_DSN && (SENTRY_TRACE_HEADER || SENTRY_BAGGAGE_HEADER)) {
|
|
451
|
+
await Sentry.continueTrace({ sentryTrace: SENTRY_TRACE_HEADER || '', baggage: SENTRY_BAGGAGE_HEADER }, () => Sentry.startSpan({ name: MCP_NAME, op: 'mcp.connection' }, connectChild));
|
|
452
|
+
await Sentry.flush(2000);
|
|
451
453
|
}
|
|
452
454
|
else {
|
|
453
|
-
|
|
455
|
+
await connectChild();
|
|
454
456
|
}
|
|
455
457
|
// ------------------------------------------------------------------------
|
|
456
458
|
// 2.5. STORE ORIGINAL TOOL SCHEMAS FOR SMART PARAMETER SANITIZATION
|
package/package.json
CHANGED