[Python] server-1 | @asynccontextmanager server-1 | NameError: name 'asynccontextmanager' is...

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to use MCP adapters for Langgraph https://github.com/langchain-ai/langchain-mcp-adapters but I am running into an error:

    server-1 | Traceback (most recent call last):
    server-1 | File "/app/server.py", line 43, in <module>
    server-1 | @asynccontextmanager
    server-1 | NameError: name 'asynccontextmanager' is not defined
    server-1 exited with code 1


    when I place the following reference code for using the mcp adapters

    # graph.py
    from contextlib import asynccontextmanager
    from langchain_mcp_adapters.client import MultiServerMCPClient
    from langgraph.prebuilt import create_react_agent
    from langchain_anthropic import ChatAnthropic

    model = ChatAnthropic(model="claude-3-5-sonnet-latest")

    @asynccontextmanager
    async def make_graph():
    async with MultiServerMCPClient(
    {
    "math": {
    "command": "python",
    # Make sure to update to the full absolute path to your math_server.py file
    "args": ["/path/to/math_server.py"],
    "transport": "stdio",
    },
    "weather": {
    # make sure you start your weather server on port 8000
    "url": "http://localhost:8000/sse",
    "transport": "sse",
    }
    }
    ) as client:
    agent = create_react_agent(model, client.get_tools())
    yield agent.

    langchain-ai/langchain-mcp-adapters


    Does anyone know why I am getting this error:

    NameError: name 'asynccontextmanager' is not defined

    Continue reading...

Compartilhe esta Página