curl-rpc 1.0.0

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.
Files changed (3) hide show
  1. package/README.md +54 -0
  2. package/bin/curl-rpc +161 -0
  3. package/package.json +30 -0
package/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # curl-rpc
2
+
3
+ Lightweight MCP RPC CLI tool with YAML/JSON support for electron-mcp.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install -g curl-rpc
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Simple syntax
14
+ ```bash
15
+ curl-rpc ping
16
+ curl-rpc open_window url=https://google.com
17
+ curl-rpc get_window_info win_id=1
18
+ ```
19
+
20
+ ### YAML format
21
+ ```bash
22
+ curl-rpc "
23
+ name: open_window
24
+ arguments:
25
+ url: https://google.com
26
+ reuseWindow: false
27
+ "
28
+ ```
29
+
30
+ ### JSON format
31
+ ```bash
32
+ curl-rpc --json '{"name":"ping","arguments":{}}'
33
+ ```
34
+
35
+ ## Configuration
36
+
37
+ Set the MCP server URL (default: http://localhost:8101):
38
+ ```bash
39
+ export ELECTRON_MCP_URL=http://your-server:8101
40
+ ```
41
+
42
+ Create token file:
43
+ ```bash
44
+ echo "your-token" > ~/electron-mcp-token.txt
45
+ ```
46
+
47
+ ## Requirements
48
+
49
+ - Node.js >= 14
50
+ - `yq` for YAML support: `pip install yq`
51
+
52
+ ## Documentation
53
+
54
+ Full documentation: https://github.com/cicy-dev/electron-mcp
package/bin/curl-rpc ADDED
@@ -0,0 +1,161 @@
1
+ #!/bin/bash
2
+ # curl-rpc - 轻量级 MCP RPC 调用工具,默认 YAML 格式
3
+
4
+ set -o pipefail
5
+
6
+ # 默认配置
7
+ ELECTRON_MCP_URL="${ELECTRON_MCP_URL:-http://localhost:8101}"
8
+
9
+ # 检查参数
10
+ if [ -z "$1" ]; then
11
+ echo "❌ Error: Missing arguments"
12
+ echo "Usage:"
13
+ echo " curl-rpc <tool_name> [key=value ...] # 简化调用"
14
+ echo " curl-rpc <method> [--json|-j] <params> # 完整调用"
15
+ echo ""
16
+ echo "Examples:"
17
+ echo " curl-rpc ping"
18
+ echo " curl-rpc open_window url=https://google.com"
19
+ echo " curl-rpc 'tools/call' 'name: ping'"
20
+ exit 1
21
+ fi
22
+
23
+ USE_JSON=0
24
+
25
+ # 检查是否是完整 method 格式 (xxx/yyy)
26
+ if [[ "$1" =~ ^[a-z_]+/[a-z_]+ ]]; then
27
+ METHOD="$1"
28
+ shift
29
+ if [ "$1" = "--json" ] || [ "$1" = "-j" ]; then
30
+ USE_JSON=1
31
+ shift
32
+ PARAMS="$@"
33
+ else
34
+ PARAMS_INPUT="$@"
35
+ fi
36
+ # 简化格式:工具名 + key=value 参数
37
+ else
38
+ TOOL_NAME="$1"
39
+ shift
40
+ METHOD="tools/call"
41
+
42
+ # 构建 YAML 参数
43
+ YAML_PARAMS="name: $TOOL_NAME"
44
+
45
+ # 解析 key=value 参数
46
+ if [ $# -gt 0 ]; then
47
+ YAML_PARAMS="${YAML_PARAMS}\narguments:"
48
+ for arg in "$@"; do
49
+ if [[ "$arg" =~ ^([a-zA-Z_][a-zA-Z0-9_]*)=(.+)$ ]]; then
50
+ key="${BASH_REMATCH[1]}"
51
+ value="${BASH_REMATCH[2]}"
52
+ YAML_PARAMS="${YAML_PARAMS}\n ${key}: ${value}"
53
+ else
54
+ echo "❌ Error: Invalid argument format: $arg"
55
+ echo "Expected: key=value"
56
+ exit 1
57
+ fi
58
+ done
59
+ fi
60
+
61
+ PARAMS_INPUT=$(echo -e "$YAML_PARAMS")
62
+ fi
63
+
64
+ # 检查 token 文件
65
+ if [ ! -f ~/electron-mcp-token.txt ]; then
66
+ echo "❌ Error: ~/electron-mcp-token.txt not found"
67
+ echo "Please contact admin to set token"
68
+ exit 1
69
+ fi
70
+
71
+ TOKEN=$(cat ~/electron-mcp-token.txt)
72
+
73
+ if [ -z "$TOKEN" ]; then
74
+ echo "❌ Error: Token is empty in ~/electron-mcp-token.txt"
75
+ echo "Please contact admin to set token"
76
+ exit 1
77
+ fi
78
+
79
+ # 默认 YAML,转换为 JSON
80
+ if [ "$USE_JSON" = "0" ]; then
81
+ # 检查是否安装了 yq
82
+ if ! command -v yq &> /dev/null; then
83
+ echo "❌ Error: yq not found. Install with: pip install yq --break-system-packages"
84
+ exit 1
85
+ fi
86
+
87
+ # 转换 YAML 到 JSON,捕获错误
88
+ PARAMS=$(echo "$PARAMS_INPUT" | yq -c . 2>&1)
89
+ if [ $? -ne 0 ]; then
90
+ echo "❌ Error: Invalid YAML format"
91
+ echo "$PARAMS"
92
+ exit 1
93
+ fi
94
+ ACCEPT_HEADER="application/yaml"
95
+ else
96
+ # 验证 JSON 格式
97
+ if ! echo "$PARAMS" | jq empty 2>/dev/null; then
98
+ echo "❌ Error: Invalid JSON format"
99
+ echo "$PARAMS"
100
+ exit 1
101
+ fi
102
+ ACCEPT_HEADER="application/json"
103
+ fi
104
+
105
+ # 检查服务是否运行
106
+ if ! curl -s --connect-timeout 2 $ELECTRON_MCP_URL/rpc/tools/list \
107
+ -H "Authorization: Bearer $TOKEN" > /dev/null 2>&1; then
108
+ echo "❌ Error: Cannot connect to MCP server at $ELECTRON_MCP_URL"
109
+ echo "Please check if the server is running: ./service.sh status"
110
+ exit 1
111
+ fi
112
+
113
+ # 执行请求
114
+ RESPONSE=$(curl -s -w "\n%{http_code}" -X POST $ELECTRON_MCP_URL/rpc/$METHOD \
115
+ -H "Content-Type: application/json" \
116
+ -H "Accept: $ACCEPT_HEADER" \
117
+ -H "Authorization: Bearer $TOKEN" \
118
+ -d "$PARAMS" 2>&1)
119
+
120
+ if [ $? -ne 0 ]; then
121
+ echo "❌ Error: curl command failed"
122
+ echo "$RESPONSE"
123
+ exit 1
124
+ fi
125
+
126
+ # 分离响应体和状态码
127
+ HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
128
+ BODY=$(echo "$RESPONSE" | sed '$d')
129
+
130
+ # 检查 HTTP 状态码
131
+ if [ "$HTTP_CODE" != "200" ]; then
132
+ echo "❌ Error: HTTP $HTTP_CODE"
133
+ echo "$BODY"
134
+ exit 1
135
+ fi
136
+
137
+ # 检查是否有错误
138
+ if echo "$BODY" | jq -e '.error' > /dev/null 2>&1; then
139
+ echo "❌ Error from server:"
140
+ echo "$BODY" | jq -r '.error'
141
+ exit 1
142
+ fi
143
+
144
+ # 输出结果
145
+ echo "----------------"
146
+ if [ "$USE_JSON" = "0" ]; then
147
+ echo "$BODY" | yq -r '.result.content[] | if .type == "text" then .text elif .type == "image" then "Image (\(.mimeType)): \(.data[:100])..." else . end' 2>&1
148
+ if [ $? -ne 0 ]; then
149
+ echo "❌ Error: Failed to parse response"
150
+ echo "$BODY"
151
+ exit 1
152
+ fi
153
+ else
154
+ echo "$BODY" | jq -r '.result.content[] | if .type == "text" then .text elif .type == "image" then "Image (\(.mimeType)): \(.data[:100])..." else . end' 2>&1
155
+ if [ $? -ne 0 ]; then
156
+ echo "❌ Error: Failed to parse response"
157
+ echo "$BODY"
158
+ exit 1
159
+ fi
160
+ fi
161
+ echo "----------------"
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "curl-rpc",
3
+ "version": "1.0.0",
4
+ "description": "Lightweight MCP RPC CLI tool with YAML/JSON support",
5
+ "bin": {
6
+ "curl-rpc": "./bin/curl-rpc"
7
+ },
8
+ "scripts": {
9
+ "test": "echo \"No tests yet\""
10
+ },
11
+ "keywords": [
12
+ "mcp",
13
+ "rpc",
14
+ "cli",
15
+ "yaml",
16
+ "electron-mcp"
17
+ ],
18
+ "author": "cicybot",
19
+ "license": "MIT",
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/cicy-dev/electron-mcp.git",
23
+ "directory": "packages/curl-rpc"
24
+ },
25
+ "homepage": "https://github.com/cicy-dev/electron-mcp#readme",
26
+ "engines": {
27
+ "node": ">=14.0.0"
28
+ },
29
+ "preferGlobal": true
30
+ }