Skip to main content
Tool calling

Connect to MCP servers

Use external tools in chat

Qwen Cloud supports the Model Context Protocol (MCP) through the Responses API. Pass one or more MCP server configurations in the tools parameter. The model connects to those servers, discovers their tools, and invokes them as needed to fulfill each request.

Getting started

This example connects to the Fetch MCP service from ModelScope to scrape web content. Before running the code:
  • Get the SSE endpoint from the Service configuration section of your MCP service on ModelScope. Replace server_url with this endpoint.
  • If authentication is required, add the token to the headers field.
Replace the server_url in the sample code with the SSE Endpoint that you got from the MCP service platform. Replace the authentication information in headers with the token provided by that platform.
  • Python
  • Node.js
  • curl
import os
from openai import OpenAI

client = OpenAI(
  api_key=os.getenv("DASHSCOPE_API_KEY"),
  base_url="https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
)

# MCP tool configuration
mcp_tool = {
  "type": "mcp",
  "server_protocol": "sse",
  "server_label": "fetch",
  "server_description": "Fetch MCP Server that provides web scraping capabilities. It can scrape the content of a specified URL and return it as text.",
  "server_url": "https://mcp.api-inference.modelscope.net/xxx/sse",
}

response = client.responses.create(
  model="qwen3.6-plus",
  input="https://news.aibase.com/news, what is the AI news today?",
  tools=[mcp_tool]
)

print("[Model Response]")
print(response.output_text)
print(f"\n[Token Usage] Input: {response.usage.input_tokens}, Output: {response.usage.output_tokens}, Total: {response.usage.total_tokens}")

How it works

1

Define MCP tool configuration

Define an MCP tool configuration with the server's SSE endpoint and optional authentication headers.
2

Pass the configuration

Pass the configuration in the tools parameter of a Responses API call.
3

Model connects and discovers tools

The model connects to the MCP server, discovers available tools, and invokes them to process the input.
4

Receive the response

The response contains the model's final output, including results from the MCP tool calls.
MCP servers are third-party services. Only connect to servers you trust. A malicious MCP server could return harmful content, exfiltrate data through tool descriptions, or incur unexpected costs. Always verify the server source before adding it to your requests.
You can get the SSE endpoint and authentication information for MCP services from platforms such as ModelScope. Only MCP servers using the SSE protocol are supported, with a maximum of 10 per request.

Response structure

A typical response includes two phases: tool listing (the model discovers available tools) and the final output (the model uses those tools and returns a result). Tool listing output The response output array begins with an mcp_list_tools item showing the tools the model discovered on the MCP server:
{
  "type": "mcp_list_tools",
  "server_label": "fetch",
  "tools": [
    {
      "name": "fetch",
      "description": "Fetches a URL and returns its content as text.",
      "input_schema": { "type": "object", "properties": { "url": { "type": "string" } }, "required": ["url"] }
    }
  ]
}
Tool call and final output After discovering tools, the model invokes them and produces a final text response. The output array includes mcp_call items for each tool invocation, followed by a message item with the synthesized answer:
[Model Response]
Here is a summary of today's AI news from AIBase:

1. **OpenAI releases new reasoning model** -- The latest model shows significant
   improvements in multi-step reasoning tasks across math and science benchmarks.

2. **AI-powered drug discovery reaches Phase II trials** -- A molecule designed
   entirely by AI enters the second phase of clinical testing...

[Token Usage] Input: 1852, Output: 395, Total: 2247
The actual response depends on the live content fetched by the MCP server. Token usage varies by request.
Error handling If the model cannot connect to an MCP server or a tool call fails, the response includes an mcp_call item with error details instead of tool output. Common causes include invalid server_url values, authentication failures, and server timeouts. Check the error field in the response output for diagnostics.

Enable streaming

MCP tool calls can involve multiple round trips with external servers. Enable streaming to receive tool call progress and model responses in real time. Add stream=True (Python), stream: true (Node.js / curl) to your request. For streaming implementation details, event types, and code examples, see Streaming output.

MCP-specific streaming events

In addition to the standard streaming events, MCP responses emit these event types:
Event typeDescription
response.mcp_list_tools.doneTool discovery from an MCP server is complete.
response.mcp_call.doneAn MCP tool call has finished and returned results.

MCP tool parameters

ParameterRequiredDescription
typeYesSet to "mcp".
server_protocolYesCommunication protocol. Only "sse" is supported.
server_labelYesA short label that identifies the MCP server.
server_descriptionNoA natural-language description of the server's capabilities. A clear description helps the model decide when to invoke the server's tools and improves tool selection accuracy. Recommended for all MCP servers.
server_urlYesThe SSE endpoint URL of the MCP server.
headersNoHTTP headers to include when connecting to the MCP server, such as Authorization for authentication.
Example with authentication:
{
  "type": "mcp",
  "server_protocol": "sse",
  "server_label": "my-mcp-service",
  "server_description": "A description of the MCP server's features to help the model understand its use cases.",
  "server_url": "https://your-mcp-server-endpoint/sse",
  "headers": {
    "Authorization": "Bearer YOUR_TOKEN"
  }
}

Supported models

MCP is available through the Responses API only. The following models support MCP tool calls:
Model familyModel IDs
Qwen-Plusqwen3.6-plus, qwen3.5-plus, qwen3.5-plus-2026-02-15
Qwen-Flashqwen3.5-flash, qwen3.5-flash-2026-02-23
Open source Qwenqwen3.5-397b-a17b, qwen3.5-122b-a10b, qwen3.5-27b, qwen3.5-35b-a3b

Billing

Two types of charges apply when using MCP:
  • Model inference fees: Billed based on the model's token usage.
  • MCP server fees: Subject to the billing rules of each MCP server provider.
Connect to MCP servers | Qwen Cloud