@anyshift/mcp-proxy 0.6.9 → 0.6.10
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 +266 -229
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
* - Wrapper Mode: When MCP_PROXY_CHILD_COMMAND is set, wraps another MCP server
|
|
15
15
|
* - Standalone Mode: When MCP_PROXY_CHILD_COMMAND is not set, provides only JQ tool
|
|
16
16
|
*/
|
|
17
|
+
import * as Sentry from '@sentry/node';
|
|
17
18
|
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
18
19
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
19
20
|
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
@@ -29,6 +30,21 @@ import { createFileWriter } from './fileWriter/index.js';
|
|
|
29
30
|
import { generateToolId } from './utils/filename.js';
|
|
30
31
|
import { PROXY_PARAMS } from './types/index.js';
|
|
31
32
|
// ============================================================================
|
|
33
|
+
// SENTRY INITIALIZATION
|
|
34
|
+
// ============================================================================
|
|
35
|
+
const MCP_NAME = process.env.MCP_PROXY_MCP_NAME || 'unknown';
|
|
36
|
+
const SENTRY_DSN = process.env.SENTRY_DSN;
|
|
37
|
+
const SENTRY_ENV = process.env.SENTRY_ENV;
|
|
38
|
+
const SENTRY_TRACE_HEADER = process.env.SENTRY_TRACE_HEADER;
|
|
39
|
+
const SENTRY_BAGGAGE_HEADER = process.env.SENTRY_BAGGAGE_HEADER;
|
|
40
|
+
if (SENTRY_DSN) {
|
|
41
|
+
Sentry.init({
|
|
42
|
+
dsn: SENTRY_DSN,
|
|
43
|
+
environment: SENTRY_ENV,
|
|
44
|
+
tracesSampleRate: 1.0,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
// ============================================================================
|
|
32
48
|
// HELPER FUNCTIONS
|
|
33
49
|
// ============================================================================
|
|
34
50
|
/**
|
|
@@ -293,10 +309,12 @@ if (ENABLE_DOCS && DOCS_INDEXED_PATHS.length === 0) {
|
|
|
293
309
|
*/
|
|
294
310
|
const childEnv = {};
|
|
295
311
|
for (const [key, value] of Object.entries(process.env)) {
|
|
296
|
-
// Skip proxy configuration variables
|
|
297
312
|
if (key.startsWith('MCP_PROXY_')) {
|
|
298
313
|
continue;
|
|
299
314
|
}
|
|
315
|
+
if (key === 'SENTRY_DSN' || key === 'SENTRY_ENV' || key === 'MCP_PROXY_MCP_NAME') {
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
300
318
|
// Only include defined values
|
|
301
319
|
if (value !== undefined) {
|
|
302
320
|
childEnv[key] = value;
|
|
@@ -586,76 +604,225 @@ async function main() {
|
|
|
586
604
|
console.debug(`[mcp-proxy] Retry of: ${toolArgs.originalToolId}`);
|
|
587
605
|
}
|
|
588
606
|
}
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
if (
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
result = await jqTool.handler({
|
|
597
|
-
params: { arguments: toolArgs }
|
|
598
|
-
});
|
|
599
|
-
// JQ tool returns directly, wrap in unified format
|
|
600
|
-
const tool_id = generateToolId(toolName, toolArgs, fileWriterConfig.toolAbbreviations);
|
|
601
|
-
const unifiedResponse = createContentResponse(tool_id, result.content?.[0]?.text, toolArgs);
|
|
602
|
-
return {
|
|
603
|
-
content: [{
|
|
604
|
-
type: 'text',
|
|
605
|
-
text: JSON.stringify(unifiedResponse, null, 2)
|
|
606
|
-
}],
|
|
607
|
-
isError: result.isError
|
|
608
|
-
};
|
|
609
|
-
}
|
|
610
|
-
// Handle Timeseries anomaly detection tool locally (if enabled)
|
|
611
|
-
if (toolName === 'detect_timeseries_anomalies' && timeseriesTool) {
|
|
612
|
-
if (ENABLE_LOGGING) {
|
|
613
|
-
console.debug('[mcp-proxy] Executing Timeseries anomaly detection tool locally');
|
|
614
|
-
}
|
|
615
|
-
result = await timeseriesTool.handler({
|
|
616
|
-
params: { arguments: toolArgs }
|
|
617
|
-
});
|
|
618
|
-
// Timeseries tool returns directly, wrap in unified format
|
|
619
|
-
const tool_id = generateToolId(toolName, toolArgs, fileWriterConfig.toolAbbreviations);
|
|
620
|
-
let outputContent = null;
|
|
621
|
-
if (result.content?.[0]?.text) {
|
|
622
|
-
try {
|
|
623
|
-
outputContent = JSON.parse(result.content[0].text);
|
|
624
|
-
}
|
|
625
|
-
catch {
|
|
626
|
-
// If JSON parsing fails, use the raw text
|
|
627
|
-
outputContent = result.content[0].text;
|
|
607
|
+
const executeToolCall = async () => {
|
|
608
|
+
try {
|
|
609
|
+
let result;
|
|
610
|
+
// Handle JQ tool locally (if enabled)
|
|
611
|
+
if (toolName === 'execute_jq_query' && jqTool) {
|
|
612
|
+
if (ENABLE_LOGGING) {
|
|
613
|
+
console.debug('[mcp-proxy] Executing JQ tool locally');
|
|
628
614
|
}
|
|
615
|
+
result = await Sentry.startSpan({ name: toolName, op: 'mcp.tool_call.local' }, async () => {
|
|
616
|
+
return await jqTool.handler({
|
|
617
|
+
params: { arguments: toolArgs }
|
|
618
|
+
});
|
|
619
|
+
});
|
|
620
|
+
// JQ tool returns directly, wrap in unified format
|
|
621
|
+
const tool_id = generateToolId(toolName, toolArgs, fileWriterConfig.toolAbbreviations);
|
|
622
|
+
const unifiedResponse = createContentResponse(tool_id, result.content?.[0]?.text, toolArgs);
|
|
623
|
+
return {
|
|
624
|
+
content: [{
|
|
625
|
+
type: 'text',
|
|
626
|
+
text: JSON.stringify(unifiedResponse, null, 2)
|
|
627
|
+
}],
|
|
628
|
+
isError: result.isError
|
|
629
|
+
};
|
|
629
630
|
}
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
wroteToFile: false,
|
|
633
|
-
outputContent
|
|
634
|
-
};
|
|
635
|
-
return {
|
|
636
|
-
content: [{
|
|
637
|
-
type: 'text',
|
|
638
|
-
text: JSON.stringify(unifiedResponse, null, 2)
|
|
639
|
-
}],
|
|
640
|
-
isError: result.isError
|
|
641
|
-
};
|
|
642
|
-
}
|
|
643
|
-
// Handle Docs tools locally (if enabled)
|
|
644
|
-
// Route through file writer for large doc handling (same as child MCP tools)
|
|
645
|
-
const docsToolNames = ['docs_glob', 'docs_grep', 'docs_read'];
|
|
646
|
-
if (docsToolNames.includes(toolName) && docsTools.length > 0) {
|
|
647
|
-
const docTool = docsTools.find(t => t.toolDefinition.name === toolName);
|
|
648
|
-
if (docTool) {
|
|
631
|
+
// Handle Timeseries anomaly detection tool locally (if enabled)
|
|
632
|
+
if (toolName === 'detect_timeseries_anomalies' && timeseriesTool) {
|
|
649
633
|
if (ENABLE_LOGGING) {
|
|
650
|
-
console.debug(
|
|
634
|
+
console.debug('[mcp-proxy] Executing Timeseries anomaly detection tool locally');
|
|
651
635
|
}
|
|
652
|
-
|
|
653
|
-
|
|
636
|
+
result = await Sentry.startSpan({ name: toolName, op: 'mcp.tool_call.local' }, async () => {
|
|
637
|
+
return await timeseriesTool.handler({
|
|
654
638
|
params: { arguments: toolArgs }
|
|
655
639
|
});
|
|
656
|
-
|
|
640
|
+
});
|
|
641
|
+
// Timeseries tool returns directly, wrap in unified format
|
|
642
|
+
const tool_id = generateToolId(toolName, toolArgs, fileWriterConfig.toolAbbreviations);
|
|
643
|
+
let outputContent = null;
|
|
644
|
+
if (result.content?.[0]?.text) {
|
|
645
|
+
try {
|
|
646
|
+
outputContent = JSON.parse(result.content[0].text);
|
|
647
|
+
}
|
|
648
|
+
catch {
|
|
649
|
+
// If JSON parsing fails, use the raw text
|
|
650
|
+
outputContent = result.content[0].text;
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
const unifiedResponse = {
|
|
654
|
+
tool_id,
|
|
655
|
+
wroteToFile: false,
|
|
656
|
+
outputContent
|
|
657
|
+
};
|
|
658
|
+
return {
|
|
659
|
+
content: [{
|
|
660
|
+
type: 'text',
|
|
661
|
+
text: JSON.stringify(unifiedResponse, null, 2)
|
|
662
|
+
}],
|
|
663
|
+
isError: result.isError
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
// Handle Docs tools locally (if enabled)
|
|
667
|
+
// Route through file writer for large doc handling (same as child MCP tools)
|
|
668
|
+
const docsToolNames = ['docs_glob', 'docs_grep', 'docs_read'];
|
|
669
|
+
if (docsToolNames.includes(toolName) && docsTools.length > 0) {
|
|
670
|
+
const docTool = docsTools.find(t => t.toolDefinition.name === toolName);
|
|
671
|
+
if (docTool) {
|
|
672
|
+
if (ENABLE_LOGGING) {
|
|
673
|
+
console.debug(`[mcp-proxy] Executing ${toolName} locally`);
|
|
674
|
+
}
|
|
675
|
+
try {
|
|
676
|
+
result = await Sentry.startSpan({ name: toolName, op: 'mcp.tool_call.local' }, async () => {
|
|
677
|
+
return await docTool.handler({
|
|
678
|
+
params: { arguments: toolArgs }
|
|
679
|
+
});
|
|
680
|
+
});
|
|
681
|
+
const contentStr = result.content?.[0]?.text || '';
|
|
682
|
+
const originalLength = contentStr.length;
|
|
683
|
+
// Route through file writer (same pipeline as child MCP tools)
|
|
684
|
+
const unifiedResponse = await fileWriter.handleResponse(toolName, toolArgs, {
|
|
685
|
+
content: [{ type: 'text', text: contentStr }]
|
|
686
|
+
});
|
|
687
|
+
if (ENABLE_LOGGING) {
|
|
688
|
+
if (unifiedResponse.wroteToFile) {
|
|
689
|
+
console.debug(`[mcp-proxy] File written for ${toolName} (${originalLength} chars) → ${unifiedResponse.filePath}`);
|
|
690
|
+
}
|
|
691
|
+
else {
|
|
692
|
+
console.debug(`[mcp-proxy] Response for ${toolName} (${originalLength} chars) returned directly`);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
// If not written to file, apply truncation to outputContent
|
|
696
|
+
if (!unifiedResponse.wroteToFile && unifiedResponse.outputContent) {
|
|
697
|
+
const outputStr = typeof unifiedResponse.outputContent === 'string'
|
|
698
|
+
? unifiedResponse.outputContent
|
|
699
|
+
: JSON.stringify(unifiedResponse.outputContent);
|
|
700
|
+
const truncated = truncateResponseIfNeeded(truncationConfig, outputStr);
|
|
701
|
+
if (truncated.length < outputStr.length) {
|
|
702
|
+
if (ENABLE_LOGGING) {
|
|
703
|
+
console.debug(`[mcp-proxy] Truncated response: ${outputStr.length} → ${truncated.length} chars`);
|
|
704
|
+
}
|
|
705
|
+
// Keep as string for docs (markdown content)
|
|
706
|
+
unifiedResponse.outputContent = truncated;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
// Add retry metadata and return unified response
|
|
710
|
+
addRetryMetadata(unifiedResponse, toolArgs);
|
|
711
|
+
return {
|
|
712
|
+
content: [{
|
|
713
|
+
type: 'text',
|
|
714
|
+
text: JSON.stringify(unifiedResponse, null, 2)
|
|
715
|
+
}],
|
|
716
|
+
isError: false
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
catch (error) {
|
|
720
|
+
const tool_id = generateToolId(toolName, toolArgs, fileWriterConfig.toolAbbreviations);
|
|
721
|
+
return {
|
|
722
|
+
content: [{
|
|
723
|
+
type: 'text',
|
|
724
|
+
text: JSON.stringify(createErrorResponse(tool_id, error.message || String(error), toolArgs), null, 2)
|
|
725
|
+
}],
|
|
726
|
+
isError: true
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
// Forward all other tools to child MCP (if child exists)
|
|
732
|
+
if (!childClient) {
|
|
733
|
+
const tool_id = generateToolId(toolName, toolArgs, fileWriterConfig.toolAbbreviations);
|
|
734
|
+
return {
|
|
735
|
+
content: [{
|
|
736
|
+
type: 'text',
|
|
737
|
+
text: JSON.stringify(createErrorResponse(tool_id, `Tool ${toolName} not available in standalone mode (no child MCP)`, toolArgs), null, 2)
|
|
738
|
+
}],
|
|
739
|
+
isError: true
|
|
740
|
+
};
|
|
741
|
+
}
|
|
742
|
+
if (ENABLE_LOGGING) {
|
|
743
|
+
console.debug(`[mcp-proxy] Forwarding to child MCP: ${toolName}`);
|
|
744
|
+
}
|
|
745
|
+
// Sanitize arguments: remove proxy parameters that weren't in original tool schema
|
|
746
|
+
const sanitizedArgs = sanitizeToolArgs(toolName, toolArgs);
|
|
747
|
+
result = await Sentry.startSpan({ name: toolName, op: 'mcp.tool_call.remote' }, () => childClient.callTool({
|
|
748
|
+
name: toolName,
|
|
749
|
+
arguments: sanitizedArgs
|
|
750
|
+
}, undefined, {
|
|
751
|
+
timeout: REQUEST_TIMEOUT_MS
|
|
752
|
+
}));
|
|
753
|
+
// Check if child MCP returned an error
|
|
754
|
+
const childReturnedError = !!result.isError;
|
|
755
|
+
// Preserve _meta from child response (e.g. parsed_commands) for passthrough
|
|
756
|
+
const childMeta = result._meta && typeof result._meta === 'object' && Object.keys(result._meta).length > 0
|
|
757
|
+
? result._meta
|
|
758
|
+
: undefined;
|
|
759
|
+
// Process result through file writer to get unified response
|
|
760
|
+
if (result.content && Array.isArray(result.content) && result.content.length > 0) {
|
|
761
|
+
// Extract text content and embedded resources from all content items
|
|
762
|
+
const textContents = [];
|
|
763
|
+
const resources = [];
|
|
764
|
+
for (const item of result.content) {
|
|
765
|
+
if (item.type === 'text' && typeof item.text === 'string') {
|
|
766
|
+
textContents.push(item.text);
|
|
767
|
+
}
|
|
768
|
+
else if (item.type === 'resource' && item.resource) {
|
|
769
|
+
// Handle EmbeddedResource - extract content from resource field
|
|
770
|
+
const resource = item.resource;
|
|
771
|
+
const resourceObj = {};
|
|
772
|
+
if (resource.uri)
|
|
773
|
+
resourceObj.uri = resource.uri;
|
|
774
|
+
if (resource.mimeType)
|
|
775
|
+
resourceObj.mimeType = resource.mimeType;
|
|
776
|
+
// Content can be in 'text' (string) or 'blob' (base64 binary)
|
|
777
|
+
if (resource.text) {
|
|
778
|
+
resourceObj.content = resource.text;
|
|
779
|
+
}
|
|
780
|
+
else if (resource.blob) {
|
|
781
|
+
// For binary content, keep as base64 or decode based on mimeType
|
|
782
|
+
resourceObj.content = typeof resource.blob === 'string'
|
|
783
|
+
? resource.blob
|
|
784
|
+
: Buffer.from(resource.blob).toString('base64');
|
|
785
|
+
}
|
|
786
|
+
resources.push(resourceObj);
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
// Build combined content object
|
|
790
|
+
let combinedContent;
|
|
791
|
+
if (resources.length > 0) {
|
|
792
|
+
// Has resources - combine text and resources into single object
|
|
793
|
+
combinedContent = {
|
|
794
|
+
text: textContents.length === 1 ? textContents[0] : textContents,
|
|
795
|
+
resources: resources.length === 1 ? resources[0] : resources
|
|
796
|
+
};
|
|
797
|
+
if (ENABLE_LOGGING) {
|
|
798
|
+
console.debug(`[mcp-proxy] Combined ${textContents.length} text items and ${resources.length} resources`);
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
else if (textContents.length > 0) {
|
|
802
|
+
// Text only - use first text content (original behavior)
|
|
803
|
+
combinedContent = textContents[0];
|
|
804
|
+
}
|
|
805
|
+
if (combinedContent !== undefined) {
|
|
806
|
+
const contentStr = typeof combinedContent === 'string'
|
|
807
|
+
? combinedContent
|
|
808
|
+
: JSON.stringify(combinedContent);
|
|
657
809
|
const originalLength = contentStr.length;
|
|
658
|
-
//
|
|
810
|
+
// If child returned error, pass through directly without file writing
|
|
811
|
+
if (childReturnedError) {
|
|
812
|
+
const tool_id = generateToolId(toolName, toolArgs, fileWriterConfig.toolAbbreviations);
|
|
813
|
+
if (ENABLE_LOGGING) {
|
|
814
|
+
console.debug(`[mcp-proxy] Child MCP returned error for ${toolName}: ${contentStr.substring(0, 100)}...`);
|
|
815
|
+
}
|
|
816
|
+
return {
|
|
817
|
+
content: [{
|
|
818
|
+
type: 'text',
|
|
819
|
+
text: JSON.stringify(createErrorResponse(tool_id, contentStr, toolArgs), null, 2)
|
|
820
|
+
}],
|
|
821
|
+
isError: true,
|
|
822
|
+
...(childMeta ? { _meta: childMeta } : {})
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
// Get unified response from file writer
|
|
659
826
|
const unifiedResponse = await fileWriter.handleResponse(toolName, toolArgs, {
|
|
660
827
|
content: [{ type: 'text', text: contentStr }]
|
|
661
828
|
});
|
|
@@ -677,191 +844,56 @@ async function main() {
|
|
|
677
844
|
if (ENABLE_LOGGING) {
|
|
678
845
|
console.debug(`[mcp-proxy] Truncated response: ${outputStr.length} → ${truncated.length} chars`);
|
|
679
846
|
}
|
|
680
|
-
//
|
|
681
|
-
|
|
847
|
+
// Re-parse if it was JSON, otherwise keep as string
|
|
848
|
+
try {
|
|
849
|
+
unifiedResponse.outputContent = JSON.parse(truncated);
|
|
850
|
+
}
|
|
851
|
+
catch {
|
|
852
|
+
unifiedResponse.outputContent = truncated;
|
|
853
|
+
}
|
|
682
854
|
}
|
|
683
855
|
}
|
|
684
|
-
// Add retry metadata and return unified response
|
|
856
|
+
// Add retry metadata and return unified response as JSON
|
|
685
857
|
addRetryMetadata(unifiedResponse, toolArgs);
|
|
686
858
|
return {
|
|
687
859
|
content: [{
|
|
688
860
|
type: 'text',
|
|
689
861
|
text: JSON.stringify(unifiedResponse, null, 2)
|
|
690
862
|
}],
|
|
691
|
-
isError:
|
|
692
|
-
|
|
693
|
-
}
|
|
694
|
-
catch (error) {
|
|
695
|
-
const tool_id = generateToolId(toolName, toolArgs, fileWriterConfig.toolAbbreviations);
|
|
696
|
-
return {
|
|
697
|
-
content: [{
|
|
698
|
-
type: 'text',
|
|
699
|
-
text: JSON.stringify(createErrorResponse(tool_id, error.message || String(error), toolArgs), null, 2)
|
|
700
|
-
}],
|
|
701
|
-
isError: true
|
|
863
|
+
isError: !!unifiedResponse.error,
|
|
864
|
+
...(childMeta ? { _meta: childMeta } : {})
|
|
702
865
|
};
|
|
703
866
|
}
|
|
704
867
|
}
|
|
868
|
+
// Fallback: return result with generated tool_id
|
|
869
|
+
const tool_id = generateToolId(toolName, toolArgs, fileWriterConfig.toolAbbreviations);
|
|
870
|
+
return {
|
|
871
|
+
content: [{
|
|
872
|
+
type: 'text',
|
|
873
|
+
text: JSON.stringify(createContentResponse(tool_id, result, toolArgs), null, 2)
|
|
874
|
+
}],
|
|
875
|
+
isError: childReturnedError,
|
|
876
|
+
...(childMeta ? { _meta: childMeta } : {})
|
|
877
|
+
};
|
|
705
878
|
}
|
|
706
|
-
|
|
707
|
-
|
|
879
|
+
catch (error) {
|
|
880
|
+
console.error(`[mcp-proxy] Error executing tool ${toolName}:`, error);
|
|
708
881
|
const tool_id = generateToolId(toolName, toolArgs, fileWriterConfig.toolAbbreviations);
|
|
709
882
|
return {
|
|
710
883
|
content: [{
|
|
711
884
|
type: 'text',
|
|
712
|
-
text: JSON.stringify(createErrorResponse(tool_id, `
|
|
885
|
+
text: JSON.stringify(createErrorResponse(tool_id, `Error executing ${toolName}: ${error.message || String(error)}`, toolArgs), null, 2)
|
|
713
886
|
}],
|
|
714
887
|
isError: true
|
|
715
888
|
};
|
|
716
889
|
}
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
result = await childClient.callTool({
|
|
723
|
-
name: toolName,
|
|
724
|
-
arguments: sanitizedArgs
|
|
725
|
-
}, undefined, {
|
|
726
|
-
timeout: REQUEST_TIMEOUT_MS
|
|
727
|
-
});
|
|
728
|
-
// Check if child MCP returned an error
|
|
729
|
-
const childReturnedError = !!result.isError;
|
|
730
|
-
// Preserve _meta from child response (e.g. parsed_commands) for passthrough
|
|
731
|
-
const childMeta = result._meta && typeof result._meta === 'object' && Object.keys(result._meta).length > 0
|
|
732
|
-
? result._meta
|
|
733
|
-
: undefined;
|
|
734
|
-
// Process result through file writer to get unified response
|
|
735
|
-
if (result.content && Array.isArray(result.content) && result.content.length > 0) {
|
|
736
|
-
// Extract text content and embedded resources from all content items
|
|
737
|
-
const textContents = [];
|
|
738
|
-
const resources = [];
|
|
739
|
-
for (const item of result.content) {
|
|
740
|
-
if (item.type === 'text' && typeof item.text === 'string') {
|
|
741
|
-
textContents.push(item.text);
|
|
742
|
-
}
|
|
743
|
-
else if (item.type === 'resource' && item.resource) {
|
|
744
|
-
// Handle EmbeddedResource - extract content from resource field
|
|
745
|
-
const resource = item.resource;
|
|
746
|
-
const resourceObj = {};
|
|
747
|
-
if (resource.uri)
|
|
748
|
-
resourceObj.uri = resource.uri;
|
|
749
|
-
if (resource.mimeType)
|
|
750
|
-
resourceObj.mimeType = resource.mimeType;
|
|
751
|
-
// Content can be in 'text' (string) or 'blob' (base64 binary)
|
|
752
|
-
if (resource.text) {
|
|
753
|
-
resourceObj.content = resource.text;
|
|
754
|
-
}
|
|
755
|
-
else if (resource.blob) {
|
|
756
|
-
// For binary content, keep as base64 or decode based on mimeType
|
|
757
|
-
resourceObj.content = typeof resource.blob === 'string'
|
|
758
|
-
? resource.blob
|
|
759
|
-
: Buffer.from(resource.blob).toString('base64');
|
|
760
|
-
}
|
|
761
|
-
resources.push(resourceObj);
|
|
762
|
-
}
|
|
763
|
-
}
|
|
764
|
-
// Build combined content object
|
|
765
|
-
let combinedContent;
|
|
766
|
-
if (resources.length > 0) {
|
|
767
|
-
// Has resources - combine text and resources into single object
|
|
768
|
-
combinedContent = {
|
|
769
|
-
text: textContents.length === 1 ? textContents[0] : textContents,
|
|
770
|
-
resources: resources.length === 1 ? resources[0] : resources
|
|
771
|
-
};
|
|
772
|
-
if (ENABLE_LOGGING) {
|
|
773
|
-
console.debug(`[mcp-proxy] Combined ${textContents.length} text items and ${resources.length} resources`);
|
|
774
|
-
}
|
|
775
|
-
}
|
|
776
|
-
else if (textContents.length > 0) {
|
|
777
|
-
// Text only - use first text content (original behavior)
|
|
778
|
-
combinedContent = textContents[0];
|
|
779
|
-
}
|
|
780
|
-
if (combinedContent !== undefined) {
|
|
781
|
-
const contentStr = typeof combinedContent === 'string'
|
|
782
|
-
? combinedContent
|
|
783
|
-
: JSON.stringify(combinedContent);
|
|
784
|
-
const originalLength = contentStr.length;
|
|
785
|
-
// If child returned error, pass through directly without file writing
|
|
786
|
-
if (childReturnedError) {
|
|
787
|
-
const tool_id = generateToolId(toolName, toolArgs, fileWriterConfig.toolAbbreviations);
|
|
788
|
-
if (ENABLE_LOGGING) {
|
|
789
|
-
console.debug(`[mcp-proxy] Child MCP returned error for ${toolName}: ${contentStr.substring(0, 100)}...`);
|
|
790
|
-
}
|
|
791
|
-
return {
|
|
792
|
-
content: [{
|
|
793
|
-
type: 'text',
|
|
794
|
-
text: JSON.stringify(createErrorResponse(tool_id, contentStr, toolArgs), null, 2)
|
|
795
|
-
}],
|
|
796
|
-
isError: true,
|
|
797
|
-
...(childMeta ? { _meta: childMeta } : {})
|
|
798
|
-
};
|
|
799
|
-
}
|
|
800
|
-
// Get unified response from file writer
|
|
801
|
-
const unifiedResponse = await fileWriter.handleResponse(toolName, toolArgs, {
|
|
802
|
-
content: [{ type: 'text', text: contentStr }]
|
|
803
|
-
});
|
|
804
|
-
if (ENABLE_LOGGING) {
|
|
805
|
-
if (unifiedResponse.wroteToFile) {
|
|
806
|
-
console.debug(`[mcp-proxy] File written for ${toolName} (${originalLength} chars) → ${unifiedResponse.filePath}`);
|
|
807
|
-
}
|
|
808
|
-
else {
|
|
809
|
-
console.debug(`[mcp-proxy] Response for ${toolName} (${originalLength} chars) returned directly`);
|
|
810
|
-
}
|
|
811
|
-
}
|
|
812
|
-
// If not written to file, apply truncation to outputContent
|
|
813
|
-
if (!unifiedResponse.wroteToFile && unifiedResponse.outputContent) {
|
|
814
|
-
const outputStr = typeof unifiedResponse.outputContent === 'string'
|
|
815
|
-
? unifiedResponse.outputContent
|
|
816
|
-
: JSON.stringify(unifiedResponse.outputContent);
|
|
817
|
-
const truncated = truncateResponseIfNeeded(truncationConfig, outputStr);
|
|
818
|
-
if (truncated.length < outputStr.length) {
|
|
819
|
-
if (ENABLE_LOGGING) {
|
|
820
|
-
console.debug(`[mcp-proxy] Truncated response: ${outputStr.length} → ${truncated.length} chars`);
|
|
821
|
-
}
|
|
822
|
-
// Re-parse if it was JSON, otherwise keep as string
|
|
823
|
-
try {
|
|
824
|
-
unifiedResponse.outputContent = JSON.parse(truncated);
|
|
825
|
-
}
|
|
826
|
-
catch {
|
|
827
|
-
unifiedResponse.outputContent = truncated;
|
|
828
|
-
}
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
// Add retry metadata and return unified response as JSON
|
|
832
|
-
addRetryMetadata(unifiedResponse, toolArgs);
|
|
833
|
-
return {
|
|
834
|
-
content: [{
|
|
835
|
-
type: 'text',
|
|
836
|
-
text: JSON.stringify(unifiedResponse, null, 2)
|
|
837
|
-
}],
|
|
838
|
-
isError: !!unifiedResponse.error,
|
|
839
|
-
...(childMeta ? { _meta: childMeta } : {})
|
|
840
|
-
};
|
|
841
|
-
}
|
|
842
|
-
}
|
|
843
|
-
// Fallback: return result with generated tool_id
|
|
844
|
-
const tool_id = generateToolId(toolName, toolArgs, fileWriterConfig.toolAbbreviations);
|
|
845
|
-
return {
|
|
846
|
-
content: [{
|
|
847
|
-
type: 'text',
|
|
848
|
-
text: JSON.stringify(createContentResponse(tool_id, result, toolArgs), null, 2)
|
|
849
|
-
}],
|
|
850
|
-
isError: childReturnedError,
|
|
851
|
-
...(childMeta ? { _meta: childMeta } : {})
|
|
852
|
-
};
|
|
853
|
-
}
|
|
854
|
-
catch (error) {
|
|
855
|
-
console.error(`[mcp-proxy] Error executing tool ${toolName}:`, error);
|
|
856
|
-
const tool_id = generateToolId(toolName, toolArgs, fileWriterConfig.toolAbbreviations);
|
|
857
|
-
return {
|
|
858
|
-
content: [{
|
|
859
|
-
type: 'text',
|
|
860
|
-
text: JSON.stringify(createErrorResponse(tool_id, `Error executing ${toolName}: ${error.message || String(error)}`, toolArgs), null, 2)
|
|
861
|
-
}],
|
|
862
|
-
isError: true
|
|
863
|
-
};
|
|
890
|
+
};
|
|
891
|
+
if (SENTRY_DSN && (SENTRY_TRACE_HEADER || SENTRY_BAGGAGE_HEADER)) {
|
|
892
|
+
const result = await Sentry.continueTrace({ sentryTrace: SENTRY_TRACE_HEADER || '', baggage: SENTRY_BAGGAGE_HEADER }, () => Sentry.startSpan({ name: `${MCP_NAME}/${toolName}`, op: 'mcp.tool_call' }, executeToolCall));
|
|
893
|
+
await Sentry.flush(2000);
|
|
894
|
+
return result;
|
|
864
895
|
}
|
|
896
|
+
return executeToolCall();
|
|
865
897
|
});
|
|
866
898
|
// ------------------------------------------------------------------------
|
|
867
899
|
// 8. CONNECT PROXY TO STDIO
|
|
@@ -870,8 +902,13 @@ async function main() {
|
|
|
870
902
|
await server.connect(transport);
|
|
871
903
|
console.debug('[mcp-proxy] Proxy server ready on stdio');
|
|
872
904
|
console.debug('[mcp-proxy] Waiting for MCP protocol messages...');
|
|
873
|
-
// Prevent EPIPE from crashing the process
|
|
874
905
|
process.on('SIGPIPE', () => { });
|
|
906
|
+
const shutdown = async () => {
|
|
907
|
+
await Sentry.close(2000);
|
|
908
|
+
process.exit(0);
|
|
909
|
+
};
|
|
910
|
+
process.on('SIGTERM', shutdown);
|
|
911
|
+
process.on('SIGINT', shutdown);
|
|
875
912
|
}
|
|
876
913
|
// ============================================================================
|
|
877
914
|
// START THE PROXY
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anyshift/mcp-proxy",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.10",
|
|
4
4
|
"description": "Generic MCP proxy that adds truncation, file writing, and JQ capabilities to any MCP server",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"@modelcontextprotocol/sdk": "^1.24.0",
|
|
17
17
|
"glob": "^13.0.0",
|
|
18
18
|
"papaparse": "^5.5.3",
|
|
19
|
+
"@sentry/node": "^9.0.0",
|
|
19
20
|
"zod": "^3.24.2"
|
|
20
21
|
},
|
|
21
22
|
"devDependencies": {
|