Skip to content

Conversation

@letr007
Copy link
Contributor

@letr007 letr007 commented Jan 30, 2026

修复更新MCP服务名称时更新失败的问题
fix #4756

Modifications / 改动点

tools.py: 支持接收oldName参数以重命名MCP服务
McpServersSection.vue: 添加oldName参数,在更新MCP服务名称时保存原名称

Screenshots or Test Results / 运行截图或测试结果

image image

Checklist / 检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。/ If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
  • 👀 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
  • 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.

Summary by Sourcery

错误修复:

  • 通过在后端和界面中分别跟踪原始名称和新名称,修复了当 MCP 服务器名称发生更改时更新该服务器会失败的问题。
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Fix failure when updating an MCP server whose name has changed by tracking the original name separately from the new name in both backend and UI.

When editing the MCP server configuration, you can now change the server name. The frontend will save the original name in edit mode, and the backend will recognize the rename operation through the oldName field.
@auto-assign auto-assign bot requested review from Fridemn and advent259141 January 30, 2026 11:07
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend area:webui The bug / feature is about webui(dashboard) of astrbot. labels Jan 30, 2026
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - 我发现了 1 个问题,并且给出了一些整体性的反馈:

  • update_mcp_server 中,name/old_name/is_rename 的分支逻辑分散在多个位置;可以考虑提取一个小的辅助函数,或至少把“重命名 vs 非重命名”的路径(配置变更 + 客户端禁用/启用)集中处理,这样在未来再次更新这块逻辑时就不容易出现分支不一致或遗漏。
  • 在前端中,originalServerName 只在从 editMcpServer 打开编辑弹窗时被设置;如果这个弹窗将来会在其他流程中复用或再次打开,那么更安全的做法可能是:始终基于当前加载的 server 初始化它(以及/或者从路由/状态中派生),以避免发送过期或为空的 oldName
给 AI Agent 的提示
Please address the comments from this code review:

## Overall Comments
- In `update_mcp_server`, the `name`/`old_name`/`is_rename` branching is spread across several places; consider extracting a small helper or at least centralizing the rename vs non-rename path (config mutation + client disable/enable) to avoid future divergence or missed branches when this logic is updated again.
- On the frontend, `originalServerName` is only set when opening the edit dialog from `editMcpServer`; if the dialog can be reused or reopened in other flows later, it may be safer to always initialize it based on the loaded server (and/or derive it from route/state) to avoid stale or empty `oldName` being sent.

## Individual Comments

### Comment 1
<location> `astrbot/dashboard/routes/tools.py:143` </location>
<code_context>
+            if old_name not in config["mcpServers"]:
+                return Response().error(f"服务器 {old_name} 不存在").__dict__
+
+            is_rename = name != old_name

             # 获取活动状态
</code_context>

<issue_to_address>
**issue:** 请考虑处理“新的服务器名称已存在”的情况,以避免被静默覆盖。

当 `is_rename``True` 时,下面这段代码:

```python
if is_rename:
    config["mcpServers"].pop(old_name)
    config["mcpServers"][name] = server_config
```

会覆盖已有的 `config['mcpServers'][name]` 条目,从而导致之前的配置丢失。可以考虑在执行前检查 `if name in config['mcpServers'] and name != old_name`,如果条件满足则返回一个错误(例如:`"服务器 X 已存在"`),而不是直接覆盖,以保持重命名行为可预测并且避免名称冲突。
</issue_to_address>

Sourcery 对开源项目是免费的——如果你觉得我们的 Review 有帮助,欢迎分享 ✨
帮我变得更有用!请对每条评论点 👍 或 👎,我会根据你的反馈来改进后续的 Review。
Original comment in English

Hey - I've found 1 issue, and left some high level feedback:

  • In update_mcp_server, the name/old_name/is_rename branching is spread across several places; consider extracting a small helper or at least centralizing the rename vs non-rename path (config mutation + client disable/enable) to avoid future divergence or missed branches when this logic is updated again.
  • On the frontend, originalServerName is only set when opening the edit dialog from editMcpServer; if the dialog can be reused or reopened in other flows later, it may be safer to always initialize it based on the loaded server (and/or derive it from route/state) to avoid stale or empty oldName being sent.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `update_mcp_server`, the `name`/`old_name`/`is_rename` branching is spread across several places; consider extracting a small helper or at least centralizing the rename vs non-rename path (config mutation + client disable/enable) to avoid future divergence or missed branches when this logic is updated again.
- On the frontend, `originalServerName` is only set when opening the edit dialog from `editMcpServer`; if the dialog can be reused or reopened in other flows later, it may be safer to always initialize it based on the loaded server (and/or derive it from route/state) to avoid stale or empty `oldName` being sent.

## Individual Comments

### Comment 1
<location> `astrbot/dashboard/routes/tools.py:143` </location>
<code_context>
+            if old_name not in config["mcpServers"]:
+                return Response().error(f"服务器 {old_name} 不存在").__dict__
+
+            is_rename = name != old_name

             # 获取活动状态
</code_context>

<issue_to_address>
**issue:** Consider handling the case where the new server name already exists to avoid silent overwrites.

When `is_rename` is `True`, this block:

```python
if is_rename:
    config["mcpServers"].pop(old_name)
    config["mcpServers"][name] = server_config
```

will overwrite an existing `config['mcpServers'][name]` entry, causing loss of the previous configuration. Consider checking `if name in config['mcpServers'] and name != old_name` and returning an error (e.g., `"服务器 X 已存在"`) instead of overwriting to keep rename behavior predictable and collision-free.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

…for name conflicts

When renaming an MCP server, add a check to see if the target name already exists. If the name exists and it is a rename operation, return an error message to avoid overwriting the configuration.
@letr007
Copy link
Contributor Author

letr007 commented Jan 31, 2026

为了快速修复问题,本次修改以最小变更为原则,便未进行架构调整

Copy link
Member

@Soulter Soulter left a comment

Choose a reason for hiding this comment

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

lgtm

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Feb 1, 2026
@Soulter Soulter merged commit dc06bc9 into AstrBotDevs:master Feb 1, 2026
6 checks passed
@Soulter Soulter changed the title fix(mcp): support renaming when editing MCP servers fix(mcp): cannot rename MCP Server Feb 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend area:webui The bug / feature is about webui(dashboard) of astrbot. lgtm This PR has been approved by a maintainer size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] webui 无法修改mcp服务器显示名称

2 participants