Skip to content

Commit ab48a97

Browse files
feat: MCP bridge + settings UI — unified capability types (Phase 1+2) (#127)
* refactor: unify capability types with data-studio-agent - Remove duplicate capabilities/types.rs and capabilities/registry.rs - Import types and registry from data-studio-agent - Replace SourceKind::SqlKit with SourceKind::AppLocal - Update init_registry() call to pass registration function array - Update agent/executor.rs and agent_adapters.rs to use data-studio-agent paths * feat: add axum-based MCP bridge HTTP server to sqlkit Introduces an embedded HTTP bridge (axum, port 9121) that exposes the capability system over HTTP for the external data-studio-mcp server. Endpoints: POST /tools — list all agent-tagged capabilities POST /invoke — execute a capability by name with connection resolution GET /health — health check Port auto-fallback via portpicker. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <[email protected]> * feat: wire MCP bridge into sqlkit Tauri setup and commands Integrates the MCP bridge into sqlkit's app lifecycle: - Initialize McpServerHandle as managed Tauri state - Read mcp-config.json and auto-start bridge in setup hook - Register get_mcp_status and save_mcp_config Tauri commands Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <[email protected]> * fix: address MCP bridge review issues - Security: reject Elevated/Destructive capabilities on bridge (403) - Lifecycle: save_mcp_config now stops server when autoStart=false - Stale port file: liveness-test (TCP connect) before reporting running - Write port file BEFORE spawning server to avoid orphaned task - app_version: use real version from package_info() - McpConfig::load: log warning on corrupt/parse error - TOCTOU: remove port_available check, bind directly with fallback Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <[email protected]> * fix: eliminate port-file restart race — await JoinHandle instead of sleeping 200ms * feat: add MCP Bridge settings UI to sqlkit settings page --------- Co-authored-by: Sisyphus <[email protected]>
1 parent 5a78ab2 commit ab48a97

6 files changed

Lines changed: 195 additions & 10 deletions

File tree

‎src-tauri/src/mcp_bridge.rs‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ use tokio::sync::oneshot;
1919

2020
pub struct McpServerHandle {
2121
pub shutdown_tx: Mutex<Option<oneshot::Sender<()>>>,
22+
pub server_task: Mutex<Option<tokio::task::JoinHandle<()>>>,
2223
}
2324

2425
impl McpServerHandle {
2526
pub fn new() -> Self {
2627
Self {
2728
shutdown_tx: Mutex::new(None),
29+
server_task: Mutex::new(None),
2830
}
2931
}
3032
}
@@ -248,7 +250,10 @@ async fn remove_port_file(app_data_dir: &Path) {
248250
let _ = tokio::fs::remove_file(path).await;
249251
}
250252

251-
async fn send_shutdown(handle: &AppHandle) {
253+
/// Shut down the bridge if it's running.
254+
/// Returns the JoinHandle for the old server task, if any, so the caller can
255+
/// await its full completion (including port file cleanup) before starting a new one.
256+
async fn send_shutdown(handle: &AppHandle) -> Option<tokio::task::JoinHandle<()>> {
252257
let server_handle: tauri::State<'_, McpServerHandle> = handle.state();
253258
let old_tx = {
254259
let mut tx = server_handle.shutdown_tx.lock().unwrap();
@@ -257,6 +262,8 @@ async fn send_shutdown(handle: &AppHandle) {
257262
if let Some(sender) = old_tx {
258263
let _ = sender.send(());
259264
}
265+
let mut task = server_handle.server_task.lock().unwrap();
266+
task.take()
260267
}
261268

262269
pub async fn start(
@@ -282,7 +289,7 @@ pub async fn start(
282289
write_port_file(&app_data_dir, preferred_port).await?;
283290

284291
let data_dir = app_data_dir.clone();
285-
tokio::spawn(async move {
292+
let join_handle = tokio::spawn(async move {
286293
log::info!("MCP bridge listening on 127.0.0.1:{}", preferred_port);
287294
axum::serve(listener, app)
288295
.with_graceful_shutdown(async {
@@ -293,6 +300,11 @@ pub async fn start(
293300
.ok();
294301
let _ = remove_port_file(&data_dir).await;
295302
});
303+
{
304+
let mcp_handle: tauri::State<'_, McpServerHandle> = handle.state();
305+
let mut task = mcp_handle.server_task.lock().unwrap();
306+
*task = Some(join_handle);
307+
}
296308

297309
Ok(preferred_port)
298310
}
@@ -322,7 +334,7 @@ pub async fn start(
322334
write_port_file(&app_data_dir, random_port).await?;
323335

324336
let data_dir = app_data_dir.clone();
325-
tokio::spawn(async move {
337+
let join_handle = tokio::spawn(async move {
326338
log::info!("MCP bridge listening on 127.0.0.1:{}", random_port);
327339
axum::serve(listener, app)
328340
.with_graceful_shutdown(async {
@@ -333,6 +345,11 @@ pub async fn start(
333345
.ok();
334346
let _ = remove_port_file(&data_dir).await;
335347
});
348+
{
349+
let mcp_handle: tauri::State<'_, McpServerHandle> = handle.state();
350+
let mut task = mcp_handle.server_task.lock().unwrap();
351+
*task = Some(join_handle);
352+
}
336353

337354
Ok(random_port)
338355
}
@@ -415,11 +432,15 @@ pub async fn save_mcp_config(
415432
let config = McpConfig { port, auto_start };
416433
config.save(&app_data_dir)?;
417434

418-
send_shutdown(&app).await;
435+
// Always shut down the current server first and await its full exit
436+
// (including port file cleanup) to prevent the new server's port file
437+
// from being deleted by stale cleanup.
438+
let old_task = send_shutdown(&app).await;
439+
if let Some(h) = old_task {
440+
let _ = h.await;
441+
}
419442

420443
if auto_start {
421-
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
422-
423444
let (new_shutdown_tx, new_shutdown_rx) = oneshot::channel();
424445
{
425446
let server_handle: tauri::State<'_, McpServerHandle> = app.state();

‎src/components/database-browser/DataTableView.vue‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ watch(
856856
<div class="px-3 py-1.5 border-b bg-muted/20 flex shrink-0 gap-1 items-center">
857857
<!-- Search input — searches across all columns -->
858858
<div class="flex-1 min-w-0 relative">
859-
<span class="i-carbon-search text-muted-foreground absolute left-2 top-1/2 -translate-y-1/2 h-3.5 w-3.5 pointer-events-none" />
859+
<span class="i-carbon-search text-muted-foreground h-3.5 w-3.5 pointer-events-none left-2 top-1/2 absolute -translate-y-1/2" />
860860
<Input
861861
ref="searchInputRef"
862862
v-model="searchTerm"
@@ -875,7 +875,7 @@ watch(
875875
<!-- Clear search button (inside input, on the right) -->
876876
<button
877877
v-if="searchTerm"
878-
class="text-muted-foreground right-1 top-1/2 absolute -translate-y-1/2 flex items-center justify-center h-5 w-5 rounded hover:bg-accent"
878+
class="text-muted-foreground rounded flex h-5 w-5 items-center right-1 top-1/2 justify-center absolute hover:bg-accent -translate-y-1/2"
879879
:title="t('components.dataTableView.clearSearch')"
880880
@click.stop="clearSearch"
881881
>
@@ -885,7 +885,7 @@ watch(
885885

886886
<!-- Filter input -->
887887
<div class="flex-1 min-w-0 relative">
888-
<span class="i-carbon-filter text-muted-foreground absolute left-2 top-1/2 -translate-y-1/2 h-3.5 w-3.5 pointer-events-none" />
888+
<span class="i-carbon-filter text-muted-foreground h-3.5 w-3.5 pointer-events-none left-2 top-1/2 absolute -translate-y-1/2" />
889889
<Input
890890
v-model="filterInput"
891891
:placeholder="t('components.dataTableView.filterPlaceholder')"
@@ -895,7 +895,7 @@ watch(
895895
<!-- Clear filter button (inside input, on the right) -->
896896
<button
897897
v-if="filterInput || appliedFilter"
898-
class="text-muted-foreground right-1 top-1/2 absolute -translate-y-1/2 flex items-center justify-center h-5 w-5 rounded hover:bg-accent"
898+
class="text-muted-foreground rounded flex h-5 w-5 items-center right-1 top-1/2 justify-center absolute hover:bg-accent -translate-y-1/2"
899899
:title="t('components.dataTableView.clearFilter')"
900900
@click.stop="clearFilter"
901901
>

‎src/lang/enUS.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,17 @@ export const enUS = {
405405
auto: 'Auto (Follow System)',
406406
},
407407
},
408+
mcp: {
409+
title: 'MCP Bridge',
410+
status: 'Status',
411+
running: 'Running on port {port}',
412+
stopped: 'Stopped',
413+
port: 'Port',
414+
portDesc: 'Port for the MCP bridge server (leave empty for auto)',
415+
restart: 'Restart Bridge',
416+
autoStart: 'Auto-start',
417+
autoStartDesc: 'Start the MCP bridge automatically when app launches',
418+
},
408419
about: {
409420
title: 'About',
410421
version: 'Version',

‎src/lang/zhCN.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,17 @@ export const zhCN = {
405405
auto: '自动(跟随系统)',
406406
},
407407
},
408+
mcp: {
409+
title: 'MCP 桥接',
410+
status: '状态',
411+
running: '运行中(端口 {port})',
412+
stopped: '已停止',
413+
port: '端口',
414+
portDesc: 'MCP 桥接服务器端口(留空自动选择)',
415+
restart: '重启桥接',
416+
autoStart: '自动启动',
417+
autoStartDesc: '应用启动时自动启动 MCP 桥接',
418+
},
408419
about: {
409420
title: '关于',
410421
version: '版本',

‎src/pages/SettingsPage.vue‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { useAppUpdater } from '@/composables/useAppUpdater'
1313
import { ThemeType, useAppStore } from '@/store/appStore'
1414
import AiSettings from '@/views/setting/ai-settings.vue'
1515
import JreDriverSection from '@/views/setting/jre-driver-section.vue'
16+
import McpBridge from '@/views/setting/mcp-bridge.vue'
1617
1718
const appStore = useAppStore()
1819
const { t, locale: _locale } = useI18n()
@@ -193,6 +194,9 @@ async function handleCheckUpdates() {
193194
<TabsTrigger value="jre">
194195
{{ t('pages.settings.jre.title') }}
195196
</TabsTrigger>
197+
<TabsTrigger value="mcp">
198+
{{ t('pages.settings.mcp.title') }}
199+
</TabsTrigger>
196200
<TabsTrigger value="about">
197201
{{ t('pages.settings.about.title') }}
198202
</TabsTrigger>
@@ -592,6 +596,11 @@ async function handleCheckUpdates() {
592596
<JreDriverSection />
593597
</TabsContent>
594598

599+
<!-- MCP Bridge Tab -->
600+
<TabsContent value="mcp" class="mt-0">
601+
<McpBridge />
602+
</TabsContent>
603+
595604
<!-- About Tab -->
596605
<TabsContent value="about" class="mt-0">
597606
<Card>

‎src/views/setting/mcp-bridge.vue‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<script setup lang="ts">
2+
import { invoke } from '@tauri-apps/api/core'
3+
import { onMounted, ref } from 'vue'
4+
import { useI18n } from 'vue-i18n'
5+
import { Button } from '@/components/ui/button'
6+
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
7+
import { Input } from '@/components/ui/input'
8+
import { Label } from '@/components/ui/label'
9+
import { Switch } from '@/components/ui/switch'
10+
11+
const { t } = useI18n()
12+
13+
const status = ref<{ running: boolean, port: number | null }>({ running: false, port: null })
14+
const portValue = ref<number | undefined>(undefined)
15+
const autoStart = ref(true)
16+
const loading = ref(false)
17+
18+
async function refreshStatus() {
19+
const raw = await invoke<string>('get_mcp_status')
20+
const data = JSON.parse(raw)
21+
status.value = { running: data.running, port: data.port ?? null }
22+
portValue.value = data.configuredPort ?? undefined
23+
autoStart.value = data.autoStart
24+
}
25+
26+
onMounted(async () => {
27+
try {
28+
await refreshStatus()
29+
}
30+
catch (e) {
31+
console.error('Failed to get MCP status:', e)
32+
}
33+
})
34+
35+
function onPortChange(val: string | number) {
36+
const num = Number(val)
37+
portValue.value = Number.isInteger(num) && num > 0 ? num : undefined
38+
}
39+
40+
async function onAutoStartChange(val: boolean) {
41+
autoStart.value = val
42+
try {
43+
await invoke('save_mcp_config', { port: portValue.value ?? null, autoStart: val })
44+
}
45+
catch (e) {
46+
console.error('Failed to save MCP config:', e)
47+
}
48+
}
49+
50+
async function restartBridge() {
51+
loading.value = true
52+
try {
53+
await invoke('save_mcp_config', { port: portValue.value ?? null, autoStart: autoStart.value })
54+
await refreshStatus()
55+
}
56+
catch (e) {
57+
console.error('Failed to restart MCP bridge:', e)
58+
}
59+
finally {
60+
loading.value = false
61+
}
62+
}
63+
</script>
64+
65+
<template>
66+
<Card>
67+
<CardHeader>
68+
<CardTitle>{{ t('pages.settings.mcp.title') }}</CardTitle>
69+
<CardDescription>{{ t('pages.settings.mcp.portDesc') }}</CardDescription>
70+
</CardHeader>
71+
<CardContent class="space-y-5">
72+
<!-- Status -->
73+
<div class="flex items-center justify-between">
74+
<Label>{{ t('pages.settings.mcp.status') }}</Label>
75+
<div class="flex gap-2 items-center">
76+
<span
77+
class="rounded-full h-2.5 w-2.5"
78+
:class="status.running ? 'bg-green-500' : 'bg-red-500'"
79+
/>
80+
<span class="text-sm text-muted-foreground">
81+
{{
82+
status.running
83+
? t('pages.settings.mcp.running', { port: status.port })
84+
: t('pages.settings.mcp.stopped')
85+
}}
86+
</span>
87+
</div>
88+
</div>
89+
90+
<!-- Port + Restart -->
91+
<div class="flex items-center justify-between">
92+
<div class="space-y-1">
93+
<Label>{{ t('pages.settings.mcp.port') }}</Label>
94+
<p class="text-xs text-muted-foreground">
95+
{{ t('pages.settings.mcp.portDesc') }}
96+
</p>
97+
</div>
98+
<div class="flex gap-3 items-center">
99+
<Input
100+
type="number"
101+
:model-value="portValue"
102+
min="1024"
103+
max="65535"
104+
class="w-28"
105+
@update:model-value="onPortChange($event)"
106+
/>
107+
<Button
108+
variant="outline"
109+
size="sm"
110+
:disabled="loading"
111+
@click="restartBridge"
112+
>
113+
{{ t('pages.settings.mcp.restart') }}
114+
</Button>
115+
</div>
116+
</div>
117+
118+
<!-- Auto-start -->
119+
<div class="flex items-center justify-between">
120+
<div class="space-y-1">
121+
<Label>{{ t('pages.settings.mcp.autoStart') }}</Label>
122+
<p class="text-xs text-muted-foreground">
123+
{{ t('pages.settings.mcp.autoStartDesc') }}
124+
</p>
125+
</div>
126+
<Switch
127+
:checked="autoStart"
128+
@update:checked="onAutoStartChange"
129+
/>
130+
</div>
131+
</CardContent>
132+
</Card>
133+
</template>

0 commit comments

Comments
 (0)