Skip to content

[langchain]: I encountered an error when running the code according to the MCP documentation. #2016

@sunstarlixp

Description

@sunstarlixp

Type of issue

issue / bug

Language

Python

Description

I'm learning to use the MCP agent, and when I run the code from the demo in the user manual, I encounter an error:
python`
from langchain_mcp_adapters.client import MultiServerMCPClient
from langchain.agents import create_agent

client = MultiServerMCPClient(
{
"math": {
"transport": "stdio", # Local subprocess communication
"command": "python",
# Absolute path to your math_server.py file
"args": ["/path/to/math_server.py"],
},
"weather": {
"transport": "http", # HTTP-based remote server
# Ensure you start your weather server on port 8000
"url": "http://localhost:8000/mcp",
}
}
)

tools = await client.get_tools()
agent = create_agent(
"claude-sonnet-4-5-20250929",
tools
)
math_response = await agent.ainvoke(
{"messages": [{"role": "user", "content": "what's (3 + 5) x 12?"}]}
)
weather_response = await agent.ainvoke(
{"messages": [{"role": "user", "content": "what is the weather in nyc?"}]}
)
Error message during program execution:
tools = await client.get_tools()
^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: 'await' outside function
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    externalUser is not a member of langchain-ailangchainFor docs changes to LangChain

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions