模型上下文協議(Model Context Protocol, MCP)可協助大模型使用外部工具與資料,相比 Function Calling,MCP 更靈活且便於使用。本文介紹通過 Responses API接入 MCP 的方法。
使用方式
使用 Responses API,在 tools 參數中配置MCP Server資訊。
可從 ModelScope 等平台擷取 MCP 服務的 SSE Endpoint 與鑒權資訊。
支援配置SSE協議的 MCP Server。
最多添加 10 個 MCP Server。
# 匯入依賴與建立用戶端...
mcp_tool = {
"type": "mcp",
"server_protocol": "sse",
"server_label": "my-mcp-service",
"server_description": "MCP 服務功能描述,協助模型理解使用情境。",
"server_url": "https://your-mcp-server-endpoint/sse",
"headers": {
"Authorization": "Bearer YOUR_TOKEN"
}
}
response = client.responses.create(
model="qwen3.6-plus",
input="你的問題...",
tools=[mcp_tool]
)
print(response.output_text)支援的模型
千問Max:Qwen3.7-Max系列
千問Plus:Qwen3.7-Plus系列、Qwen3.6-Plus系列、Qwen3.5-Plus系列
千問Flash:Qwen3.6-Flash系列、Qwen3.5-Flash系列
Qwen3.6開源系列(qwen3.6-27b除外)
Qwen3.5開源系列
僅支援通過 Responses API 呼叫。
快速開始
以接入 ModelScope 提供的Fetch網頁抓取MCP為例。通過右側的Service configuration,擷取服務的 SSE Endpoint 與鑒權資訊。
請將範例程式碼中的server_url替換為您從 MCP 服務平台擷取的 SSE Endpoint,將headers中的鑒權資訊替換為對應平台提供的 Token。
import os
from openai import OpenAI
client = OpenAI(
# 若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx"(不建議),
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下為新加坡地區URL,調用時請將WorkspaceId替換為真實的業務空間ID,各地區的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)
# MCP 工具配置
# 請將 server_url 替換為您從 ModelScope 等平台擷取的 SSE Endpoint
# 如需鑒權,請在 headers 中添加對應平台的 Token
mcp_tool = {
"type": "mcp",
"server_protocol": "sse",
"server_label": "fetch",
"server_description": "Fetch MCP Server,提供網頁內容擷取能力,可以抓取指定URL的網頁內容並返迴文本。",
"server_url": "https://mcp.api-inference.modelscope.net/xxx/sse",
}
response = client.responses.create(
model="qwen3.6-plus",
input="https://news.aibase.com/zh/news,今天有哪些AI新聞?",
tools=[mcp_tool]
)
print("[模型回複]")
print(response.output_text)
print(f"\n[Token 用量] 輸入: {response.usage.input_tokens}, 輸出: {response.usage.output_tokens}, 合計: {response.usage.total_tokens}")import OpenAI from "openai";
import process from 'process';
const openai = new OpenAI({
// 若沒有配置環境變數,請用百鍊API Key將下行替換為:apiKey: "sk-xxx",
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下為新加坡地區URL,調用時請將WorkspaceId替換為真實的業務空間ID,各地區的URL不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
});
async function main() {
// MCP 工具配置
// 請將 server_url 替換為您從 ModelScope 等平台擷取的 SSE Endpoint
// 如需鑒權,請在 headers 中添加對應平台的 Token
const mcpTool = {
type: "mcp",
server_protocol: "sse",
server_label: "fetch",
server_description: "Fetch MCP Server,提供網頁內容擷取能力,可以抓取指定URL的網頁內容並返迴文本。",
server_url: "https://mcp.api-inference.modelscope.net/xxx/sse",
};
const response = await openai.responses.create({
model: "qwen3.6-plus",
input: "https://news.aibase.com/zh/news,今天有哪些AI新聞?",
tools: [mcpTool]
});
console.log("[模型回複]");
console.log(response.output_text);
console.log(`\n[Token 用量] 輸入: ${response.usage.input_tokens}, 輸出: ${response.usage.output_tokens}, 合計: ${response.usage.total_tokens}`);
}
main();# 請將 server_url 替換為您從 ModelScope 等平台擷取的 SSE Endpoint
# 如需鑒權,請在 headers 中添加對應平台的 Token
# 以下為新加坡地區URL,調用時請將WorkspaceId替換為真實的業務空間ID,各地區的URL不同。
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.6-plus",
"input": "https://news.aibase.com/zh/news,今天有哪些AI新聞?",
"tools": [
{
"type": "mcp",
"server_protocol": "sse",
"server_label": "fetch",
"server_description": "Fetch MCP Server,提供網頁內容擷取能力,可以抓取指定URL的網頁內容並返迴文本。",
"server_url": "https://mcp.api-inference.modelscope.net/xxx/sse"
}
]
}'運行以上代碼可擷取如下回複:
[模型回複]
根據阿里雲百鍊的官方文檔,MCP(Model Context Protocol)目前支援的模型如下:
* 千問Plus系列:Qwen3.6-Plus系列、Qwen3.5-Plus系列
* 千問Flash系列:Qwen3.6-Flash系列、Qwen3.5-Flash系列
* 千問開源系列:Qwen3.6開源系列(註:qwen3.6-27b 除外)、Qwen3.5開源系列
特別注意:
MCP 功能僅支援通過 Responses API 呼叫。
[Token 用量] 輸入: 20698, 輸出: 711, 合計: 21409流式輸出
MCP 工具調用可能涉及多次外部服務互動,建議啟用流式輸出,即時擷取工具調用過程與回複內容。
import os
from openai import OpenAI
client = OpenAI(
# 若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx",
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 以下為新加坡地區URL,調用時請將WorkspaceId替換為真實的業務空間ID,各地區的URL不同。
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)
# 請將 server_url 替換為您從 ModelScope 等平台擷取的 SSE Endpoint
# 如需鑒權,請在 headers 中添加對應平台的 Token
mcp_tool = {
"type": "mcp",
"server_protocol": "sse",
"server_label": "fetch",
"server_description": "Fetch MCP Server,提供網頁內容擷取能力,可以抓取指定URL的網頁內容並返迴文本。",
"server_url": "https://mcp.api-inference.modelscope.net/xxx/sse",
}
stream = client.responses.create(
model="qwen3.6-plus",
input="https://news.aibase.com/zh/news,今天有哪些AI新聞?",
tools=[mcp_tool],
stream=True
)
for event in stream:
# 模型回複開始
if event.type == "response.content_part.added":
print("[模型回複]")
# 流式文本輸出
elif event.type == "response.output_text.delta":
print(event.delta, end="", flush=True)
# 響應完成,輸出用量
elif event.type == "response.completed":
usage = event.response.usage
print(f"\n\n[Token 用量] 輸入: {usage.input_tokens}, 輸出: {usage.output_tokens}, 合計: {usage.total_tokens}")import OpenAI from "openai";
import process from 'process';
const openai = new OpenAI({
// 若沒有配置環境變數,請用百鍊API Key將下行替換為:apiKey: "sk-xxx",
apiKey: process.env.DASHSCOPE_API_KEY,
// 以下為新加坡地區URL,調用時請將WorkspaceId替換為真實的業務空間ID,各地區的URL不同。
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
});
async function main() {
// 請將 server_url 替換為您從 ModelScope 等平台擷取的 SSE Endpoint
// 如需鑒權,請在 headers 中添加對應平台的 Token
const mcpTool = {
type: "mcp",
server_protocol: "sse",
server_label: "fetch",
server_description: "Fetch MCP Server,提供網頁內容擷取能力,可以抓取指定URL的網頁內容並返迴文本。",
server_url: "https://mcp.api-inference.modelscope.net/xxx/sse",
};
const stream = await openai.responses.create({
model: "qwen3.6-plus",
input: "https://news.aibase.com/zh/news,今天有哪些AI新聞?",
tools: [mcpTool],
stream: true
});
for await (const event of stream) {
// 模型回複開始
if (event.type === "response.content_part.added") {
console.log("[模型回複]");
}
// 流式文本輸出
else if (event.type === "response.output_text.delta") {
process.stdout.write(event.delta);
}
// 響應完成,輸出用量
else if (event.type === "response.completed") {
const usage = event.response.usage;
console.log(`\n\n[Token 用量] 輸入: ${usage.input_tokens}, 輸出: ${usage.output_tokens}, 合計: ${usage.total_tokens}`);
}
}
}
main();# 請將 server_url 替換為您從 ModelScope 等平台擷取的 SSE Endpoint
# 如需鑒權,請在 headers 中添加對應平台的 Token
# 以下為新加坡地區URL,調用時請將WorkspaceId替換為真實的業務空間ID,各地區的URL不同。
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.6-plus",
"input": "https://news.aibase.com/zh/news,今天有哪些AI新聞?",
"tools": [
{
"type": "mcp",
"server_protocol": "sse",
"server_label": "fetch",
"server_description": "Fetch MCP Server,提供網頁內容擷取能力,可以抓取指定URL的網頁內容並返迴文本。",
"server_url": "https://mcp.api-inference.modelscope.net/xxx/sse"
}
],
"stream": true
}'運行以上代碼可擷取如下回複:
[模型回複]
根據阿里雲百鍊(Model Studio)官方文檔,目前 MCP(Model Context Protocol)功能支援的模型如下:
* 千問 Plus 系列:
* Qwen3.6-Plus 系列
* Qwen3.5-Plus 系列
* 千問 Flash 系列:
* Qwen3.6-Flash 系列
* Qwen3.5-Flash 系列
* 千問開源系列:
* Qwen3.6 開源系列(註:不支援 qwen3.6-27b 模型)
* Qwen3.5 開源系列
注意:目前 MCP 功能僅支援通過 Responses API 進行調用。
[Token 用量] 輸入: 20784, 輸出: 867, 合計: 21651參數說明
mcp 工具支援以下參數:
| 樣本: |
計費說明
計費包含以下部分:
模型推理費用:按模型的 Token 用量計費。
MCP 服務費用:以各 MCP 服務的計費為準。