Files
tw2/examples/functionality/mcp
jimi a842f1861f
Some checks failed
Pre-commit / run (ubuntu-latest) (push) Has been cancelled
Deploy Sphinx documentation to Pages / build_en (ubuntu-latest, 3.10) (push) Has been cancelled
Deploy Sphinx documentation to Pages / build_zh (ubuntu-latest, 3.10) (push) Has been cancelled
Python Unittest Coverage / test (macos-15, 3.10) (push) Has been cancelled
Python Unittest Coverage / test (macos-15, 3.11) (push) Has been cancelled
Python Unittest Coverage / test (macos-15, 3.12) (push) Has been cancelled
Python Unittest Coverage / test (ubuntu-latest, 3.10) (push) Has been cancelled
Python Unittest Coverage / test (ubuntu-latest, 3.11) (push) Has been cancelled
Python Unittest Coverage / test (ubuntu-latest, 3.12) (push) Has been cancelled
Python Unittest Coverage / test (windows-latest, 3.10) (push) Has been cancelled
Python Unittest Coverage / test (windows-latest, 3.11) (push) Has been cancelled
Python Unittest Coverage / test (windows-latest, 3.12) (push) Has been cancelled
chore: initial import of standalone agentscope project
2026-03-02 18:21:40 +08:00
..

MCP in AgentScope

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.

Prerequisites

  • Python 3.10 or higher
  • DashScope API key from Alibaba Cloud

Installation

Install AgentScope

# Install from source
cd {PATH_TO_AGENTSCOPE}
pip install -e .

QuickStart

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 agentscope

Start 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.py

Two 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.py

The agent will:

  1. Register the MCP tools from the servers
  2. Use a ReAct agent to solve a calculation problem (multiplying two numbers and then adding another number)
  3. Return structured output with the final result