VSCode 扩展桥接 Copilot 模型
通过 VSCode 扩展可以将 GitHub Copilot 中的 Claude 模型桥接出来,供外部 AI Agent 调用。这种实现方式主要基于以下技术方案:
实现原理
-
VSCode Extension API
- 使用 VSCode 扩展开发框架创建自定义扩展
- 通过
vscode.languages和vscode.commandsAPI 注册命令和服务 - 利用 Language Server Protocol (LSP) 实现与外部通信
-
GitHub Copilot API 调用
- 扩展内部通过 Copilot 的内部 API 访问 Claude 模型
- 使用
vscode.authentication获取 GitHub 认证令牌 - 调用 Copilot Chat API 发送请求并接收响应
-
MCP (Model Context Protocol) 服务器
- 在扩展中实现 MCP 服务器,暴露标准化的接口
- MCP 定义了统一的消息格式和通信协议
- 支持通过 stdio、HTTP 或 WebSocket 与外部 Agent 通信
核心实现步骤
- 创建 VSCode 扩展
// extension.ts
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
// 启动 MCP 服务器
const mcpServer = new MCPServer();
mcpServer.start();
// 注册命令处理 Copilot 请求
context.subscriptions.push(
vscode.commands.registerCommand('extension.queryCopilot', async (prompt) => {
return await queryCopilotModel(prompt);
})
);
}
- 实现 MCP 服务器
// mcp-server.ts
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
class MCPServer {
private server: Server;
constructor() {
this.server = new Server({
name: 'copilot-bridge',
version: '1.0.0'
}, {
capabilities: {
resources: {},
tools: {},
prompts: {}
}
});
// 注册工具
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [{
name: 'query_claude',
description: 'Query Claude model via Copilot',
inputSchema: { /* ... */ }
}]
}));
}
async start() {
const transport = new StdioServerTransport();
await this.server.connect(transport);
}
}
- 桥接 Copilot API
// copilot-bridge.ts
async function queryCopilotModel(prompt: string) {
// 获取 Copilot Chat API
const copilot = await vscode.lm.selectChatModels({
vendor: 'copilot',
family: 'claude'
});
// 发送请求
const messages = [
vscode.LanguageModelChatMessage.User(prompt)
];
const response = await copilot[0].sendRequest(messages);
// 收集响应流
let result = '';
for await (const chunk of response.text) {
result += chunk;
}
return result;
}
- 外部 Agent 调用
# external_agent.py
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
async def call_copilot_claude(prompt: str):
# 连接到 VSCode 扩展的 MCP 服务器
server_params = StdioServerParameters(
command="code",
args=["--extensionDevelopmentPath=/path/to/extension"]
)
async with stdio_client(server_params) as (read, write):
async with ClientSession(read, write) as session:
# 初始化连接
await session.initialize()
# 调用工具
result = await session.call_tool("query_claude", {
"prompt": prompt
})
return result
关键技术点
-
认证与权限
- 利用 VSCode 的 GitHub 认证状态
- 扩展运行在 VSCode 进程中,自动继承 Copilot 订阅权限
-
通信方式
- Stdio:最简单,通过标准输入输出通信
- HTTP Server:扩展启动 HTTP 服务器,外部通过 REST API 调用
- WebSocket:支持双向实时通信
-
消息序列化
- 使用 JSON-RPC 2.0 协议
- MCP 定义了标准的请求/响应格式
- 支持流式响应(SSE)
优势
- 复用认证:无需单独配置 API Key
- 成本节省:使用已有的 Copilot 订阅
- 统一接口:通过 MCP 提供标准化接口
- 安全隔离:扩展运行在 VSCode 沙箱中
相关项目
- MCP SDK:
@modelcontextprotocol/sdk- MCP 协议实现 - VSCode Extension API:VSCode 扩展开发框架
- Language Model API:
vscode.lm- VSCode 语言模型接口
这种桥接方案使得开发者可以在本地开发环境中,通过统一的 MCP 接口调用多种 AI 模型,包括 GitHub Copilot 提供的 Claude、GPT 等模型。
相关文章
- AI Agent Development
- Intent Recognition and Slot Filling
- Function Calling
- Tool Calling Patterns
- Chain of Thought (CoT)
- Output Constraint
- LangChain
- LlamaIndex
- Agent Token Routing
维护记录
| 时间 | 修改内容 | 原因 |
|---|---|---|
| 2026-07-23 | 自 ai-agent-development.md 拆出为本篇 |
母文过长,按主题拆分为独立文档 |