Skip to content

Commit 74c4a3c

Browse files
committed
优化mcp校验
1 parent 1233733 commit 74c4a3c

File tree

5 files changed

+94
-5
lines changed

5 files changed

+94
-5
lines changed

src-tauri/src/models.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl Default for McpServerConfig {
3737
Self {
3838
enabled: false,
3939
host: "127.0.0.1".to_string(),
40-
port: 8080,
40+
port: 9800,
4141
allow_query: true,
4242
allow_create: true,
4343
allow_update: true,

src/components/McpServer.vue

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,20 @@
4949
v-model="mcpConfig.host"
5050
type="text"
5151
placeholder="127.0.0.1"
52-
class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white"
52+
@blur="validateHost"
53+
:class="[
54+
'w-full px-3 py-2 border rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-white',
55+
hostError
56+
? 'border-red-500 focus:border-red-500 focus:ring-red-500'
57+
: 'border-gray-300 dark:border-gray-600',
58+
]"
5359
/>
60+
<p
61+
v-if="hostError"
62+
class="mt-1 text-xs text-red-500 dark:text-red-400"
63+
>
64+
{{ hostError }}
65+
</p>
5466
</div>
5567
<div>
5668
<label
@@ -61,9 +73,21 @@
6173
<input
6274
v-model.number="mcpConfig.port"
6375
type="number"
64-
placeholder="8080"
65-
class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white"
76+
placeholder="9800"
77+
@blur="validatePort"
78+
:class="[
79+
'w-full px-3 py-2 border rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-white',
80+
portError
81+
? 'border-red-500 focus:border-red-500 focus:ring-red-500'
82+
: 'border-gray-300 dark:border-gray-600',
83+
]"
6684
/>
85+
<p
86+
v-if="portError"
87+
class="mt-1 text-xs text-red-500 dark:text-red-400"
88+
>
89+
{{ portError }}
90+
</p>
6791
</div>
6892
</div>
6993

@@ -200,6 +224,7 @@
200224
import { ref, onMounted, watch } from "vue";
201225
import { invoke } from "@tauri-apps/api/core";
202226
import { useSettingsDatabase } from "../composables/useSettingsDatabase";
227+
import { useI18n } from "vue-i18n";
203228
204229
interface McpServerConfig {
205230
autoStart: boolean;
@@ -208,6 +233,7 @@ interface McpServerConfig {
208233
}
209234
210235
const { saveSetting, getSetting } = useSettingsDatabase();
236+
const { t } = useI18n();
211237
212238
// 响应式数据
213239
const mcpConfig = ref<McpServerConfig>({
@@ -221,6 +247,8 @@ const isStarting = ref(false);
221247
const isStopping = ref(false);
222248
const isServerRunning = ref(false);
223249
const serverAddress = ref<string>("");
250+
const hostError = ref<string>("");
251+
const portError = ref<string>("");
224252
225253
// 加载配置
226254
const loadConfig = async () => {
@@ -271,6 +299,14 @@ const checkServerStatus = async () => {
271299
272300
// 启动 MCP Server
273301
const startMcpServer = async () => {
302+
// 验证输入
303+
const isHostValid = validateHost();
304+
const isPortValid = validatePort();
305+
306+
if (!isHostValid || !isPortValid) {
307+
return;
308+
}
309+
274310
isStarting.value = true;
275311
try {
276312
const result = await invoke("start_mcp_server", {
@@ -316,6 +352,47 @@ const getMcpJson = () => {
316352
return JSON.stringify(mcpJsonConfig, null, 2);
317353
};
318354
355+
// 验证IP地址
356+
const validateHost = () => {
357+
const host = mcpConfig.value.host.trim();
358+
if (!host) {
359+
hostError.value = t("settings.hostRequired");
360+
return false;
361+
}
362+
363+
// IP地址正则表达式
364+
const ipRegex =
365+
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
366+
const localhostRegex = /^localhost$/;
367+
368+
if (!ipRegex.test(host) && !localhostRegex.test(host)) {
369+
hostError.value = t("settings.invalidHost");
370+
return false;
371+
}
372+
373+
hostError.value = "";
374+
return true;
375+
};
376+
377+
// 验证端口
378+
const validatePort = () => {
379+
const port = mcpConfig.value.port;
380+
if (!port || port < 1 || port > 65535) {
381+
portError.value = t("settings.portRangeError");
382+
return false;
383+
}
384+
385+
// 检查是否为常用端口
386+
const commonPorts = [80, 443, 22, 21, 23, 25, 53, 110, 143, 993, 995];
387+
if (commonPorts.includes(port)) {
388+
portError.value = t("settings.commonPortWarning");
389+
return true; // 返回true因为这只是警告
390+
}
391+
392+
portError.value = "";
393+
return true;
394+
};
395+
319396
// 复制 MCP JSON 配置
320397
const copyMcpJson = async () => {
321398
try {

src/components/PluginSettings.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
<input
8181
v-model.number="mcpConfig.port"
8282
type="number"
83-
placeholder="8080"
83+
placeholder="9800"
8484
class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white"
8585
/>
8686
</div>

src/i18n/locales/en-US.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ export default {
215215
copyConfig: "Copy Config",
216216
runningStatus: "Running Status",
217217
address: "Address",
218+
219+
// Validation error messages
220+
hostRequired: "Please enter an IP address",
221+
invalidHost: "Please enter a valid IP address or localhost",
222+
portRangeError: "Port number must be between 1-65535",
223+
commonPortWarning: "Warning: This port is commonly used and may be occupied by other services",
218224

219225
// Update
220226
checkUpdate: "Check for Updates",

src/i18n/locales/zh-CN.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ export default {
215215
copyConfig: "复制配置",
216216
runningStatus: "运行状态",
217217
address: "地址",
218+
219+
// 验证错误信息
220+
hostRequired: "请输入IP地址",
221+
invalidHost: "请输入有效的IP地址或localhost",
222+
portRangeError: "端口号必须在1-65535之间",
223+
commonPortWarning: "警告:该端口为常用端口,可能被其他服务占用",
218224
},
219225

220226
// 快捷键

0 commit comments

Comments
 (0)