diff --git a/README.md b/README.md index c1f48f5..7157339 100644 --- a/README.md +++ b/README.md @@ -218,7 +218,7 @@ mcps call [options] [arguments...] - ``: Name of the configured MCP server - ``: Name of the tool to call -- `[options]`: Optional flags (`--raw`, `--json`) +- `[options]`: Optional flags (`--raw`, `--json`, `--timeout`) - `[arguments...]`: Arguments passed as `key=value` pairs **Options:** @@ -227,6 +227,7 @@ mcps call [options] [arguments...] |--------|-------------| | `-r, --raw` | Treat all values as raw strings (disable JSON parsing) | | `-j, --json ` | Load parameters from JSON string or file | +| `-t, --timeout ` | Request timeout in seconds (default: 300) | **Default Mode (Auto JSON Parsing):** @@ -262,6 +263,14 @@ mcps call alibaba-dms createDataChangeOrder --raw \ # Sends: { "database_id": "36005357", "script": "...", "logic": "true" } ``` +**--timeout Mode (Custom Timeout):** + +Use `--timeout` to override the default 300-second (5-minute) request timeout for long-running tools: +```bash +# Set a 3-minute timeout for a slow tool +mcps call my-server download_label order_id="123" --timeout 180 +``` + **--json Mode (Complex Parameters):** For complex parameters, use `--json` to load from a JSON string or file: diff --git a/README.zh.md b/README.zh.md index 8a43b75..5159931 100644 --- a/README.zh.md +++ b/README.zh.md @@ -219,7 +219,7 @@ mcps call [options] [arguments...] - ``: 已配置的 MCP 服务名称 - ``: 要调用的工具名称 -- `[options]`: 可选参数(`--raw`, `--json`) +- `[options]`: 可选参数(`--raw`, `--json`, `--timeout`) - `[arguments...]`: 以 `key=value` 形式传递的参数 **选项:** @@ -228,6 +228,7 @@ mcps call [options] [arguments...] |------|------| | `-r, --raw` | 将所有值作为原始字符串处理(禁用 JSON 解析) | | `-j, --json ` | 从 JSON 字符串或文件加载参数 | +| `-t, --timeout ` | 请求超时时间(秒),默认 300(5分钟) | **默认模式(自动 JSON 解析):** @@ -263,6 +264,14 @@ mcps call alibaba-dms createDataChangeOrder --raw \ # 实际发送: { "database_id": "36005357", "script": "...", "logic": "true" } ``` +**--timeout 模式(自定义超时):** + +使用 `--timeout` 覆盖默认的 300 秒(5分钟)请求超时,适用于长时间运行的工具: +```bash +# 设 3 分钟超时 +mcps call my-server download_label order_id="123" --timeout 180 +``` + **--json 模式(复杂参数):** 对于复杂参数,使用 `--json` 从 JSON 字符串或文件加载: diff --git a/package-lock.json b/package-lock.json index b26e559..870d424 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@maplezzk/mcps", - "version": "1.6.7", + "version": "1.7.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@maplezzk/mcps", - "version": "1.6.7", + "version": "1.7.0", "license": "ISC", "dependencies": { "@modelcontextprotocol/sdk": "^1.25.3", diff --git a/package.json b/package.json index a02b148..511e20f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@maplezzk/mcps", - "version": "1.6.7", + "version": "1.7.0", "description": "A CLI to manage and use MCP servers", "publishConfig": { "access": "public" diff --git a/src/commands/call.ts b/src/commands/call.ts index d29ff4c..11e73ce 100644 --- a/src/commands/call.ts +++ b/src/commands/call.ts @@ -76,6 +76,7 @@ export const registerCallCommand = (program: Command) => { .description('Call a tool on a server. Arguments format: key=value') .option('-r, --raw', 'Treat all values as raw strings (no JSON parsing)') .option('-j, --json ', 'Load parameters from a JSON file') + .option('-t, --timeout ', 'Request timeout in seconds (default: 60)') .addHelpText('after', ` Examples: $ mcps call my-server echo message="Hello World" @@ -89,11 +90,15 @@ Examples: # Use --json to load parameters from a file $ mcps call my-server createUser --json params.json + # Use --timeout to set custom request timeout (in seconds) + $ mcps call my-server download_label order_id="123" --timeout 180 + Notes: - Arguments are parsed as key=value pairs. - By default, values are automatically parsed as JSON if possible (numbers, booleans, objects). - Use --raw to disable JSON parsing and treat all values as strings. - Use --json to load parameters from a JSON file or JSON string. + - Use --timeout to override the default 60s request timeout. - For strings with spaces, wrap the value in quotes (e.g., msg="hello world"). `) .action(async (serverName, toolName, args, options) => { @@ -122,8 +127,11 @@ Notes: // Auto-start daemon if needed await DaemonClient.ensureDaemon(); + // Parse timeout option (convert seconds to ms for SDK, default 5min = 300s) + const timeout = (options.timeout ? parseInt(options.timeout as string, 10) : 300) * 1000; + // Execute via daemon - const result = await DaemonClient.executeTool(serverName, toolName, params); + const result = await DaemonClient.executeTool(serverName, toolName, params, timeout); console.log(chalk.green('Tool execution successful:')); printResult(result); diff --git a/src/commands/daemon.ts b/src/commands/daemon.ts index 13fdb56..f594816 100644 --- a/src/commands/daemon.ts +++ b/src/commands/daemon.ts @@ -600,7 +600,7 @@ const startDaemon = (port: number) => { req.on('data', chunk => { body += chunk.toString(); }); req.on('end', async () => { try { - const { server: serverName, tool, args } = JSON.parse(body); + const { server: serverName, tool, args, timeout } = JSON.parse(body); if (!serverName || !tool) { res.writeHead(400, { 'Content-Type': 'application/json' }); @@ -609,7 +609,7 @@ const startDaemon = (port: number) => { } const client = await connectionPool.getClient(serverName); - const result = await client.callTool(tool, args || {}); + const result = await client.callTool(tool, args || {}, timeout); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ result })); diff --git a/src/core/client.ts b/src/core/client.ts index da14516..80420da 100644 --- a/src/core/client.ts +++ b/src/core/client.ts @@ -241,16 +241,17 @@ export class McpClientService { return this.client.listTools(); } - async callTool(toolName: string, args: any) { + async callTool(toolName: string, args: any, timeout?: number) { if (!this.client) throw new Error('Client not connected'); // 记录工具调用请求 log(`[Tool Request] Server: ${this.serverName}, Tool: ${toolName}, Args: ${JSON.stringify(args)}`); + const options = timeout !== undefined ? { timeout } : undefined; const result = await this.client.callTool({ name: toolName, arguments: args, - }); + }, undefined, options); // 记录工具调用响应 log(`[Tool Response] Server: ${this.serverName}, Tool: ${toolName}, Result: ${JSON.stringify(result)}`); diff --git a/src/core/daemon-client.ts b/src/core/daemon-client.ts index 98b5f35..faa21fc 100644 --- a/src/core/daemon-client.ts +++ b/src/core/daemon-client.ts @@ -48,11 +48,11 @@ export class DaemonClient { await this.startDaemon(timeout); } - static async executeTool(serverName: string, toolName: string, args: any) { + static async executeTool(serverName: string, toolName: string, args: any, timeout?: number) { const response = await fetch(`${DAEMON_BASE_URL}/call`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ server: serverName, tool: toolName, args }), + body: JSON.stringify({ server: serverName, tool: toolName, args, timeout }), }); if (!response.ok) {