Skip to content

Conversation

@ensorrow
Copy link
Contributor

@ensorrow ensorrow commented May 19, 2025

Types

  • 🪚 Refactors

Background or solution

refactor: update tool registration to prevent duplicates

Changelog

refactor: update tool registration to prevent duplicates

Summary by CodeRabbit

  • Bug Fixes
    • 修复了重复工具名称的问题,工具列表中相同名称的工具会被更新替换,避免重复。
    • 增加了对聊天消息列表为空时的安全检查,防止访问不存在的消息ID导致错误。
    • 优化了服务器启动流程,改为静态导入相关模块,提升启动效率。
  • New Features
    • 多个工具支持将输入参数从snake_case自动转换为camelCase,提升兼容性和一致性。
    • 新增创建文件并写入文本功能,支持自动创建文件夹并写入指定内容,操作更简便。

@ensorrow ensorrow requested a review from life2015 May 19, 2025 07:43
@opensumi opensumi bot added the ⚙️ refactor Refactor code label May 19, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 19, 2025

"""

Walkthrough

本次更改修改了registerMCPTool方法的内部逻辑,确保在向tools数组注册新工具时不会出现同名工具的重复项。如果发现同名工具,则替换原有工具,否则追加新工具。此外,更新了@modelcontextprotocol/sdk依赖版本,多个工具模块中增加了访问最后消息ID的安全检查,防止空消息数组导致的运行时错误;mcp-server.sse.ts中将SSEClientTransport的动态导入改为静态导入。部分工具输入schema增加了snake_case到camelCase的转换,相关处理逻辑做了对应调整。测试中将一个启动服务器的测试用例标记为跳过,等待MCP SDK升级后修改。部分工具中路径拼接改用path.join替代字符串拼接。新增CreateNewFileWithTextHandler类并重构相关工具以委托该处理器完成文件创建及写入。

Changes

文件路径 变更摘要
packages/ai-native/src/browser/mcp/mcp-server.feature.registry.ts 修改registerMCPTool方法,避免同名工具重复注册,若存在则替换,否则追加。
packages/ai-native/src/browser/mcp/tools/fileSearch.ts 增加访问最后消息ID的安全检查,防止空消息数组导致的运行时错误。
packages/ai-native/src/browser/mcp/tools/grepSearch.ts 增加访问最后消息ID的安全检查;输入schema增加snake_case到camelCase转换,处理逻辑对应调整。
packages/ai-native/src/browser/mcp/tools/listDir.ts 增加访问最后消息ID的安全检查,防止空消息数组导致的运行时错误;路径拼接改用path.join替代字符串拼接。
packages/ai-native/src/browser/mcp/tools/createNewFileWithText.ts 输入schema增加snake_case到camelCase转换,处理逻辑对应调整,重构为委托新引入的CreateNewFileWithTextHandler处理。
packages/ai-native/src/browser/mcp/tools/handlers/RunCommand.ts 输入schema增加snake_case到camelCase转换,处理逻辑对应调整,使用camelCase属性。
packages/ai-native/src/browser/mcp/tools/handlers/ListDir.ts 修改导入路径模块;路径拼接改用path.join替代字符串拼接。
packages/ai-native/src/browser/mcp/tools/handlers/ReadFile.ts 新增path模块导入;路径拼接改用path.join替代字符串拼接。
packages/ai-native/src/browser/mcp/tools/handlers/CreateNewFileWithText.ts 新增CreateNewFileWithTextHandler类,封装创建文件及写入逻辑,使用依赖注入管理工作区和文件服务。
packages/ai-native/src/node/mcp-server.sse.ts SSEClientTransport的动态导入改为静态导入,移除启动时的异步导入。
packages/ai-native/package.json 更新依赖@modelcontextprotocol/sdk版本,从^1.3.1升级到^1.11.4
packages/ai-native/test/node/mcp-server.sse.test.ts SSEMCPServer测试套件中“should start the server successfully”测试用例标记为跳过,等待MCP SDK升级后修改。

Possibly related PRs

Suggested labels

🎨 feature
"""

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

yarn install v1.22.22
[1/4] Resolving packages...
(node:11652) [DEP0169] DeprecationWarning: url.parse() behavior is not standardized and prone to errors that have security implications. Use the WHATWG URL API instead. CVEs are not issued for url.parse() vulnerabilities.
(Use node --trace-deprecation ... to show where the warning was created)
warning eslint@8.57.1: This version is no longer supported. Please see https://eslint.org/version-support for other options.
warning eslint > @humanwhocodes/config-array@0.13.0: Use @eslint/config-array instead
warning eslint > @humanwhocodes/config-array > @humanwhocodes/object-schema@2.0.3: Use @eslint/object-schema instead
warning eslint > file-entry-cache > flat-cache > rimraf@3.0.2: Rimraf versions prior to v4 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob@7.2.3: Glob versions prior to v9 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob > inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
warning eslint-import-resolver-typescript > glob@7.2.3: Glob versions prior to v9 are no longer supported
error Couldn't find any versions for "@opensumi/ide-dev-tool" that matches "workspace:*"
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d14998b and 960b014.

📒 Files selected for processing (1)
  • packages/ai-native/src/browser/mcp/tools/handlers/CreateNewFileWithText.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/ai-native/src/browser/mcp/tools/handlers/CreateNewFileWithText.ts
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: unittest (macos-latest, 18.x, jsdom)
  • GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
  • GitHub Check: unittest (ubuntu-latest, 18.x, node)
  • GitHub Check: unittest (macos-latest, 18.x, node)
  • GitHub Check: build (ubuntu-latest, 20.x)
  • GitHub Check: build (macos-latest, 20.x)
  • GitHub Check: build-windows
  • GitHub Check: ubuntu-latest, Node.js 20.x
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/ai-native/src/browser/mcp/mcp-server.feature.registry.ts (1)

47-52: 代码更改有效地防止了工具重复注册

此更改通过检查已存在的同名工具并进行替换而非简单追加,成功地防止了工具列表中出现重复项。这是一个很好的实现,可以确保每个工具名称在注册表中的唯一性。

不过,我注意到getMCPTool方法(第42-44行)在查找工具时考虑了serverName参数,而此处只比较工具名称。为了保持一致性,可以考虑在检查重复时也将serverName纳入考虑:

- const existingIndex = this.tools.findIndex((t) => t.name === tool.name);
+ // 考虑serverName进行更精确的工具匹配
+ const existingIndex = this.tools.findIndex((t) => {
+   return getToolName(t.name, t.serverName || BUILTIN_MCP_SERVER_NAME) === 
+          getToolName(tool.name, tool.serverName || BUILTIN_MCP_SERVER_NAME);
+ });

这样修改可以确保工具的唯一性更加精确,与getMCPTool方法的查找逻辑保持一致。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 517461a and ffc339a.

📒 Files selected for processing (1)
  • packages/ai-native/src/browser/mcp/mcp-server.feature.registry.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: ubuntu-latest, Node.js 20.x
  • GitHub Check: unittest (ubuntu-latest, 18.x, node)
  • GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
  • GitHub Check: unittest (macos-latest, 18.x, node)
  • GitHub Check: unittest (macos-latest, 18.x, jsdom)
  • GitHub Check: build (ubuntu-latest, 20.x)
  • GitHub Check: build (macos-latest, 20.x)
  • GitHub Check: build-windows

@codecov
Copy link

codecov bot commented May 19, 2025

Codecov Report

Attention: Patch coverage is 1.66667% with 59 lines in your changes missing coverage. Please review.

Project coverage is 52.63%. Comparing base (281361f) to head (960b014).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...rowser/mcp/tools/handlers/CreateNewFileWithText.ts 0.00% 22 Missing and 1 partial ⚠️
...ive/src/browser/mcp/tools/createNewFileWithText.ts 0.00% 7 Missing ⚠️
...ages/ai-native/src/browser/mcp/tools/grepSearch.ts 0.00% 5 Missing and 2 partials ⚠️
...ackages/ai-native/src/browser/mcp/tools/listDir.ts 0.00% 4 Missing and 2 partials ⚠️
...ive/src/browser/mcp/mcp-server.feature.registry.ts 0.00% 4 Missing and 1 partial ⚠️
...ative/src/browser/mcp/tools/handlers/RunCommand.ts 0.00% 2 Missing and 2 partials ⚠️
...ages/ai-native/src/browser/mcp/tools/fileSearch.ts 0.00% 1 Missing and 2 partials ⚠️
...i-native/src/browser/mcp/tools/handlers/ListDir.ts 0.00% 2 Missing ⚠️
...-native/src/browser/mcp/tools/handlers/ReadFile.ts 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4561      +/-   ##
==========================================
- Coverage   52.64%   52.63%   -0.02%     
==========================================
  Files        1684     1685       +1     
  Lines      104044   104064      +20     
  Branches    22577    22576       -1     
==========================================
- Hits        54773    54769       -4     
- Misses      40956    40973      +17     
- Partials     8315     8322       +7     
Flag Coverage Δ
jsdom 48.15% <0.00%> (-0.02%) ⬇️
node 12.05% <1.66%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ensorrow
Copy link
Contributor Author

/next

@ensorrow ensorrow requested review from Aaaaash, Ricbet and erha19 May 21, 2025 09:03
@opensumi
Copy link
Contributor

opensumi bot commented May 21, 2025

🎉 PR Next publish successful!

3.8.3-next-1747818383.0

@ensorrow
Copy link
Contributor Author

/next

@opensumi
Copy link
Contributor

opensumi bot commented May 21, 2025

🎉 PR Next publish successful!

3.9.1-next-1747829468.0

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/ai-native/__test__/node/mcp-server.sse.test.ts (1)

52-53: 记录为什么禁用测试很好,但可以提供更多详细信息

添加TODO注释解释为什么跳过测试是一个好的做法,但建议提供更多关于具体需要修改的内容的详细信息。这将帮助其他开发人员理解为什么这个测试不再有效以及如何修复它。

