-
Notifications
You must be signed in to change notification settings - Fork 0
feat(profile): AWAN-152 Implemented MCP settings & token management #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2c74661
AWAN-210: Add MCP network DTOs, Room entity/DAO, Room migration v1->v…
ZeiadT e6214e3
AWAN-210: Add MCP domain models, use cases, UI screens, ViewModel, an…
ZeiadT 08a0156
AWAN-210: Remediate deep-review issues - atomic DB transaction, prese…
ZeiadT 004ee8c
AWAN-210: Clean up temporary diff files
ZeiadT 930b4f5
AWAN-210: Align MCP endpoints with Postman Awan API Keys endpoints (v…
ZeiadT 10f2c23
AWAN-210: Prevent Dialog Constraints IllegalArgumentException in Awan…
ZeiadT c41e224
AWAN-210: Wrap JSON snippets in SelectionContainer and update backend…
ZeiadT a82ed11
AWAN-210: Add Connection Details card with copy buttons for Server UR…
ZeiadT 0196d2e
AWAN-210: Convert token creation and raw token reveal dialogs into Mo…
ZeiadT 56c2111
AWAN-210: Add AI setup prompt card, refined copy toast notifications,…
ZeiadT 2c1e04e
AWAN-152: Remove obsolete MCP Client ID from settings
ZeiadT ebcf90e
AWAN-152: Show token creation errors in snackbar
ZeiadT cbf8259
AWAN-152: Resolve MCP review findings
ZeiadT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,3 +19,4 @@ skills-lock.json | |
| .gemini/ | ||
| .claude/ | ||
| .artifacts/ | ||
| vm_repo.txt | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
core/data/src/main/kotlin/com/awan/app/core/data/mcp/di/McpDataModule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.awan.app.core.data.mcp.di | ||
|
|
||
| import com.awan.app.core.data.mcp.repository.McpRepositoryImpl | ||
| import com.awan.app.core.domain.mcp.repository.McpRepository | ||
| import dagger.Binds | ||
| import dagger.Module | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| abstract class McpDataModule { | ||
|
|
||
| @Binds | ||
| @Singleton | ||
| abstract fun bindMcpRepository( | ||
| impl: McpRepositoryImpl, | ||
| ): McpRepository | ||
| } |
40 changes: 40 additions & 0 deletions
40
core/data/src/main/kotlin/com/awan/app/core/data/mcp/mapper/McpMappers.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package com.awan.app.core.data.mcp.mapper | ||
|
|
||
| import com.awan.app.core.database.model.McpTokenEntity | ||
| import com.awan.app.core.domain.mcp.model.CreatedMcpToken | ||
| import com.awan.app.core.domain.mcp.model.McpToken | ||
| import com.awan.app.core.network.dto.mcp.ApiKeyResponseDto | ||
| import com.awan.app.core.network.dto.mcp.ApiKeySummaryDto | ||
|
|
||
| fun ApiKeySummaryDto.toEntity(): McpTokenEntity = McpTokenEntity( | ||
| id = id, | ||
| name = name, | ||
| maskedToken = keyPrefix, | ||
| createdAt = createdAt, | ||
| lastUsedAt = null, | ||
| ) | ||
|
|
||
| fun ApiKeyResponseDto.toDomain(): CreatedMcpToken = CreatedMcpToken( | ||
| id = id, | ||
| name = name, | ||
| rawToken = keyValue, | ||
| maskedToken = if (keyValue.length >= 12) keyValue.take(12) + "..." else keyValue, | ||
| createdAt = createdAt, | ||
| ) | ||
|
|
||
| fun ApiKeyResponseDto.toEntity(): McpTokenEntity = McpTokenEntity( | ||
| id = id, | ||
| name = name, | ||
| maskedToken = if (keyValue.length >= 12) keyValue.take(12) + "..." else keyValue, | ||
| createdAt = createdAt, | ||
| lastUsedAt = null, | ||
| ) | ||
|
|
||
| fun McpTokenEntity.toDomain(): McpToken = McpToken( | ||
| id = id, | ||
| name = name, | ||
| maskedToken = maskedToken, | ||
| createdAt = createdAt, | ||
| lastUsedAt = lastUsedAt, | ||
| ) | ||
|
|
133 changes: 133 additions & 0 deletions
133
core/data/src/main/kotlin/com/awan/app/core/data/mcp/repository/McpRepositoryImpl.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package com.awan.app.core.data.mcp.repository | ||
|
|
||
| import com.awan.app.core.common.dispatcher.AwanDispatchers | ||
| import com.awan.app.core.common.dispatcher.Dispatcher | ||
| import com.awan.app.core.common.error.AppError | ||
| import com.awan.app.core.common.result.Result | ||
| import com.awan.app.core.data.mcp.mapper.toDomain | ||
| import com.awan.app.core.data.mcp.mapper.toEntity | ||
| import com.awan.app.core.database.dao.McpTokenDao | ||
| import com.awan.app.core.domain.mcp.model.CreatedMcpToken | ||
| import com.awan.app.core.domain.mcp.model.McpConnectionDetails | ||
| import com.awan.app.core.domain.mcp.model.McpToken | ||
| import com.awan.app.core.domain.mcp.repository.McpRepository | ||
| import com.awan.app.core.domain.network.NetworkConnectivityMonitor | ||
| import com.awan.app.core.network.BuildConfig | ||
| import com.awan.app.core.network.api.McpApiService | ||
| import com.awan.app.core.network.dto.mcp.CreateApiKeyRequestDto | ||
| import com.awan.app.core.network.error.safeApiCall | ||
| import kotlinx.coroutines.CancellationException | ||
| import kotlinx.coroutines.CoroutineDispatcher | ||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.emitAll | ||
| import kotlinx.coroutines.flow.first | ||
| import kotlinx.coroutines.flow.flow | ||
| import kotlinx.coroutines.flow.flowOn | ||
| import kotlinx.coroutines.flow.map | ||
| import retrofit2.HttpException | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class McpRepositoryImpl @Inject constructor( | ||
| private val mcpApiService: McpApiService, | ||
| private val mcpTokenDao: McpTokenDao, | ||
| private val connectivityMonitor: NetworkConnectivityMonitor, | ||
| @Dispatcher(AwanDispatchers.IO) private val ioDispatcher: CoroutineDispatcher, | ||
| ) : McpRepository { | ||
|
|
||
| override fun getMcpConnectionDetails(): Flow<Result<McpConnectionDetails>> = flow { | ||
| emit( | ||
| Result.Success( | ||
| McpConnectionDetails( | ||
| mcpUrl = "${BuildConfig.AWAN_BASE_URL.trimEnd('/')}/v1/mcp", | ||
| clientId = "awan-android-client", | ||
| ) | ||
| ) | ||
| ) | ||
| }.flowOn(ioDispatcher) | ||
|
|
||
| override fun getMcpTokens(): Flow<Result<List<McpToken>>> = flow { | ||
| if (connectivityMonitor.isCurrentlyOnline()) { | ||
| try { | ||
| val response = mcpApiService.getApiKeys() | ||
| if (response.isSuccessful) { | ||
| val dtos = response.body().orEmpty() | ||
| val entities = dtos.map { it.toEntity() } | ||
| mcpTokenDao.replaceMcpTokens(entities) | ||
| } | ||
| } catch (e: Exception) { | ||
| if (e is CancellationException) throw e | ||
| // If network sync fails, fallback to Room cached tokens | ||
| } | ||
| } | ||
| emitAll( | ||
| mcpTokenDao.getMcpTokens().map { entities -> | ||
| Result.Success(entities.map { it.toDomain() }) | ||
| } | ||
| ) | ||
| }.flowOn(ioDispatcher) | ||
|
|
||
| override suspend fun createMcpToken(name: String): Result<CreatedMcpToken> { | ||
| if (!connectivityMonitor.isCurrentlyOnline()) { | ||
| return Result.Error(AppError.Network) | ||
| } | ||
| return safeApiCall(ioDispatcher) { | ||
| val response = mcpApiService.createApiKey(CreateApiKeyRequestDto(name = name)) | ||
| if (!response.isSuccessful) throw HttpException(response) | ||
| val dto = response.body() ?: throw IllegalStateException("Empty response body") | ||
| val createdToken = dto.toDomain() | ||
| try { | ||
| mcpTokenDao.upsertMcpTokens(listOf(dto.toEntity())) | ||
| } catch (e: Exception) { | ||
| if (e is CancellationException) throw e | ||
| // Local DB cache write failure must NOT drop or cause failure of the returned raw token | ||
| } | ||
| createdToken | ||
| } | ||
| } | ||
|
|
||
| override suspend fun deleteMcpToken(id: String): Result<Unit> { | ||
| if (!connectivityMonitor.isCurrentlyOnline()) { | ||
| return Result.Error(AppError.Network) | ||
| } | ||
| val result = safeApiCall(ioDispatcher) { | ||
| val response = mcpApiService.revokeApiKey(id) | ||
| if (!response.isSuccessful) throw HttpException(response) | ||
| } | ||
| if (result is Result.Success) { | ||
| try { | ||
| mcpTokenDao.deleteMcpToken(id) | ||
| } catch (e: Exception) { | ||
| if (e is CancellationException) throw e | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| override suspend fun regenerateMcpToken(id: String): Result<CreatedMcpToken> { | ||
| if (!connectivityMonitor.isCurrentlyOnline()) { | ||
| return Result.Error(AppError.Network) | ||
| } | ||
| val existingTokenName = mcpTokenDao.getMcpTokens().first().find { it.id == id }?.name ?: "MCP Token" | ||
| return safeApiCall(ioDispatcher) { | ||
| val createResponse = mcpApiService.createApiKey(CreateApiKeyRequestDto(name = existingTokenName)) | ||
| if (!createResponse.isSuccessful) throw HttpException(createResponse) | ||
| val dto = createResponse.body() ?: throw IllegalStateException("Empty response body") | ||
| val createdToken = dto.toDomain() | ||
|
|
||
| val revokeResponse = mcpApiService.revokeApiKey(id) | ||
| if (!revokeResponse.isSuccessful) throw HttpException(revokeResponse) | ||
|
|
||
| try { | ||
| mcpTokenDao.deleteMcpToken(id) | ||
| mcpTokenDao.upsertMcpTokens(listOf(dto.toEntity())) | ||
| } catch (e: Exception) { | ||
| if (e is CancellationException) throw e | ||
| // Local DB cache write failure must NOT drop or cause failure of the returned raw token | ||
| } | ||
| createdToken | ||
| } | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.