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
13 changes: 13 additions & 0 deletions plugins/AI-Agent-MCP/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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,30 @@ 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 enabledTools(tools: List<McpTool>): Set<String> =
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`.
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,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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SwitchMaterial>(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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,23 @@ 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()
}

/** 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()
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
)
}

Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions plugins/AI-Agent-MCP/src/main/res/layout/fragment_mcp_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@
android:textColor="@color/plugin_text_muted"
android:textSize="14sp" />

<!-- ADFA-7300: lets the SQLite debugger entries offer execute. Off by default; the
label is the switch's own text so a tap anywhere on it toggles, and it wraps at 2x font. -->
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/mcpAllowDatabaseWrites"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/mcp_allow_database_writes"
android:textColor="@color/plugin_on_surface"
android:textSize="16sp" />

<TextView
android:id="@+id/mcpAllowDatabaseWritesSummary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/mcp_allow_database_writes_summary"
android:textColor="@color/plugin_text_muted"
android:textSize="13sp" />

<LinearLayout
android:id="@+id/mcpServerList"
android:layout_width="match_parent"
Expand Down
2 changes: 2 additions & 0 deletions plugins/AI-Agent-MCP/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<string name="mcp_description">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.</string>
<string name="mcp_empty">No servers yet. Add one to give the Agent remote tools.</string>
<string name="mcp_add_server">Add server</string>
<string name="mcp_allow_database_writes">Let the Agent change app databases</string>
<string name="mcp_allow_database_writes_summary">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.</string>
<string name="mcp_server_tools_summary">%1$d of %2$d tools enabled</string>
<string name="mcp_server_url_cleartext">Not connecting: this http:// URL is not on this device. Tap to change it to https://.</string>
<string name="mcp_cd_server_enabled">Use this server</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>(), 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", ""))
Expand Down
Loading
Loading