モデルコンテキストプロトコル (Model Context Protocol) は、大規模言語モデルが外部ツールとデータを使用できるようにします。関数呼び出しと比較して、MCP は柔軟性と使いやすさに優れています。このトピックでは、Responses API を使用して MCP に接続する方法について説明します。
使用方法
Responses API を使用する際に、tools パラメーターに MCP サーバー情報を追加します。
ModelScope などのプラットフォームから、MCP サービスの Server-Sent Events (SSE) エンドポイントと認証情報を取得します。
SSE プロトコルを使用する MCP サーバーをサポートします。
最大 10 個の MCP サーバーに対応しています。
# 依存関係をインポートしてクライアントを作成します...
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.7-plus",
input="Your question...",
tools=[mcp_tool]
)
print(response.output_text)
対応モデル
-
Qwen-Max:Qwen3.7-Max シリーズ
-
Qwen-Plus:Qwen3.7-Plus シリーズ、Qwen3.6-Plus シリーズ、Qwen3.5-Plus シリーズ
-
Qwen-Flash:Qwen3.6-Flash シリーズ、Qwen3.5-Flash シリーズ
-
Qwen3.6 オープンソースシリーズ (qwen3.6-27b は除く)
-
Qwen3.5 オープンソースシリーズ
Responses API でのみ利用できます。
クイックスタート
この例では、ModelScope の Fetch web scraping MCP サービスを使用します。サービスの SSE エンドポイントと認証情報は、右側にある「Service configuration」セクションから取得できます。
server_urlを MCP サービスプラットフォームの SSE エンドポイントに置き換えてください。headersの認証情報は、そのプラットフォームから提供されるトークンに置き換えてください。
import os
from openai import OpenAI
client = OpenAI(
# 環境変数がない場合は、api_key="sk-xxx" を使用します (非推奨)。
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)
# MCP ツール設定
# server_url を ModelScope などのプラットフォームから取得した SSE Endpoint に置き換えてください
# 認証が必要な場合は、対応するプラットフォームのトークンをヘッダーに追加してください
mcp_tool = {
"type": "mcp",
"server_protocol": "sse",
"server_label": "fetch",
"server_description": "Web スクレイピング機能を提供する Fetch MCP Server。指定した URL のコンテンツをスクレイピングし、テキストとして返します。",
"server_url": "https://mcp.api-inference.modelscope.net/xxx/sse",
}
response = client.responses.create(
model="qwen3.7-plus",
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({
// 環境変数がない場合は、apiKey: "sk-xxx" を使用します (非推奨)。
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
});
async function main() {
// MCP ツール設定
// server_url を ModelScope などのプラットフォームから取得した SSE Endpoint に置き換えてください
// 認証が必要な場合は、対応するプラットフォームのトークンをヘッダーに追加してください
const mcpTool = {
type: "mcp",
server_protocol: "sse",
server_label: "fetch",
server_description: "Web スクレイピング機能を提供する Fetch MCP Server。指定した URL のコンテンツをスクレイピングし、テキストとして返します。",
server_url: "https://mcp.api-inference.modelscope.net/xxx/sse",
};
const response = await openai.responses.create({
model: "qwen3.7-plus",
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();# server_url を ModelScope などのプラットフォームから取得した SSE Endpoint に置き換えてください
# 認証が必要な場合は、対応するプラットフォームのトークンをヘッダーに追加してください
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.7-plus",
"input": "https://news.aibase.com/zh/news, what is the AI news today?",
"tools": [
{
"type": "mcp",
"server_protocol": "sse",
"server_label": "fetch",
"server_description": "Web スクレイピング機能を提供する Fetch MCP Server。指定した URL のコンテンツをスクレイピングし、テキストとして返します。",
"server_url": "https://mcp.api-inference.modelscope.net/xxx/sse"
}
]
}'コードを実行すると、次の応答が返されます:
[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 Plus:
* Qwen3.7-Plus series
* Qwen3.6-Plus series
* Qwen3.5-Plus series
* Qwen Flash:
* 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
ストリーミング出力
MCP ツールの呼び出しでは、外部サービスとの複数回のやり取りが発生する場合があります。リアルタイムで中間結果を取得するには、ストリーミングを有効にしてください。
import os
from openai import OpenAI
client = OpenAI(
# 環境変数がない場合は、api_key="sk-xxx" を使用します (非推奨)。
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
)
# server_url を ModelScope などのプラットフォームから取得した SSE Endpoint に置き換えてください
# 認証が必要な場合は、対応するプラットフォームのトークンをヘッダーに追加してください
mcp_tool = {
"type": "mcp",
"server_protocol": "sse",
"server_label": "fetch",
"server_description": "Web スクレイピング機能を提供する Fetch MCP Server。指定した URL のコンテンツをスクレイピングし、テキストとして返します。",
"server_url": "https://mcp.api-inference.modelscope.net/xxx/sse",
}
stream = client.responses.create(
model="qwen3.7-plus",
input="https://news.aibase.com/zh/news, what is the AI news today?",
tools=[mcp_tool],
stream=True
)
for event in stream:
# モデルの応答が開始されます
if event.type == "response.content_part.added":
print("[Model Response]")
# テキストをストリーミング出力します
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: {usage.input_tokens}, Output: {usage.output_tokens}, Total: {usage.total_tokens}")import OpenAI from "openai";
import process from 'process';
const openai = new OpenAI({
// 環境変数がない場合は、apiKey: "sk-xxx" を使用します (非推奨)。
apiKey: process.env.DASHSCOPE_API_KEY,
baseURL: "https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1"
});
async function main() {
// server_url を ModelScope などのプラットフォームから取得した SSE Endpoint に置き換えてください
// 認証が必要な場合は、対応するプラットフォームのトークンをヘッダーに追加してください
const mcpTool = {
type: "mcp",
server_protocol: "sse",
server_label: "fetch",
"server_description": "Web スクレイピング機能を提供する Fetch MCP Server。指定した URL のコンテンツをスクレイピングし、テキストとして返します。",
"server_url": "https://mcp.api-inference.modelscope.net/xxx/sse",
};
const stream = await openai.responses.create({
model: "qwen3.7-plus",
input: "https://news.aibase.com/zh/news, what is the AI news today?",
tools: [mcpTool],
stream: true
});
for await (const event of stream) {
// モデルの応答が開始されます
if (event.type === "response.content_part.added") {
console.log("[Model Response]");
}
// テキストをストリーミング出力します
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: ${usage.input_tokens}, Output: ${usage.output_tokens}, Total: ${usage.total_tokens}`);
}
}
}
main();# server_url を ModelScope などのプラットフォームから取得した SSE Endpoint に置き換えてください
# 認証が必要な場合は、対応するプラットフォームのトークンをヘッダーに追加してください
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.7-plus",
"input": "https://news.aibase.com/zh/news, what is the AI news today?",
"tools": [
{
"type": "mcp",
"server_protocol": "sse",
"server_label": "fetch",
"server_description": "Web スクレイピング機能を提供する Fetch MCP Server。指定した URL のコンテンツをスクレイピングし、テキストとして返します。",
"server_url": "https://mcp.api-inference.modelscope.net/xxx/sse"
}
],
"stream": true
}'コードを実行すると、次の応答が返されます:
[Model Response]
Based on the documentation page for MCP on Alibaba Cloud Model Studio, the following models are supported:
* Qwen Plus Series: Qwen3.7-Plus series, Qwen3.6-Plus series and Qwen3.5-Plus series
* Qwen 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
パラメーター
mcp ツールは、次のパラメーターをサポートします:
|
例:
|
課金
課金には、次の項目が含まれます:
-
モデル推論料金: モデルのトークン使用量に基づいて課金されます。
-
MCP サーバー料金: 各 MCP サーバーの課金ルールに従います。