From 7560587e9b5fc806c363ce41388b476a4c5711cc Mon Sep 17 00:00:00 2001
From: Claude
Date: Wed, 25 Nov 2026 07:19:47 +0000
Subject: [PATCH] ADFA-7300 AI-Agent-MCP: a switch to let the Agent change app
databases
Phase 2, P2-5 (ADFA-7131 comment 41476, decision D14). On #166
(sqlite-debugger/ADFA-7139 at 38b5268a). Needs CodeOnTheGo P2-3 and
P2-4 (execute, acknowledge_room) on the device.
- The automatic SQLite debugger entry has no settings, so nothing could
switch its execute on. David chose one switch (2026-11-25): "Let the
Agent change app databases" at the top of the MCP servers screen, off
by default, for every app (McpServerStore.allowDatabaseWrites). It
tells the change listeners, so the Agent's tool list follows.
- ReadOnlyTrust.WRITE_TOOLS ("execute") is offered only while it is on,
and never declared read-only, whatever hint the server sends
(isReadOnly). So AI-Core asks before every call and never offers
Always Allow for it.
- Tooltip entry and long-press wiring for the switch; README,
ai-agent-mcp.html, the in-IDE help and the plugin tooltip updated.
- Version 2.1.0 / versionCode 4 (MINOR, on #166's 2.0.0 / 3).
With the switch on, each app offers five tools; in compact mode (4)
AI-Core drops the first app's get_events (README).
Tests: WriteToolTest (off by default even claiming the hint; on: asks
and not read-only; off again; listeners told; a hand-added server's
execute stays ordinary) and three ReadOnlyHintParsingTest cases. Not
run here: no Android SDK or AndroidX (dl.google.com is blocked), so
neither Gradle nor these tests ran. ReadOnlyTrust and McpTool were
compiled alone with a scratch check of the same rules (8 checks pass;
isReadOnly without the WRITE_TOOLS exclusion fails "execute never
read-only"). The addons check passes. Not run: testDebugUnitTest,
assemblePlugin, the device check, font scale 1.0 and 2.0.
Co-Authored-By: Claude Opus 5.5
Claude-Session: https://claude.ai/code/session_01Vh2DAZ8yszEP4MqbR9PLba
---
plugins/AI-Agent-MCP/README.md | 13 ++
plugins/AI-Agent-MCP/ai-agent-mcp.html | 4 +-
plugins/AI-Agent-MCP/build.gradle.kts | 4 +-
.../src/main/assets/docs/index.html | 8 ++
.../aiagentmcp/debugger/AutomaticServers.kt | 15 ++-
.../aiagentmcp/debugger/ReadOnlyTrust.kt | 24 +++-
.../plugins/aiagentmcp/plugin/McpPlugin.kt | 31 ++++-
.../aiagentmcp/settings/McpServerStore.kt | 17 +++
.../settings/McpSettingsFragment.kt | 11 ++
.../settings/McpSettingsViewModel.kt | 14 ++
.../plugins/aiagentmcp/tools/McpToolSource.kt | 6 +-
.../main/res/layout/fragment_mcp_settings.xml | 19 +++
.../src/main/res/values/strings.xml | 2 +
.../debugger/ReadOnlyHintParsingTest.kt | 22 ++++
.../aiagentmcp/debugger/WriteToolTest.kt | 122 ++++++++++++++++++
15 files changed, 296 insertions(+), 16 deletions(-)
create mode 100644 plugins/AI-Agent-MCP/src/test/kotlin/com/itsaky/androidide/plugins/aiagentmcp/debugger/WriteToolTest.kt
diff --git a/plugins/AI-Agent-MCP/README.md b/plugins/AI-Agent-MCP/README.md
index 29218cbf9..f1faaced3 100644
--- a/plugins/AI-Agent-MCP/README.md
+++ b/plugins/AI-Agent-MCP/README.md
@@ -107,6 +107,19 @@ Servers you add yourself keep the name prefix taken from their label.
at most 12 contributed tools per message (4 for a small on-device model), in
order, and drops the rest; every app has its own entry. Switching the others
on will come with showing this entry in the settings screen (phase 2).
+- Writes (ADFA-7300). The entry's `execute` tool, which runs one statement that
+ changes the app's database, is offered only while **Let the Agent change app
+ databases** is on in this plugin's settings (one switch for every app, off by
+ default, `McpServerStore.allowDatabaseWrites`). It is never declared
+ read-only, whatever hint the server sends (`ReadOnlyTrust.WRITE_TOOLS`,
+ `ReadOnlyTrust.isReadOnly`), so AI-Core asks before every call and never
+ offers "Always Allow" for it. A change is committed at once and cannot be
+ undone. On a Room database the server refuses the first write until the Agent
+ repeats it with `acknowledge_room: true` (ADFA-7299), so that call asks too.
+ With the switch on, each app offers five tools, in the order the server lists
+ them (today `list_databases`, `get_schema`, `query`, `execute`, `get_events`),
+ so two apps take 10 of AI-Core's 12 slots, and in compact mode (4) AI-Core
+ drops the first app's `get_events` and every later tool.
- Approval. This plugin declares each of that entry's tools read-only when the
server sends `readOnlyHint: true` for it (`McpToolSource.Spec.isReadOnly`).
AI-Core 3.6.0 or later then offers "Always Allow" in the approval dialog for
diff --git a/plugins/AI-Agent-MCP/ai-agent-mcp.html b/plugins/AI-Agent-MCP/ai-agent-mcp.html
index c857fc016..732ac5806 100644
--- a/plugins/AI-Agent-MCP/ai-agent-mcp.html
+++ b/plugins/AI-Agent-MCP/ai-agent-mcp.html
@@ -151,7 +151,9 @@ Usage
New tools always arrive switched off, including on the connection
that discovers them. The one exception is four read-only tools (list databases, get schema,
query and get events) of the SQLite debugger entry that Code on the Go
- supplies for your own debug app. Nothing a server adds later becomes visible to the model
+ supplies for your own debug app. Its tool that changes the app's database, execute, is offered
+ only after you turn on Let the Agent change app databases, and every call of it asks for
+ your approval. Nothing a server adds later becomes visible to the model
without your say-so.
diff --git a/plugins/AI-Agent-MCP/build.gradle.kts b/plugins/AI-Agent-MCP/build.gradle.kts
index 5dc962237..0b31e31ef 100644
--- a/plugins/AI-Agent-MCP/build.gradle.kts
+++ b/plugins/AI-Agent-MCP/build.gradle.kts
@@ -15,8 +15,8 @@ android {
applicationId = "com.itsaky.androidide.plugins.aiagentmcp"
minSdk = 33
targetSdk = 36
- versionCode = 3
- versionName = "2.0.0"
+ versionCode = 4
+ versionName = "2.1.0"
}
buildFeatures {
diff --git a/plugins/AI-Agent-MCP/src/main/assets/docs/index.html b/plugins/AI-Agent-MCP/src/main/assets/docs/index.html
index be9b9b44b..23447fc88 100644
--- a/plugins/AI-Agent-MCP/src/main/assets/docs/index.html
+++ b/plugins/AI-Agent-MCP/src/main/assets/docs/index.html
@@ -86,6 +86,14 @@ Choosing tools
query and get_events. Its other tools start off. This
version does not show the entry on the MCP servers screen, so it has no switches
there yet.
+ The one switch it has is Let the Agent change app databases, at the top
+ of the MCP servers screen, off at first. While it is on, every app's SQLite
+ debugger entry also offers execute, which runs one SQL statement that
+ changes data or the schema. The Agent asks for your approval before every
+ change, and "Always Allow" is never offered for it. A change is saved at once
+ and can't be undone. For an app that uses Room, the first change is refused
+ until the Agent asks you again, because Room doesn't notice it and the app can
+ show old data until it reads the database again.
The Agent reads this list from memory, so tap Connect again after the
server changes; a chat that is already open picks the new list up.
diff --git a/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/debugger/AutomaticServers.kt b/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/debugger/AutomaticServers.kt
index d1a4d6c28..59943e672 100644
--- a/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/debugger/AutomaticServers.kt
+++ b/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/debugger/AutomaticServers.kt
@@ -253,18 +253,25 @@ object AutomaticServers {
return changed
}
- /** The entries to offer, with their tool switches already decided by [ReadOnlyTrust]. */
- fun servers(): List = states.map { (id, state) ->
+ /**
+ * The entries to offer, with their tool switches already decided by [ReadOnlyTrust] and the
+ * settings switch for database writes.
+ */
+ fun servers(): List {
+ val allowWrites = McpServerStore.allowDatabaseWrites()
+ return states.map { (id, state) -> server(id, state, allowWrites) }
+ }
+
+ private fun server(id: String, state: State, allowWrites: Boolean) =
McpServer(
id = id,
name = "SQLite debugger (${state.endpoint.applicationId})",
url = urlFor(state.endpoint),
enabled = true,
knownTools = state.tools.map { it.name },
- enabledTools = ReadOnlyTrust.enabledTools(state.tools),
+ enabledTools = ReadOnlyTrust.enabledTools(state.tools, allowWrites),
automatic = true,
)
- }
fun isAutomatic(serverId: String): Boolean = serverId.startsWith(ID_PREFIX)
diff --git a/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/debugger/ReadOnlyTrust.kt b/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/debugger/ReadOnlyTrust.kt
index 6741e97ac..2dc37726d 100644
--- a/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/debugger/ReadOnlyTrust.kt
+++ b/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/debugger/ReadOnlyTrust.kt
@@ -34,12 +34,30 @@ object ReadOnlyTrust {
*/
val CORE_TOOLS: Set = linkedSetOf("list_databases", "get_schema", "query", "get_events")
+ /**
+ * The SQLite debugger's tools that change an app's database (ADFA-7300). Switched on only by the
+ * settings switch [com.itsaky.androidide.plugins.aiagentmcp.settings.McpServerStore.allowDatabaseWrites],
+ * and never read-only, whatever hint the server sends: "Always Allow" must never cover a write.
+ */
+ val WRITE_TOOLS: Set = linkedSetOf("execute")
+
/**
* @param tools what `tools/list` returned.
- * @return the names of the [CORE_TOOLS] that declared `annotations.readOnlyHint` as boolean true.
+ * @param allowWrites the user switched on database writes in this plugin's settings.
+ * @return the names of the [CORE_TOOLS] that declared `annotations.readOnlyHint` as boolean true,
+ * then, when [allowWrites], the listed [WRITE_TOOLS].
+ */
+ fun enabledTools(tools: List, allowWrites: Boolean = false): Set {
+ val on = tools.filter { isReadOnly(it) && it.name in CORE_TOOLS }.mapTo(linkedSetOf()) { it.name }
+ if (allowWrites) tools.filter { it.name in WRITE_TOOLS }.mapTo(on) { it.name }
+ return on
+ }
+
+ /**
+ * Whether this plugin declares [tool] of an SQLite debugger entry read-only to AI-Core, which then
+ * offers "Always Allow" for it. A [WRITE_TOOLS] tool never is, even if its server claims the hint.
*/
- fun enabledTools(tools: List): Set =
- tools.filter { it.readOnlyHint && it.name in CORE_TOOLS }.mapTo(linkedSetOf()) { it.name }
+ fun isReadOnly(tool: McpTool): Boolean = tool.readOnlyHint && tool.name !in WRITE_TOOLS
/**
* Reads the hint from one `tools/list` entry's `annotations`.
diff --git a/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/plugin/McpPlugin.kt b/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/plugin/McpPlugin.kt
index 2434288e8..f004d7629 100644
--- a/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/plugin/McpPlugin.kt
+++ b/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/plugin/McpPlugin.kt
@@ -146,6 +146,7 @@ class McpPlugin : IPlugin, SettingsExtension, DocumentationExtension {
const val TOOLTIP_TAG_HEADER_VALUE = "mcp_header_value"
const val TOOLTIP_TAG_HEADER_REMOVE = "mcp_header_remove"
const val TOOLTIP_TAG_BACK = "mcp_back"
+ const val TOOLTIP_TAG_ALLOW_DATABASE_WRITES = "mcp_allow_database_writes"
/**
* Logs a failure that escaped a coroutine of [newScope]. Platform logging, not
@@ -648,10 +649,12 @@ class McpPlugin : IPlugin, SettingsExtension, DocumentationExtension {
on.
The one exception is the SQLite debugger entry that CoGo adds
for an app you build with the SQLite debugger: four of its read-only tools
start on (list databases, get schema, query, and get events). This version
- doesn't list that entry on the MCP servers screen.
The Agent asks for
- your approval every time it uses a tool. For those four, you can choose
- Always Allow, which lasts for the rest of the chat (needs AI Core 3.6.0 or
- later).
+ doesn't list that entry on the MCP servers screen. Its tool that changes the
+ app's database, execute, stays off until you turn on Let the Agent change
+ app databases on that screen.
The Agent asks for your approval
+ every time it uses a tool. For those four, you can choose Always Allow,
+ which lasts for the rest of the chat (needs AI Core 3.6.0 or later). Never
+ for execute.
""".trimIndent(),
buttons = listOf(
PluginTooltipButton(
@@ -697,6 +700,26 @@ class McpPlugin : IPlugin, SettingsExtension, DocumentationExtension {
),
),
),
+ PluginTooltipEntry(
+ tag = TOOLTIP_TAG_ALLOW_DATABASE_WRITES,
+ summary = "Lets the Agent change the databases of apps you build with the SQLite debugger. It asks you before every change.",
+ detail = """
+ When this is on, each app's SQLite debugger entry offers its execute tool,
+ which runs one SQL statement that changes data or the schema. The Agent asks
+ for your approval every time, and Always Allow is never offered for it.
+ A change is saved at once and can't be undone. For an app that uses Room, the
+ first change asks you again, because Room doesn't notice it: the app can show
+ old data until it reads the database again.
+ """.trimIndent(),
+ buttons = listOf(
+ PluginTooltipButton(
+ description = "Learn more",
+ uri = "i/ai-addons-other.html#mcp",
+ order = 0,
+ directPath = true,
+ ),
+ ),
+ ),
PluginTooltipEntry(
tag = TOOLTIP_TAG_SERVER_ENABLED,
summary = "Turns this server on or off without deleting it.",
diff --git a/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/settings/McpServerStore.kt b/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/settings/McpServerStore.kt
index e648bebb7..4b9f54edb 100644
--- a/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/settings/McpServerStore.kt
+++ b/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/settings/McpServerStore.kt
@@ -31,6 +31,7 @@ object McpServerStore {
private const val KEY_SERVERS = "servers"
private const val KEY_TOKEN_PREFIX = "token_"
private const val KEY_HEADERS_PREFIX = "headers_"
+ private const val KEY_ALLOW_DATABASE_WRITES = "allowDatabaseWrites"
private const val FIELD_ID = "id"
private const val FIELD_NAME = "name"
@@ -269,6 +270,22 @@ object McpServerStore {
fireChanged()
}
+ /**
+ * Whether the SQLite debugger entries offer their tools that change an app's database
+ * (`ReadOnlyTrust.WRITE_TOOLS`, ADFA-7300). Off unless the user switched it on; every call of
+ * such a tool still asks, and none is read-only, so "Always Allow" never covers one.
+ */
+ fun allowDatabaseWrites(): Boolean = prefs()?.getBoolean(KEY_ALLOW_DATABASE_WRITES, false) ?: false
+
+ /** Switches [allowDatabaseWrites], then tells the listeners, so the Agent's tool list follows. */
+ fun setAllowDatabaseWrites(allow: Boolean) {
+ val prefs = prefs() ?: return
+ if (!prefs.edit().putBoolean(KEY_ALLOW_DATABASE_WRITES, allow).commit()) {
+ Log.w(TAG, "Could not store the database-writes switch")
+ }
+ fireChanged()
+ }
+
/**
* The stored record for [id].
* @param id the server.
diff --git a/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/settings/McpSettingsFragment.kt b/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/settings/McpSettingsFragment.kt
index 6621e0807..5fd1c2d25 100644
--- a/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/settings/McpSettingsFragment.kt
+++ b/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/settings/McpSettingsFragment.kt
@@ -105,6 +105,17 @@ class McpSettingsFragment : Fragment() {
viewLifecycleOwner.lifecycleScope.launch {
viewModel.servers.collect { servers -> renderServers(view, servers) }
}
+
+ // The switch is set from the store before its listener goes on, so showing it writes nothing.
+ val writes = view.findViewById(R.id.mcpAllowDatabaseWrites)
+ wireTooltip(writes, McpPlugin.TOOLTIP_TAG_ALLOW_DATABASE_WRITES)
+ viewLifecycleOwner.lifecycleScope.launch {
+ viewModel.allowDatabaseWrites.collect { allow ->
+ writes.setOnCheckedChangeListener(null)
+ writes.isChecked = allow
+ writes.setOnCheckedChangeListener { _, isChecked -> viewModel.setAllowDatabaseWrites(isChecked) }
+ }
+ }
}
override fun onResume() {
diff --git a/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/settings/McpSettingsViewModel.kt b/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/settings/McpSettingsViewModel.kt
index df01a9433..cd207167b 100644
--- a/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/settings/McpSettingsViewModel.kt
+++ b/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/settings/McpSettingsViewModel.kt
@@ -39,9 +39,23 @@ class McpSettingsViewModel(
/** The configured servers, re-read after every change. */
val servers: StateFlow> = _servers.asStateFlow()
+ private val _allowDatabaseWrites = MutableStateFlow(McpServerStore.allowDatabaseWrites())
+
+ /** The switch that lets the SQLite debugger entries offer their write tool (ADFA-7300). */
+ val allowDatabaseWrites: StateFlow = _allowDatabaseWrites.asStateFlow()
+
/** Re-reads the stored servers, e.g. after returning to the screen. */
fun reload() {
_servers.value = McpServerStore.servers()
+ _allowDatabaseWrites.value = McpServerStore.allowDatabaseWrites()
+ }
+
+ /** Stores the database-writes switch off the main thread, then re-reads it. */
+ fun setAllowDatabaseWrites(allow: Boolean) {
+ viewModelScope.launch {
+ withContext(Dispatchers.IO) { McpServerStore.setAllowDatabaseWrites(allow) }
+ reload()
+ }
}
/**
diff --git a/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/tools/McpToolSource.kt b/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/tools/McpToolSource.kt
index 610c2ee52..aa75d5cbf 100644
--- a/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/tools/McpToolSource.kt
+++ b/plugins/AI-Agent-MCP/src/main/kotlin/com/itsaky/androidide/plugins/aiagentmcp/tools/McpToolSource.kt
@@ -6,6 +6,7 @@ import com.itsaky.androidide.plugins.aiagentmcp.client.McpConnections
import com.itsaky.androidide.plugins.aiagentmcp.client.McpTool
import com.itsaky.androidide.plugins.aiagentmcp.debugger.AutomaticServers
import com.itsaky.androidide.plugins.aiagentmcp.debugger.McpServers
+import com.itsaky.androidide.plugins.aiagentmcp.debugger.ReadOnlyTrust
import com.itsaky.androidide.plugins.aiagentmcp.errors.McpErrorFormatter
import com.itsaky.androidide.plugins.aiagentmcp.R
import com.itsaky.androidide.plugins.aiagentmcp.logging.LOG_PREFIX
@@ -92,7 +93,7 @@ class McpToolSource internal constructor(
name = exposed.name,
description = exposed.description,
parametersSchema = exposed.tool.inputSchema,
- preApproved = exposed.server.automatic && exposed.tool.readOnlyHint,
+ preApproved = exposed.server.automatic && ReadOnlyTrust.isReadOnly(exposed.tool),
)
}
@@ -351,7 +352,8 @@ class McpToolSource internal constructor(
* Every remote tool declares that it requires approval and is not read-only: this plugin cannot
* know what a server's tool does, and guessing on the permissive side is the one guess that
* cannot be undone. The one exception is [preApproved]: a readOnlyHint tool of the automatic
- * debugger entry, whose server Code on the Go itself installed, declares no approval needed.
+ * debugger entry, whose server Code on the Go itself installed, declares no approval needed. Its
+ * `execute`, which writes, never does, whatever hint it carries (`ReadOnlyTrust.isReadOnly`).
* AI-Core overrides `requiresApproval` to true for every contributed tool
* (`ContributedToolHandler`), so the Agent still asks. For a read-only tool, AI-Core 3.6.0 or
* later offers "Always Allow" for the rest of the chat (ADFA-7147); an older AI-Core asks on each
diff --git a/plugins/AI-Agent-MCP/src/main/res/layout/fragment_mcp_settings.xml b/plugins/AI-Agent-MCP/src/main/res/layout/fragment_mcp_settings.xml
index 88154b8c9..b83c80c01 100644
--- a/plugins/AI-Agent-MCP/src/main/res/layout/fragment_mcp_settings.xml
+++ b/plugins/AI-Agent-MCP/src/main/res/layout/fragment_mcp_settings.xml
@@ -67,6 +67,25 @@
android:textColor="@color/plugin_text_muted"
android:textSize="14sp" />
+
+
+
+
+
Connect the Agent to Model Context Protocol servers. Their tools appear alongside the Agent\'s own, and every one of them asks for your approval before it runs.
No servers yet. Add one to give the Agent remote tools.
Add server
+ Let the Agent change app databases
+ Offers the SQLite debugger\'s execute tool for each app. The Agent asks before every change, and Always Allow is never offered for it. A change is saved at once and cannot be undone, and the app may show old data until it reads again.
%1$d of %2$d tools enabled
Not connecting: this http:// URL is not on this device. Tap to change it to https://.
Use this server
diff --git a/plugins/AI-Agent-MCP/src/test/kotlin/com/itsaky/androidide/plugins/aiagentmcp/debugger/ReadOnlyHintParsingTest.kt b/plugins/AI-Agent-MCP/src/test/kotlin/com/itsaky/androidide/plugins/aiagentmcp/debugger/ReadOnlyHintParsingTest.kt
index 6301ddfc4..f1c0693c7 100644
--- a/plugins/AI-Agent-MCP/src/test/kotlin/com/itsaky/androidide/plugins/aiagentmcp/debugger/ReadOnlyHintParsingTest.kt
+++ b/plugins/AI-Agent-MCP/src/test/kotlin/com/itsaky/androidide/plugins/aiagentmcp/debugger/ReadOnlyHintParsingTest.kt
@@ -74,6 +74,28 @@ class ReadOnlyHintParsingTest {
assertEquals(setOf("list_databases", "get_schema", "query", "get_events"), ReadOnlyTrust.CORE_TOOLS)
}
+ @Test
+ fun `execute is off unless writes are allowed, whatever its hint`() {
+ val tools = listOf(McpTool("query", "", readOnlyHint = true), McpTool("execute", "", readOnlyHint = true))
+ assertEquals(setOf("query"), ReadOnlyTrust.enabledTools(tools))
+ assertEquals(setOf("query", "execute"), ReadOnlyTrust.enabledTools(tools, allowWrites = true))
+ assertEquals(setOf("execute"), ReadOnlyTrust.enabledTools(listOf(McpTool("execute", "")), allowWrites = true))
+ }
+
+ @Test
+ fun `allowing writes switches on no other tool`() {
+ val tools = listOf(McpTool("get_stats", "", readOnlyHint = true), McpTool("run_sql", ""))
+ assertEquals(emptySet(), ReadOnlyTrust.enabledTools(tools, allowWrites = true))
+ }
+
+ @Test
+ fun `execute is never read-only, even claiming the hint`() {
+ assertFalse(ReadOnlyTrust.isReadOnly(McpTool("execute", "", readOnlyHint = true)))
+ assertTrue(ReadOnlyTrust.isReadOnly(McpTool("query", "", readOnlyHint = true)))
+ assertFalse(ReadOnlyTrust.isReadOnly(McpTool("query", "")))
+ assertEquals(setOf("execute"), ReadOnlyTrust.WRITE_TOOLS)
+ }
+
@Test
fun `a query tool that is not read only stays off`() {
val tools = listOf(McpTool("query", "", readOnlyHint = false), McpTool("list_databases", ""))
diff --git a/plugins/AI-Agent-MCP/src/test/kotlin/com/itsaky/androidide/plugins/aiagentmcp/debugger/WriteToolTest.kt b/plugins/AI-Agent-MCP/src/test/kotlin/com/itsaky/androidide/plugins/aiagentmcp/debugger/WriteToolTest.kt
new file mode 100644
index 000000000..15e52166b
--- /dev/null
+++ b/plugins/AI-Agent-MCP/src/test/kotlin/com/itsaky/androidide/plugins/aiagentmcp/debugger/WriteToolTest.kt
@@ -0,0 +1,122 @@
+package com.itsaky.androidide.plugins.aiagentmcp.debugger
+
+import com.itsaky.androidide.plugins.PluginContext
+import com.itsaky.androidide.plugins.aiagentmcp.client.McpConnections
+import com.itsaky.androidide.plugins.aiagentmcp.plugin.McpPlugin
+import com.itsaky.androidide.plugins.aiagentmcp.settings.McpServerStore
+import com.itsaky.androidide.plugins.aiagentmcp.testing.FakeMcpServer
+import com.itsaky.androidide.plugins.aiagentmcp.testing.FakeMcpServer.Companion.tool
+import com.itsaky.androidide.plugins.aiagentmcp.testing.FakeSharedPreferences
+import com.itsaky.androidide.plugins.aiagentmcp.tools.McpToolCatalog
+import com.itsaky.androidide.plugins.aiagentmcp.tools.McpToolSource
+import com.itsaky.androidide.plugins.security.KeystoreSecretStore
+import io.mockk.every
+import io.mockk.mockk
+import io.mockk.mockkObject
+import io.mockk.unmockkObject
+import org.junit.After
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Before
+import org.junit.Test
+
+/**
+ * The SQLite debugger's `execute` (ADFA-7300): offered only while the settings switch is on, and
+ * never read-only, so AI-Core asks before every call and never offers "Always Allow" for it.
+ */
+class WriteToolTest {
+
+ private val app = "com.example.app"
+ private var server: FakeMcpServer? = null
+ private val source = McpToolSource()
+ private lateinit var plugin: McpPlugin
+
+ @Before
+ fun setUp() {
+ val context = mockk(relaxed = true)
+ every { context.getPluginSharedPreferences(any()) } returns FakeSharedPreferences()
+ plugin = McpPlugin().also { it.initialize(context) }
+ mockkObject(McpServerStore)
+ every { McpServerStore.token(any()) } returns KeystoreSecretStore.Stored.Absent
+ every { McpServerStore.headers(any()) } returns emptyMap()
+ }
+
+ @After
+ fun tearDown() {
+ source.close()
+ AutomaticServers.install(null)
+ McpConnections.closeAll()
+ McpToolCatalog.clear()
+ server?.stop()
+ plugin.dispose()
+ unmockkObject(McpServerStore)
+ }
+
+ /** A debugger whose execute claims readOnlyHint, as a buggy or hostile server could. */
+ private fun connect() {
+ val fake = FakeMcpServer("t1", listOf(tool("list_databases", true), tool("query", true), tool("execute", true)))
+ server = fake
+ AutomaticServers.install { listOf(AutomaticEndpoint(app, fake.port, "t1")) }
+ McpToolCatalog.connect(AutomaticServers.servers().single())
+ }
+
+ /** The offered tools, keyed by the tool name after the app prefix (`db_app_list_databases` -> `databases`). */
+ private fun offered() = source.listTools().associateBy { it.name.substringAfterLast("_") }
+
+ @Test
+ fun `the switch is off by default and execute is not offered, even claiming the hint`() {
+ assertFalse(McpServerStore.allowDatabaseWrites())
+ connect()
+ assertEquals(setOf("databases", "query"), offered().keys)
+ assertEquals(setOf("list_databases", "query"), AutomaticServers.servers().single().enabledTools)
+ }
+
+ @Test
+ fun `with the switch on execute is offered, asks every time and is not read-only`() {
+ connect()
+ McpServerStore.setAllowDatabaseWrites(true)
+ assertTrue(McpServerStore.allowDatabaseWrites())
+ val execute = offered().getValue("execute")
+ assertTrue(execute.requiresApproval())
+ assertFalse(execute.isReadOnly)
+ // the read-only tools keep their grant
+ assertFalse(offered().getValue("query").requiresApproval())
+ }
+
+ @Test
+ fun `switching it off takes execute away again`() {
+ connect()
+ McpServerStore.setAllowDatabaseWrites(true)
+ McpServerStore.setAllowDatabaseWrites(false)
+ assertFalse("execute" in offered())
+ }
+
+ @Test
+ fun `changing the switch tells the change listeners, so the Agent's list follows`() {
+ var calls = 0
+ val listener = { calls++; Unit }
+ McpServerStore.addChangeListener(listener)
+ try {
+ McpServerStore.setAllowDatabaseWrites(true)
+ McpServerStore.setAllowDatabaseWrites(false)
+ } finally {
+ McpServerStore.removeChangeListener(listener)
+ }
+ assertEquals(2, calls)
+ }
+
+ @Test
+ fun `a hand-added server's execute is an ordinary tool, off until switched on, never read-only`() {
+ McpServerStore.setAllowDatabaseWrites(true)
+ val fake = FakeMcpServer("", listOf(tool("execute", true)))
+ server = fake
+ val handAdded = McpServerStore.saveDetails(McpServerStore.newServer("Mine", "http://127.0.0.1:${fake.port}/mcp"))
+ McpToolCatalog.connect(handAdded)
+ assertTrue(offered().isEmpty())
+ McpServerStore.setToolEnabled(handAdded.id, "execute", true)
+ val spec = offered().values.single()
+ assertTrue(spec.requiresApproval())
+ assertFalse(spec.isReadOnly)
+ }
+}