把 MCP Server 打包进 VS Code extension

大家好!我是韩老师。

本文是 MCP 系列文章的第六篇,之前的五篇是:

写在最前:如果你正好在用 Node.js 开发一个 MCP Server,那么,这篇文章将对你有用!

WHY

首先,你肯定要问,把 MCP Server 打包进 VS Code extension,有什么好处?

好处,有三:

  1. 在 VS Code 中,用户不需要任何配置,就可以直接安装 MCP Server。
  2. 用户不需要安装任何额外的工具来运行 MCP Server。甚至于,用户不需要安装 Node.js/npx。
  3. 整个 MCP Server + VS Code extension 的体积很小。以我的 Code Runner MCP Server 的 VS Code extension 为例,在没有使用 webpack 的情况下,整个体积都只有 4 MB。

HOW

你肯定想要知道,如何做到?

其实,很简单,三步就能搞定!

  1. 在你的 MCP Server 中,export 一个启动 Streamable Http MCP Server 的函数:
复制代码
export async function startMcpServer(transport: Transport, options?: HttpServerOptions): Promise<void | McpServerEndpoint> {
    if (transport === 'stdio') {
        return startStdioMcpServer();
    } else if (transport === 'http') {
        return startStreamableHttpMcpServer(options?.port);
    } else {
        throw new Error('Invalid transport. Must be either "stdio" or "http"');
    }
}
  1. 在 VS Code extension 中,引用你的 MCP Server 的 npm,然后启动 Streamable Http MCP Server,然后得到一个 localhost 的 MCP URL:
复制代码
import { startMcpServer } from "mcp-server-code-runner";

async function startHttpMcpServer(): Promise<string | undefined> {
    const result = await startMcpServer("http", { port: 3098 });

    return result ? result.url : undefined;
}
  1. 把这个 localhost 的 MCP URL,配置到 VS Code 的 settings.json 中:
复制代码
async function updateMcpUrlToVsCodeSettings(mcpUrl: string) {
    const configuration = vscode.workspace.getConfiguration();
    const mcpServers = configuration.get<any>("mcp.servers", {});
    mcpServers["code-runner-streamable-http-mcp-server"] = {
        type: "http",
        url: mcpUrl,
    };
    await configuration.update("mcp.servers", mcpServers, vscode.ConfigurationTarget.Global);
}

好了,大功告成!

原理其实也很简单:因为 VS Code extension,就是一个 Node.js 的程序。MCP Server 作为 VS Code extension 的一部分,一起运行在了 Extension Host 这个进程中。所以,不需要用户的机器上额外安装 Node.js/npx 了!

完整代码,完全开源,欢迎大家围观!

MCP Server:

https://github.com/formulahendry/mcp-server-code-runner

VS Code extension:

https://github.com/formulahendry/vscode-code-runner-mcp-server

大家也可以也可以在 VS Code Marketplace 搜索 Code Runner MCP Server 来直接试用:

注意需要使用最新的 VS Code Insider,版本号 >= 1.100
https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner-mcp-server

相关推荐
coder_pig9 小时前
Antigravity 登录问题/数据泄露风险 (附:白嫖一个月 Gemini Enterprise 攻略)
aigc·visual studio code·gemini
Breath5710 小时前
代码执行 + MCP:AI 代理 token 省 98%!
agent·ai agent·mcp·上下文工程
魁首10 小时前
AI Agent 协议演进:从 MCP 到 ACP 的架构对比与未来展望
openai·gemini·mcp
奇舞精选15 小时前
我用 Chrome 扩展验证了 MCP 的安全风险,结果发现
chrome·mcp
该用户已不存在1 天前
6款MCP工具,让AI真正懂业务
ai编程·mcp
喵个咪2 天前
代码特殊注释完整规范:IDE 支持、使用示例与团队协作指南
intellij idea·visual studio code
疯狂踩坑人2 天前
Node写MCP入门教程
前端·agent·mcp
绿荫阿广2 天前
使用.NET开发并上线一个小智AI对话机器人的MCP服务转接平台
.net·asp.net core·mcp
带刺的坐椅2 天前
(让 Java IA & MCP 更简单 )Solon AI v3.7.2 发布
ai·chatgpt·openai·solon·mcp
洗澡水加冰2 天前
MCP与Skills的辨析
后端·aigc·mcp