Settings › MCP servers: how each is signed in to, and a sign-in opened in the browser
CI / check (push) Canceled after 0s
CI / check (push) Canceled after 0s
`GET /api/v2/mcp` as a page: each server's name, how many tools it offers, and its state (ready, signed in, not signed in, needs a new sign-in), with who the provider says signed in and until when. One that waits for this person's sign-in has a button that opens the instance's own sign-in page in the browser (a Custom Tab): the sign-in happens there, against the instance, on its cookie door. The app starts and finishes nothing and its token is in no address it opens. Only a page of the instance the app is signed in to is opened: an address on another origin is read as its path on that instance. A shared sign-in an administrator makes says so and has no button. The list is read again each time the page comes back to the front. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
16 files changed
+449
-12
No files matched your search
@@ -58,6 +58,7 @@ import eu.llembas.android.ui.scheduled.ScheduleEditScreen
|
||||
import eu.llembas.android.ui.scheduled.ScheduleScreen
|
||||
import eu.llembas.android.ui.scheduled.ScheduledScreen
|
||||
import eu.llembas.android.ui.scheduled.SchedulingModelsScreen
|
||||
import eu.llembas.android.ui.settings.McpScreen
|
||||
import eu.llembas.android.ui.settings.SettingsScreen
|
||||
import eu.llembas.android.ui.signin.SignInScreen
|
||||
import eu.llembas.android.ui.theme.LembasTheme
|
||||
@@ -320,6 +321,7 @@ class MainActivity : ComponentActivity() {
|
||||
composable("files") { FilesScreen(onBack = { nav.popBackStack() }) }
|
||||
// Files at a folder: where a generated file's "Open in Files" leads.
|
||||
composable("files-at/{at}") { e -> FilesScreen(onBack = { nav.popBackStack() }, start = e.arguments?.getString("at")) }
|
||||
composable("mcp") { McpScreen(onBack = { nav.popBackStack() }) }
|
||||
composable("archived") {
|
||||
ArchivedScreen(onBack = { nav.popBackStack() }, onOpen = { top("chat/$it") }, onRestored = { chats.load() })
|
||||
}
|
||||
@@ -388,7 +390,7 @@ class MainActivity : ComponentActivity() {
|
||||
ScheduleEditScreen(e.arguments?.getString("id"), onBack = { nav.popBackStack() }, onSaved = { nav.popBackStack() })
|
||||
}
|
||||
composable("settings") {
|
||||
SettingsScreen(onBack = { nav.popBackStack() }, onAddInstance = { over("add") })
|
||||
SettingsScreen(onBack = { nav.popBackStack() }, onAddInstance = { over("add") }, onMcp = { over("mcp") })
|
||||
}
|
||||
// Signing in to one more instance makes it the one in use, which starts a
|
||||
// fresh navigation for it (the `key` above).
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
@file:OptIn(androidx.compose.material3.ExperimentalMaterial3Api::class)
|
||||
|
||||
package eu.llembas.android.ui.settings
|
||||
|
||||
import androidx.compose.foundation.border
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.heightIn
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.filled.ArrowBack
|
||||
import androidx.compose.material.icons.filled.Refresh
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ButtonDefaults
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBar
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableIntStateOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.compose.LifecycleEventEffect
|
||||
import eu.llembas.android.R
|
||||
import eu.llembas.android.api.ApiException
|
||||
import eu.llembas.android.api.McpServer
|
||||
import eu.llembas.android.api.signInAddress
|
||||
import eu.llembas.android.lembas
|
||||
import eu.llembas.android.ui.files.filesSay
|
||||
import eu.llembas.android.ui.scheduled.ScheduleLogic
|
||||
import eu.llembas.android.ui.theme.LocalPalette
|
||||
import eu.llembas.android.ui.viewer.openOutside
|
||||
import kotlinx.coroutines.CancellationException
|
||||
|
||||
/** The instance's MCP servers as the page draws them: on their way, refused, or the list. */
|
||||
data class McpState(val servers: List<McpServer> = emptyList(), val loading: Boolean = true, val error: String? = null)
|
||||
|
||||
/**
|
||||
* Settings › MCP servers: the servers whose tools the person's chats may be offered, and how
|
||||
* each is signed in to. One that waits for a sign-in has a button that opens the instance's own
|
||||
* sign-in page **in the browser** -- the sign-in happens there, against the instance, with the
|
||||
* person signed in to the instance by its cookie. The app starts and finishes nothing, and sees,
|
||||
* sends and stores no token of a server; its own token is in no address it opens. The list is
|
||||
* read again whenever the page comes back to the front, which is when the browser was left.
|
||||
*/
|
||||
@Composable
|
||||
fun McpScreen(onBack: () -> Unit) {
|
||||
val context = LocalContext.current
|
||||
var state by remember { mutableStateOf(McpState()) }
|
||||
var reads by remember { mutableIntStateOf(0) }
|
||||
var base by remember { mutableStateOf("") }
|
||||
LifecycleEventEffect(Lifecycle.Event.ON_RESUME) { reads++ }
|
||||
LaunchedEffect(reads) {
|
||||
state = state.copy(loading = true, error = null)
|
||||
state = try {
|
||||
val api = context.lembas.instances.client() ?: throw ApiException(401, context.getString(R.string.assist_signed_out))
|
||||
base = api.base
|
||||
McpState(api.mcpServers(), loading = false)
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
state.copy(loading = false, error = context.filesSay(e))
|
||||
}
|
||||
}
|
||||
McpContent(
|
||||
state,
|
||||
onBack = onBack,
|
||||
onReload = { reads++ },
|
||||
// Only a page of the instance the app is signed in to is ever opened (signInAddress).
|
||||
onSignIn = { server -> signInAddress(base, server.auth.signinUrl)?.let { openOutside(context, it) } },
|
||||
)
|
||||
}
|
||||
|
||||
/** The words for how a server is signed in to; a state this app does not know is said as the instance says it. */
|
||||
@Composable
|
||||
internal fun mcpState(server: McpServer): String = when (server.auth.state) {
|
||||
"ready" -> stringResource(R.string.mcp_ready)
|
||||
"signed_in" -> stringResource(R.string.mcp_signed_in)
|
||||
"signed_out" -> stringResource(R.string.mcp_signed_out)
|
||||
"needs_signin" -> stringResource(R.string.mcp_needs_signin)
|
||||
else -> server.auth.state
|
||||
}
|
||||
|
||||
/** The page, from [state] alone. Each server is a card: its words stacked, and under them the one button, the card's whole width. */
|
||||
@Composable
|
||||
fun McpContent(state: McpState, onBack: () -> Unit = {}, onReload: () -> Unit = {}, onSignIn: (McpServer) -> Unit = {}) {
|
||||
val palette = LocalPalette.current
|
||||
Scaffold(
|
||||
containerColor = palette.bg,
|
||||
topBar = {
|
||||
TopAppBar(
|
||||
colors = TopAppBarDefaults.topAppBarColors(containerColor = palette.bg, scrolledContainerColor = palette.bg),
|
||||
title = { Text(stringResource(R.string.mcp_title), maxLines = 1, overflow = TextOverflow.Ellipsis) },
|
||||
navigationIcon = {
|
||||
IconButton(onClick = onBack) { Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = stringResource(R.string.back)) }
|
||||
},
|
||||
actions = { IconButton(onClick = onReload) { Icon(Icons.Filled.Refresh, contentDescription = stringResource(R.string.reload)) } },
|
||||
)
|
||||
},
|
||||
) { padding ->
|
||||
Box(Modifier.padding(padding).fillMaxSize()) {
|
||||
when {
|
||||
state.error != null -> Text(state.error, color = MaterialTheme.colorScheme.error, modifier = Modifier.padding(16.dp))
|
||||
|
||||
state.loading && state.servers.isEmpty() -> CircularProgressIndicator(Modifier.align(Alignment.Center))
|
||||
|
||||
state.servers.isEmpty() -> Text(
|
||||
stringResource(R.string.mcp_none),
|
||||
color = palette.inkMuted,
|
||||
modifier = Modifier.align(Alignment.Center).padding(32.dp),
|
||||
)
|
||||
|
||||
else -> LazyColumn(
|
||||
Modifier.fillMaxSize(),
|
||||
contentPadding = PaddingValues(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
item { Text(stringResource(R.string.mcp_intro), style = MaterialTheme.typography.bodyMedium, color = palette.inkMuted) }
|
||||
items(state.servers, key = { it.id }) { server -> McpCard(server, onSignIn) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun McpCard(server: McpServer, onSignIn: (McpServer) -> Unit) {
|
||||
val palette = LocalPalette.current
|
||||
val auth = server.auth
|
||||
Column(
|
||||
Modifier.fillMaxWidth().border(1.dp, palette.border, RoundedCornerShape(16.dp)).padding(16.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(4.dp),
|
||||
) {
|
||||
Text(server.label, style = MaterialTheme.typography.titleMedium, color = palette.ink)
|
||||
Text(
|
||||
listOf(mcpState(server), stringResource(R.string.mcp_tools, server.tools.toString())).joinToString(" · "),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = if (auth.needsSignIn) palette.danger else palette.inkMuted,
|
||||
)
|
||||
// Who the provider says signed in: a stranger's text, drawn as plain text.
|
||||
if (auth.account.isNotBlank()) {
|
||||
Text(stringResource(R.string.mcp_account, auth.account), style = MaterialTheme.typography.bodySmall, color = palette.inkMuted)
|
||||
}
|
||||
if (!auth.needsSignIn) {
|
||||
ScheduleLogic.moment(auth.expiresAt)?.let {
|
||||
Text(stringResource(R.string.mcp_until, it), style = MaterialTheme.typography.bodySmall, color = palette.inkMuted)
|
||||
}
|
||||
}
|
||||
if (auth.needsSignIn) {
|
||||
if (auth.signinUrl.isNullOrBlank()) {
|
||||
// One sign-in for everyone, which only an administrator makes: nothing for this person to do.
|
||||
Text(stringResource(R.string.mcp_shared_note), style = MaterialTheme.typography.bodySmall, color = palette.inkMuted)
|
||||
} else {
|
||||
Text(stringResource(R.string.mcp_browser_note), style = MaterialTheme.typography.bodySmall, color = palette.inkMuted)
|
||||
Button(
|
||||
onClick = { onSignIn(server) },
|
||||
colors = ButtonDefaults.buttonColors(containerColor = palette.accent, contentColor = palette.accentInk),
|
||||
modifier = Modifier.fillMaxWidth().padding(top = 8.dp).heightIn(min = 48.dp),
|
||||
) { Text(stringResource(R.string.mcp_sign_in)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,7 @@ import androidx.compose.material.icons.filled.CheckCircle
|
||||
import androidx.compose.material.icons.filled.Dns
|
||||
import androidx.compose.material.icons.filled.Download
|
||||
import androidx.compose.material.icons.filled.ErrorOutline
|
||||
import androidx.compose.material.icons.filled.Extension
|
||||
import androidx.compose.material.icons.filled.Forum
|
||||
import androidx.compose.material.icons.filled.GraphicEq
|
||||
import androidx.compose.material.icons.filled.Hearing
|
||||
@@ -135,6 +136,9 @@ import kotlinx.coroutines.launch
|
||||
interface SettingsActions {
|
||||
fun use(instance: Instance) {}
|
||||
fun add() {}
|
||||
|
||||
/** The instance's MCP servers and their sign-ins. */
|
||||
fun mcpServers() {}
|
||||
fun signOut(instance: Instance) {}
|
||||
fun theme(value: String) {}
|
||||
fun language(value: String) {}
|
||||
@@ -207,7 +211,7 @@ data class SettingsState(
|
||||
)
|
||||
|
||||
@Composable
|
||||
fun SettingsScreen(onBack: () -> Unit, onAddInstance: () -> Unit) {
|
||||
fun SettingsScreen(onBack: () -> Unit, onAddInstance: () -> Unit, onMcp: () -> Unit = {}) {
|
||||
val context = LocalContext.current
|
||||
val store = context.lembas.instances
|
||||
val scope = rememberCoroutineScope()
|
||||
@@ -265,6 +269,8 @@ fun SettingsScreen(onBack: () -> Unit, onAddInstance: () -> Unit) {
|
||||
|
||||
override fun add() = onAddInstance()
|
||||
|
||||
override fun mcpServers() = onMcp()
|
||||
|
||||
override fun signOut(instance: Instance) {
|
||||
scope.launch {
|
||||
Push.disable(context, instance)
|
||||
@@ -550,6 +556,9 @@ fun SettingsContent(state: SettingsState, actions: SettingsActions, onBack: () -
|
||||
if (state.instances.isNotEmpty()) Rule()
|
||||
SettingRow(Icons.Filled.Add, stringResource(R.string.add_instance), onClick = actions::add)
|
||||
state.active?.let { current ->
|
||||
Rule()
|
||||
// The MCP servers of the instance in use, and which of them wait for a sign-in.
|
||||
SettingRow(Icons.Filled.Extension, stringResource(R.string.mcp_title), onClick = actions::mcpServers)
|
||||
Rule()
|
||||
SettingRow(Icons.AutoMirrored.Filled.Logout, stringResource(R.string.sign_out), tint = palette.danger, onClick = {
|
||||
actions.signOut(current)
|
||||
|
||||
Reference in new issue
Block a user