pythonlanggraphmodel-context-protocol

Error KeyError: 'transport' while trying to get mcp tools from MultiServerMCPClient


I am trying to use airbnb mcp as a tool in my langgraph agent. I am using MultiServerMCPClient. Below is my code.

from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.prebuilt import create_react_agent
from langchain_openai import AzureChatOpenAI
from langchain_openai import ChatOpenAI
from src.mcp_use.adapters.langchain_adapter import LangChainAdapter
from dotenv import load_dotenv
import json
import asyncio
from mcp_use import MCPAgent, MCPClient

load_dotenv()
async def main():
    config = {
    "mcpServers": {
        "airbnb": {
        "command": "npx",
        "args": ["-y", "@openbnb/mcp-server-airbnb", "--ignore-robots-txt"]
        }
        }
    }
        # Create MCPClient from configuration dictionary
    client = MultiServerMCPClient(
       connections= config
    )
    tools = await client.get_tools()
        # the get_tools() method returns a list of tools from all the connected servers
    print(f" _tools is {tools}")
    llm = AzureChatOpenAI(
            temperature=0,
            api_version="2024-12-01-preview",
            azure_deployment="gpt-4o"
        ) 


#agent = MCPAgent(llm=llm, client=client, max_steps=30)

    agent = create_react_agent(model=llm,
                                tools= tools,
                                name="agenttool" ,
                                prompt="you are a assistant with tool airbnb . use tool and do not answer by your self")
    async for chunk in agent.astream(
{"messages": [{"role": "user", "content": "show me the top 5 hotels in San Francisco "}]}
    ):
        print(chunk)

if __name__ == "__main__":
    asyncio.run(main())

While running the code I am getting the following error:

    transport = connection["transport"]
                ~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 'transport'

However the same airbnb mcp config works when using the MCPAgent and MCPClient from mcp_use. Any guidance on how to use mcp using the MultiServerMCPClient for langraph is appreciated.


Solution

  • I just hit this, though trying to use a streamable HTTP connection. Unlike the blog post I was following (and in fact the FastMCP Multi-Server Client documentation) it seems you don't need the overall property name, "mcpServers" in your case. This worked for me:

      mcp_client = MultiServerMCPClient(
        {
          "customers": {
            "transport": "streamable_http",
            "url": "http://localhost:4001/mcp"
          },
          "otherservice": {
            "transport": "streamable_http",
            "url": "http://localhost:4002/mcp"
          }
        }
      )