Mobile MCP: let AI agents drive your iOS and Android apps. MCP server for simulators, emulators and real devices. Works with Claude Code, Codex, Cursor and any MCP client.
TypeScript agentandroidandroid-automationappium-alternativeclaude-codecodexcursoremulatoriosios-automationmcpmcp-servermobilemobile-automationmobile-testingreal-devicesimulatorskills
fix: address CodeRabbit review on streamable http
gmegidish5d16416c24f4
diff --git a/README.ja.md b/README.ja.mdindex b6880d9..bd042bb 100644--- a/README.ja.md+++ b/README.ja.md@@ -401,7 +401,7 @@ npx @mobilenext/mobile-mcp@latest --listen 3000npx @mobilenext/mobile-mcp@latest --listen 0.0.0.0:3000```-その後、MCP クライアントが `http://<host>:3000/mcp`(TLS 配下なら `https://…/mcp`)に接続するよう設定します。エンドポイントは Streamable HTTP(`/mcp` への `POST`/`GET`/`DELETE`)を受け付けます。リモートモードは **ステートレス** です(セッション親和性不要)。+その後、MCP クライアントが `http://<host>:3000/mcp`(TLS 配下なら `https://…/mcp`)に接続するよう設定します。エンドポイントは Streamable HTTP(`/mcp` への `POST`)を受け付けます。リモートモードは **ステートレス** です(セッション親和性不要)。> **移行メモ:** 以前の `--listen` は非推奨の HTTP+SSE を `/mcp` で提供していました。クライアントは `http(s)://host:port/mcp` に対して Streamable HTTP を使う必要があります。 [#98](https://github.com/mobile-next/mobile-mcp/issues/98)diff --git a/README.md b/README.mdindex a0f0117..9dbec40 100644--- a/README.md+++ b/README.md@@ -402,7 +402,7 @@ This binds to `localhost:3000`. To bind to a specific interface:npx @mobilenext/mobile-mcp@latest --listen 0.0.0.0:3000```-Then configure your MCP client to connect to `http://<host>:3000/mcp` (or `https://…/mcp` behind TLS). The endpoint accepts Streamable HTTP (`POST`/`GET`/`DELETE` on `/mcp`); remote mode is **stateless** (no session affinity required), which works well with Smithery and other horizontal hosts.+Then configure your MCP client to connect to `http://<host>:3000/mcp` (or `https://…/mcp` behind TLS). The endpoint accepts Streamable HTTP (`POST` on `/mcp`); remote mode is **stateless** (no session affinity required), which works well with Smithery and other horizontal hosts.> **Migration note:** `--listen` previously served the deprecated HTTP+SSE transport on `/mcp`. Clients must use Streamable HTTP against `http(s)://host:port/mcp`. The old pure-SSE flow on `/mcp` is no longer available. See [#98](https://github.com/mobile-next/mobile-mcp/issues/98).diff --git a/README.zh-CN.md b/README.zh-CN.mdindex dc24d71..63bd6e5 100644--- a/README.zh-CN.md+++ b/README.zh-CN.md@@ -401,7 +401,7 @@ npx @mobilenext/mobile-mcp@latest --listen 3000npx @mobilenext/mobile-mcp@latest --listen 0.0.0.0:3000```-然后将你的 MCP 客户端配置为连接 `http://<host>:3000/mcp`(TLS 后可用 `https://…/mcp`)。该端点接受 Streamable HTTP(对 `/mcp` 的 `POST`/`GET`/`DELETE`);远程模式为 **无状态**(无需会话亲和性)。+然后将你的 MCP 客户端配置为连接 `http://<host>:3000/mcp`(TLS 后可用 `https://…/mcp`)。该端点接受 Streamable HTTP(对 `/mcp` 的 `POST`);远程模式为 **无状态**(无需会话亲和性)。> **迁移说明:** 此前 `--listen` 在 `/mcp` 上提供已弃用的 HTTP+SSE。客户端需对 `http(s)://host:port/mcp` 使用 Streamable HTTP。见 [#98](https://github.com/mobile-next/mobile-mcp/issues/98)。diff --git a/scripts/verify-streamable-http.mjs b/scripts/verify-streamable-http.mjsindex 34cc9e3..6530c7d 100755--- a/scripts/verify-streamable-http.mjs+++ b/scripts/verify-streamable-http.mjs@@ -85,10 +85,14 @@ async function main() {console.error("--- server log ---\n" + serverLog);process.exitCode = 1;} finally {- child.kill("SIGTERM");- await new Promise(r => setTimeout(r, 300));- if (!child.killed) {- child.kill("SIGKILL");+ // child.killed only means a signal was sent; wait for the actual exit.+ if (child.exitCode === null && child.signalCode === null) {+ const exited = new Promise(r => child.once("exit", () => r(true)));+ child.kill("SIGTERM");+ const didExit = await Promise.race([exited, new Promise(r => setTimeout(() => r(false), 3000))]);+ if (!didExit) {+ child.kill("SIGKILL");+ }}}}diff --git a/src/index.ts b/src/index.tsindex 2a5e44a..3ce7bc9 100644--- a/src/index.ts+++ b/src/index.ts@@ -49,12 +49,13 @@ const startHttpServer = async (host: string, port: number) => {const transport = new StreamableHTTPServerTransport({sessionIdGenerator: undefined,});- await server.connect(transport);- await transport.handleRequest(req, res, req.body);+ // Register cleanup first: the response can close before handleRequest resolves.res.on("close", () => {transport.close();server.close();});+ await server.connect(transport);+ await transport.handleRequest(req, res, req.body);} catch (err: unknown) {error("Error handling MCP request: " + (err instanceof Error ? err.stack : String(err)));if (!res.headersSent) {diff --git a/test/validate-response.js b/test/validate-response.jsindex 1ebffe6..8fa13a6 100644--- a/test/validate-response.js+++ b/test/validate-response.js@@ -2,24 +2,66 @@const fs = require("node:fs");-const raw = fs.readFileSync(0, "utf8").trim();+// Returns the index of the "}" closing the object that opens at start, or -1.+// Braces inside JSON strings are ignored.+const findMatchingBrace = (text, start) => {+ let depth = 0;+ let inString = false;+ for (let i = start; i < text.length; i++) {+ const ch = text[i];+ if (inString) {+ if (ch === "\\") {+ i++;+ } else if (ch === "\"") {+ inString = false;+ }-console.log(raw);+ continue;+ }++ if (ch === "\"") {+ inString = true;+ } else if (ch === "{") {+ depth++;+ } else if (ch === "}") {+ depth--;+ if (depth === 0) {+ return i;+ }+ }+ }++ return -1;+};++// Agents occasionally wrap the verdict in prose or a code fence despite being told+// to emit JSON only. Keep the last balanced {...} that parses.+const findLastJsonObject = text => {+ let found = null;+ for (let start = text.indexOf("{"); start !== -1; start = text.indexOf("{", start + 1)) {+ const end = findMatchingBrace(text, start);+ if (end === -1) {+ continue;+ }-const fenced = raw.match(/```(?:json)?\s*\n([\s\S]*?)\n```/);-const unfenced = fenced ? fenced[1].trim() : raw;+ try {+ found = JSON.parse(text.slice(start, end + 1));+ start = end;+ } catch {+ // not json, keep scanning+ }+ }-// Agents occasionally prepend a sentence despite being told to emit JSON only.-// Take the outermost {...} rather than failing the whole run over a preamble.-const start = unfenced.indexOf("{");-const end = unfenced.lastIndexOf("}");-const json = (start !== -1 && end > start) ? unfenced.slice(start, end + 1) : unfenced;+ return found;+};++const raw = fs.readFileSync(0, "utf8").trim();++console.log(raw);-let response;-try {- response = JSON.parse(json);-} catch (error) {- console.error(`Response is not valid JSON: ${error.message}`);+const response = findLastJsonObject(raw);+if (response === null) {+ console.error("Response is not valid JSON: no parseable JSON object found");process.exit(1);}