@1mcp/agent 0.30.3 → 0.31.0-beta1
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/README.md +75 -2
- package/build/commands/auth/index.d.ts +3 -0
- package/build/commands/auth/index.d.ts.map +1 -0
- package/build/commands/auth/index.js +47 -0
- package/build/commands/auth/index.js.map +1 -0
- package/build/commands/auth/login.d.ts +7 -0
- package/build/commands/auth/login.d.ts.map +1 -0
- package/build/commands/auth/login.js +83 -0
- package/build/commands/auth/login.js.map +1 -0
- package/build/commands/auth/logout.d.ts +7 -0
- package/build/commands/auth/logout.d.ts.map +1 -0
- package/build/commands/auth/logout.js +46 -0
- package/build/commands/auth/logout.js.map +1 -0
- package/build/commands/auth/status.d.ts +6 -0
- package/build/commands/auth/status.d.ts.map +1 -0
- package/build/commands/auth/status.js +54 -0
- package/build/commands/auth/status.js.map +1 -0
- package/build/commands/cliSetup/cliSetup.d.ts +12 -0
- package/build/commands/cliSetup/cliSetup.d.ts.map +1 -0
- package/build/commands/cliSetup/cliSetup.js +49 -0
- package/build/commands/cliSetup/cliSetup.js.map +1 -0
- package/build/commands/cliSetup/index.d.ts +3 -0
- package/build/commands/cliSetup/index.d.ts.map +1 -0
- package/build/commands/cliSetup/index.js +52 -0
- package/build/commands/cliSetup/index.js.map +1 -0
- package/build/commands/cliSetup/setupFiles.d.ts +24 -0
- package/build/commands/cliSetup/setupFiles.d.ts.map +1 -0
- package/build/commands/cliSetup/setupFiles.js +233 -0
- package/build/commands/cliSetup/setupFiles.js.map +1 -0
- package/build/commands/inspect/index.d.ts +3 -0
- package/build/commands/inspect/index.d.ts.map +1 -0
- package/build/commands/inspect/index.js +60 -0
- package/build/commands/inspect/index.js.map +1 -0
- package/build/commands/inspect/inspect.d.ts +36 -0
- package/build/commands/inspect/inspect.d.ts.map +1 -0
- package/build/commands/inspect/inspect.js +274 -0
- package/build/commands/inspect/inspect.js.map +1 -0
- package/build/commands/inspect/inspectUtils.d.ts +76 -0
- package/build/commands/inspect/inspectUtils.d.ts.map +1 -0
- package/build/commands/inspect/inspectUtils.js +303 -0
- package/build/commands/inspect/inspectUtils.js.map +1 -0
- package/build/commands/instructions/index.d.ts +3 -0
- package/build/commands/instructions/index.d.ts.map +1 -0
- package/build/commands/instructions/index.js +34 -0
- package/build/commands/instructions/index.js.map +1 -0
- package/build/commands/instructions/instructions.d.ts +10 -0
- package/build/commands/instructions/instructions.d.ts.map +1 -0
- package/build/commands/instructions/instructions.js +86 -0
- package/build/commands/instructions/instructions.js.map +1 -0
- package/build/commands/instructions/instructionsUtils.d.ts +24 -0
- package/build/commands/instructions/instructionsUtils.d.ts.map +1 -0
- package/build/commands/instructions/instructionsUtils.js +74 -0
- package/build/commands/instructions/instructionsUtils.js.map +1 -0
- package/build/commands/proxy/proxy.d.ts.map +1 -1
- package/build/commands/proxy/proxy.js +26 -30
- package/build/commands/proxy/proxy.js.map +1 -1
- package/build/commands/run/index.d.ts +3 -0
- package/build/commands/run/index.d.ts.map +1 -0
- package/build/commands/run/index.js +62 -0
- package/build/commands/run/index.js.map +1 -0
- package/build/commands/run/run.d.ts +35 -0
- package/build/commands/run/run.d.ts.map +1 -0
- package/build/commands/run/run.js +340 -0
- package/build/commands/run/run.js.map +1 -0
- package/build/commands/run/runUtils.d.ts +49 -0
- package/build/commands/run/runUtils.d.ts.map +1 -0
- package/build/commands/run/runUtils.js +195 -0
- package/build/commands/run/runUtils.js.map +1 -0
- package/build/commands/shared/apiClient.d.ts +27 -0
- package/build/commands/shared/apiClient.d.ts.map +1 -0
- package/build/commands/shared/apiClient.js +96 -0
- package/build/commands/shared/apiClient.js.map +1 -0
- package/build/commands/shared/authProfileStore.d.ts +16 -0
- package/build/commands/shared/authProfileStore.d.ts.map +1 -0
- package/build/commands/shared/authProfileStore.js +109 -0
- package/build/commands/shared/authProfileStore.js.map +1 -0
- package/build/commands/shared/baseConfigUtils.d.ts.map +1 -1
- package/build/commands/shared/baseConfigUtils.js +28 -2
- package/build/commands/shared/baseConfigUtils.js.map +1 -1
- package/build/commands/shared/cliContext.d.ts +13 -0
- package/build/commands/shared/cliContext.d.ts.map +1 -0
- package/build/commands/shared/cliContext.js +71 -0
- package/build/commands/shared/cliContext.js.map +1 -0
- package/build/commands/shared/commandRunner.d.ts +3 -0
- package/build/commands/shared/commandRunner.d.ts.map +1 -0
- package/build/commands/shared/commandRunner.js +13 -0
- package/build/commands/shared/commandRunner.js.map +1 -0
- package/build/commands/shared/serveClient.d.ts +68 -0
- package/build/commands/shared/serveClient.d.ts.map +1 -0
- package/build/commands/shared/serveClient.js +204 -0
- package/build/commands/shared/serveClient.js.map +1 -0
- package/build/commands/shared/serveTargetResolver.d.ts +21 -0
- package/build/commands/shared/serveTargetResolver.d.ts.map +1 -0
- package/build/commands/shared/serveTargetResolver.js +33 -0
- package/build/commands/shared/serveTargetResolver.js.map +1 -0
- package/build/config/configManager.d.ts +10 -0
- package/build/config/configManager.d.ts.map +1 -1
- package/build/config/configManager.js +54 -2
- package/build/config/configManager.js.map +1 -1
- package/build/config/projectConfigLoader.d.ts +9 -0
- package/build/config/projectConfigLoader.d.ts.map +1 -1
- package/build/config/projectConfigLoader.js +72 -20
- package/build/config/projectConfigLoader.js.map +1 -1
- package/build/config/templateProcessor.d.ts.map +1 -1
- package/build/config/templateProcessor.js +2 -2
- package/build/config/templateProcessor.js.map +1 -1
- package/build/constants/api.d.ts +6 -0
- package/build/constants/api.d.ts.map +1 -1
- package/build/constants/api.js +7 -0
- package/build/constants/api.js.map +1 -1
- package/build/constants/mcp.d.ts +1 -1
- package/build/constants/mcp.d.ts.map +1 -1
- package/build/constants/mcp.js +1 -1
- package/build/constants/mcp.js.map +1 -1
- package/build/core/capabilities/lazyLoadingOrchestrator.d.ts +7 -2
- package/build/core/capabilities/lazyLoadingOrchestrator.d.ts.map +1 -1
- package/build/core/capabilities/lazyLoadingOrchestrator.js +12 -12
- package/build/core/capabilities/lazyLoadingOrchestrator.js.map +1 -1
- package/build/core/capabilities/metaToolProvider.d.ts +1 -1
- package/build/core/capabilities/metaToolProvider.d.ts.map +1 -1
- package/build/core/capabilities/metaToolProvider.js +22 -21
- package/build/core/capabilities/metaToolProvider.js.map +1 -1
- package/build/core/server/connectionResolver.d.ts +4 -0
- package/build/core/server/connectionResolver.d.ts.map +1 -1
- package/build/core/server/connectionResolver.js +18 -11
- package/build/core/server/connectionResolver.js.map +1 -1
- package/build/core/server/serverManager.d.ts +4 -0
- package/build/core/server/serverManager.d.ts.map +1 -1
- package/build/core/server/serverManager.js +6 -0
- package/build/core/server/serverManager.js.map +1 -1
- package/build/core/tools/handlers/serverManagementHandler.d.ts +2 -2
- package/build/core/tools/internal/schemas/discovery.d.ts +3 -3
- package/build/core/tools/internal/schemas/index.d.ts +4 -4
- package/build/core/tools/internal/schemas/management.d.ts +1 -1
- package/build/globalOptions.d.ts +6 -0
- package/build/globalOptions.d.ts.map +1 -1
- package/build/globalOptions.js +5 -0
- package/build/globalOptions.js.map +1 -1
- package/build/index.js +20 -1
- package/build/index.js.map +1 -1
- package/build/transport/http/routes/apiRoutes.d.ts +10 -0
- package/build/transport/http/routes/apiRoutes.d.ts.map +1 -0
- package/build/transport/http/routes/apiRoutes.js +18 -0
- package/build/transport/http/routes/apiRoutes.js.map +1 -0
- package/build/transport/http/routes/cliTokenRoute.d.ts +4 -0
- package/build/transport/http/routes/cliTokenRoute.d.ts.map +1 -0
- package/build/transport/http/routes/cliTokenRoute.js +35 -0
- package/build/transport/http/routes/cliTokenRoute.js.map +1 -0
- package/build/transport/http/routes/inspectHelpers.d.ts +68 -0
- package/build/transport/http/routes/inspectHelpers.d.ts.map +1 -0
- package/build/transport/http/routes/inspectHelpers.js +141 -0
- package/build/transport/http/routes/inspectHelpers.js.map +1 -0
- package/build/transport/http/routes/inspectRoutes.d.ts +8 -0
- package/build/transport/http/routes/inspectRoutes.d.ts.map +1 -0
- package/build/transport/http/routes/inspectRoutes.js +348 -0
- package/build/transport/http/routes/inspectRoutes.js.map +1 -0
- package/build/transport/http/routes/toolRoutes.d.ts +5 -0
- package/build/transport/http/routes/toolRoutes.d.ts.map +1 -0
- package/build/transport/http/routes/toolRoutes.js +204 -0
- package/build/transport/http/routes/toolRoutes.js.map +1 -0
- package/build/transport/http/server.d.ts.map +1 -1
- package/build/transport/http/server.js +16 -1
- package/build/transport/http/server.js.map +1 -1
- package/build/transport/http/utils/contextExtractor.d.ts +5 -1
- package/build/transport/http/utils/contextExtractor.d.ts.map +1 -1
- package/build/transport/http/utils/contextExtractor.js +75 -12
- package/build/transport/http/utils/contextExtractor.js.map +1 -1
- package/build/transport/stdioProxyTransport.d.ts +2 -2
- package/build/transport/stdioProxyTransport.d.ts.map +1 -1
- package/build/transport/stdioProxyTransport.js +11 -75
- package/build/transport/stdioProxyTransport.js.map +1 -1
- package/build/types/context.d.ts +1 -0
- package/build/types/context.d.ts.map +1 -1
- package/build/types/context.js.map +1 -1
- package/build/utils/context/contextHash.d.ts +11 -0
- package/build/utils/context/contextHash.d.ts.map +1 -0
- package/build/utils/context/contextHash.js +37 -0
- package/build/utils/context/contextHash.js.map +1 -0
- package/build/utils/typeGuards.d.ts +2 -0
- package/build/utils/typeGuards.d.ts.map +1 -0
- package/build/utils/typeGuards.js +4 -0
- package/build/utils/typeGuards.js.map +1 -0
- package/build/utils/urlUtils.d.ts +2 -0
- package/build/utils/urlUtils.d.ts.map +1 -0
- package/build/utils/urlUtils.js +4 -0
- package/build/utils/urlUtils.js.map +1 -0
- package/build/utils/validation/urlDetection.d.ts +1 -0
- package/build/utils/validation/urlDetection.d.ts.map +1 -1
- package/build/utils/validation/urlDetection.js +8 -3
- package/build/utils/validation/urlDetection.js.map +1 -1
- package/package.json +3 -2
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
function formatStatus(status) {
|
|
3
|
+
if (!status)
|
|
4
|
+
return undefined;
|
|
5
|
+
return status === 'connected'
|
|
6
|
+
? chalk.green(status)
|
|
7
|
+
: status === 'disconnected'
|
|
8
|
+
? chalk.red(status)
|
|
9
|
+
: chalk.yellow(status);
|
|
10
|
+
}
|
|
11
|
+
function formatMetadataLines(item) {
|
|
12
|
+
const lines = [`server: ${chalk.bold(item.server)}`];
|
|
13
|
+
if (item.type) {
|
|
14
|
+
lines.push(`type: ${chalk.dim(item.type)}`);
|
|
15
|
+
}
|
|
16
|
+
const status = formatStatus(item.status);
|
|
17
|
+
if (status) {
|
|
18
|
+
lines.push(`status: ${status}`);
|
|
19
|
+
}
|
|
20
|
+
if (item.available !== undefined) {
|
|
21
|
+
lines.push(`available: ${item.available ? 'yes' : 'no'}`);
|
|
22
|
+
}
|
|
23
|
+
lines.push(`tools: ${item.toolCount}`);
|
|
24
|
+
lines.push(`instructions: ${item.hasInstructions ? 'yes' : 'no'}`);
|
|
25
|
+
return lines;
|
|
26
|
+
}
|
|
27
|
+
export function formatInstructionsOutput(output) {
|
|
28
|
+
const sections = [
|
|
29
|
+
chalk.bold.cyan('1MCP CLI Instructions'),
|
|
30
|
+
chalk.bold('=== PLAYBOOK ==='),
|
|
31
|
+
[
|
|
32
|
+
'1. Start here before selecting tools.',
|
|
33
|
+
'2. Review the available servers below and choose the server that matches the task.',
|
|
34
|
+
"3. Run `1mcp inspect <server>` to list that server's tools.",
|
|
35
|
+
'4. Run `1mcp inspect <server>/<tool>` to inspect the tool schema and arguments.',
|
|
36
|
+
"5. Run `1mcp run <server>/<tool> --args '<json>'` only after inspecting the tool.",
|
|
37
|
+
'6. Use `--preset`, `--tags`, or `--tag-filter` to narrow the server set when needed.',
|
|
38
|
+
'7. If authentication is required, run `1mcp auth login --url <server-url> --token <token>` and retry.',
|
|
39
|
+
].join('\n'),
|
|
40
|
+
];
|
|
41
|
+
const serverLines = [chalk.bold('=== SERVER SUMMARY ===')];
|
|
42
|
+
for (const server of output.servers) {
|
|
43
|
+
serverLines.push('');
|
|
44
|
+
serverLines.push(`<server_summary name="${server.server}">`);
|
|
45
|
+
for (const line of formatMetadataLines(server)) {
|
|
46
|
+
serverLines.push(`\t${line}`);
|
|
47
|
+
}
|
|
48
|
+
serverLines.push(`</server_summary>`);
|
|
49
|
+
}
|
|
50
|
+
sections.push(serverLines.join('\n'));
|
|
51
|
+
const detailSections = [chalk.bold('=== SERVER DETAILS ===')];
|
|
52
|
+
for (const detail of output.details) {
|
|
53
|
+
const lines = [`<server_detail name="${detail.server}">`];
|
|
54
|
+
for (const line of formatMetadataLines(detail)) {
|
|
55
|
+
lines.push(`\t${line}`);
|
|
56
|
+
}
|
|
57
|
+
if (detail.instructions?.trim()) {
|
|
58
|
+
lines.push(`\t<server_instructions name="${detail.server}">`);
|
|
59
|
+
lines.push(detail.instructions);
|
|
60
|
+
lines.push('\t</server_instructions>');
|
|
61
|
+
}
|
|
62
|
+
else if (detail.note) {
|
|
63
|
+
lines.push(`\t<note>${chalk.dim(detail.note)}</note>`);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
lines.push(`\t<note>${chalk.dim('(none provided)')}</note>`);
|
|
67
|
+
}
|
|
68
|
+
lines.push(`</server_detail>`);
|
|
69
|
+
detailSections.push(lines.join('\n'));
|
|
70
|
+
}
|
|
71
|
+
sections.push(detailSections.join('\n\n'));
|
|
72
|
+
return sections.join('\n\n');
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=instructionsUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"instructionsUtils.js","sourceRoot":"","sources":["../../../src/commands/instructions/instructionsUtils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AA2B1B,SAAS,YAAY,CAAC,MAAe;IACnC,IAAI,CAAC,MAAM;QAAE,OAAO,SAAS,CAAC;IAC9B,OAAO,MAAM,KAAK,WAAW;QAC3B,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC;QACrB,CAAC,CAAC,MAAM,KAAK,cAAc;YACzB,CAAC,CAAC,KAAK,CAAC,GAAG,CAA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M,CAAC,IAAI,EAAE,CAAC;YACvB,KAAK,CAAC,IAAI,CAAC,WAAW,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACzD,CAAC;aAAM,CAAC;YACN,KAAK,CAAC,IAAI,CAAC,WAAW,KAAK,CAAC,GAAG,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAC;QAC/D,CAAC;QAED,KAAK,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;QAE/B,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACxC,CAAC;IACD,QAAQ,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAE3C,OAAO,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;AAC/B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../../../src/commands/proxy/proxy.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"proxy.d.ts","sourceRoot":"","sources":["../../../src/commands/proxy/proxy.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE1C;;GAEG;AACH,wBAAsB,YAAY,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CA6EvE"}
|
|
@@ -1,66 +1,62 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { buildCliContext, generateStreamableSessionId } from '../../commands/shared/cliContext.js';
|
|
2
|
+
import { resolveServeTarget } from '../../commands/shared/serveTargetResolver.js';
|
|
3
|
+
import { MCP_SERVER_VERSION } from '../../constants/mcp.js';
|
|
2
4
|
import logger from '../../logger/logger.js';
|
|
3
5
|
import { StdioProxyTransport } from '../../transport/stdioProxyTransport.js';
|
|
4
|
-
import { discoverServerWithPidFile, validateServer1mcpUrl } from '../../utils/validation/urlDetection.js';
|
|
5
6
|
/**
|
|
6
7
|
* Proxy command - Start STDIO proxy to running 1MCP HTTP server
|
|
7
8
|
*/
|
|
8
9
|
export async function proxyCommand(options) {
|
|
9
10
|
try {
|
|
10
|
-
|
|
11
|
-
const projectConfig = await loadProjectConfig();
|
|
12
|
-
// Merge configuration with priority: CLI options > .1mcprc > defaults
|
|
13
|
-
const preset = options.preset || projectConfig?.preset;
|
|
14
|
-
const filter = options.filter || projectConfig?.filter;
|
|
15
|
-
const tags = options.tags || normalizeTags(projectConfig?.tags);
|
|
11
|
+
const { cwd, projectConfig, projectRoot, mergedOptions, discoveredUrl, source } = await resolveServeTarget(options);
|
|
16
12
|
// Auto-discover server URL
|
|
17
13
|
logger.info('🔍 Discovering running 1MCP server...');
|
|
18
|
-
const { url: serverUrl, source } = await discoverServerWithPidFile(options['config-dir'], options.url);
|
|
19
14
|
// Log discovery source
|
|
20
15
|
switch (source) {
|
|
21
16
|
case 'user':
|
|
22
|
-
logger.info(`📍 Using user-provided URL: ${
|
|
17
|
+
logger.info(`📍 Using user-provided URL: ${discoveredUrl}`);
|
|
23
18
|
break;
|
|
24
19
|
case 'pidfile':
|
|
25
|
-
logger.info(`✅ Found server via PID file: ${
|
|
20
|
+
logger.info(`✅ Found server via PID file: ${discoveredUrl}`);
|
|
26
21
|
break;
|
|
27
22
|
case 'portscan':
|
|
28
|
-
logger.info(`✅ Found server via port scan: ${
|
|
23
|
+
logger.info(`✅ Found server via port scan: ${discoveredUrl}`);
|
|
29
24
|
break;
|
|
30
25
|
}
|
|
31
|
-
// Validate server connectivity
|
|
32
|
-
const validation = await validateServer1mcpUrl(serverUrl);
|
|
33
|
-
if (!validation.valid) {
|
|
34
|
-
logger.error(`❌ Cannot connect to 1MCP server: ${validation.error}`);
|
|
35
|
-
process.exit(1);
|
|
36
|
-
}
|
|
37
26
|
// Apply priority logic: preset > filter > tags (only one will be used)
|
|
38
27
|
let finalPreset;
|
|
39
28
|
let finalFilter;
|
|
40
29
|
let finalTags;
|
|
41
|
-
if (preset) {
|
|
42
|
-
finalPreset = preset;
|
|
43
|
-
logger.info(`📦 Using preset: ${preset}`);
|
|
30
|
+
if (mergedOptions.preset) {
|
|
31
|
+
finalPreset = mergedOptions.preset;
|
|
32
|
+
logger.info(`📦 Using preset: ${mergedOptions.preset}`);
|
|
44
33
|
}
|
|
45
|
-
else if (filter) {
|
|
46
|
-
finalFilter = filter;
|
|
47
|
-
logger.info(`🔍 Using filter: ${filter}`);
|
|
34
|
+
else if (mergedOptions.filter) {
|
|
35
|
+
finalFilter = mergedOptions.filter;
|
|
36
|
+
logger.info(`🔍 Using filter: ${mergedOptions.filter}`);
|
|
48
37
|
}
|
|
49
|
-
else if (tags && tags.length > 0) {
|
|
50
|
-
finalTags = tags;
|
|
51
|
-
logger.info(`🏷️ Using tags: ${tags.join(', ')}`);
|
|
38
|
+
else if (mergedOptions.tags && mergedOptions.tags.length > 0) {
|
|
39
|
+
finalTags = mergedOptions.tags;
|
|
40
|
+
logger.info(`🏷️ Using tags: ${mergedOptions.tags.join(', ')}`);
|
|
52
41
|
}
|
|
53
42
|
// Create and start proxy transport
|
|
54
43
|
logger.info('📡 Starting STDIO proxy...');
|
|
55
44
|
const proxyTransport = new StdioProxyTransport({
|
|
56
|
-
serverUrl,
|
|
45
|
+
serverUrl: discoveredUrl,
|
|
57
46
|
preset: finalPreset,
|
|
58
47
|
filter: finalFilter,
|
|
59
48
|
tags: finalTags,
|
|
60
|
-
|
|
49
|
+
context: buildCliContext({
|
|
50
|
+
cwd,
|
|
51
|
+
projectConfig,
|
|
52
|
+
projectRoot,
|
|
53
|
+
transportType: 'stdio-proxy',
|
|
54
|
+
version: MCP_SERVER_VERSION,
|
|
55
|
+
sessionId: generateStreamableSessionId(),
|
|
56
|
+
}),
|
|
61
57
|
});
|
|
62
58
|
await proxyTransport.start();
|
|
63
|
-
logger.info(`📡 STDIO proxy running, forwarding to ${
|
|
59
|
+
logger.info(`📡 STDIO proxy running, forwarding to ${discoveredUrl}`);
|
|
64
60
|
// Set up graceful shutdown
|
|
65
61
|
const shutdown = async () => {
|
|
66
62
|
logger.info('Shutting down STDIO proxy...');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../../../src/commands/proxy/proxy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../../../src/commands/proxy/proxy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,2BAA2B,EAAE,MAAM,oCAAoC,CAAC;AAClG,OAAO,EAAE,kBAAkB,EAAE,MAAM,6CAA6C,CAAC;AACjF,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,MAAM,MAAM,uBAAuB,CAAC;AAC3C,OAAO,EAAE,mBAAmB,EAAE,MAAM,uCAAuC,CAAC;AAI5E;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,OAAqB;IACtD,IAAI,CAAC;QACH,MAAM,EAAE,GAAG,EAAE,aAAa,EAAE,WAAW,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,EAAE,GAAG,MAAM,kBAAkB,CAAC,OAAO,CAAC,CAAC;QAEpH,2BAA2B;QAC3B,MAAM,CAAC,IAAI,CAAC,uCAAuC,CAAC,CAAC;QAErD,uBAAuB;QACvB,QAAQ,MAAM,EAAE,CAAC;YACf,KAAK,MAAM;gBACT,MAAM,CAAC,IAAI,CAAC,+BAA+B,aAAa,EAAE,CAAC,CAAC;gBAC5D,MAAM;YACR,KAAK,SAAS;gBACZ,MAAM,CAAC,IAAI,CAAC,gCAAgC,aAAa,EAAE,CAAC,CAAC;gBAC7D,MAAM;YACR,KAAK,UAAU;gBACb,MAAM,CAAC,IAAI,CAAC,iCAAiC,aAAa,EAAE,CAAC,CAAC;gBAC9D,MAAM;QACV,CAAC;QAED,uEAAuE;QACvE,IAAI,WAA+B,CAAC;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C,8BAA8B,CAAC,CAAC;YAC5C,MAAM,cAAc,CAAC,KAAK,EAAE,CAAC;YAC7B,MAAM,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAC;YAC7C,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC,CAAC;QAEF,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;QAC/B,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;QAChC,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IACjC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;YAC3B,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC9B,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,KAAK,CAAC,8BAA8B,EAAE,KAAK,CAAC,CAAC;QACtD,CAAC;QACD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/commands/run/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC;AAElC,wBAAgB,eAAe,CAAC,KAAK,EAAE,IAAI,GAAG,IAAI,CAkEjD"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { runCliCommand } from '../../commands/shared/commandRunner.js';
|
|
2
|
+
import { globalOptions } from '../../globalOptions.js';
|
|
3
|
+
export function setupRunCommand(yargs) {
|
|
4
|
+
return yargs.command('run <tool>', 'Run an MCP tool against a running 1MCP serve instance', (commandYargs) => commandYargs
|
|
5
|
+
.options(globalOptions || {})
|
|
6
|
+
.positional('tool', {
|
|
7
|
+
describe: 'Tool reference in the format <server>/<tool>',
|
|
8
|
+
type: 'string',
|
|
9
|
+
})
|
|
10
|
+
.option('url', {
|
|
11
|
+
alias: 'u',
|
|
12
|
+
describe: 'Override auto-detected 1MCP server URL',
|
|
13
|
+
type: 'string',
|
|
14
|
+
})
|
|
15
|
+
.option('preset', {
|
|
16
|
+
alias: 'p',
|
|
17
|
+
describe: 'Use a preset when calling the running 1MCP server',
|
|
18
|
+
type: 'string',
|
|
19
|
+
})
|
|
20
|
+
.option('tag-filter', {
|
|
21
|
+
alias: 'f',
|
|
22
|
+
describe: 'Apply an advanced tag filter expression',
|
|
23
|
+
type: 'string',
|
|
24
|
+
})
|
|
25
|
+
.option('tags', {
|
|
26
|
+
describe: 'Apply simple comma-separated tags',
|
|
27
|
+
type: 'array',
|
|
28
|
+
string: true,
|
|
29
|
+
})
|
|
30
|
+
.option('args', {
|
|
31
|
+
describe: 'Tool arguments as a JSON object',
|
|
32
|
+
type: 'string',
|
|
33
|
+
})
|
|
34
|
+
.option('format', {
|
|
35
|
+
describe: 'Output format',
|
|
36
|
+
type: 'string',
|
|
37
|
+
choices: ['toon', 'json', 'text', 'compact'],
|
|
38
|
+
})
|
|
39
|
+
.option('raw', {
|
|
40
|
+
describe: 'Alias for --format json',
|
|
41
|
+
type: 'boolean',
|
|
42
|
+
default: false,
|
|
43
|
+
})
|
|
44
|
+
.option('max-chars', {
|
|
45
|
+
describe: 'Maximum characters for compact output',
|
|
46
|
+
type: 'number',
|
|
47
|
+
default: 2000,
|
|
48
|
+
})
|
|
49
|
+
.example([
|
|
50
|
+
['$0 run filesystem/read_file --args \'{"path":"./foo.txt"}\'', 'Call a tool with explicit JSON arguments'],
|
|
51
|
+
['$0 run summarizer/summarize < README.md', 'Pipe raw stdin into the first required string argument'],
|
|
52
|
+
[
|
|
53
|
+
'$0 run --preset development validator/validate --args \'{"path":"./schema.json"}\'',
|
|
54
|
+
'Call through a preset',
|
|
55
|
+
],
|
|
56
|
+
])
|
|
57
|
+
.epilogue('This command requires a running `1mcp serve` instance. For pipe-friendly usage, stdout is reserved for tool output and errors are sent to stderr.'), async (argv) => {
|
|
58
|
+
const { runCommand } = await import('./run.js');
|
|
59
|
+
await runCliCommand(argv, runCommand);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/commands/run/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,uCAAuC,CAAC;AACtE,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAItD,MAAM,UAAU,eAAe,CAAC,KAAW;IACzC,OAAO,KAAK,CAAC,OAAO,CAClB,YAAY,EACZ,uDAAuD,EACvD,CAAC,YAAY,EAAE,EAAE,CACf,YAAY;SACT,OAAO,CAAC,aAAa,IAAI,EAAE,CAAC;SAC5B,UAAU,CAAC,MAAM,EAAE;QAClB,QAAQ,EAAE,8CAA8C;QACxD,IAAI,EAAE,QAAQ;KACf,CAAC;SACD,MAAM,CAAC,KAAK,EAAE;QACb,KAAK,EAAE,GAAG;QACV,QAAQ,EAAE,wCAAwC;QAClD,IAAI,EAAE,QAAQ;KACf,CAAC;SACD,MAAM,CAAC,QAAQ,EAAE;QAChB,KAAK,EAAE,GAAG;QACV,QAAQ,EAAE,mDAAmD;QAC7D,IAAI,EAAE,QAAQ;KACf,CAAC;SACD,MAAM,CAAC,YAAY,EAAE;QACpB,KAAK,EAAE,GAAG;QACV,QAAQ,EAAE,yCAAyC;QACnD,IAAI,EAAE,QAAQ;KACf,CAAC;SACD,MAAM,CAAC,MAAM,EAAE;QACd,QAAQ,EAAE,mCAAmC;QAC7C,IAAI,EAAE,OAAO;QACb,MAAM,EAAE,IAAI;KACb,CAAC;SACD,MAAM,CAAC,MAAM,EAAE;QACd,QAAQ,EAAE,iCAAiC;QAC3C,IAAI,EAAE,QAAQ;KACf,CAAC;SACD,MAAM,CAAC,QAAQ,EAAE;QAChB,QAAQ,EAAE,eAAe;QACzB,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,CAAC;KAC7C,CAAC;SACD,MAAM,CAAC,KAAK,EAAE;QACb,QAAQ,EAAE,yBAAyB;QACnC,IAAI,EAAE,SAAS;QACf,OAAO,EAAE,KAAK;KACf,CAAC;SACD,MAAM,CAAC,WAAW,EAAE;QACnB,QAAQ,EAAE,uCAAuC;QACjD,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE,IAAI;KACd,CAAC;SACD,OAAO,CAAC;QACP,CAAC,6DAA6D,EAAE,0CAA0C,CAAC;QAC3G,CAAC,yCAAyC,EAAE,wDAAwD,CAAC;QACrG;YACE,oFAAoF;YACpF,uBAAuB;SACxB;KACF,CAAC;SACD,QAAQ,CACP,mJAAmJ,CACpJ,EACL,KAAK,EAAE,IAAI,EAAE,EAAE;QACb,MAAM,EAAE,UAAU,EAAE,GAAG,MAAM,MAAM,CAAC,UAAU,CAAC,CAAC;QAChD,MAAM,aAAa,CAAC,IAAwC,EAAE,UAAU,CAAC,CAAC;IAC5E,CAAC,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type CallToolResult } from '@modelcontextprotocol/sdk/types.js';
|
|
2
|
+
import { type RunOutputFormat } from '../../commands/run/runUtils.js';
|
|
3
|
+
import { type JsonRpcResponse } from '../../commands/shared/serveClient.js';
|
|
4
|
+
import type { GlobalOptions } from '../../globalOptions.js';
|
|
5
|
+
import type { ContextData } from '../../types/context.js';
|
|
6
|
+
export interface RunCommandOptions extends GlobalOptions {
|
|
7
|
+
url?: string;
|
|
8
|
+
preset?: string;
|
|
9
|
+
filter?: string;
|
|
10
|
+
tags?: string[];
|
|
11
|
+
'tag-filter'?: string;
|
|
12
|
+
format?: RunOutputFormat;
|
|
13
|
+
raw?: boolean;
|
|
14
|
+
'max-chars'?: number;
|
|
15
|
+
args?: string;
|
|
16
|
+
tool: string;
|
|
17
|
+
}
|
|
18
|
+
export { buildServerUrl, deleteCliSessionCache, getCliSessionCachePath, getCliSessionContextHash, readCliSessionCache, SESSION_CACHE_TTL_MS, writeCliSessionCache, } from '../../commands/shared/serveClient.js';
|
|
19
|
+
export declare function runCommand(options: RunCommandOptions): Promise<void>;
|
|
20
|
+
export declare function invokeTool(options: {
|
|
21
|
+
serverUrl: URL;
|
|
22
|
+
sessionId?: string;
|
|
23
|
+
bearerToken?: string;
|
|
24
|
+
displayToolName: string;
|
|
25
|
+
qualifiedToolName: string;
|
|
26
|
+
explicitArgs?: string;
|
|
27
|
+
stdinText?: string;
|
|
28
|
+
resolveTool: boolean;
|
|
29
|
+
initializeContext?: ContextData;
|
|
30
|
+
}): Promise<{
|
|
31
|
+
rawResponse: JsonRpcResponse<CallToolResult>;
|
|
32
|
+
sessionId?: string;
|
|
33
|
+
retryWithFreshSession: boolean;
|
|
34
|
+
}>;
|
|
35
|
+
//# sourceMappingURL=run.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../../../src/commands/run/run.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,cAAc,EAAa,MAAM,oCAAoC,CAAC;AAGpF,OAAO,EAQL,KAAK,eAAe,EAErB,MAAM,+BAA+B,CAAC;AAIvC,OAAO,EAKL,KAAK,eAAe,EAIrB,MAAM,qCAAqC,CAAC;AAG7C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAGzD,MAAM,WAAW,iBAAkB,SAAQ,aAAa;IACtD,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,MAAM,CAAC,EAAE,eAAe,CAAC;IACzB,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;CACd;AAYD,OAAO,EACL,cAAc,EACd,qBAAqB,EACrB,sBAAsB,EACtB,wBAAwB,EACxB,mBAAmB,EACnB,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,qCAAqC,CAAC;AAE7C,wBAAsB,UAAU,CAAC,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC,CAsM1E;AA+CD,wBAAsB,UAAU,CAAC,OAAO,EAAE;IACxC,SAAS,EAAE,GAAG,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,eAAe,EAAE,MAAM,CAAC;IACxB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,OAAO,CAAC;IACrB,iBAAiB,CAAC,EAAE,WAAW,CAAC;CACjC,GAAG,OAAO,CAAC;IACV,WAAW,EAAE,eAAe,CAAC,cAAc,CAAC,CAAC;IAC7C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,qBAAqB,EAAE,OAAO,CAAC;CAChC,CAAC,CA+HD"}
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import { StreamableHTTPError } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
2
|
+
import { extractInspectToolInfo } from '../../commands/inspect/inspectUtils.js';
|
|
3
|
+
import { findToolByQualifiedName, formatToolCallOutput, parseExplicitArgs, parseJsonObject, parseToolReference, resolveToolArguments, RunCommandInputError, validateToolArgs, } from '../../commands/run/runUtils.js';
|
|
4
|
+
import { ApiClient } from '../../commands/shared/apiClient.js';
|
|
5
|
+
import { loadAuthProfile, normalizeServerUrl } from '../../commands/shared/authProfileStore.js';
|
|
6
|
+
import { buildCliContext } from '../../commands/shared/cliContext.js';
|
|
7
|
+
import { deleteCliSessionCache, getCliSessionCachePath, getCliSessionContextHash, readCliSessionCache, StreamableServeClient, writeCliSessionCache, } from '../../commands/shared/serveClient.js';
|
|
8
|
+
import { resolveServeTarget } from '../../commands/shared/serveTargetResolver.js';
|
|
9
|
+
import { API_INSPECT_ENDPOINT, API_TOOL_INVOCATIONS_ENDPOINT } from '../../constants/api.js';
|
|
10
|
+
import { stripMcpSuffix } from '../../utils/urlUtils.js';
|
|
11
|
+
export { buildServerUrl, deleteCliSessionCache, getCliSessionCachePath, getCliSessionContextHash, readCliSessionCache, SESSION_CACHE_TTL_MS, writeCliSessionCache, } from '../../commands/shared/serveClient.js';
|
|
12
|
+
export async function runCommand(options) {
|
|
13
|
+
const toolReference = parseToolReference(options.tool);
|
|
14
|
+
const format = options.raw ? 'json' : options.format || 'toon';
|
|
15
|
+
const maxChars = options['max-chars'] ?? 2000;
|
|
16
|
+
const [{ cwd, projectConfig, projectRoot, discoveredUrl, serverPid, serverUrl }, stdinText] = await Promise.all([
|
|
17
|
+
resolveServeTarget(options),
|
|
18
|
+
readStdin(),
|
|
19
|
+
]);
|
|
20
|
+
const runContext = buildCliContext({
|
|
21
|
+
cwd,
|
|
22
|
+
projectConfig,
|
|
23
|
+
projectRoot,
|
|
24
|
+
transportType: 'run',
|
|
25
|
+
version: 'run',
|
|
26
|
+
});
|
|
27
|
+
const contextHash = getCliSessionContextHash(runContext);
|
|
28
|
+
const cachePath = getCliSessionCachePath({
|
|
29
|
+
cachePathTemplate: options['cli-session-cache-path'],
|
|
30
|
+
serverPid,
|
|
31
|
+
serverUrl: serverUrl.toString(),
|
|
32
|
+
});
|
|
33
|
+
const cachedSession = await readCliSessionCache(cachePath, serverUrl.toString(), contextHash);
|
|
34
|
+
const baseUrl = stripMcpSuffix(discoveredUrl);
|
|
35
|
+
const authProfile = await loadAuthProfile(options['config-dir'], normalizeServerUrl(baseUrl));
|
|
36
|
+
const apiClient = new ApiClient({
|
|
37
|
+
baseUrl,
|
|
38
|
+
bearerToken: authProfile?.token,
|
|
39
|
+
sessionId: cachedSession?.sessionId,
|
|
40
|
+
context: runContext,
|
|
41
|
+
});
|
|
42
|
+
let cachedRestEndpointSupport = cachedSession?.hasRestEndpoint;
|
|
43
|
+
// REST-first path: skip MCP handshake when we know the server supports it,
|
|
44
|
+
// or probe once on first run. Fall back to MCP on missing/unsupported/upstream errors.
|
|
45
|
+
const canTryRest = cachedSession?.hasRestEndpoint !== false;
|
|
46
|
+
const needsSchemaForStdin = options.args === undefined && stdinText !== undefined;
|
|
47
|
+
const needsSchemaForValidation = options.args !== undefined;
|
|
48
|
+
if (canTryRest) {
|
|
49
|
+
const restArgs = options.args !== undefined
|
|
50
|
+
? parseExplicitArgs(options.args)
|
|
51
|
+
: stdinText !== undefined
|
|
52
|
+
? parseJsonObject(stdinText)
|
|
53
|
+
: {};
|
|
54
|
+
const toolInfo = (needsSchemaForStdin && restArgs === null) || needsSchemaForValidation
|
|
55
|
+
? await fetchToolInfoFromApi(apiClient, toolReference, options.tool)
|
|
56
|
+
: null;
|
|
57
|
+
if (restArgs !== null || toolInfo) {
|
|
58
|
+
const resolvedArguments = restArgs !== null
|
|
59
|
+
? restArgs
|
|
60
|
+
: resolveToolArguments({
|
|
61
|
+
stdinText,
|
|
62
|
+
tool: toTool(toolInfo),
|
|
63
|
+
}).arguments;
|
|
64
|
+
if (toolInfo) {
|
|
65
|
+
const validation = validateToolArgs(resolvedArguments, toolInfo.inputSchema, options.tool);
|
|
66
|
+
if (!validation.valid) {
|
|
67
|
+
process.stderr.write(`${validation.errorMessage}\n`);
|
|
68
|
+
process.exitCode = 1;
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Forward filter params (preset/tags/filter) as query params so server-side
|
|
73
|
+
// filtering applies on the REST path, same as the MCP path.
|
|
74
|
+
const restEndpoint = serverUrl.search
|
|
75
|
+
? `${API_TOOL_INVOCATIONS_ENDPOINT}${serverUrl.search}`
|
|
76
|
+
: API_TOOL_INVOCATIONS_ENDPOINT;
|
|
77
|
+
const apiResponse = await apiClient.post(restEndpoint, {
|
|
78
|
+
tool: options.tool,
|
|
79
|
+
args: resolvedArguments,
|
|
80
|
+
_meta: {
|
|
81
|
+
context: runContext,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
const shouldFallbackToMcp = shouldFallbackToMcpForRest(apiResponse.status, apiResponse.error);
|
|
85
|
+
const shouldPersistRestDisabled = shouldPersistRestSupportDisabled(apiResponse.status, apiResponse.error);
|
|
86
|
+
if (!shouldFallbackToMcp) {
|
|
87
|
+
await writeCliSessionCache(cachePath, {
|
|
88
|
+
sessionId: apiResponse.sessionId ?? cachedSession?.sessionId ?? 'rest',
|
|
89
|
+
serverUrl: serverUrl.toString(),
|
|
90
|
+
contextHash,
|
|
91
|
+
savedAt: Date.now(),
|
|
92
|
+
hasRestEndpoint: true,
|
|
93
|
+
});
|
|
94
|
+
cachedRestEndpointSupport = true;
|
|
95
|
+
let rawResponse;
|
|
96
|
+
if (apiResponse.ok && apiResponse.data) {
|
|
97
|
+
rawResponse = { jsonrpc: '2.0', id: 0, result: apiResponse.data.result };
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
rawResponse = {
|
|
101
|
+
jsonrpc: '2.0',
|
|
102
|
+
id: 0,
|
|
103
|
+
error: { code: -32000, message: apiResponse.error ?? `HTTP ${apiResponse.status}` },
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
const output = formatToolCallOutput(rawResponse, format, maxChars);
|
|
107
|
+
if ('error' in rawResponse) {
|
|
108
|
+
process.stderr.write(`${output}\n`);
|
|
109
|
+
process.exitCode = 1;
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (rawResponse.result.isError) {
|
|
113
|
+
process.stderr.write(`${output}\n`);
|
|
114
|
+
process.exitCode = 2;
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (output.length > 0) {
|
|
118
|
+
process.stdout.write(`${output}\n`);
|
|
119
|
+
}
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (shouldPersistRestDisabled) {
|
|
123
|
+
await writeCliSessionCache(cachePath, {
|
|
124
|
+
sessionId: cachedSession?.sessionId ?? 'mcp',
|
|
125
|
+
serverUrl: serverUrl.toString(),
|
|
126
|
+
contextHash,
|
|
127
|
+
savedAt: Date.now(),
|
|
128
|
+
hasRestEndpoint: false,
|
|
129
|
+
});
|
|
130
|
+
cachedRestEndpointSupport = false;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
let response = await invokeTool({
|
|
135
|
+
serverUrl,
|
|
136
|
+
sessionId: cachedSession?.sessionId,
|
|
137
|
+
bearerToken: authProfile?.token,
|
|
138
|
+
displayToolName: options.tool,
|
|
139
|
+
qualifiedToolName: toolReference.qualifiedName,
|
|
140
|
+
explicitArgs: options.args,
|
|
141
|
+
stdinText,
|
|
142
|
+
resolveTool: options.args === undefined,
|
|
143
|
+
initializeContext: runContext,
|
|
144
|
+
});
|
|
145
|
+
if (response.retryWithFreshSession) {
|
|
146
|
+
await deleteCliSessionCache(cachePath);
|
|
147
|
+
response = await invokeTool({
|
|
148
|
+
serverUrl,
|
|
149
|
+
bearerToken: authProfile?.token,
|
|
150
|
+
displayToolName: options.tool,
|
|
151
|
+
qualifiedToolName: toolReference.qualifiedName,
|
|
152
|
+
explicitArgs: options.args,
|
|
153
|
+
stdinText,
|
|
154
|
+
resolveTool: options.args === undefined,
|
|
155
|
+
initializeContext: runContext,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
if (response.sessionId) {
|
|
159
|
+
await writeCliSessionCache(cachePath, {
|
|
160
|
+
sessionId: response.sessionId,
|
|
161
|
+
serverUrl: serverUrl.toString(),
|
|
162
|
+
contextHash,
|
|
163
|
+
savedAt: Date.now(),
|
|
164
|
+
hasRestEndpoint: cachedRestEndpointSupport,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
const output = formatToolCallOutput(response.rawResponse, format, maxChars);
|
|
168
|
+
if ('error' in response.rawResponse) {
|
|
169
|
+
process.stderr.write(`${output}\n`);
|
|
170
|
+
process.exitCode = 1;
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
if (response.rawResponse.result.isError) {
|
|
174
|
+
process.stderr.write(`${output}\n`);
|
|
175
|
+
process.exitCode = 2;
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
if (output.length > 0) {
|
|
179
|
+
process.stdout.write(`${output}\n`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function isEndpointNotFoundResponse(status, error) {
|
|
183
|
+
return status === 404 && /^HTTP 404\b/u.test(error ?? '');
|
|
184
|
+
}
|
|
185
|
+
function shouldFallbackToMcpForRest(status, error) {
|
|
186
|
+
return status === 405 || status === 503 || status === 0 || isEndpointNotFoundResponse(status, error);
|
|
187
|
+
}
|
|
188
|
+
function shouldPersistRestSupportDisabled(status, error) {
|
|
189
|
+
return status === 405 || isEndpointNotFoundResponse(status, error);
|
|
190
|
+
}
|
|
191
|
+
async function fetchToolInfoFromApi(apiClient, toolReference, displayToolName) {
|
|
192
|
+
const apiResponse = await apiClient.get(API_INSPECT_ENDPOINT, {
|
|
193
|
+
target: displayToolName,
|
|
194
|
+
});
|
|
195
|
+
if (!apiResponse.ok || !apiResponse.data || apiResponse.data.kind !== 'tool') {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
return extractInspectToolInfo({
|
|
199
|
+
name: apiResponse.data.qualifiedName,
|
|
200
|
+
description: apiResponse.data.description,
|
|
201
|
+
inputSchema: apiResponse.data.inputSchema,
|
|
202
|
+
outputSchema: apiResponse.data.outputSchema,
|
|
203
|
+
}, toolReference);
|
|
204
|
+
}
|
|
205
|
+
function toTool(toolInfo) {
|
|
206
|
+
return {
|
|
207
|
+
name: toolInfo.qualifiedName,
|
|
208
|
+
description: toolInfo.description,
|
|
209
|
+
inputSchema: toolInfo.inputSchema,
|
|
210
|
+
outputSchema: toolInfo.outputSchema,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
export async function invokeTool(options) {
|
|
214
|
+
const client = new StreamableServeClient(options.serverUrl, options.sessionId, options.bearerToken);
|
|
215
|
+
await client.start();
|
|
216
|
+
try {
|
|
217
|
+
let tool;
|
|
218
|
+
if (!options.sessionId) {
|
|
219
|
+
const initializeResponse = await client.initialize(options.initializeContext);
|
|
220
|
+
if ('error' in initializeResponse) {
|
|
221
|
+
return {
|
|
222
|
+
rawResponse: initializeResponse,
|
|
223
|
+
sessionId: client.sessionId,
|
|
224
|
+
retryWithFreshSession: false,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (options.sessionId && !options.resolveTool) {
|
|
229
|
+
const toolsResponse = await client.listTools();
|
|
230
|
+
if ('error' in toolsResponse) {
|
|
231
|
+
return {
|
|
232
|
+
rawResponse: toolsResponse,
|
|
233
|
+
sessionId: client.sessionId,
|
|
234
|
+
retryWithFreshSession: false,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
tool = findToolByQualifiedName(toolsResponse.result.tools, options.qualifiedToolName);
|
|
238
|
+
if (!tool) {
|
|
239
|
+
return {
|
|
240
|
+
rawResponse: {
|
|
241
|
+
jsonrpc: '2.0',
|
|
242
|
+
id: 0,
|
|
243
|
+
error: {
|
|
244
|
+
code: -32004,
|
|
245
|
+
message: 'Cached session missing requested tool.',
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
retryWithFreshSession: true,
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (options.resolveTool) {
|
|
253
|
+
const toolsResponse = await client.listTools();
|
|
254
|
+
if ('error' in toolsResponse) {
|
|
255
|
+
return {
|
|
256
|
+
rawResponse: toolsResponse,
|
|
257
|
+
sessionId: client.sessionId,
|
|
258
|
+
retryWithFreshSession: false,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
tool = findToolByQualifiedName(toolsResponse.result.tools, options.qualifiedToolName);
|
|
262
|
+
if (!tool) {
|
|
263
|
+
if (options.sessionId) {
|
|
264
|
+
return {
|
|
265
|
+
rawResponse: {
|
|
266
|
+
jsonrpc: '2.0',
|
|
267
|
+
id: 0,
|
|
268
|
+
error: {
|
|
269
|
+
code: -32004,
|
|
270
|
+
message: 'Cached session missing requested tool.',
|
|
271
|
+
},
|
|
272
|
+
},
|
|
273
|
+
retryWithFreshSession: true,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
throw new RunCommandInputError(`Unknown tool: ${options.displayToolName}`);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
const resolvedArguments = resolveToolArguments({
|
|
280
|
+
explicitArgs: options.explicitArgs,
|
|
281
|
+
stdinText: options.stdinText,
|
|
282
|
+
tool,
|
|
283
|
+
});
|
|
284
|
+
if (tool) {
|
|
285
|
+
const validation = validateToolArgs(resolvedArguments.arguments, tool.inputSchema, options.displayToolName);
|
|
286
|
+
if (!validation.valid) {
|
|
287
|
+
return {
|
|
288
|
+
rawResponse: {
|
|
289
|
+
jsonrpc: '2.0',
|
|
290
|
+
id: 0,
|
|
291
|
+
error: { code: -32602, message: validation.errorMessage },
|
|
292
|
+
},
|
|
293
|
+
sessionId: client.sessionId,
|
|
294
|
+
retryWithFreshSession: false,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
const response = await client.callTool(options.qualifiedToolName, resolvedArguments.arguments);
|
|
299
|
+
return {
|
|
300
|
+
rawResponse: response,
|
|
301
|
+
sessionId: client.sessionId,
|
|
302
|
+
retryWithFreshSession: false,
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
catch (error) {
|
|
306
|
+
if (error instanceof StreamableHTTPError && error.code === 404 && options.sessionId) {
|
|
307
|
+
return {
|
|
308
|
+
rawResponse: {
|
|
309
|
+
jsonrpc: '2.0',
|
|
310
|
+
id: 0,
|
|
311
|
+
error: {
|
|
312
|
+
code: -32004,
|
|
313
|
+
message: 'Cached session expired.',
|
|
314
|
+
},
|
|
315
|
+
},
|
|
316
|
+
retryWithFreshSession: true,
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
throw error;
|
|
320
|
+
}
|
|
321
|
+
finally {
|
|
322
|
+
try {
|
|
323
|
+
await client.close();
|
|
324
|
+
}
|
|
325
|
+
catch {
|
|
326
|
+
// Best-effort cleanup for CLI run sessions.
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
async function readStdin() {
|
|
331
|
+
if (process.stdin.isTTY) {
|
|
332
|
+
return undefined;
|
|
333
|
+
}
|
|
334
|
+
const chunks = [];
|
|
335
|
+
for await (const chunk of process.stdin) {
|
|
336
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)));
|
|
337
|
+
}
|
|
338
|
+
return Buffer.concat(chunks).toString('utf8');
|
|
339
|
+
}
|
|
340
|
+
//# sourceMappingURL=run.js.map
|