Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.
Review fixes: sticky redirect endpoint, docs, replay policy
Rishabh Bajpai19d1fb2945df
diff --git a/app/src/main/java/com/gotcha/connectors/mcp/McpHttpClient.kt b/app/src/main/java/com/gotcha/connectors/mcp/McpHttpClient.ktindex 62fe5da..21572c4 100644--- a/app/src/main/java/com/gotcha/connectors/mcp/McpHttpClient.kt+++ b/app/src/main/java/com/gotcha/connectors/mcp/McpHttpClient.kt@@ -132,11 +132,30 @@ class McpHttpClient(inner class Session internal constructor(initialEndpoint: McpEndpoint) {/*** Where requests go. The slash-tolerant retry may move it to the- * working spelling; the setter stays private so all writes happen- * under the session mutex.+ * working spelling, and a followed redirect promotes the effective+ * URL — an http-to-https upgrade must stick, or every later request+ * would resend the session credentials over plain http. Written only+ * under the session mutex, via [noteRequest] and [adoptUrl].*/var endpoint: McpEndpoint = initialEndpointprivate set++ /** The URL the last request actually went to (after redirects). */+ var lastRequestUrl: String = initialEndpoint.url+ private set++ /**+ * Records the URL a request goes out on. Call before executing, so a+ * failure still knows which spelling was attempted.+ */+ internal fun noteRequest(url: String) {+ lastRequestUrl = url+ }++ /** Moves future requests to [url]: a followed redirect or a working spelling. */+ internal fun adoptUrl(url: String) {+ if (url != endpoint.url) endpoint = endpoint.copy(url = url)+ }private val mutex = Mutex()private var sessionId: String? = nullprivate var protocolVersion: String? = null@@ -220,12 +239,14 @@ class McpHttpClient(/*** "/mcp" and "/mcp/" are the same thing: when the server 404s one spelling* before any session exists, try the other once (issue #154) rather than- * fail the whole setup. A second failure restores the configured URL and- * reports the retry's error, so the message describes the last attempt.+ * fail the whole setup. Toggles the spelling that actually failed —+ * after a redirect that is the destination, not the configured URL. A+ * second failure restores the configured URL and reports the retry's+ * error, so the message describes the last attempt.*/private suspend fun retryInitialize(first: McpException): Reply {val original = endpoint- val alternate = slashToggled(original.url)+ val alternate = slashToggled(lastRequestUrl)if (first.kind != McpFailure.NOT_FOUND || alternate == null) throw firstendpoint = original.copy(url = alternate)var ok = false@@ -323,18 +344,22 @@ class McpHttpClient(var url = session.endpoint.urlvar lastCode = 0repeat(MAX_REDIRECTS + 1) {+ session.noteRequest(url)val response = executePost(session, out, url)try {if (response.isSuccessful) {+ // Stay where a redirect took us: the next request must not+ // replay the session credentials against the old URL.+ session.adoptUrl(url)val sessionId = response.header(HEADER_SESSION_ID)val message = if (out.id == null) null else readResponse(response, out.id)return Reply(message, sessionId)}lastCode = response.codeval location = response.header("Location")- // Only the codes that keep POST semantics are replayed (301,- // 302, 307, 308). A 300 or 303 answers a GET instead, so those- // fail here rather than repeat a side-effecting tool call.+ // Replay policy (see FOLLOW_CODES): the same POST goes to the+ // destination. Anything else fails here rather than repeat a+ // side-effecting tool call as a GET.if (response.code in FOLLOW_CODES && location != null) {url = redirectTarget(url, location, response.code)} else {@@ -552,7 +577,12 @@ class McpHttpClient(private const val MAX_TOOL_PAGES = 20private const val MAX_REDIRECTS = 5- /** Redirect codes replayed as POST; 300/303 answer a GET instead. */+ /**+ * Redirect codes replayed as POST with the same body and headers.+ * Deliberate policy: 307/308 preserve the method by definition, and+ * servers also use 301/302 for trailing-slash normalization; 300/303+ * answer a GET instead and never replay here.+ */private val FOLLOW_CODES = setOf(301, 302, 307, 308)private const val MAX_REPLY_BYTES = 4L * 1024 * 1024private const val MAX_OUTPUT_CHARS = 50_000diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xmlindex 7242a1a..797826a 100644--- a/app/src/main/res/values/strings.xml+++ b/app/src/main/res/values/strings.xml@@ -1745,7 +1745,7 @@<string name="mcp_tools_capped">Only the first %1$d are used.</string><string name="mcp_removed_while_saving">That server was removed in the meantime.</string><string name="mcp_error_name">Enter a name for the server.</string>- <string name="mcp_error_bad_url">Enter a full URL that starts with https:// (or http:// on your local network).</string>+ <string name="mcp_error_bad_url">Enter a full URL starting with http:// or https://.</string><string name="mcp_error_cleartext">Use https for a server outside your local network. A token or header sent over http can be read on the way.</string><string name="mcp_error_bad_header">Write each header on its own line, as Name: value.</string><string name="mcp_error_auth">The server refused the token or headers (HTTP %1$d). Check them and try again.</string>diff --git a/app/src/test/java/com/gotcha/connectors/mcp/McpHttpClientTest.kt b/app/src/test/java/com/gotcha/connectors/mcp/McpHttpClientTest.ktindex 198e349..a294ea3 100644--- a/app/src/test/java/com/gotcha/connectors/mcp/McpHttpClientTest.kt+++ b/app/src/test/java/com/gotcha/connectors/mcp/McpHttpClientTest.kt@@ -260,11 +260,54 @@ class McpHttpClientTest {}@Test- fun `a same-origin redirect is followed with the headers intact`() = runTest {- val redirecting = MockWebServer().apply {+ fun `same-origin redirects are followed with the headers intact`() = runTest {+ // Deliberate replay policy, not spec literalism: 301/302 permit+ // POST-to-GET elsewhere, but MCP servers use them for trailing-slash+ // normalization, so this client re-POSTs the same body and headers.+ // 307/308 preserve the method by definition; 300/303 never replay.+ for (code in listOf(301, 302, 307, 308)) {+ val redirecting = MockWebServer().apply {+ dispatcher = object : Dispatcher() {+ override fun dispatch(request: RecordedRequest): MockResponse {+ if (request.path == "/mcp") {+ return MockResponse()+ .setResponseCode(code)+ .setHeader("Location", "/mcp/")+ }+ return fake.dispatch(request)+ }+ }+ start()+ }+ try {+ val info = client.session(+ McpEndpoint(redirecting.url("/mcp").toString(), mapOf("X-API-Key" to "secret"))+ ).initialize()+ assertEquals("Docs", info.name)+ // Both hops carried the secret: same origin, so nothing leaked.+ val first = redirecting.takeRequest(5, TimeUnit.SECONDS)!!+ val followed = redirecting.takeRequest(5, TimeUnit.SECONDS)!!+ assertEquals("/mcp", first.path)+ assertEquals("/mcp/", followed.path)+ assertEquals("secret", followed.getHeader("X-API-Key"))+ // The replay carries the same bytes (bodySize is recorded at+ // receipt; the fake drains the buffer while dispatching).+ assertTrue(followed.bodySize > 0)+ assertEquals(first.bodySize, followed.bodySize)+ } finally {+ redirecting.shutdown()+ }+ }+ }++ @Test+ fun `the followed destination sticks for later requests`() = runTest {+ // A path-changing redirect: after initialize, tools/list must go+ // straight to the destination instead of replaying the old URL.+ val moved = MockWebServer().apply {dispatcher = object : Dispatcher() {override fun dispatch(request: RecordedRequest): MockResponse {- if (request.path == "/mcp") {+ if (request.path == "/entry") {return MockResponse().setResponseCode(307).setHeader("Location", "/mcp/")@@ -275,17 +318,46 @@ class McpHttpClientTest {start()}try {- val info = client.session(- McpEndpoint(redirecting.url("/mcp").toString(), mapOf("X-API-Key" to "secret"))- ).initialize()+ val session = client.session(McpEndpoint(moved.url("/entry").toString()))+ session.initialize()+ assertEquals(moved.url("/mcp/").toString(), session.endpoint.url)+ // Drain initialize's requests (entry, initialize, notification).+ repeat(3) { moved.takeRequest(5, TimeUnit.SECONDS) }+ session.listTools()+ // Both tools/list pages go straight to the destination.+ repeat(2) {+ assertEquals("/mcp/", moved.takeRequest(5, TimeUnit.SECONDS)!!.path)+ }+ } finally {+ moved.shutdown()+ }+ }++ @Test+ fun `a redirect into a missing spelling toggles the destination`() = runTest {+ // /entry redirects to /mcp, /mcp 404s, /mcp/ serves: the retry must+ // toggle the URL that actually failed, not the configured one.+ val moved = MockWebServer().apply {+ dispatcher = object : Dispatcher() {+ override fun dispatch(request: RecordedRequest): MockResponse {+ if (request.path == "/entry") {+ return MockResponse()+ .setResponseCode(307)+ .setHeader("Location", "/mcp")+ }+ if (request.path == "/mcp") return MockResponse().setResponseCode(404)+ return fake.dispatch(request)+ }+ }+ start()+ }+ try {+ val session = client.session(McpEndpoint(moved.url("/entry").toString()))+ val info = session.initialize()assertEquals("Docs", info.name)- // Both hops carried the secret: same origin, so nothing leaked.- redirecting.takeRequest(5, TimeUnit.SECONDS)- val followed = redirecting.takeRequest(5, TimeUnit.SECONDS)!!- assertEquals("/mcp/", followed.path)- assertEquals("secret", followed.getHeader("X-API-Key"))+ assertEquals(moved.url("/mcp/").toString(), session.endpoint.url)} finally {- redirecting.shutdown()+ moved.shutdown()}}diff --git a/app/src/test/java/com/gotcha/connectors/mcp/McpServersConnectorTest.kt b/app/src/test/java/com/gotcha/connectors/mcp/McpServersConnectorTest.ktindex b1589cf..36d5059 100644--- a/app/src/test/java/com/gotcha/connectors/mcp/McpServersConnectorTest.kt+++ b/app/src/test/java/com/gotcha/connectors/mcp/McpServersConnectorTest.kt@@ -117,7 +117,7 @@ class McpServersConnectorTest {@Testfun `invalid input is refused before anything is sent`() = runTest {assertEquals(- "Enter a full URL that starts with https:// (or http:// on your local network).",+ "Enter a full URL starting with http:// or https://.",connector.save(null, "Docs", "not a url", "", emptyMap(), strings).second)assertEquals("Enter a name for the server.", connector.save(null, " ", url(), "", emptyMap(), strings).second)diff --git a/docs/mcp-servers.md b/docs/mcp-servers.mdindex 5002b02..856b6a1 100644--- a/docs/mcp-servers.md+++ b/docs/mcp-servers.md@@ -20,11 +20,17 @@ assistant can call them while that server is switched on.why it failed. **Save** tests again and stores the server only if it answers. The token andheaders are kept in Gotcha's encrypted connector store on the phone.-Plain `http://` works for servers on your own network: `localhost`, `.local` and `.lan`-names, and private, link-local and Tailscale (100.64.0.0/10) addresses. A token or header-sent over `http://` to anywhere else could be read on the way, so Gotcha asks for `https://`-there. For the same reason Gotcha never follows a redirect: the token and headers would go to-a URL it never checked.+Both `http://` and `https://` addresses are accepted. When a token or header+would travel over plain `http://` outside your own network (`localhost`,+`.local` and `.lan` names, private, link-local and Tailscale addresses), the+card keeps working but warns that it could be read on the way — prefer+`https://` there. Redirects are followed when they cannot leak anything: the+same address (scheme, host and port), or an `http://` to `https://` upgrade on+the same host, with later requests staying on the upgraded address. Anything+else — another host or port, or a downgrade back to plain `http://` — is+refused, naming where it points so you can enter that address instead. A+missing trailing slash is forgiven too: when the server answers 404 on one of+`/mcp` and `/mcp/`, Gotcha tries the other spelling once.## What the assistant gets