Le Model Context Protocol (MCP) permet aux grands modèles de langage d'utiliser des outils et des données externes. Cette rubrique explique comment se connecter à un serveur MCP à l'aide de l'API Responses.
Utilisation
Lorsque vous utilisez l'API Responses, ajoutez les informations du serveur MCP dans le paramètre tools.
Récupérez l'endpoint Server-Sent Events (SSE) ainsi que les informations d'authentification du service MCP auprès de plateformes telles que ModelScope .
Prise en charge des serveurs MCP utilisant le protocole SSE.
10 serveurs MCP au maximum.
# Import dependencies and create a client...
mcp_tool = {
"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"
}
}
response = client.responses.create(
model="qwen3.8-max",
input="Your question...",
tools=[mcp_tool]
)
print(response.output_text)
Modèles pris en charge
- Qwen-Max : séries Qwen3,8-Max et Qwen3,7-Max
- Qwen-Plus : séries Qwen3,7-Plus, Qwen3,6-Plus et Qwen3,5-Plus
- Qwen-Flash : séries Qwen3,7-Flash, Qwen3,6-Flash et Qwen3,5-Flash
- Série open source Qwen3,8
- Série open source Qwen3,6 (à l'exception de qwen3,6-27b)
- Série open source Qwen3,5
Disponible uniquement via l'API Responses.
Prise en main
Cet exemple utilise le service MCP Fetch web scraping disponible sur ModelScope. Vous pouvez obtenir l'endpoint SSE et les informations d'authentification du service dans la section Service configuration située à droite.
Obtenez une clé API et configurez-la en tant que variable d'environnement.
Remplacez la valeur de
server_urlpar l'endpoint SSE fourni par la plateforme du service MCP. Remplacez également les informations d'authentification dansheaderspar le jeton fourni par cette plateforme.
import os
from openai import OpenAI
client = OpenAI(
# If no environment variable, use: api_key="sk-xxx" (not recommended).
api_key=os.getenv("DASHSCOPE_API_KEY"),
# The following URL is for the Singapore region. Replace {WorkspaceId} with your actual workspace ID. URLs vary by region.
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)
# MCP tool configuration
# Replace server_url with the SSE Endpoint that you got from a platform such as ModelScope
# If authentication is required, add the token from the corresponding platform to the headers
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.8-max",
input="https://news.aibase.com/zh/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}")
import OpenAI from "openai";
import process from 'process';
const openai = new OpenAI({
// If no environment variable, use: apiKey: "sk-xxx" (not recommended).
apiKey: process.env.DASHSCOPE_API_KEY,
// The following URL is for the Singapore region. Replace {WorkspaceId} with your actual workspace ID. URLs vary by region.
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
});
async function main() {
// MCP tool configuration
// Replace server_url with the SSE Endpoint that you got from a platform such as ModelScope
// If authentication is required, add the token from the corresponding platform to the headers
const mcpTool = {
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",
};
const response = await openai.responses.create({
model: "qwen3.8-max",
input: "https://news.aibase.com/zh/news, what is the AI news today?",
tools: [mcpTool]
});
console.log("[Model Response]");
console.log(response.output_text);
console.log(`\n[Token Usage] Input: ${response.usage.input_tokens}, Output: ${response.usage.output_tokens}, Total: ${response.usage.total_tokens}`);
}
main();
# Replace server_url with the SSE Endpoint that you got from a platform such as ModelScope
# If authentication is required, add the token from the corresponding platform to the headers
# The following URL is for the Singapore region. Replace {WorkspaceId} with your actual workspace ID. URLs vary by region.
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.8-max",
"input": "https://news.aibase.com/zh/news, what is the AI news today?",
"tools": [
{
"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"
}
]
}'
Après exécution du code, la réponse suivante est renvoyée :
[Model Response]
Based on the documentation for the Model Context Protocol (MCP) on the Alibaba Cloud Model Studio (Bailian) help center, the supported models are:
* Qwen Max:
* Qwen3.8-Max series
* Qwen3.7-Max series
* Qwen Plus:
* Qwen3.7-Plus series
* Qwen3.6-Plus series
* Qwen3.5-Plus series
* Qwen Flash:
* Qwen3.7-Flash series
* Qwen3.6-Flash series
* Qwen3.5-Flash series
* Qwen3.6 Open Source Series (excluding qwen3.6-27b)
* Qwen3.5 Open Source Series
Note: The documentation specifies that MCP is only supported via the Responses API (client.responses.create) and not the standard Chat Completions API.
[Token Usage] Input: 20583, Output: 1638, Total: 22221
Sortie en streaming
Les appels d'outils MCP peuvent impliquer plusieurs interactions avec des services externes. Activez le mode streaming pour recevoir les résultats intermédiaires en temps réel.
import os
from openai import OpenAI
client = OpenAI(
# If no environment variable, use: api_key="sk-xxx" (not recommended).
api_key=os.getenv("DASHSCOPE_API_KEY"),
# The following URL is for the Singapore region. Replace {WorkspaceId} with your actual workspace ID. URLs vary by region.
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)
# Replace server_url with the SSE Endpoint that you got from a platform such as ModelScope
# If authentication is required, add the token from the corresponding platform to the headers
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",
}
stream = client.responses.create(
model="qwen3.8-max",
input="https://news.aibase.com/zh/news, what is the AI news today?",
tools=[mcp_tool],
stream=True
)
for event in stream:
# The model response starts
if event.type == "response.content_part.added":
print("[Model Response]")
# Streaming text output
elif event.type == "response.output_text.delta":
print(event.delta, end="", flush=True)
# The response is complete, output the usage
elif event.type == "response.completed":
usage = event.response.usage
print(f"\n\n[Token Usage] Input: {usage.input_tokens}, Output: {usage.output_tokens}, Total: {usage.total_tokens}")
import OpenAI from "openai";
import process from 'process';
const openai = new OpenAI({
// If no environment variable, use: apiKey: "sk-xxx" (not recommended).
apiKey: process.env.DASHSCOPE_API_KEY,
// The following URL is for the Singapore region. Replace {WorkspaceId} with your actual workspace ID. URLs vary by region.
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
});
async function main() {
// Replace server_url with the SSE Endpoint that you got from a platform such as ModelScope
// If authentication is required, add the token from the corresponding platform to the headers
const mcpTool = {
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",
};
const stream = await openai.responses.create({
model: "qwen3.8-max",
input: "https://news.aibase.com/zh/news, what is the AI news today?",
tools: [mcpTool],
stream: true
});
for await (const event of stream) {
// The model response starts
if (event.type === "response.content_part.added") {
console.log("[Model Response]");
}
// Streaming text output
else if (event.type === "response.output_text.delta") {
process.stdout.write(event.delta);
}
// The response is complete, output the usage
else if (event.type === "response.completed") {
const usage = event.response.usage;
console.log(`\n\n[Token Usage] Input: ${usage.input_tokens}, Output: ${usage.output_tokens}, Total: ${usage.total_tokens}`);
}
}
}
main();
# Replace server_url with the SSE Endpoint that you got from a platform such as ModelScope
# If authentication is required, add the token from the corresponding platform to the headers
# The following URL is for the Singapore region. Replace {WorkspaceId} with your actual workspace ID. URLs vary by region.
curl -X POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "qwen3.8-max",
"input": "https://news.aibase.com/zh/news, what is the AI news today?",
"tools": [
{
"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"
}
],
"stream": true
}'
Après exécution du code, la réponse suivante est renvoyée :
[Model Response]
Based on the documentation page for MCP on Alibaba Cloud Model Studio, the following models are supported:
* Qwen Max Series: Qwen3.8-Max series and Qwen3.7-Max series
* Qwen Plus Series: Qwen3.7-Plus series, Qwen3.6-Plus series and Qwen3.5-Plus series
* Qwen Flash Series: Qwen3.7-Flash series, Qwen3.6-Flash series and Qwen3.5-Flash series
* Qwen3.6 Open Source Series (excluding qwen3.6-27b)
* Qwen3.5 Open Source Series
Note: These models support MCP functionality only via the Responses API.
[Token Usage] Input: 20472, Output: 945, Total: 21417
Paramètres
L'outil mcp prend en charge les paramètres suivants :
| Exemple : |
Facturation
La facturation comprend :
- Frais d'inférence du modèle : facturés en fonction de l'utilisation des tokens par le modèle.
- Frais du serveur MCP : soumis aux règles de facturation propres à chaque serveur MCP.