Skip to content

[BUG] 看起来mcp的支持有问题,点进来查看更多内容 #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
phpmac opened this issue Apr 8, 2025 · 6 comments
Open
Labels
bug Something isn't working

Comments

@phpmac
Copy link

phpmac commented Apr 8, 2025

首先是参数的问题,参数有大小写,但是它并没有按照zod严格的来筛选,经过我的测试对比,发现兼容性没有 @langchain/core/tools 好

然后就是对话还没有结束,程序就停止了,这是一个问题严重的问题

我相信只需要按照我的对话里面的功能进行对话,就能够重新尝试这个问题

结论:
1.mcp兼容性存在问题,大小写和类型验证判断(可能没有准确传入参数给ai?)
2.流程控制有问题,无论哪个ai模型,已测试过各种模型

claude3.5 3.7 4o quasar-alpha gemini any
这些模型都测试过上面了,下面是代码

// mcp_client.ts
import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";

// Create an MCP server
const server = new McpServer({
    name: "Demo",
    version: "1.0.0"
});

// Add an addition tool
server.tool("add",
    { a: z.number().describe("第一个数字"), b: z.number().describe("第二个数字") },
    async ({ a, b }) => {
        console.debug(`加法: ${a} + ${b} = ${a + b}`);
        return {
            content: [{ type: "text", text: String(a + b) }]
        }
    }
);

// 获取最新价格工具
server.tool("getLastPrice",
    {
        symbol: z.string().describe("交易对,示例: BTCUSDT"),
        exchange: z.enum(["Binance", "Bybit", "OKX", "FTX", "Coinbase", "Gate.io"]).default("Binance"),
        productType: z.enum(["SPOT", "SWAP"]).default("SPOT")
    },
    async ({ symbol, exchange, productType }) => {
        console.debug(`获取最新价格: ${symbol} ${exchange} ${productType}`);
        return {
            content: [{ type: "text", text: `10000` }]
        }
    }
);

// Add a dynamic greeting resource
server.resource(
    "greeting",
    new ResourceTemplate("greeting://{name}", { list: undefined }),
    async (uri, { name }) => ({
        contents: [{
            uri: uri.href,
            text: `Hello, ${name}!`
        }]
    })
);

// Start receiving messages on stdin and sending messages on stdout
const transport = new StdioServerTransport();
await server.connect(transport);
// main.ts
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { MultiServerMCPClient } from "@langchain/mcp-adapters";
import { ChatOpenAI } from "@langchain/openai";

async function main() {
    // Create client and connect to server
    const client = new MultiServerMCPClient({
        // Global tool configuration options
        // Whether to throw on errors if a tool fails to load (optional, default: true)
        throwOnLoadError: true,
        // Whether to prefix tool names with the server name (optional, default: true)
        prefixToolNameWithServerName: true,
        // Optional additional prefix for tool names (optional, default: "mcp")
        additionalToolNamePrefix: "mcp",


        // Server configuration
        mcpServers: {
            "add": {
                "transport": "stdio",
                "command": "node",
                "args": ["scripts/mcp_client.ts"],
            },

        },
    });

    const mcpTools = await client.getTools();

    const llm = new ChatOpenAI({
        model: process.env.OPENAI_MODEL,
        temperature: 0,
    });

    try {
        const agent = createReactAgent({
            llm,
            tools: mcpTools,
        });

        const result = await agent.invoke({
            messages: [
                {
                    role: "user",
                    content: "我有哪些工具",
                },
                {
                    role: "user",
                    content: "1+1",
                },
                {
                    role: "user",
                    content: "查看比特币最新价格,合约",
                    // content: "使用 getLastPrice 查询 BTCUSDT 的SWAP价格,BINANCE",
                },
            ],
        });

        console.debug(result);

        console.debug(`==============`);
        const lastMessage = result.messages[result.messages.length - 1];
        console.debug(lastMessage.content);
    } catch (error) {
        console.error(error);
    } finally {
        client?.close();
    }
}

main().catch((error) => {
    console.error(error);
    process.exit(1);
});

你只需要运行 main.ts

为了帮助改进我已经提供案例代码,

经常遇到的问题,默认是 Binance,但是总是会使用BINANCE,
我还在开发项目,我相信我有时间的时候也会加入一起开发和修复,
同样的对话 @langchain/core/tools 没有问题

@benjamincburns
Copy link
Contributor

你好,

很难准确判断你遇到的问题,但我怀疑你碰到了 #52 中描述的问题。我刚刚更新了 @langchain/core,现在 LangChain 的工具可以使用 JSON Schema 来构建,而不是使用 zod。从 @langchain/mcp-adapters v0.4.1 开始,这项更改使我们能够在不修改的情况下,将 MCP 工具中定义的输入 schema 直接传递给大语言模型(LLM)。这项更改还要求所有主流模型提供商相关的包(例如 @langchain/anthropic@langchain/openai 等)也进行更新。

请确保你使用的是这些包的最新版本,并再次尝试运行。如果你想验证 MCP 服务器为工具生成的 JSON Schema,可以使用 [mcp inspector](https://modelcontextprotocol.io/docs/tools/inspector) 工具。如果你在 inspector 中看到的 schema 不符合预期,可以尝试直接使用 JSON Schema 来定义服务器端的 schema,而不是使用 zod。

如果这些方法仍然不能解决问题,请回复我,我会进一步查看。


Hello,

It's hard to say exactly what's happening here, but I suspect that you're running into the issue described in #52. I just updated @langchain/core so that LangChain tools can be constructed using JSON schema instead of zod. This allows us to pass the input schema defined by the MCP tool to the LLM without modification in @langchain/mcp-adapters as of v0.4.1 and later. This change also required updates to all of the major provider packages (like @langchain/anthropic, @langchain/openai, etc).

Please make sure that you're using the latest versions of all of these packages and try again. You can also use the mcp inspector tool to validate the JSON schema generated for your tools by your MCP server implementation. If the schema you see in the inspector doesn't match your expectations, you can try defining the schema in your server using JSON schema directly, rather than using zod.

If none of that works, please report back and I'll have a closer look.

@phpmac
Copy link
Author

phpmac commented Apr 9, 2025

Upgrading to the latest version as you said does solve the problem. At least the problem I encountered is solved, but it shows that there were indeed big problems before. I will write down my previous versions of the package and the current package for you to see.

按照你所说的升级到了最新,确实解决了问题,至少目前我所遇到的问题是解决了,但表明之前确实存在很大问题,并把我之前的各个版本的包和现在的包写出来给你看

    "@langchain/community": "^0.3.39",
    "@langchain/core": "^0.3.43",
    "@langchain/langgraph": "^0.2.63",
    "@langchain/langgraph-checkpoint-postgres": "^0.0.4",
    "@langchain/langgraph-supervisor": "^0.0.9",
    "@langchain/mcp-adapters": "^0.4.0",
    "@langchain/openai": "^0.4.9",
    "@langchain/community": "^0.3.40",
    "@langchain/core": "^0.3.44",
    "@langchain/langgraph": "^0.2.63",
    "@langchain/langgraph-checkpoint-postgres": "^0.0.4",
    "@langchain/langgraph-supervisor": "^0.0.10",
    "@langchain/mcp-adapters": "^0.4.2",
    "@langchain/openai": "^0.5.5",
    "@modelcontextprotocol/sdk": "^1.9.0",

I believe you have been working hard on the construction. Since I have not participated in the construction, I have raised many bugs, but this can be frustrating sometimes. I have known you for a long time, and I have used a lot of your products. You can see that I am using all your products above. Although this problem frustrated me yesterday, I am full of confidence today. One reason is that you gave a detailed reply. For example, after you wrote so many words, you used both Chinese and English. For the sake of politeness, I will reply in English to show my respect.

我相信你们也一直在努力在建设,因为我还没有参与到建设,所以我提出了很多bug,但这有时候会让人有一些沮丧,我很早就知道你们,然后我也使用了很多,你看这上面的包你们上面的产品我都有在使用,虽然昨天这个问题让我很沮丧,但是今天我又重新充满信心,一个是因为你给出了详细的回复,就比如你的字数这么多后,你有使用了中英文双语,为了礼貌,我会使用英文回复表示尊敬

The above code is what I have tested. You can also use the old version to test and find the problem. At that time, in order to let you understand the problem, I also wrote the test code.

上面的代码是我经过测试的,你也可以使用旧的版本进行测试,就能够发现问题,当时为了考虑到让你们了解问题,所以我把测试的代码也写出来了

@phpmac
Copy link
Author

phpmac commented Apr 9, 2025

After testing, I found that @langchain/mcp-adapters solved the problem, but @langchain/langgraph-supervisor ended before completing the problem and was interrupted. The effect was very bad. I think @langchain/langgraph-supervisor may not be used in production, but only developed for testing. Therefore, I encountered problems when applying it in the production environment, indicating that it cannot be used in the production environment.

经过测试发现 @langchain/mcp-adapters 算是解决了,但是 @langchain/langgraph-supervisor 在处理问题的时候没有完成就结束了,中断了,效果非常不好,我认为 @langchain/langgraph-supervisor 可能并没有用于生产,而仅仅是测试开发出来而已,所以我在应用在生产环境中就会遇到问题,表明他不能用做生产环境

@phpmac
Copy link
Author

phpmac commented Apr 9, 2025

After testing, I found that @langchain/mcp-adapters solved the problem, but @langchain/langgraph-supervisor ended before completing the problem and was interrupted. The effect was very bad. I think @langchain/langgraph-supervisor may not be used in production, but only developed for testing. Therefore, I encountered problems when applying it in the production environment, indicating that it cannot be used in the production environment.

经过测试发现 @langchain/mcp-adapters 算是解决了,但是 @langchain/langgraph-supervisor 在处理问题的时候没有完成就结束了,中断了,效果非常不好,我认为 @langchain/langgraph-supervisor 可能并没有用于生产,而仅仅是测试开发出来而已,所以我在应用在生产环境中就会遇到问题,表明他不能用做生产环境

The test found that google/gemini-2.0-flash-001 has problems, openrouter/quasar-alpha has no problems,
openai/gpt-4o-mini has problems
anthropic/claude-3.5-sonnet has problems

The task is interrupted before it is completed

@phpmac
Copy link
Author

phpmac commented Apr 9, 2025

When I use claude, it prompts

New LangChain packages are available that more efficiently handle tool calling.

Please upgrade your packages to versions that set message tool calls. e.g., `yarn add @langchain/anthropic`, yarn add @langchain/openai`, etc.
New LangChain packages are available that more efficiently handle tool calling.

Please upgrade your packages to versions that set message tool calls. e.g., `yarn add @langchain/anthropic`, yarn add @langchain/openai`, etc.

@phpmac
Copy link
Author

phpmac commented Apr 9, 2025

found other people also have feedback langchain-ai/langchainjs#6349

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants