mobile-mcp-server 1.0.3 → 1.0.5
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/package.json +1 -1
- package/proxy_client.js +41 -3
package/package.json
CHANGED
package/proxy_client.js
CHANGED
|
@@ -18,6 +18,24 @@ async function main() {
|
|
|
18
18
|
if (!line.trim()) return;
|
|
19
19
|
try {
|
|
20
20
|
const request = JSON.parse(line);
|
|
21
|
+
|
|
22
|
+
// 🛡️ JSON-RPC Spec: If ID is missing, it's a notification.
|
|
23
|
+
// We should forward it but NEVER respond to it.
|
|
24
|
+
if (request.id === undefined) {
|
|
25
|
+
// Forward notification silently
|
|
26
|
+
fetch(SERVER_URL, {
|
|
27
|
+
method: "POST",
|
|
28
|
+
headers: {
|
|
29
|
+
"Content-Type": "application/json",
|
|
30
|
+
"Accept": "application/json, text/event-stream",
|
|
31
|
+
"x-mcp-key": API_KEY
|
|
32
|
+
},
|
|
33
|
+
body: JSON.stringify(request)
|
|
34
|
+
}).catch(() => { }); // Fire and forget
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// It's a request, use the robust forwarder
|
|
21
39
|
await forwardRequest(request);
|
|
22
40
|
} catch (e) {
|
|
23
41
|
process.stderr.write(`[Proxy Error] Malformed JSON from Stdio: ${e.message}\n`);
|
|
@@ -70,7 +88,26 @@ async function forwardRequest(request) {
|
|
|
70
88
|
throw new Error(errorMessage);
|
|
71
89
|
}
|
|
72
90
|
|
|
73
|
-
const
|
|
91
|
+
const text = await response.text();
|
|
92
|
+
let jsonResponse;
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
// 1. Try to parse as raw JSON first
|
|
96
|
+
jsonResponse = JSON.parse(text);
|
|
97
|
+
} catch (e) {
|
|
98
|
+
// 2. If it fails, check if it's an SSE "event: message" stream
|
|
99
|
+
const match = text.match(/data: (\{.*\})/);
|
|
100
|
+
if (match) {
|
|
101
|
+
try {
|
|
102
|
+
jsonResponse = JSON.parse(match[1]);
|
|
103
|
+
} catch (innerE) {
|
|
104
|
+
throw new Error(`Invalid JSON inside SSE data: ${innerE.message}`);
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
// Not JSON and not SSE - throw the original parse error
|
|
108
|
+
throw new Error(`${e.message} (Raw: ${text.substring(0, 100)})`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
74
111
|
|
|
75
112
|
// 🛡️ CRITICAL: Ensure we return a spec-compliant ID
|
|
76
113
|
// If the server returned id: null but we have a valid request ID, fix it before sending to Claude
|
|
@@ -83,8 +120,9 @@ async function forwardRequest(request) {
|
|
|
83
120
|
} catch (error) {
|
|
84
121
|
process.stderr.write(`[Proxy Error] ${error.message}\n`);
|
|
85
122
|
|
|
86
|
-
//
|
|
87
|
-
if
|
|
123
|
+
// 🛡️ CRITICAL: Never respond to a Notification (no ID)
|
|
124
|
+
// Only respond if we have a requestId (even 0 is valid)
|
|
125
|
+
if (requestId !== null) {
|
|
88
126
|
process.stdout.write(JSON.stringify({
|
|
89
127
|
jsonrpc: "2.0",
|
|
90
128
|
error: {
|