You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AutoGen调用MCP Server时提示工具无可调用function的问题求助

解决AutoGen调用FastMCP MCP Server时的ValueError:"The selected tool does not have a callable 'function'"

问题根源

AutoGen 0.5.5+版本的工具加载逻辑要求每个工具定义必须包含可调用的function属性,用于实际执行工具调用;但FastMCP的MCP Server默认仅返回工具的元数据(name/description/parameters),缺少该执行入口绑定,因此触发ValueError。

解决方案

在AutoGen客户端侧手动为每个从MCP Server获取的工具绑定调用函数,具体步骤如下:

  1. 获取MCP Server的工具元数据
    使用httpx请求MCP Server的工具列表接口,拿到基础工具定义:

    import httpx
    
    # 替换为你的MCP Server地址
    MCP_SERVER_URL = "http://localhost:8000"
    tools_metadata = httpx.get(f"{MCP_SERVER_URL}/tools").json()
    
  2. 创建通用MCP工具调用函数
    编写一个统一的函数,负责向MCP Server发送调用请求:

    def invoke_mcp_tool(tool_name: str, **parameters):
        response = httpx.post(
            url=f"{MCP_SERVER_URL}/call/{tool_name}",
            json={"parameters": parameters}
        )
        response.raise_for_status()  # 抛出HTTP请求错误
        return response.json()
    
  3. 为每个工具绑定function属性
    遍历工具元数据,为每个工具添加可调用的function字段(注意闭包陷阱,需将tool_name作为默认参数传入lambda):

    for tool in tools_metadata:
        # 绑定调用函数,确保每个lambda对应正确的工具名
        tool["function"] = lambda **kwargs, name=tool["name"]: invoke_mcp_tool(name, **kwargs)
    
  4. 在AutoGen中使用补全后的工具
    将补全后的工具列表传入UserProxyAgent的tools参数即可:

    from autogen import AssistantAgent, UserProxyAgent
    
    user_proxy = UserProxyAgent(
        name="UserProxy",
        human_input_mode="NEVER",
        max_consecutive_auto_reply=10,
        code_execution_config=False,
        tools=tools_metadata  # 已包含function属性的工具列表
    )
    
    assistant = AssistantAgent(
        name="WeatherAssistant",
        system_message="你是专业的天气查询助手,仅使用提供的工具回答用户的天气查询请求。",
        llm_config={"config_list": [{"model": "gpt-3.5-turbo", "api_key": "YOUR_API_KEY"}]}
    )
    
    # 发起对话测试
    assistant.initiate_chat(user_proxy, message="查询上海今天的气温和降水情况")
    

注意事项

  • 确保MCP Server的/tools和/call/{tool_name}接口正常可用
  • 若使用其他LLM模型,调整llm_config中的配置项即可
  • 避免lambda闭包陷阱:必须将tool["name"]作为默认参数传入lambda,否则所有工具会指向最后一个遍历的工具名

内容的提问来源于stack exchange,提问作者Vineet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 06:30:06