Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ mcps call <server_name> <tool_name> [options] [arguments...]

- `<server_name>`: Name of the configured MCP server
- `<tool_name>`: 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:**
Expand All @@ -227,6 +227,7 @@ mcps call <server_name> <tool_name> [options] [arguments...]
|--------|-------------|
| `-r, --raw` | Treat all values as raw strings (disable JSON parsing) |
| `-j, --json <value>` | Load parameters from JSON string or file |
| `-t, --timeout <seconds>` | Request timeout in seconds (default: 300) |

**Default Mode (Auto JSON Parsing):**

Expand Down Expand Up @@ -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:
Expand Down
11 changes: 10 additions & 1 deletion README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ mcps call <server_name> <tool_name> [options] [arguments...]

- `<server_name>`: 已配置的 MCP 服务名称
- `<tool_name>`: 要调用的工具名称
- `[options]`: 可选参数(`--raw`, `--json`)
- `[options]`: 可选参数(`--raw`, `--json`, `--timeout`)
- `[arguments...]`: 以 `key=value` 形式传递的参数

**选项:**
Expand All @@ -228,6 +228,7 @@ mcps call <server_name> <tool_name> [options] [arguments...]
|------|------|
| `-r, --raw` | 将所有值作为原始字符串处理(禁用 JSON 解析) |
| `-j, --json <value>` | 从 JSON 字符串或文件加载参数 |
| `-t, --timeout <seconds>` | 请求超时时间(秒),默认 300(5分钟) |

**默认模式(自动 JSON 解析):**

Expand Down Expand Up @@ -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 字符串或文件加载:
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
10 changes: 9 additions & 1 deletion src/commands/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <file>', 'Load parameters from a JSON file')
.option('-t, --timeout <seconds>', 'Request timeout in seconds (default: 60)')
.addHelpText('after', `
Examples:
$ mcps call my-server echo message="Hello World"
Expand All @@ -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) => {
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/commands/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand All @@ -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 }));
Expand Down
5 changes: 3 additions & 2 deletions src/core/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`);
Expand Down
4 changes: 2 additions & 2 deletions src/core/daemon-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading