AIaW icon indicating copy to clipboard operation
AIaW copied to clipboard

Bug: 不能正确处理prompts为空的mcp服务器,mcp服务器添加失败

Open ptbsare opened this issue 10 months ago • 4 comments

Before you submit

  • [ *] I have read the Common Issues and ensured this issue is not covered there.
  • [ *] I have searched the existing issues and this is not a duplicate.

Describe the bug 1.添加一个mcp服务器,假设mcp服务器声称支持prompts/resource等能力但是返回列表空的prompts/resource列表。 2.浏览器终端会报错:

To Reproduce Steps to reproduce the behavior:

  1. 添加一个mcp服务器,假设mcp服务器支持prompts/resource等但是返回列表空,则mcp服务器添加失败,界面显示正在连接中无反应。
index-DzTsy1hj.js:14 TypeError: Cannot read properties of undefined (reading 'forEach')
    at index-DzTsy1hj.js:940:858
    at Array.map (<anonymous>)
    at CPe (index-DzTsy1hj.js:940:796)
    at index-DzTsy1hj.js:982:1584
    at Array.map (<anonymous>)
    at EK.fn (index-DzTsy1hj.js:982:1484)
    at hD (index-DzTsy1hj.js:10:3166)
    at US (index-DzTsy1hj.js:10:2854)
    at uD.runIfDirty (index-DzTsy1hj.js:10:2036)
    at bv (index-DzTsy1hj.js:14:46)

对应的混淆后js代码报错位置:

      , f = e.prompts.map(m => {
        const {name: g, description: v} = m
          , y = {};
        return m.arguments.forEach(_ => {
            const k = Kt({
                title: _.name,
                description: _.description
            });
            y[_.name] = _.required ? k : mr(k)
        }

2.查阅源码,buildMcpPlugin函数接收的是一个 McpPluginDump 对象 [AIaW/src/utils/types.ts],这个对象是通过 dumpMcpPlugin函数生成的。函数会调用客户端的 listTools(), listResources(), listPrompts() 方法来获取数据,并将结果存储在 McpPluginDump 对象中。

问题可能出在 dumpMcpPlugin函数获取数据时,客户端的 TypeError 很可能发生在 [AIaW/src/utils/plugins.ts]的 buildMcpPlugin函数中: https://github.com/NitroRCr/AIaW/blob/cd36918695b129955ec27bccec41b2ae5fbdcf6d/src/utils/plugins.ts#L405C12-L405C21 这里的prompt.arguments 像是一个undefined

3、建议更健壮的错误处理,如果 client.listTools()、client.listResources() 或 client.listPrompts() 调用失败或返回空(例如,由于网络问题或服务器内部错误),并且没有正确处理错误,也可能导致 dump 对象中的 tools、resources 或 prompts 属性为 undefined。

Expected behavior 服务器添加成功,更健壮的错误处理,即便prompt返回空依然可以支持对应mcp服务器的tool调用。

Screenshots .

Environment

  • Platform: [Web]
  • Version: [v1.7.3]
  • Browser/Webview version: [Chrome 131]

Additional context Add any other context about the problem here.

ptbsare avatar May 31 '25 04:05 ptbsare

看这个报错,似乎是因为 prompt.arguments 为 undefined 导致的,确实有这个问题。不过 listPrompts 应该是正常返回了列表的。根据 mcp sdk 的类型定义,返回的 prompts 不会为 undefined。

我会添加对 arguments 为空的处理

NitroRCr avatar May 31 '25 05:05 NitroRCr

hi @NitroRCr 作者您好,感谢您做的非常好的客户端;prompt.arguments在listPrompts()返回空列表时应该是undefined;在目前的代码逻辑下,倘若listResources() listPrompts()在网络波动/服务器内部错误时返回什么?因为这种情况下前端web页面是没有任何提示的,只会显示正在连接,可能让人误以为需要等待。可否将相应exception返回给前端告知服务器添加异常,或者比如在listPrompts或listResources()任一返回错误时handle exception而不影响tools工具集的组建。

ptbsare avatar May 31 '25 08:05 ptbsare

listPromtps/listRescources 若出现异常应该会直接抛出异常,最终在 src\composables\install-plugin.ts 捕获并提示。

Image

只是上述由 arguments 为 undefined 导致的异常是在 buildMcpPlugin 触发的,这里预期是不会抛出异常的。

NitroRCr avatar May 31 '25 10:05 NitroRCr

OK,了解。

ptbsare avatar May 31 '25 10:05 ptbsare

已在 v1.7.4 中修复

NitroRCr avatar Jun 01 '25 12:06 NitroRCr