基于远程MCP服务器的模型工具接入与实现原理

引言

随着大语言模型(LLM)在各类应用中的广泛使用,如何扩展模型的能力成为技术发展重点。MCP(Model Context Protocol)是一种开放协议,旨在标准化应用程序如何向LLM提供工具与上下文。本文将介绍如何通过远程MCP服务器为模型接入工具、探讨其实现机制、关键参数配置、安全风险与系统架构。


MCP协议与远程工具接入架构

MCP协议允许开发者和组织在互联网范围内部署MCP服务器,向模型开放特定工具。客户端(如Responses API)可通过配置参数,调用远程MCP服务器,获取并执行工具功能。此机制提升了模型在特定领域的自动化操作能力。


Responses API集成远程MCP工具实现流程

1. 工具列表获取

当在Responses API的tools数组中配置远程MCP服务器时,系统首先尝试获取该服务器提供的工具列表。支持的传输协议包括:
- Streamable HTTP
- HTTP SSE(Server-Sent Events)

工具列表会以mcp_list_tools结构体被导入模型上下文。例如:

# 获取远程MCP服务器工具列表示例
from openai import OpenAI

client = OpenAI()
resp = client.responses.create(
    model="gpt-4.1",
    tools=[{
        "type": "mcp",
        "server_label": "deepwiki",
        "server_url": "https://zzzzapi.com/mcp",
        "require_approval": "never",
    }],
    input="列出2025-03-26版MCP协议支持的传输协议。",
)
print(resp.output_text)

工具列表结构如下:

{
  "id": "mcpl_xxxx",
  "type": "mcp_list_tools",
  "server_label": "deepwiki",
  "tools": [
    {
      "name": "read_wiki_structure",
      "input_schema": {
        "type": "object",
        "properties": {
          "repoName": {
            "type": "string",
            "description": "GitHub仓库名: owner/repo (如 facebook/react)"
          }
        },
        "required": ["repoName"],
        "additionalProperties": false
      }
    }
    # ...更多工具
  ]
}
工具过滤

若MCP服务器工具过多,可通过allowed_tools参数限制导入的工具。例如:

from openai import OpenAI

client = OpenAI()
resp = client.responses.create(
    model="gpt-4.1",
    tools=[{
        "type": "mcp",
        "server_label": "deepwiki",
        "server_url": "https://zzzzapi.com/mcp",
        "require_approval": "never",
        "allowed_tools": ["ask_question"],
    }],
    input="2025-03-26版MCP协议支持哪些传输协议?",
)
print(resp.output_text)

2. 工具调用流程

在工具定义导入后,模型可根据上下文决定调用特定工具。每次调用会生成mcp_call结构体,包括参数与返回结果。例如:

{
  "id": "mcp_xxxx",
  "type": "mcp_call",
  "arguments": {
    "repoName": "modelcontextprotocol/modelcontextprotocol",
    "question": "2025-03-26版MCP协议支持哪些传输协议?"
  },
  "name": "ask_question",
  "output": "2025-03-26版MCP协议支持stdio和Streamable HTTP两种标准传输机制...",
  "server_label": "deepwiki",
  "error": null
}

若调用失败,将在error字段中返回相关错误信息。

3. 调用审批机制

默认情况下,API会请求用户审批后,才将数据发送到远程MCP服务器。审批请求结构如下:

{
  "id": "mcpr_xxxx",
  "type": "mcp_approval_request",
  "arguments": {
    "repoName": "modelcontextprotocol/modelcontextprotocol",
    "question": "2025-03-26版MCP协议支持哪些传输协议?"
  },
  "name": "ask_question",
  "server_label": "deepwiki"
}

审批响应示例:

from openai import OpenAI

client = OpenAI()
resp = client.responses.create(
    model="gpt-4.1",
    tools=[{
        "type": "mcp",
        "server_label": "deepwiki",
        "server_url": "https://zzzzapi.com/mcp",
    }],
    previous_response_id="resp_xxxx",
    input=[{
        "type": "mcp_approval_response",
        "approve": True,
        "approval_request_id": "mcpr_xxxx"
    }],
)
print(resp.output_text)
跳过审批

对于信任的MCP服务器,可通过require_approval配置跳过审批,以优化延迟。如:

from openai import OpenAI

client = OpenAI()
resp = client.responses.create(
    model="gpt-4.1",
    tools=[{
        "type": "mcp",
        "server_label": "deepwiki",
        "server_url": "https://zzzzapi.com/mcp",
        "require_approval": {
            "never": {
                "tool_names": ["ask_question", "read_wiki_structure"]
            }
        }
    }],
    input="2025-03-26版MCP协议支持哪些传输协议?",
)
print(resp.output_text)

远程MCP服务器认证机制

部分MCP服务器(如Stripe)需要认证信息。可通过headers参数配置HTTP头实现,包括API密钥或OAuth令牌。

from openai import OpenAI

client = OpenAI()
resp = client.responses.create(
    model="gpt-4.1",
    input="创建一个价值20元的支付链接",
    tools=[{
        "type": "mcp",
        "server_label": "stripe",
        "server_url": "https://zzzzapi.com/mcp",
        "headers": {
            "Authorization": "Bearer STRIPE_API_KEY"
        }
    }]
)
print(resp.output_text)

API不会存储或暴露headers中的密钥,需每次请求时传递完整信息。


安全风险与数据合规性

  • 数据泄露风险:与第三方MCP服务器交互可能引发敏感数据泄露,需审查工具功能及对外共享内容。
  • Prompt Injection威胁:恶意MCP服务器可能包含隐藏指令,影响模型行为。建议仅连接可信服务器,并监控输入与输出。
  • 工具行为变更:第三方工具定义可能随时更新,需持续关注工具行为。
  • 数据驻留与零数据保留(Zero Data Retention):MCP服务器的数据存储遵循自身政策,开发者需确保第三方服务器也符合数据合规性要求。

API限流与使用说明

API类型 速率限制
Responses Tier 1: 200 RPM
Chat Completions Tier 2/3: 1000 RPM
Assistants Tier 4/5: 2000 RPM

总结

通过远程MCP服务器,开发者可灵活扩展模型工具能力,实现多种自动化业务场景。MCP协议与Responses API架构为LLM开放生态提供了标准化入口。实现过程中需关注工具配置、认证机制、以及数据安全与合规性。

Logo

更多推荐