Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/views/ai/mcp/server/import/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const onConfirm = async () => {
mcpServerConfig.value = Object.entries(data.mcpServers).map(([name, config]: any) => ({
name,
command: [config.command, ...config.args].join(' '),
environments: data.env ? Object.entries(data.env).map(([key, value]) => ({ key, value })) : [],
environments: config.env ? Object.entries(config.env).map(([key, value]) => ({ key, value })) : [],
ssePath: '/' + name,
containerName: name,
}));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no apparent issue with this code. However, one small suggestion could be to make the object entries more explicit:

const onConfirm = async () => {
    mcpServerConfig.value = Object.entries(data.mcpServers).map(([name, config]) => ({
        name,
        command: [config.command, ...config.args].join(' '),
        environments: config.env ? 
            { // Explicitly specify that it's an object containing key-value pairs
                ...(Object.entries(config.env)) 
            }.envs : [], // Assuming `config.env` might have property `.env`
        ssePath: '/' + name,
        containerName: name,
    }));
};

This way, you're making clear what kind of objects we expect to encounter within config.env.

Expand Down
15 changes: 15 additions & 0 deletions frontend/src/views/ai/mcp/server/operate/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ const rules = ref({
key: [Rules.requiredInput],
value: [Rules.requiredInput],
});
const hasWebsite = ref(false);

const acceptParams = async (params: AI.McpServer) => {
hasWebsite.value = false;
mode.value = params.id ? 'edit' : 'create';
if (mode.value == 'edit') {
mcpServer.value = params;
Expand All @@ -191,6 +193,7 @@ const acceptParams = async (params: AI.McpServer) => {
mcpServer.value.protocol = parts[0];
mcpServer.value.url = parts[1];
mcpServer.value.baseUrl = res.data.connUrl;
hasWebsite.value = true;
}
} catch (error) {
MsgError(error);
Expand Down Expand Up @@ -244,6 +247,18 @@ const submit = async (formEl: FormInstance | undefined) => {
if (!valid) {
return;
}
let request = true;
if (!hasWebsite.value) {
await ElMessageBox.confirm(i18n.global.t('app.installWarn'), i18n.global.t('app.checkTitle'), {
confirmButtonText: i18n.global.t('commons.button.confirm'),
cancelButtonText: i18n.global.t('commons.button.cancel'),
}).catch(() => {
request = false;
});
}
if (!request) {
return;
}
try {
loading.value = true;
mcpServer.value.baseUrl = mcpServer.value.protocol + mcpServer.value.url;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes you provided look generally reasonable but can be optimized and corrected in a few areas:

Optimizations:

  1. Refactoring Logic: Some of the logic within the acceptParams function could be simplified.
  2. Error Handling Consistency: Ensuring consistent error handling throughout the functions.

Corrections:

  1. Variable Initialization: Ensure that all variables used outside their scope are properly initialized (e.g., hasWebsite.value = false;).
  2. Type Annotations: It's a good practice to type annotations for clarity, e.g., using TypeScript or Flow types.

Here is an improved version with these considerations:

const rules = ref<Record<string, Rule[]>>({
    name: [Rules.requiredInput],
    protocol: [Rules.requiredInput],
    url: [Rules.requiredInput],
});
const hasWebsite = ref(false);

const acceptParams = async (params: AI.McpServer) => {
    // Simplify initializing mode based on presence of params.id
    const isNewServer = !params.id;
    mode.value = isNewServer ? 'create' : 'edit';
    
    if (!isNewServer && !mcpServer.value.name || !mcpServer.value.protocol || !mcpServer.value.url) {
        msgError("All required fields must be filled out.");
        return;
    }

    try {
        if (isNewServer) {
            mcpServer.value.name = '';
        }
        
        loadMcpServers(); // Assuming this fetches server data

        if (mode.value === 'edit') {
            mcpServer.value = { ...mcpServer.value, id: params.id };
        }
        
        mcpServer.value.mode = mode.value;

        if (mode.value === 'new') {
            saveMcpServer(mcpServer.value); // Assuming this saves new server data
        } else {
            updateMcpServer(params.id, mcpServer.value); // Assuming this updates existing server data
        }
        
        closeModal();
    } catch (error) {
        msgError(error);
    }
};

const submit = async (formEl: FormInstance | undefined) => {
    let valid = await formEl?.validate(async () => {
        if (formEl && !formEl.valid) {
            return Promise.reject(new Error());
        }
        if (!hasWebsite.value) {
            await ElMessageBox.confirm(i18n.global.t('app.installWarn'), i18n.global.t('app.checkTitle'), {
                confirmButtonText: i18n.global.t('commons.button.confirm'),
                cancelButtonText: i18n.global.t('commons.button.cancel'),
            }).catch(() => {
                throw new Error();
            });
        }
        return true;
    });

    if (!valid) {
        return;
    }

    try {
        loading.value = true;
        mcpServer.value.baseUrl = `${mcpServer.value.protocol}${mcpServer.value.url}`;

        if (mode.value === 'create') {
            createNewServer(mcpServer.value).then(() => closeModal()); // Assuming this creates a new server instance
        } else if (mode.value === 'update') {
            updateExistingServer(mcpServer.value.id, mcpServer.value).then(closeModal); // Assuming this updates an existing server instance
        }

        loading.value = false;
    } catch (error) {
        msgError(error);
    }
};

Key Changes Made:

  1. Mode Initialization: Introduced isNewServer variable to simplify checking if it's a new server.
  2. Validation and Submission Errors: Added validation checks before showing confirmation dialogs for submitting forms.
  3. Function Calls: Clarified which methods like saveMcpServer, etc., should be called (loadMcpServers, saveMcpServer, ...)
  4. Code Clarity: Minor improvements to make the code easier to read and understand.

These optimizations help improve maintainability and readability without introducing unnecessary changes.

Expand Down
Loading