建议:

  1. 添加具体的MCP SDK版本号信息
  2. 简要说明哪些API或行为发生了变化
  3. 如果可能,链接到相关的任务或问题追踪
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2903c6f and 741f894.

📒 Files selected for processing (1)
  • packages/ai-native/__test__/node/mcp-server.sse.test.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: build (ubuntu-latest, 20.x)
  • GitHub Check: build-windows
  • GitHub Check: ubuntu-latest, Node.js 20.x
  • GitHub Check: build (macos-latest, 20.x)
  • GitHub Check: unittest (macos-latest, 18.x, node)
  • GitHub Check: unittest (macos-latest, 18.x, jsdom)
  • GitHub Check: unittest (ubuntu-latest, 18.x, node)
  • GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/ai-native/src/browser/mcp/tools/handlers/ListDir.ts (1)

93-93: 建议:同样使用path.join处理此处的路径连接

为了保持一致性,建议此处也使用path.join代替字符串拼接。

-          const filePath = `${absolutePath}/${uri.displayName}`;
+          const filePath = path.join(absolutePath, uri.displayName);
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c3886f0 and d9d1842.

📒 Files selected for processing (2)
  • packages/ai-native/src/browser/mcp/tools/handlers/ListDir.ts (2 hunks)
  • packages/ai-native/src/browser/mcp/tools/handlers/ReadFile.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: unittest (macos-latest, 18.x, jsdom)
  • GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
  • GitHub Check: unittest (ubuntu-latest, 18.x, node)
  • GitHub Check: unittest (macos-latest, 18.x, node)
  • GitHub Check: ubuntu-latest, Node.js 20.x
  • GitHub Check: build (macos-latest, 20.x)
  • GitHub Check: build (ubuntu-latest, 20.x)
  • GitHub Check: build-windows
🔇 Additional comments (4)
packages/ai-native/src/browser/mcp/tools/handlers/ListDir.ts (2)

2-2: 导入了path模块以改进路径处理

引入path模块是一个很好的改进,它允许使用更健壮的路径连接方法。


70-70: 改进了路径连接方式

将字符串拼接改为path.join是一个很好的改进,这样可以:

  1. 处理不同操作系统的路径分隔符
  2. 避免路径分隔符重复
  3. 处理路径末尾斜杠等边缘情况
packages/ai-native/src/browser/mcp/tools/handlers/ReadFile.ts (2)

4-4: 导入了path模块以改进路径处理

引入path模块是一个很好的改进,注意这里从@opensumi/ide-core-common导入,而ListDir.ts从@opensumi/ide-core-browser导入。这可能是基于各文件的不同依赖关系,但值得注意保持一致性。


110-110: 改进了路径连接方式

将字符串拼接改为path.join是一个很好的改进,可以更安全地处理文件路径,避免因路径分隔符导致的问题。

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
packages/ai-native/src/browser/mcp/tools/handlers/CreateNewFileWithText.ts (1)

30-34: 错误消息语言不一致

代码中的注释和日志消息使用中文,但抛出的错误消息使用英文,应保持语言一致性。

应用此差异修复语言不一致问题:

-      throw new Error("can't find project dir");
+      throw new Error('无法找到项目目录');
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d9d1842 and 530be4f.

📒 Files selected for processing (2)
  • packages/ai-native/src/browser/mcp/tools/createNewFileWithText.ts (2 hunks)
  • packages/ai-native/src/browser/mcp/tools/handlers/CreateNewFileWithText.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/ai-native/src/browser/mcp/tools/createNewFileWithText.ts
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/ai-native/src/browser/mcp/tools/handlers/CreateNewFileWithText.ts (1)
packages/ai-native/src/browser/types.ts (1)
  • MCPLogger (355-357)
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: unittest (ubuntu-latest, 18.x, node)
  • GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
  • GitHub Check: unittest (macos-latest, 18.x, node)
  • GitHub Check: ubuntu-latest, Node.js 20.x
  • GitHub Check: unittest (macos-latest, 18.x, jsdom)
  • GitHub Check: build (ubuntu-latest, 20.x)
  • GitHub Check: build (macos-latest, 20.x)
  • GitHub Check: build-windows
🔇 Additional comments (3)
packages/ai-native/src/browser/mcp/tools/handlers/CreateNewFileWithText.ts (3)

1-7: 导入声明合理且符合预期

导入的依赖项都是实现文件创建功能所必需的,包括依赖注入、URI处理、文件服务和工作区服务。


9-22: 依赖注入模式实现正确

类使用标准的依赖注入模式,所有必要的服务都已正确注入。注释清晰地说明了类的用途。


37-39: 路径构建改进很好

使用 path.join 进行路径拼接是一个很好的改进,提高了跨平台兼容性,符合 AI 总结中提到的路径处理标准化。

@ensorrow ensorrow force-pushed the refactor/tool-override branch from c7eb9a8 to d14998b Compare June 5, 2025 09:12
@ensorrow ensorrow merged commit 5f86502 into main Jun 6, 2025
15 of 19 checks passed
@ensorrow ensorrow deleted the refactor/tool-override branch June 6, 2025 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚙️ refactor Refactor code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants