全部產品
Search
文件中心

Alibaba Cloud Model Studio:MCP

更新時間:Feb 25, 2026

模型上下文協議(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.5-plus",
    input="你的問題...",
    tools=[mcp_tool]
)

print(response.output_text)

支援的模型

  • 千問Plus:qwen3.5-plusqwen3.5-plus-2026-02-15

  • 千問Flash:qwen3.5-flashqwen3.5-flash-2026-02-23

  • 千問開源:qwen3.5-397b-a17bqwen3.5-122b-a10bqwen3.5-27bqwen3.5-35b-a3b

僅支援通過 Responses API 呼叫。

快速開始

以接入 ModelScope 提供的Fetch網頁抓取MCP為例。通過右側的Service configuration,擷取服務的 SSE Endpoint 與鑒權資訊。

需要已擷取API Key配置API Key到環境變數(準備下線,併入配置 API Key)

請將範例程式碼中的 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"),
    base_url="https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/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.5-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,
    baseURL: "https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/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.5-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
curl -X POST https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3.5-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"
        }
    ]
}'

運行以上代碼可擷取如下回複:

[模型回複]
從北京到上海駕車,您可以選擇以下路線:

1. 推薦路線(京滬高速)
   - 沿G2京滬高速公路一路向南行駛,途經河北、天津、山東、江蘇等省市
   - 全程約1,200公裡,預計駕駛時間約13–15小時

2. 備選路線(京台高速轉滬昆高速)
   - 沿G3京台高速南下,進入安徽後轉入G60滬昆高速前往上海
   - 全程約1,250公裡,預計駕駛時間約14–16小時

...

[Token 用量] 輸入: 55, 輸出: 195, 合計: 250

流式輸出

MCP 工具調用可能涉及多次外部服務互動,建議啟用流式輸出,即時擷取工具調用過程與回複內容。

import os
from openai import OpenAI

client = OpenAI(
    # 若沒有配置環境變數,請用百鍊API Key將下行替換為:api_key="sk-xxx",
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/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.5-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,
    baseURL: "https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/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.5-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
curl -X POST https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3.5-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
}'

運行以上代碼可擷取如下回複:

[模型回複]
從北京到上海駕車,您可以選擇以下路線:

1. 推薦路線(京滬高速)
   - 沿G2京滬高速公路一路向南行駛,途經河北、天津、山東、江蘇等省市
   - 全程約1,200公裡,預計駕駛時間約13–15小時

...

[Token 用量] 輸入: 55, 輸出: 195, 合計: 250

參數說明

mcp 工具支援以下參數:

參數

必填

說明

type

固定為 "mcp"

server_protocol

與 MCP 服務的通訊協定,當前僅支援 "sse"

server_label

MCP 服務的標籤名稱,用於標識該服務。

server_description

MCP 服務的功能描述,供模型理解該服務的能力與適用情境。建議填寫以提升模型調用準確性。

server_url

MCP 服務的端點 URL。

headers

串連 MCP 服務時攜帶的要求標頭,例如 Authorization 等認證資訊。

樣本:

{
    "type": "mcp",
    "server_protocol": "sse",
    "server_label": "fetch",
    "server_description": "Fetch MCP Server,提供網頁內容擷取能力,可以抓取指定URL的網頁內容並返迴文本。",
    "server_url": "https://mcp.api-inference.modelscope.net/xxx/sse"
}

計費說明

計費包含以下部分:

  • 模型推理費用:按模型的 Token 用量計費。

  • MCP 服務費用:以各 MCP 服務的計費為準。