@mcpjam/cli 1.0.1 → 1.0.2
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 +492 -406
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -55,17 +55,134 @@ var EnvironmentFileSchema = z2.object({
|
|
|
55
55
|
});
|
|
56
56
|
|
|
57
57
|
// src/runner/test-runner.ts
|
|
58
|
-
import { createServer } from "http";
|
|
59
58
|
import { serve } from "@hono/node-server";
|
|
60
59
|
import { Hono as Hono2 } from "hono";
|
|
61
60
|
|
|
62
61
|
// src/server/tests-router.ts
|
|
63
62
|
import { Hono } from "hono";
|
|
64
|
-
import { createAnthropic } from "@ai-sdk/anthropic";
|
|
65
|
-
import { createOpenAI } from "@ai-sdk/openai";
|
|
66
|
-
import { createOllama } from "ollama-ai-provider";
|
|
67
63
|
import { Agent } from "@mastra/core/agent";
|
|
68
64
|
|
|
65
|
+
// src/utils/logger.ts
|
|
66
|
+
import chalk from "chalk";
|
|
67
|
+
import Table from "cli-table3";
|
|
68
|
+
var Logger = class {
|
|
69
|
+
static progressCounter = 0;
|
|
70
|
+
static totalTests = 0;
|
|
71
|
+
static header(version) {
|
|
72
|
+
console.log(chalk.bold.blue(`MCPJAM CLI ${version}`));
|
|
73
|
+
console.log();
|
|
74
|
+
}
|
|
75
|
+
static startTests(count) {
|
|
76
|
+
this.progressCounter = 0;
|
|
77
|
+
this.totalTests = count;
|
|
78
|
+
console.log(chalk.gray(`Running ${count} tests...`));
|
|
79
|
+
console.log();
|
|
80
|
+
}
|
|
81
|
+
static toolCall(toolName) {
|
|
82
|
+
console.log(chalk.gray(`Tool called: ${toolName}`));
|
|
83
|
+
}
|
|
84
|
+
static testResult(result) {
|
|
85
|
+
this.progressCounter++;
|
|
86
|
+
const status = result.passed ? chalk.green("PASS") : chalk.red("FAIL");
|
|
87
|
+
const symbol = "\u25CF";
|
|
88
|
+
const testName = result.title.padEnd(40);
|
|
89
|
+
if (result.passed) {
|
|
90
|
+
const tools = result.calledTools.length > 0 ? chalk.gray(`(tools: ${result.calledTools.join(", ")})`) : chalk.gray("(no tools)");
|
|
91
|
+
console.log(` ${symbol} ${testName} ${status} ${tools}`);
|
|
92
|
+
} else {
|
|
93
|
+
console.log(` ${symbol} ${testName} ${status}`);
|
|
94
|
+
if (result.error) {
|
|
95
|
+
console.log(chalk.red(` Error: ${result.error}`));
|
|
96
|
+
} else {
|
|
97
|
+
if (result.calledTools.length > 0) {
|
|
98
|
+
console.log(
|
|
99
|
+
chalk.gray(` Called: ${result.calledTools.join(", ")}`)
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
if (result.missingTools.length > 0) {
|
|
103
|
+
console.log(
|
|
104
|
+
chalk.red(` Missing: ${result.missingTools.join(", ")}`)
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
if (result.unexpectedTools.length > 0) {
|
|
108
|
+
console.log(
|
|
109
|
+
chalk.yellow(
|
|
110
|
+
` Unexpected: ${result.unexpectedTools.join(", ")}`
|
|
111
|
+
)
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
static summary(results) {
|
|
118
|
+
console.log();
|
|
119
|
+
const table = new Table({
|
|
120
|
+
chars: {
|
|
121
|
+
top: "\u2500",
|
|
122
|
+
"top-mid": "\u252C",
|
|
123
|
+
"top-left": "\u250C",
|
|
124
|
+
"top-right": "\u2510",
|
|
125
|
+
bottom: "\u2500",
|
|
126
|
+
"bottom-mid": "\u2534",
|
|
127
|
+
"bottom-left": "\u2514",
|
|
128
|
+
"bottom-right": "\u2518",
|
|
129
|
+
left: "\u2502",
|
|
130
|
+
"left-mid": "\u251C",
|
|
131
|
+
mid: "\u2500",
|
|
132
|
+
"mid-mid": "\u253C",
|
|
133
|
+
right: "\u2502",
|
|
134
|
+
"right-mid": "\u2524",
|
|
135
|
+
middle: "\u2502"
|
|
136
|
+
},
|
|
137
|
+
style: {
|
|
138
|
+
"padding-left": 1,
|
|
139
|
+
"padding-right": 1,
|
|
140
|
+
border: ["gray"]
|
|
141
|
+
},
|
|
142
|
+
colWidths: [65]
|
|
143
|
+
});
|
|
144
|
+
const passedText = results.passed > 0 ? chalk.green(`${results.passed} passed`) : "0 passed";
|
|
145
|
+
const failedText = results.failed > 0 ? chalk.red(`${results.failed} failed`) : "0 failed";
|
|
146
|
+
const summaryText = `Results: ${passedText}, ${failedText} (${results.duration}s total)`;
|
|
147
|
+
table.push([summaryText]);
|
|
148
|
+
console.log(table.toString());
|
|
149
|
+
}
|
|
150
|
+
static error(message) {
|
|
151
|
+
console.log();
|
|
152
|
+
console.log(chalk.red(`\u2715 Error: ${message}`));
|
|
153
|
+
}
|
|
154
|
+
static progress(current, total, testName) {
|
|
155
|
+
const progress = `[${current}/${total}]`;
|
|
156
|
+
console.log(chalk.gray(`${progress} ${testName}...`));
|
|
157
|
+
}
|
|
158
|
+
static testStarting(testName) {
|
|
159
|
+
console.log(chalk.gray(` Running ${testName}...`));
|
|
160
|
+
}
|
|
161
|
+
static serverConnection(serverCount, toolCount) {
|
|
162
|
+
console.log(
|
|
163
|
+
chalk.gray(
|
|
164
|
+
` Connected to ${serverCount} servers (${toolCount} tools available)`
|
|
165
|
+
)
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
static testError(testName, error) {
|
|
169
|
+
console.log(chalk.red(` \u2715 ${testName} failed: ${error}`));
|
|
170
|
+
}
|
|
171
|
+
static connectionError(serverName, error) {
|
|
172
|
+
console.log(chalk.red(` \u2715 Failed to connect to ${serverName}: ${error}`));
|
|
173
|
+
}
|
|
174
|
+
static apiKeyError(provider, error) {
|
|
175
|
+
console.log(chalk.red(` \u2715 API key error for ${provider}: ${error}`));
|
|
176
|
+
}
|
|
177
|
+
static modelCreationError(provider, modelId, error) {
|
|
178
|
+
console.log(
|
|
179
|
+
chalk.red(
|
|
180
|
+
` \u2715 Failed to create ${provider} model "${modelId}": ${error}`
|
|
181
|
+
)
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
|
|
69
186
|
// ../server/services/mcpjam-client-manager.ts
|
|
70
187
|
import { MCPClient as MCPClient2 } from "@mastra/mcp";
|
|
71
188
|
|
|
@@ -537,361 +654,348 @@ var MCPJamClientManager = class {
|
|
|
537
654
|
}
|
|
538
655
|
};
|
|
539
656
|
|
|
540
|
-
// src/utils/
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
console.log(chalk.bold.blue(`MCPJAM CLI ${version}`));
|
|
548
|
-
console.log();
|
|
657
|
+
// src/utils/test-errors.ts
|
|
658
|
+
var TestError = class extends Error {
|
|
659
|
+
constructor(message, code, details) {
|
|
660
|
+
super(message);
|
|
661
|
+
this.code = code;
|
|
662
|
+
this.details = details;
|
|
663
|
+
this.name = "TestError";
|
|
549
664
|
}
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
665
|
+
};
|
|
666
|
+
var ModelError = class extends TestError {
|
|
667
|
+
constructor(provider, modelId, originalError) {
|
|
668
|
+
const message = formatModelError(provider, originalError);
|
|
669
|
+
super(message, "MODEL_ERROR", { provider, modelId, originalError });
|
|
555
670
|
}
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
671
|
+
};
|
|
672
|
+
var ServerConnectionError = class extends TestError {
|
|
673
|
+
constructor(serverName, originalError) {
|
|
674
|
+
super(
|
|
675
|
+
`Failed to connect to server '${serverName}': ${originalError}`,
|
|
676
|
+
"SERVER_CONNECTION_ERROR",
|
|
677
|
+
{
|
|
678
|
+
serverName,
|
|
679
|
+
originalError
|
|
680
|
+
}
|
|
681
|
+
);
|
|
682
|
+
}
|
|
683
|
+
};
|
|
684
|
+
var ValidationError = class extends TestError {
|
|
685
|
+
constructor(field, value, expected) {
|
|
686
|
+
super(
|
|
687
|
+
`Invalid ${field}: expected ${expected}, got ${value}`,
|
|
688
|
+
"VALIDATION_ERROR",
|
|
689
|
+
{
|
|
690
|
+
field,
|
|
691
|
+
value,
|
|
692
|
+
expected
|
|
693
|
+
}
|
|
694
|
+
);
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
function formatModelError(provider, originalError) {
|
|
698
|
+
const lowerError = originalError.toLowerCase();
|
|
699
|
+
if (lowerError.includes("api key") || lowerError.includes("401") || lowerError.includes("unauthorized") || lowerError.includes("authentication")) {
|
|
700
|
+
return `Invalid or missing API key for ${provider}. Check your API key configuration.`;
|
|
701
|
+
}
|
|
702
|
+
if (lowerError.includes("model") && lowerError.includes("not found")) {
|
|
703
|
+
return `Model not found for ${provider}. Check if the model ID is correct.`;
|
|
704
|
+
}
|
|
705
|
+
if (lowerError.includes("quota") || lowerError.includes("rate limit")) {
|
|
706
|
+
return `Rate limit or quota exceeded for ${provider}. Try again later.`;
|
|
707
|
+
}
|
|
708
|
+
return `Failed to create ${provider} model: ${originalError}`;
|
|
709
|
+
}
|
|
710
|
+
function createTestResult(testId, title, passed, error, calledTools = [], missingTools = [], unexpectedTools = []) {
|
|
711
|
+
return {
|
|
712
|
+
testId,
|
|
713
|
+
title,
|
|
714
|
+
passed,
|
|
715
|
+
calledTools,
|
|
716
|
+
missingTools,
|
|
717
|
+
unexpectedTools,
|
|
718
|
+
error,
|
|
719
|
+
duration: 0
|
|
720
|
+
};
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
// src/utils/connection-manager.ts
|
|
724
|
+
var TestConnectionManager = class {
|
|
725
|
+
clientManager;
|
|
726
|
+
connectedServers = /* @__PURE__ */ new Set();
|
|
727
|
+
constructor() {
|
|
728
|
+
this.clientManager = new MCPJamClientManager();
|
|
729
|
+
}
|
|
730
|
+
async connectToServers(serverConfigs) {
|
|
731
|
+
const connectionPromises = Object.entries(serverConfigs).map(
|
|
732
|
+
async ([name, config]) => {
|
|
733
|
+
if (!this.connectedServers.has(name)) {
|
|
734
|
+
try {
|
|
735
|
+
await this.clientManager.connectToServer(name, config);
|
|
736
|
+
this.connectedServers.add(name);
|
|
737
|
+
} catch (error) {
|
|
738
|
+
throw new ServerConnectionError(
|
|
739
|
+
name,
|
|
740
|
+
error?.message || "Unknown connection error"
|
|
741
|
+
);
|
|
742
|
+
}
|
|
585
743
|
}
|
|
586
744
|
}
|
|
745
|
+
);
|
|
746
|
+
await Promise.all(connectionPromises);
|
|
747
|
+
Logger.serverConnection(
|
|
748
|
+
Object.keys(serverConfigs).length,
|
|
749
|
+
this.clientManager.getAvailableTools().length
|
|
750
|
+
);
|
|
751
|
+
}
|
|
752
|
+
getAvailableTools() {
|
|
753
|
+
return this.clientManager.getAvailableTools();
|
|
754
|
+
}
|
|
755
|
+
getFlattenedTools() {
|
|
756
|
+
const allTools = this.getAvailableTools();
|
|
757
|
+
const flattenedTools = {};
|
|
758
|
+
for (const tool of allTools) {
|
|
759
|
+
flattenedTools[tool.name] = tool;
|
|
587
760
|
}
|
|
761
|
+
return flattenedTools;
|
|
588
762
|
}
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
"bottom-right": "\u2518",
|
|
601
|
-
left: "\u2502",
|
|
602
|
-
"left-mid": "\u251C",
|
|
603
|
-
mid: "\u2500",
|
|
604
|
-
"mid-mid": "\u253C",
|
|
605
|
-
right: "\u2502",
|
|
606
|
-
"right-mid": "\u2524",
|
|
607
|
-
middle: "\u2502"
|
|
608
|
-
},
|
|
609
|
-
style: {
|
|
610
|
-
"padding-left": 1,
|
|
611
|
-
"padding-right": 1,
|
|
612
|
-
border: ["gray"]
|
|
613
|
-
},
|
|
614
|
-
colWidths: [65]
|
|
615
|
-
});
|
|
616
|
-
const passedText = results.passed > 0 ? chalk.green(`${results.passed} passed`) : "0 passed";
|
|
617
|
-
const failedText = results.failed > 0 ? chalk.red(`${results.failed} failed`) : "0 failed";
|
|
618
|
-
const summaryText = `Results: ${passedText}, ${failedText} (${results.duration}s total)`;
|
|
619
|
-
table.push([summaryText]);
|
|
620
|
-
console.log(table.toString());
|
|
763
|
+
async cleanup() {
|
|
764
|
+
try {
|
|
765
|
+
for (const serverName of this.connectedServers) {
|
|
766
|
+
await this.clientManager.disconnectFromServer(serverName);
|
|
767
|
+
}
|
|
768
|
+
this.connectedServers.clear();
|
|
769
|
+
} catch (error) {
|
|
770
|
+
throw new Error(
|
|
771
|
+
`Cleanup failed: ${error?.message || "Unknown error"}`
|
|
772
|
+
);
|
|
773
|
+
}
|
|
621
774
|
}
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
console.log(chalk.red(`\u2715 Error: ${message}`));
|
|
775
|
+
isConnected(serverName) {
|
|
776
|
+
return this.connectedServers.has(serverName);
|
|
625
777
|
}
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
console.log(chalk.gray(`${progress} ${testName}...`));
|
|
778
|
+
getConnectedServers() {
|
|
779
|
+
return Array.from(this.connectedServers);
|
|
629
780
|
}
|
|
630
|
-
|
|
631
|
-
|
|
781
|
+
};
|
|
782
|
+
|
|
783
|
+
// src/utils/model-factory.ts
|
|
784
|
+
import { createAnthropic } from "@ai-sdk/anthropic";
|
|
785
|
+
import { createOpenAI } from "@ai-sdk/openai";
|
|
786
|
+
import { createOllama } from "ollama-ai-provider";
|
|
787
|
+
var MODEL_CREATORS = {
|
|
788
|
+
anthropic: (apiKey, modelId) => createAnthropic({ apiKey })(modelId),
|
|
789
|
+
openai: (apiKey, modelId) => createOpenAI({ apiKey })(modelId),
|
|
790
|
+
deepseek: (apiKey, modelId) => createOpenAI({
|
|
791
|
+
apiKey,
|
|
792
|
+
baseURL: "https://api.deepseek.com/v1"
|
|
793
|
+
})(modelId),
|
|
794
|
+
ollama: (_, modelId) => createOllama({
|
|
795
|
+
baseURL: process.env.OLLAMA_BASE_URL || "http://localhost:11434/api"
|
|
796
|
+
})(modelId, { simulateStreaming: true })
|
|
797
|
+
};
|
|
798
|
+
var ENV_KEYS = {
|
|
799
|
+
anthropic: "ANTHROPIC_API_KEY",
|
|
800
|
+
openai: "OPENAI_API_KEY",
|
|
801
|
+
deepseek: "DEEPSEEK_API_KEY"
|
|
802
|
+
};
|
|
803
|
+
function createModel(modelConfig, providerApiKeys = {}) {
|
|
804
|
+
const { provider, id: modelId } = modelConfig;
|
|
805
|
+
if (!(provider in MODEL_CREATORS)) {
|
|
806
|
+
throw new ModelError(
|
|
807
|
+
provider,
|
|
808
|
+
modelId,
|
|
809
|
+
`Unsupported provider: ${provider}`
|
|
810
|
+
);
|
|
632
811
|
}
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
)
|
|
812
|
+
try {
|
|
813
|
+
let apiKey;
|
|
814
|
+
if (provider !== "ollama") {
|
|
815
|
+
apiKey = providerApiKeys[provider] || process.env[ENV_KEYS[provider]];
|
|
816
|
+
if (!apiKey) {
|
|
817
|
+
throw new Error(
|
|
818
|
+
`Missing ${provider.toUpperCase()} API key. Set ${ENV_KEYS[provider]} environment variable or provide in environment file.`
|
|
819
|
+
);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
const creator = MODEL_CREATORS[provider];
|
|
823
|
+
return creator(apiKey || "", modelId);
|
|
824
|
+
} catch (error) {
|
|
825
|
+
throw new ModelError(
|
|
826
|
+
provider,
|
|
827
|
+
modelId,
|
|
828
|
+
error?.message || "Unknown error"
|
|
638
829
|
);
|
|
639
830
|
}
|
|
640
|
-
|
|
641
|
-
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
// src/utils/test-validation.ts
|
|
834
|
+
function validateTest(test) {
|
|
835
|
+
if (!test) {
|
|
836
|
+
throw new ValidationError("test", test, "non-null object");
|
|
642
837
|
}
|
|
643
|
-
|
|
644
|
-
|
|
838
|
+
if (!test.id || typeof test.id !== "string") {
|
|
839
|
+
throw new ValidationError("test.id", test.id, "non-empty string");
|
|
645
840
|
}
|
|
646
|
-
|
|
647
|
-
|
|
841
|
+
if (!test.title || typeof test.title !== "string") {
|
|
842
|
+
throw new ValidationError("test.title", test.title, "non-empty string");
|
|
648
843
|
}
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
844
|
+
if (!test.model || typeof test.model !== "object") {
|
|
845
|
+
throw new ValidationError("test.model", test.model, "object");
|
|
846
|
+
}
|
|
847
|
+
if (!test.model.id || typeof test.model.id !== "string") {
|
|
848
|
+
throw new ValidationError(
|
|
849
|
+
"test.model.id",
|
|
850
|
+
test.model.id,
|
|
851
|
+
"non-empty string"
|
|
654
852
|
);
|
|
655
853
|
}
|
|
656
|
-
|
|
854
|
+
if (!test.model.provider || typeof test.model.provider !== "string") {
|
|
855
|
+
throw new ValidationError(
|
|
856
|
+
"test.model.provider",
|
|
857
|
+
test.model.provider,
|
|
858
|
+
"non-empty string"
|
|
859
|
+
);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
function validateServerConfigs(serverConfigs, selectedServers) {
|
|
863
|
+
let validConfigs = {};
|
|
864
|
+
if (selectedServers && selectedServers.length > 0) {
|
|
865
|
+
for (const name of selectedServers) {
|
|
866
|
+
if (serverConfigs[name]) {
|
|
867
|
+
validConfigs[name] = serverConfigs[name];
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
} else {
|
|
871
|
+
validConfigs = serverConfigs;
|
|
872
|
+
}
|
|
873
|
+
if (Object.keys(validConfigs).length === 0) {
|
|
874
|
+
throw new ValidationError(
|
|
875
|
+
"serverConfigs",
|
|
876
|
+
validConfigs,
|
|
877
|
+
"at least one valid server configuration"
|
|
878
|
+
);
|
|
879
|
+
}
|
|
880
|
+
return validConfigs;
|
|
881
|
+
}
|
|
882
|
+
function analyzeToolResults(calledTools, expectedTools = []) {
|
|
883
|
+
const called = Array.from(calledTools);
|
|
884
|
+
const expectedSet = new Set(expectedTools);
|
|
885
|
+
const missing = expectedTools.filter((t) => !calledTools.has(t));
|
|
886
|
+
const unexpected = called.filter((t) => !expectedSet.has(t));
|
|
887
|
+
const passed = missing.length === 0 && unexpected.length === 0;
|
|
888
|
+
return {
|
|
889
|
+
called,
|
|
890
|
+
missing,
|
|
891
|
+
unexpected,
|
|
892
|
+
passed
|
|
893
|
+
};
|
|
894
|
+
}
|
|
657
895
|
|
|
658
896
|
// src/server/tests-router.ts
|
|
659
897
|
function createTestsRouter() {
|
|
660
898
|
const tests = new Hono();
|
|
661
|
-
|
|
662
|
-
|
|
899
|
+
let connectionManager = null;
|
|
900
|
+
tests.post("/run", async (c) => {
|
|
663
901
|
try {
|
|
664
|
-
let createModel2 = function(model) {
|
|
665
|
-
let apiKey;
|
|
666
|
-
switch (model.provider) {
|
|
667
|
-
case "anthropic":
|
|
668
|
-
apiKey = providerApiKeys?.anthropic || process.env.ANTHROPIC_API_KEY;
|
|
669
|
-
if (!apiKey) {
|
|
670
|
-
throw new Error(
|
|
671
|
-
"Missing Anthropic API key. Set ANTHROPIC_API_KEY environment variable or provide in environment file."
|
|
672
|
-
);
|
|
673
|
-
}
|
|
674
|
-
return createAnthropic({ apiKey })(model.id);
|
|
675
|
-
case "openai":
|
|
676
|
-
apiKey = providerApiKeys?.openai || process.env.OPENAI_API_KEY;
|
|
677
|
-
if (!apiKey) {
|
|
678
|
-
throw new Error(
|
|
679
|
-
"Missing OpenAI API key. Set OPENAI_API_KEY environment variable or provide in environment file."
|
|
680
|
-
);
|
|
681
|
-
}
|
|
682
|
-
return createOpenAI({ apiKey })(model.id);
|
|
683
|
-
case "deepseek":
|
|
684
|
-
apiKey = providerApiKeys?.deepseek || process.env.DEEPSEEK_API_KEY;
|
|
685
|
-
if (!apiKey) {
|
|
686
|
-
throw new Error(
|
|
687
|
-
"Missing DeepSeek API key. Set DEEPSEEK_API_KEY environment variable or provide in environment file."
|
|
688
|
-
);
|
|
689
|
-
}
|
|
690
|
-
return createOpenAI({
|
|
691
|
-
apiKey,
|
|
692
|
-
baseURL: "https://api.deepseek.com/v1"
|
|
693
|
-
})(model.id);
|
|
694
|
-
case "ollama":
|
|
695
|
-
return createOllama({
|
|
696
|
-
baseURL: process.env.OLLAMA_BASE_URL || "http://localhost:11434"
|
|
697
|
-
})(model.id, { simulateStreaming: true });
|
|
698
|
-
default:
|
|
699
|
-
throw new Error(`Unsupported provider: ${model.provider}`);
|
|
700
|
-
}
|
|
701
|
-
};
|
|
702
|
-
var createModel = createModel2;
|
|
703
902
|
const body = await c.req.json();
|
|
704
|
-
const
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
return c.json({ success: false, error: "No tests provided" }, 400);
|
|
903
|
+
const { test, allServers = {}, providerApiKeys = {} } = body;
|
|
904
|
+
validateTest(test);
|
|
905
|
+
if (!connectionManager) {
|
|
906
|
+
connectionManager = new TestConnectionManager();
|
|
709
907
|
}
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
const
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
} else if (errorMessage.includes("401") || errorMessage.includes("unauthorized") || errorMessage.includes("authentication")) {
|
|
746
|
-
throw new Error(
|
|
747
|
-
`Authentication failed for ${test.model.provider}. Check your API key.`
|
|
748
|
-
);
|
|
749
|
-
} else if (errorMessage.includes("model") && errorMessage.includes("not found")) {
|
|
750
|
-
throw new Error(
|
|
751
|
-
`Model "${test.model.id}" not found for ${test.model.provider}`
|
|
752
|
-
);
|
|
753
|
-
} else {
|
|
754
|
-
throw new Error(
|
|
755
|
-
`Failed to create ${test.model.provider} model: ${errorMessage}`
|
|
756
|
-
);
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
const allTools = clientManager.getAvailableTools();
|
|
760
|
-
const toolsByServer = {};
|
|
761
|
-
for (const tool of allTools) {
|
|
762
|
-
if (!toolsByServer[tool.serverId]) {
|
|
763
|
-
toolsByServer[tool.serverId] = {};
|
|
764
|
-
}
|
|
765
|
-
toolsByServer[tool.serverId][tool.name] = {
|
|
766
|
-
description: tool.description,
|
|
767
|
-
inputSchema: tool.inputSchema,
|
|
768
|
-
execute: async (params) => {
|
|
769
|
-
const result = await clientManager.executeToolDirect(
|
|
770
|
-
`${tool.serverId}:${tool.name}`,
|
|
771
|
-
params
|
|
772
|
-
);
|
|
773
|
-
return result.result;
|
|
908
|
+
Logger.testStarting(test.title);
|
|
909
|
+
const calledTools = /* @__PURE__ */ new Set();
|
|
910
|
+
const serverConfigs = validateServerConfigs(
|
|
911
|
+
allServers,
|
|
912
|
+
test.selectedServers
|
|
913
|
+
);
|
|
914
|
+
try {
|
|
915
|
+
await connectionManager.connectToServers(serverConfigs);
|
|
916
|
+
const model = createModel(test.model, providerApiKeys);
|
|
917
|
+
const flattenedTools = connectionManager.getFlattenedTools();
|
|
918
|
+
const agent = new Agent({
|
|
919
|
+
name: `TestAgent-${test.id}`,
|
|
920
|
+
instructions: "You are a helpful assistant with access to MCP tools",
|
|
921
|
+
model,
|
|
922
|
+
tools: flattenedTools
|
|
923
|
+
});
|
|
924
|
+
const executeOptions = {
|
|
925
|
+
maxSteps: 15,
|
|
926
|
+
timeout: 6e4
|
|
927
|
+
// 60 second timeout
|
|
928
|
+
};
|
|
929
|
+
const result = await agent.generate(
|
|
930
|
+
[{ role: "user", content: test.prompt || "" }],
|
|
931
|
+
executeOptions
|
|
932
|
+
);
|
|
933
|
+
if (result.steps) {
|
|
934
|
+
for (let i = 0; i < result.steps.length; i++) {
|
|
935
|
+
const step = result.steps[i];
|
|
936
|
+
if (step) {
|
|
937
|
+
if (step.toolCalls) {
|
|
938
|
+
for (const toolCall of step.toolCalls) {
|
|
939
|
+
const toolName = toolCall?.toolName;
|
|
940
|
+
if (toolName) {
|
|
941
|
+
Logger.toolCall(toolName);
|
|
942
|
+
calledTools.add(toolName);
|
|
774
943
|
}
|
|
775
|
-
};
|
|
776
|
-
}
|
|
777
|
-
Logger.serverConnection(
|
|
778
|
-
Object.keys(toolsByServer).length,
|
|
779
|
-
allTools.length
|
|
780
|
-
);
|
|
781
|
-
const agent = new Agent({
|
|
782
|
-
name: `TestAgent-${test.id}`,
|
|
783
|
-
instructions: "You are a helpful assistant with access to MCP tools",
|
|
784
|
-
model
|
|
785
|
-
});
|
|
786
|
-
const streamOptions = {
|
|
787
|
-
maxSteps: 10,
|
|
788
|
-
toolsets: toolsByServer,
|
|
789
|
-
onStepFinish: ({
|
|
790
|
-
text,
|
|
791
|
-
toolCalls,
|
|
792
|
-
toolResults
|
|
793
|
-
}) => {
|
|
794
|
-
(toolCalls || []).forEach((c2) => {
|
|
795
|
-
const toolName = c2?.name || c2?.toolName;
|
|
796
|
-
if (toolName) {
|
|
797
|
-
calledTools.add(toolName);
|
|
798
|
-
}
|
|
799
|
-
});
|
|
800
|
-
}
|
|
801
|
-
};
|
|
802
|
-
const tAny = test;
|
|
803
|
-
if (tAny?.advancedConfig?.toolChoice) {
|
|
804
|
-
streamOptions.toolChoice = tAny.advancedConfig.toolChoice;
|
|
805
|
-
}
|
|
806
|
-
const stream = await agent.stream(
|
|
807
|
-
[{ role: "user", content: test.prompt || "" }],
|
|
808
|
-
streamOptions
|
|
809
|
-
);
|
|
810
|
-
for await (const _ of stream.textStream) {
|
|
811
|
-
}
|
|
812
|
-
const called = Array.from(calledTools);
|
|
813
|
-
const missing = Array.from(expectedSet).filter(
|
|
814
|
-
(t) => !calledTools.has(t)
|
|
815
|
-
);
|
|
816
|
-
const unexpected = called.filter((t) => !expectedSet.has(t));
|
|
817
|
-
const passed = missing.length === 0 && unexpected.length === 0;
|
|
818
|
-
if (!passed) failed = true;
|
|
819
|
-
controller.enqueue(
|
|
820
|
-
encoder.encode(
|
|
821
|
-
`data: ${JSON.stringify({
|
|
822
|
-
type: "result",
|
|
823
|
-
testId: test.id,
|
|
824
|
-
passed,
|
|
825
|
-
calledTools: called,
|
|
826
|
-
missingTools: missing,
|
|
827
|
-
unexpectedTools: unexpected
|
|
828
|
-
})}
|
|
829
|
-
|
|
830
|
-
`
|
|
831
|
-
)
|
|
832
|
-
);
|
|
833
|
-
} catch (err) {
|
|
834
|
-
const errorMessage = err?.message || "Unknown error";
|
|
835
|
-
Logger.testError(test.title, errorMessage);
|
|
836
|
-
failed = true;
|
|
837
|
-
controller.enqueue(
|
|
838
|
-
encoder.encode(
|
|
839
|
-
`data: ${JSON.stringify({
|
|
840
|
-
type: "result",
|
|
841
|
-
testId: test.id,
|
|
842
|
-
passed: false,
|
|
843
|
-
error: errorMessage
|
|
844
|
-
})}
|
|
845
|
-
|
|
846
|
-
`
|
|
847
|
-
)
|
|
848
|
-
);
|
|
849
|
-
} finally {
|
|
850
|
-
for (const serverName of Object.keys(serverConfigs)) {
|
|
851
|
-
try {
|
|
852
|
-
await clientManager.disconnectFromServer(serverName);
|
|
853
|
-
} catch (disconnectErr) {
|
|
854
|
-
const errorMessage = disconnectErr?.message || "Unknown disconnect error";
|
|
855
|
-
Logger.connectionError(serverName, errorMessage);
|
|
856
944
|
}
|
|
857
945
|
}
|
|
858
946
|
}
|
|
859
947
|
}
|
|
860
|
-
controller.enqueue(
|
|
861
|
-
encoder.encode(
|
|
862
|
-
`data: ${JSON.stringify({
|
|
863
|
-
type: "run_complete",
|
|
864
|
-
passed: !failed
|
|
865
|
-
})}
|
|
866
|
-
|
|
867
|
-
`
|
|
868
|
-
)
|
|
869
|
-
);
|
|
870
|
-
controller.enqueue(encoder.encode(`data: [DONE]
|
|
871
|
-
|
|
872
|
-
`));
|
|
873
|
-
controller.close();
|
|
874
948
|
}
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
949
|
+
const { called, missing, unexpected, passed } = analyzeToolResults(
|
|
950
|
+
calledTools,
|
|
951
|
+
test.expectedTools
|
|
952
|
+
);
|
|
953
|
+
return c.json({
|
|
954
|
+
success: true,
|
|
955
|
+
testId: test.id,
|
|
956
|
+
passed,
|
|
957
|
+
calledTools: called,
|
|
958
|
+
missingTools: missing,
|
|
959
|
+
unexpectedTools: unexpected
|
|
960
|
+
});
|
|
961
|
+
} catch (err) {
|
|
962
|
+
const errorMessage = err?.message || "Unknown error";
|
|
963
|
+
Logger.testError(test.title, errorMessage);
|
|
964
|
+
const failedResult = createTestResult(
|
|
965
|
+
test.id,
|
|
966
|
+
test.title,
|
|
967
|
+
false,
|
|
968
|
+
errorMessage,
|
|
969
|
+
[],
|
|
970
|
+
test.expectedTools || [],
|
|
971
|
+
[]
|
|
972
|
+
);
|
|
973
|
+
return c.json({ success: false, ...failedResult });
|
|
974
|
+
}
|
|
883
975
|
} catch (err) {
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
500
|
|
887
|
-
);
|
|
976
|
+
const errorMessage = err?.message || "Unknown error";
|
|
977
|
+
return c.json({ success: false, error: errorMessage }, 500);
|
|
888
978
|
}
|
|
889
979
|
});
|
|
980
|
+
tests.post("/cleanup", async (c) => {
|
|
981
|
+
if (connectionManager) {
|
|
982
|
+
try {
|
|
983
|
+
await connectionManager.cleanup();
|
|
984
|
+
connectionManager = null;
|
|
985
|
+
return c.json({ success: true });
|
|
986
|
+
} catch (err) {
|
|
987
|
+
const errorMessage = err?.message || "Unknown error";
|
|
988
|
+
return c.json({ success: false, error: errorMessage }, 500);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
return c.json({ success: true });
|
|
992
|
+
});
|
|
890
993
|
return tests;
|
|
891
994
|
}
|
|
892
995
|
|
|
893
|
-
// src/
|
|
894
|
-
|
|
996
|
+
// src/utils/utils.ts
|
|
997
|
+
import { createServer } from "http";
|
|
998
|
+
function findAvailablePort(startPort = 3500) {
|
|
895
999
|
return new Promise((resolve2, reject) => {
|
|
896
1000
|
const server = createServer();
|
|
897
1001
|
server.listen(0, () => {
|
|
@@ -905,6 +1009,8 @@ async function findAvailablePort(startPort = 3500) {
|
|
|
905
1009
|
});
|
|
906
1010
|
});
|
|
907
1011
|
}
|
|
1012
|
+
|
|
1013
|
+
// src/runner/test-runner.ts
|
|
908
1014
|
async function runTests(tests, environment) {
|
|
909
1015
|
const startTime = Date.now();
|
|
910
1016
|
const app = new Hono2();
|
|
@@ -916,36 +1022,49 @@ async function runTests(tests, environment) {
|
|
|
916
1022
|
});
|
|
917
1023
|
await new Promise((resolve2) => setTimeout(resolve2, 100));
|
|
918
1024
|
try {
|
|
919
|
-
const
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
)
|
|
1025
|
+
const backendServers = convertServersConfig(environment.mcpServers);
|
|
1026
|
+
const results = [];
|
|
1027
|
+
for (let i = 0; i < tests.length; i++) {
|
|
1028
|
+
const test = tests[i];
|
|
1029
|
+
if (!test) {
|
|
1030
|
+
throw new Error(`Test ${i} is undefined`);
|
|
1031
|
+
}
|
|
1032
|
+
const payload = {
|
|
1033
|
+
test: { ...test, id: `test_${i}` },
|
|
1034
|
+
allServers: backendServers,
|
|
1035
|
+
providerApiKeys: environment.providerApiKeys || {}
|
|
1036
|
+
};
|
|
1037
|
+
try {
|
|
1038
|
+
const response = await fetch(`http://localhost:${port}/mcp/tests/run`, {
|
|
1039
|
+
method: "POST",
|
|
1040
|
+
headers: { "Content-Type": "application/json" },
|
|
1041
|
+
body: JSON.stringify(payload)
|
|
1042
|
+
});
|
|
1043
|
+
if (!response.ok) {
|
|
1044
|
+
throw new Error(
|
|
1045
|
+
`Server error: ${response.status} ${response.statusText}`
|
|
1046
|
+
);
|
|
1047
|
+
}
|
|
1048
|
+
const result = await response.json();
|
|
1049
|
+
const testResult = { ...result, title: test.title, duration: 0 };
|
|
1050
|
+
results.push(testResult);
|
|
1051
|
+
Logger.testResult(testResult);
|
|
1052
|
+
} catch (testError) {
|
|
1053
|
+
const errorMessage = testError?.message || "Unknown error";
|
|
1054
|
+
const testResult = createTestResult(
|
|
1055
|
+
`test_${i}`,
|
|
1056
|
+
test.title,
|
|
1057
|
+
false,
|
|
1058
|
+
errorMessage,
|
|
1059
|
+
[],
|
|
1060
|
+
test.expectedTools || [],
|
|
1061
|
+
[]
|
|
1062
|
+
);
|
|
1063
|
+
results.push(testResult);
|
|
1064
|
+
Logger.testResult(testResult);
|
|
1065
|
+
}
|
|
947
1066
|
}
|
|
948
|
-
|
|
1067
|
+
await cleanupServerConnections(port);
|
|
949
1068
|
const duration = ((Date.now() - startTime) / 1e3).toFixed(1);
|
|
950
1069
|
return {
|
|
951
1070
|
passed: results.filter((r) => r.passed).length,
|
|
@@ -966,63 +1085,30 @@ function convertServerConfig(config) {
|
|
|
966
1085
|
args: config.args || [],
|
|
967
1086
|
env: config.env || {}
|
|
968
1087
|
};
|
|
969
|
-
} else {
|
|
970
|
-
return {
|
|
971
|
-
url: config.url,
|
|
972
|
-
requestInit: {
|
|
973
|
-
headers: config.headers || {}
|
|
974
|
-
},
|
|
975
|
-
eventSourceInit: {
|
|
976
|
-
headers: config.headers || {}
|
|
977
|
-
}
|
|
978
|
-
};
|
|
979
1088
|
}
|
|
1089
|
+
return {
|
|
1090
|
+
url: config.url,
|
|
1091
|
+
requestInit: { headers: config.headers || {} },
|
|
1092
|
+
eventSourceInit: { headers: config.headers || {} }
|
|
1093
|
+
};
|
|
980
1094
|
}
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
const data = line.slice(6);
|
|
998
|
-
if (data === "[DONE]") break;
|
|
999
|
-
try {
|
|
1000
|
-
const event = JSON.parse(data);
|
|
1001
|
-
if (event.type === "result") {
|
|
1002
|
-
const testIndex = parseInt(event.testId.split("_")[1]);
|
|
1003
|
-
const test = tests[testIndex];
|
|
1004
|
-
const testStart = Date.now();
|
|
1005
|
-
const result = {
|
|
1006
|
-
testId: event.testId,
|
|
1007
|
-
title: test?.title || "Unknown Test",
|
|
1008
|
-
passed: event.passed,
|
|
1009
|
-
calledTools: event.calledTools || [],
|
|
1010
|
-
missingTools: event.missingTools || [],
|
|
1011
|
-
unexpectedTools: event.unexpectedTools || [],
|
|
1012
|
-
error: event.error,
|
|
1013
|
-
duration: 0
|
|
1014
|
-
// We don't have individual timing from the stream
|
|
1015
|
-
};
|
|
1016
|
-
results.push(result);
|
|
1017
|
-
Logger.testResult(result);
|
|
1018
|
-
} else if (event.type === "trace_step") {
|
|
1019
|
-
}
|
|
1020
|
-
} catch (e) {
|
|
1021
|
-
}
|
|
1022
|
-
}
|
|
1023
|
-
}
|
|
1095
|
+
function convertServersConfig(mcpServers) {
|
|
1096
|
+
return Object.fromEntries(
|
|
1097
|
+
Object.entries(mcpServers).map(([name, config]) => [
|
|
1098
|
+
name,
|
|
1099
|
+
convertServerConfig(config)
|
|
1100
|
+
])
|
|
1101
|
+
);
|
|
1102
|
+
}
|
|
1103
|
+
async function cleanupServerConnections(port) {
|
|
1104
|
+
try {
|
|
1105
|
+
await fetch(`http://localhost:${port}/mcp/tests/cleanup`, {
|
|
1106
|
+
method: "POST",
|
|
1107
|
+
headers: { "Content-Type": "application/json" }
|
|
1108
|
+
});
|
|
1109
|
+
} catch (cleanupError) {
|
|
1110
|
+
console.warn("Warning: Failed to cleanup server connections");
|
|
1024
1111
|
}
|
|
1025
|
-
return results;
|
|
1026
1112
|
}
|
|
1027
1113
|
|
|
1028
1114
|
// src/utils/env-resolver.ts
|