Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions plugins/AI-Agent-MCP/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,29 @@ 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.
While the switch is off, the entry's `query` description adds that the user
can switch writes on there and that the databases must not be changed any
other way (`ReadOnlyTrust.needsWriteSwitchHint`). Told only "use execute", a
tool it did not have, the model asked to run `sqlite3` in the shell instead
(a Note 20, 2026-11-25). The shell would not reach another app's private
files, but the user was never told about the switch. When SQLite refuses a
write sent through `query` ("not authorized", or a read-only database), the
same sentence follows the failure's text (`ReadOnlyTrust.isWriteRefusal`),
which AI-Core shows under the chat: after a failed call, AI-Core replaces the
Agent's own answer with "I couldn't complete that, the last action failed".
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
Expand Down
4 changes: 3 additions & 1 deletion plugins/AI-Agent-MCP/ai-agent-mcp.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ <h2>Usage</h2>
New tools always arrive switched <b>off</b>, 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 <b>Let the Agent change app databases</b>, and every call of it asks for
your approval. Nothing a server adds later becomes visible to the model
without your say-so.
</div>

Expand Down
4 changes: 2 additions & 2 deletions plugins/AI-Agent-MCP/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions plugins/AI-Agent-MCP/src/main/assets/docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ <h2>Choosing tools</h2>
<code>query</code> and <code>get_events</code>. Its other tools start off. This
version does not show the entry on the MCP servers screen, so it has no switches
there yet.</p>
<p>The one switch it has is <b>Let the Agent change app databases</b>, at the top
of the MCP servers screen, off at first. While it is on, every app's SQLite
debugger entry also offers <code>execute</code>, 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.</p>
<p>The Agent reads this list from memory, so tap <b>Connect</b> again after the
server changes; a chat that is already open picks the new list up.</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,25 @@ object AutomaticServers {
return changed
}

