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
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
200224import { ref , onMounted , watch } from " vue" ;
201225import { invoke } from " @tauri-apps/api/core" ;
202226import { useSettingsDatabase } from " ../composables/useSettingsDatabase" ;
227+ import { useI18n } from " vue-i18n" ;
203228
204229interface McpServerConfig {
205230 autoStart: boolean ;
@@ -208,6 +233,7 @@ interface McpServerConfig {
208233}
209234
210235const { saveSetting, getSetting } = useSettingsDatabase ();
236+ const { t } = useI18n ();
211237
212238// 响应式数据
213239const mcpConfig = ref <McpServerConfig >({
@@ -221,6 +247,8 @@ const isStarting = ref(false);
221247const isStopping = ref (false );
222248const isServerRunning = ref (false );
223249const serverAddress = ref <string >(" " );
250+ const hostError = ref <string >(" " );
251+ const portError = ref <string >(" " );
224252
225253// 加载配置
226254const loadConfig = async () => {
@@ -271,6 +299,14 @@ const checkServerStatus = async () => {
271299
272300// 启动 MCP Server
273301const 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 配置
320397const copyMcpJson = async () => {
321398 try {
0 commit comments