🚀 基于 FastMCP 的 VSCode 扩展框架,将 VSCode 转变为一个 MCP(模型上下文协议)服务器,允许外部客户端通过 HTTP 流传输和服务器发送事件(SSE)执行 VSCode 内部命令。
text/event-stream 协议,支持实时通信git clone https://github.com/bestk/vscode-internal-command-mcp-server
cd vscode-internal-command-mcp-server
npm install
npm run compile
F5 启动扩展开发主机.vsix 文件安装在 VSCode 设置中配置服务器参数:
{
"vscode-internal-command-mcp-server.port": 8080,
"vscode-internal-command-mcp-server.host": "localhost",
"vscode-internal-command-mcp-server.autoStart": true,
"vscode-internal-command-mcp-server.asyncExecution": true,
"vscode-内部命令-mcp-server.executionDelay": 1000,
"vscode-internal-command-mcp-server.showAsyncNotifications": false,
"vscode-internal-command-mcp-server.allowedCommands": [
"editor.action.formatDocument",
"workbench.action.files.save",
"editor.action.clipboardCopyAction"
]
}
| 配置项 | 类型 | 默认值 | 描述 |
|---|---|---|---|
port | number | 8080 | MCP 服务器端口 |
host | string | localhost | MCP 服务器主机地址 |
autoStart | boolean | true | 扩展激活时自动启动服务器 |
asyncExecution | boolean | true | 启用异步命令执行(立即返回,后台执行) |
executionDelay | number | 0 | 命令执行延迟(毫秒) |
showAsyncNotifications | boolean | false | 显示异步命令完成通知 |
allowedCommands | string[] | [] | 允许执行的命令列表(空数组表示允许所有命令) |
autoStart 为 true)VSCode 内部命令 MCP 服务器:启动服务器http://localhost:8080/mcphttp://localhost:8080/healthVSCode 内部命令 MCP 服务器:显示状态 查看详细状态执行 VSCode 内部命令
参数
{
command: string; // VSCode 命令 ID
arguments?: string[]; // 命令参数(可选)
}
异步执行响应示例
{
"success": true,
"async": true,
"taskId": "bg_task_1_1756952250790",
"message": "命令 'composer.cancelComposerStep' 已提交到后台执行,将在 1000ms 后执行",
"command": "composer.cancelComposerStep",
"arguments": [],
"executionDelay": 1000,
"queueLength": 1,
"taskStats": {
"total": 1,
"pending": 1,
"running": 0,
"completed": 0,
"failed": 0,
"cancelled": 0
}
}
列出所有可用的 VSCode 命令
参数:无
返回:命令列表(前 20 条,超出部分显示省略号)
获取当前工作区信息
参数:无
返回
{
name: string; // 工作区名称
folders: Array<{
// 工作区文件夹
name: string;
uri: string;
}>;
activeEditor: string; // 当前活动编辑器文件路径
}
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
const transport = new StreamableHTTPClientTransport(new URL('http://localhost:8080/mcp'), {
requestInit: {
headers: {
'Content-Type': 'application/json',
Accept: 'application/json, text/event-stream',
},
},
});
const client = new Client({
name: 'vscode-mcp-client',
version: '1.0.0',
});
// 连接并使用
await client.connect(transport);
// 调用工具
const result = await client.callTool({
name: 'execute_vscode_command',
arguments: {
command: 'editor.action.formatDocument',
},
});
console.log('命令结果:', result);
在 Cursor 中配置 MCP 服务器:
{
"mcpServers": {
"vscode-internal-commands": {
"url": "http://localhost:8080/mcp",
"transport": "http"
}
}
}
# 健康检查
curl http://localhost:8080/health
# 列出工具
curl -X POST http://localhost:8080/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/list"
}'
# 执行命令
curl -X POST http://localhost:8080/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "execute_vscode_command",
"arguments": {
"command": "workbench.action.files.save"
}
}
}'
┌─────────────────────────┐
│ VSCode 扩展 │
├─────────────────────────┤
│ FastMcpServer │ ← 基于 FastMCP 框架
├─────────────────────────┤
│ ServerManager │ ← 服务器管理和状态
├─────────────────────────┤
│ CommandExecutor │ ← VSCode 命令执行器
├─────────────────────────┤
│ BackgroundTaskExecutor │ ← 后台任务执行器
├─────────────────────────┤
│ TaskProvider │ ← VS Code 任务提供者
└─────────────────────────┘
httpStreamtext/event-streamvscode-internal-command-mcp-server/
├── src/
│ ├── extension.ts # 扩展入口点
│ ├── fastMcpServer.ts # FastMCP 服务器实现
│ ├── serverManager.ts # 服务器管理器
│ ├── commandExecutor.ts # VSCode 命令执行器
│ ├── backgroundTaskExecutor.ts # 后台任务执行器
│ └── taskProvider.ts # VS Code 任务提供者
├── out/ # 编译输出
├── package.json # 扩展配置和依赖
├── tsconfig.json # TypeScript 配置
└── README.md # 项目文档
# 开发模式编译
npm run compile
# 监视模式编译
npm run watch
# 启动开发
code . # 打开 VSCode,按 F5 启动调试
F5 启动扩展开发主机VSCode 内部命令 MCP 服务器:测试 MCP 工具# 使用 FastMCP 开发工具测试
npx fastmcp dev src/fastMcpServer.ts
# 使用 MCP Inspector 检查
npx fastmcp inspect src/fastMcpServer.ts
出于安全原因,建议配置 allowedCommands 白名单:
{
"vscode-internal-command-mcp-server.allowedCommands": [
"editor.action.formatDocument",
"workbench.action.files.save",
"workbench.action.files.saveAll",
"editor.action.clipboardCopyAction",
"editor.action.clipboardPasteAction"
]
}
localhost 的监控,防止外部访问欢迎提交 Issues 和 Pull Requests!
git checkout -b feature/amazing-featuregit commit -m '添加神奇功能'git push origin feature/amazing-featureMIT 许可证 - 详情见 LICENSE 文件
如果您遇到任何问题或有任何疑问:
🚀 将 VSCode 转变为您的 MCP 服务器,解锁无限可能!