/** The entries to offer, with their tool switches already decided by [ReadOnlyTrust]. */
fun servers(): List<McpServer> = 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<McpServer> {
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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,57 @@ object ReadOnlyTrust {
*/
val CORE_TOOLS: Set<String> = 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<String> = 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<McpTool>, allowWrites: Boolean = false): Set<String> {
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 isReadOnly(tool: McpTool): Boolean = tool.readOnlyHint && tool.name !in WRITE_TOOLS

/**
* Whether [toolName] of an SQLite debugger entry should tell the model how writes get allowed: it
* is `query`, whose own description says "Writes are refused: use execute", and the entry's server
* offers a [WRITE_TOOLS] tool that the settings switch keeps off. Told only to use a tool it does
* not have, the model looked for another way: on a Note 20 it asked to run `sqlite3` in the shell
* (ADFA-7300, 2026-11-25).
*
* @param knownTools every tool the server listed.
* @param enabledTools the tools offered now.
*/
fun needsWriteSwitchHint(toolName: String, knownTools: Collection<String>, enabledTools: Collection<String>): Boolean =
toolName == "query" && WRITE_TOOLS.any { it in knownTools && it !in enabledTools }

/**
* Whether [message], a failed `query`'s text, is SQLite refusing a write on the server's read-only
* connection: "not authorized" from its authorizer, or "attempt to write a readonly database" from
* `query_only`. On a Note 20 the model sent an INSERT through `query`, and that refusal was the last
* thing the chat showed (ADFA-7300, 2026-11-25).
*
* It also matches a refused BEGIN, SAVEPOINT, ATTACH or setting PRAGMA, which `execute` refuses
* too, so the hint then names a switch that would not help. Accepted: the server reports every
* refusal as SQLite's plain "not authorized", and copying its authorizer's rules here would drift.
*/
fun enabledTools(tools: List<McpTool>): Set<String> =
tools.filter { it.readOnlyHint && it.name in CORE_TOOLS }.mapTo(linkedSetOf()) { it.name }
fun isWriteRefusal(message: String?): Boolean =
Comment thread
davidschachterADFA marked this conversation as resolved.
Resolved
message != null && (message.contains("not authorized", ignoreCase = true) ||
message.contains("readonly database", ignoreCase = true))

/**
* Reads the hint from one `tools/list` entry's `annotations`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -648,10 +649,12 @@ class McpPlugin : IPlugin, SettingsExtension, DocumentationExtension {
on.<br><br>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.<br><br>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 <b>Let the Agent change
app databases</b> on that screen.<br><br>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(
Expand Down Expand Up @@ -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.<br><br>
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.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -269,6 +270,32 @@ 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.
*
* @return false when the value could not be stored, and the switch is then unchanged. A failed
* commit still leaves the new value in SharedPreferences' memory, where it would hold until the
* app restarts and then turn back, so it is put back to the stored one.
*/
fun setAllowDatabaseWrites(allow: Boolean): Boolean {
val prefs = prefs() ?: return false
val stored = prefs.getBoolean(KEY_ALLOW_DATABASE_WRITES, false)
if (!prefs.edit().putBoolean(KEY_ALLOW_DATABASE_WRITES, allow).commit()) {
Log.w(TAG, "Could not store the database-writes switch")
Comment thread
davidschachterADFA marked this conversation as resolved.
Resolved
prefs.edit().putBoolean(KEY_ALLOW_DATABASE_WRITES, stored).commit()
return false
}
fireChanged()
return true
}

/**
* The stored record for [id].
* @param id the server.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.widget.EditText
import android.widget.ImageButton
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
Expand Down Expand Up @@ -105,6 +106,25 @@ 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<SwitchMaterial>(R.id.mcpAllowDatabaseWrites)
wireTooltip(writes, McpPlugin.TOOLTIP_TAG_ALLOW_DATABASE_WRITES)
fun showWrites(allow: Boolean) {
writes.setOnCheckedChangeListener(null)
writes.isChecked = allow
writes.setOnCheckedChangeListener { _, isChecked -> viewModel.setAllowDatabaseWrites(isChecked) }
}
viewLifecycleOwner.lifecycleScope.launch {
viewModel.allowDatabaseWrites.collect { showWrites(it) }
}
viewLifecycleOwner.lifecycleScope.launch {
viewModel.writesNotSaved.collect {
showWrites(viewModel.allowDatabaseWrites.value)
val text = getString(R.string.mcp_allow_database_writes_not_saved)
Toast.makeText(requireContext(), text, Toast.LENGTH_LONG).show()
}
}
}

override fun onResume() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import com.itsaky.androidide.plugins.aiagentmcp.tools.McpToolCatalog
import com.itsaky.androidide.plugins.aiagentmcp.transport.McpExchangeLog
import com.itsaky.androidide.plugins.security.KeystoreSecretStore
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
Expand All @@ -39,9 +42,32 @@ class McpSettingsViewModel(
/** The configured servers, re-read after every change. */
val servers: StateFlow<List<McpServer>> = _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<Boolean> = _allowDatabaseWrites.asStateFlow()

/** Re-reads the stored servers, e.g. after returning to the screen. */
fun reload() {
_servers.value = McpServerStore.servers()
_allowDatabaseWrites.value = McpServerStore.allowDatabaseWrites()
}

private val _writesNotSaved = MutableSharedFlow<Unit>(extraBufferCapacity = 1)

/**
* Fires when the database-writes switch could not be stored. [allowDatabaseWrites] keeps the stored
* value, which may equal what it held, so the screen sets the switch back itself.
*/
val writesNotSaved: SharedFlow<Unit> = _writesNotSaved.asSharedFlow()

/** Stores the database-writes switch off the main thread, then re-reads it. */
fun setAllowDatabaseWrites(allow: Boolean) {
viewModelScope.launch {
val stored = withContext(Dispatchers.IO) { McpServerStore.setAllowDatabaseWrites(allow) }
reload()
if (!stored) _writesNotSaved.tryEmit(Unit)
}
}

/**
Expand Down
Loading
Loading