This example demonstrates how to
- create MCP client with different transports (SSE and Streamable HTTP) and type (Stateless and Stateful),
- register MCP tool functions and use them in a ReAct agent, and
- get MCP tool function as a local callable object from the MCP client.
- Python 3.10 or higher
- DashScope API key from Alibaba Cloud
# Install from source
cd {PATH_TO_AGENTSCOPE}
pip install -e .Install agentscope and ensure you have a valid DashScope API key in your environment variables.
Note: The example is built with DashScope chat model. If you want to change the model in this example, don't forget to change the formatter at the same time! The corresponding relationship between built-in models and formatters are list in our tutorial
pip install agentscopeStart the MCP servers by the following commands in two separate terminals:
# In one terminal, run:
python mcp_add.py
# In another terminal, run:
python mcp_multiply.pyTwo MCP servers will be started on http://127.0.0.1:8001 (SSE server) and http://127.0.0.1:8002 (streamable
HTTP server).
After starting the MCP servers, you can run the agent example:
python main.pyThe agent will:
- Register the MCP tools from the servers
- Use a ReAct agent to solve a calculation problem (multiplying two numbers and then adding another number)
- Return structured output with the final result