> For the complete documentation index, see [llms.txt](https://docs.ipwo.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ipwo.net/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-wei-mcp-gong-ju-he-mcp-server-pei-zhi-ipwo-zhu-zhai-dai-li.md).

# 如何为 MCP 工具和 MCP Server 配置 IPWO 住宅代理

随着 AI Agent 和自动化工作流的发展，越来越多的开发者开始使用 MCP（Model Context Protocol）连接外部工具和数据源。

当 MCP Server 需要访问外部 API、网站或第三方服务时，配置代理可以帮助改善网络连通性，并根据业务需求选择不同国家或地区的出口 IP。

本文将介绍如何为 MCP 工具和 MCP Server 配置 IPWO 住宅代理。

***

### 什么是 MCP？

MCP（Model Context Protocol）是一种开放协议，用于连接大语言模型与外部工具、数据库、API 和服务。

常见 MCP 使用场景包括：

* AI Agent
* Browser Automation
* Web Search
* 数据采集
* 文件管理
* API 调用
* 企业内部系统集成

常见支持 MCP 的应用包括：

* Claude Desktop
* Cursor
* Windsurf
* Cline
* Roo Code
* Cherry Studio
* Open WebUI

***

### 为什么要为 MCP 配置代理？

在以下场景中，建议使用代理：

* MCP Server 需要访问海外 API
* MCP 工具需要访问公开网站
* 数据采集任务需要轮换 IP
* AI Agent 需要调用多个第三方服务
* 希望根据目标地区切换出口 IP

***

### 环境准备

开始之前，请确保已经获取：

```
Host: us.ipwo.net
Port: 7878

Username: username
Password: password
```

***

### 通过环境变量配置代理

大多数 MCP Server 都会继承系统环境变量。

Linux/macOS：

```bash
export HTTP_PROXY=http://username:password@us.ipwo.net:7878
export HTTPS_PROXY=http://username:password@us.ipwo.net:7878
```

Windows：

```cmd
set HTTP_PROXY=http://username:password@us.ipwo.net:7878
set HTTPS_PROXY=http://username:password@us.ipwo.net:7878
```

启动 MCP Server：

```bash
npx your-mcp-server
```

或：

```bash
python server.py
```

此时 MCP Server 发出的 HTTP 请求将通过代理转发。

***

### Node.js MCP Server 配置代理

如果 MCP Server 基于 Node.js 开发，可以通过代理 Agent 配置。

安装依赖：

```bash
npm install https-proxy-agent
```

示例：

```javascript
import { HttpsProxyAgent } from "https-proxy-agent";

const proxyAgent = new HttpsProxyAgent(
  "http://username:password@us.ipwo.net:7878"
);

fetch("https://api.example.com", {
  agent: proxyAgent
})
  .then(res => res.json())
  .then(console.log);
```

***

### Python MCP Server 配置代理

如果 MCP Server 使用 Python 开发，可以使用 requests 或 httpx 配置代理。

### requests

```python
import requests

proxies = {
    "http": "http://username:password@us.ipwo.net:7878",
    "https": "http://username:password@us.ipwo.net:7878"
}

response = requests.get(
    "https://api.ipify.org?format=json",
    proxies=proxies
)

print(response.text)
```

***

#### httpx

```python
import httpx

proxy = "http://username:password@us.ipwo.net:7878"

with httpx.Client(
    proxy=proxy
) as client:
    response = client.get(
        "https://api.ipify.org?format=json"
    )

print(response.text)
```

***

### Docker 中运行 MCP Server

如果 MCP Server 部署在 Docker 容器中，可以直接传入代理环境变量。

```bash
docker run \
-e HTTP_PROXY=http://username:password@us.ipwo.net:7878 \
-e HTTPS_PROXY=http://username:password@us.ipwo.net:7878 \
your-mcp-image
```

Docker 内的 MCP Server 将自动通过代理访问外部网络。

***

### 验证代理是否生效

启动 MCP Server 前，可以先验证代理连接。

```bash
curl -x "us.ipwo.net:7878" \
-U "username:password" \
ipinfo.io
```

返回结果示例：

```json
{
  "ip": "x.x.x.x",
  "country": "US"
}
```

说明代理连接正常。

***

### 常见问题

#### MCP Server 无法访问外部 API

请检查：

* 代理信息是否正确
* 代理账号是否有效
* 网络是否允许访问代理节点

***

#### Connection Timeout

可能原因：

* 目标网站响应慢
* 网络环境限制
* 防火墙拦截连接

建议：

* 更换代理节点
* 增加请求超时时间
* 测试代理连通性

***

#### Proxy Authentication Failed

通常表示：

* 用户名错误
* 密码错误
* 代理认证格式错误

请重新检查代理配置。

***

### 最佳实践

* 为不同 MCP 工具配置独立 Session，避免频繁切换 IP。
* 长时间运行的 Agent 建议使用 Sticky Session。
* 在 Docker、Kubernetes 和云服务器环境中统一管理代理配置。
* 对于数据采集类 MCP Server，建议结合 IP 轮换策略使用。
* 在生产环境中通过环境变量管理代理信息，避免将账号密码写入代码仓库。

***

### 相关阅读

完成 MCP 工具代理配置后，您可能还需要：

* [OpenAI SDK 使用代理](/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-openai-sdk-zhong-shi-yong-ipwo-dai-li.md)
* [Gemini API 使用代理](/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-gemini-api-zhong-shi-yong-ipwo-dai-li.md)
* [LangChain 使用住宅代理](/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-langchain-xiang-mu-zhong-pei-zhi-ipwo-dai-li.md)
* [Browser Use 使用住宅代理](/kai-fa-zhe-wen-dang/ai-agent-kai-fa/ru-he-zai-browser-use-zhong-pei-zhi-ipwo-dai-li.md)
* [获取和配置住宅代理](/kai-fa-zhe-wen-dang/kuai-su-kai-shi/huo-qu-dai-li-xin-xi.md)
* [如何检测代理是否生效](/dai-li-jian-ce-yu-yin-si-an-quan/ru-he-jian-ce-dai-li-shi-fou-sheng-xiao.md)
* [407 Proxy Authentication Required](/kai-fa-zhe-wen-dang/chang-jian-cuo-wu/407-proxy-authentication-required.md)
* [超时 Timeout](/kai-fa-zhe-wen-dang/chang-jian-cuo-wu/chao-shi-timeout.md)
