前言
由于 Claude Code 和 Qwen 的请求格式有点不兼容,需要另外写一个代理来确保正常地请求
版本
Claude Code v2.1.156
使用代理拦截和修改请求
const http = require('http');
const https = require('https');
const targetHost = 'dashscope.aliyuncs.com';
// 阿里云实际的 Anthropic 兼容接口路径可能需要确认
const targetPath = '/apps/anthropic/v1/messages'; // 尝试这个路径
const server = http.createServer((req, res) => {
// 处理 OPTIONS 预检请求(CORS)
if (req.method === 'OPTIONS') {
res.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': '*'
});
res.end();
return;
}
// 只处理 POST 请求
if (req.method !== 'POST') {
res.writeHead(405);
res.end('Method Not Allowed');
return;
}
let body = '';
req.on('data', chunk => { body += chunk; });
req.on('end', () => {
console.log('收到的原始请求体:', body);
if (!body || body.trim() === '') {
console.error('收到空请求体');
res.writeHead(400);
res.end('Empty request body');
return;
}
try {
const originalReq = JSON.parse(body);
console.log('解析后的请求:', JSON.stringify(originalReq, null, 2));
// 阿里云可能需要使用 API Key 而不是 auth token
const apiKey = 'your api key';
// 修改请求头
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`, // 使用 Bearer token
'Content-Length': Buffer.byteLength(JSON.stringify(originalReq))
};
// 复制其他重要的头
if (req.headers['x-api-key']) {
headers['x-api-key'] = req.headers['x-api-key'];
}
const options = {
hostname: targetHost,
path: targetPath,
method: 'POST',
headers: headers
};
console.log('转发请求到:', targetHost + targetPath);
console.log('请求头:', headers);
const proxyReq = https.request(options, (proxyRes) => {
let responseBody = '';
proxyRes.on('data', chunk => {
responseBody += chunk;
});
proxyRes.on('end', () => {
console.log('代理响应状态:', proxyRes.statusCode);
console.log('代理响应体:', responseBody);
// 转发响应
res.writeHead(proxyRes.statusCode, {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json'
});
res.end(responseBody);
});
});
proxyReq.on('error', (e) => {
console.error('代理请求错误:', e);
res.writeHead(500);
res.end(JSON.stringify({ error: e.message }));
});
proxyReq.write(JSON.stringify(originalReq));
proxyReq.end();
} catch(e) {
console.error('❌ Proxy error:', e.message);
console.error('原始 body 内容:', body.substring(0, 200));
res.writeHead(500);
res.end(JSON.stringify({ error: e.message }));
}
});
});
server.listen(8080, () => {
console.log('✅ Proxy running on http://localhost:8080');
console.log('等待请求转发到:', targetHost + targetPath);
});
然后修改 settings.json:**
{
"env": {
"ANTHROPIC_AUTH_TOKEN": "你的apikey",
"ANTHROPIC_BASE_URL": "http://localhost:8080",
"ANTHROPIC_MODEL": "qwen3-coder-plus"
}
}
如果还需要中文回答,可以配置提示词
在 claude 根目录下放 CLAUDE.md ( settings.json同级位置 )
# 语言规则
- 请始终使用简体中文回答我的所有问题和请求。
- 代码注释使用中文,但函数名和变量名保持英文。
- 技术术语可以保留英文,但需提供中文解释。
使用方法
先用 node 运行 proxy.js 最后再启动 claude 就可以用了