把 MCP Server 打包進(jìn) VS Code extension
大家好!我是韓老師。
寫在最前:如果你正好在用 Node.js 開發(fā)一個 MCP Server,那么,這篇文章將對你有用!
WHY
首先,你肯定要問,把 MCP Server 打包進(jìn) VS Code extension,有什么好處?
好處,有三:
在 VS Code 中,用戶不需要任何配置,就可以直接安裝 MCP Server。 用戶不需要安裝任何額外的工具來運(yùn)行 MCP Server。甚至于,用戶不需要安裝 Node.js/npx。 整個 MCP Server + VS Code extension 的體積很小。以我的 Code Runner MCP Server 的 VS Code extension 為例,在沒有使用 webpack 的情況下,整個體積都只有 4 MB。
HOW
你肯定想要知道,如何做到?
其實(shí),很簡單,三步就能搞定!
1. 在你的 MCP Server 中,export 一個啟動 Streamable Http MCP Server 的函數(shù):
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"'); } }
2. 在 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; }
3. 把這個 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); }
好了,大功告成!
原理其實(shí)也很簡單:因?yàn)?VS Code extension,就是一個 Node.js 的程序。MCP Server 作為 VS Code extension 的一部分,一起運(yùn)行在了 Extension Host 這個進(jìn)程中。所以,不需要用戶的機(jī)器上額外安裝 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
到此這篇關(guān)于把 MCP Server 打包進(jìn) VS Code extension的文章就介紹到這了,更多相關(guān)MCP Server 打包進(jìn) VS Code擴(kuò)展內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 教你如何實(shí)現(xiàn)容器化 MCP Server
- 輕松開發(fā) Streamable HTTP MCP Server
- 在 VS Code 中,一鍵安裝 MCP Server!
- 從零開始開發(fā)一個 MCP Server!
- VS?Code擴(kuò)展Code?Runner?MCP?Server來了
- Go語言開發(fā)實(shí)現(xiàn)查詢IP信息的MCP服務(wù)器
- 一文弄懂用Go實(shí)現(xiàn)MCP服務(wù)的示例代碼
- 一文帶你學(xué)會使用Go語言實(shí)現(xiàn)自己的MCP服務(wù)端
- SpringBoot項目引入MCP的實(shí)現(xiàn)示例
- .NET?MCP?文檔詳細(xì)指南
相關(guān)文章
使用Node.js創(chuàng)建一個簡單的HTTP服務(wù)器的示例代碼
Node.js 是一種強(qiáng)大的 JavaScript 運(yùn)行環(huán)境,允許開發(fā)者在服務(wù)器端運(yùn)行 JavaScript 代碼,它以異步事件驅(qū)動的方式處理大量連接,適合構(gòu)建高效的網(wǎng)絡(luò)應(yīng)用程序,在這篇文章中,我們將一起學(xué)習(xí)如何使用 Node.js 創(chuàng)建一個簡單的 HTTP 服務(wù)器,并通過示例代碼幫你快速上手2025-02-02利用node.js+mongodb如何搭建一個簡單登錄注冊的功能詳解
這篇文章主要給大家介紹了關(guān)于利用node.js+mongodb如何搭建一個簡單登錄注冊功能的相關(guān)資料,文中通過示例代碼介紹非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07詳解基于Node.js的微信JS-SDK后端接口實(shí)現(xiàn)代碼
本篇文章主要介紹了詳解基于Node.js的微信JS-SDK后端接口實(shí)現(xiàn)代碼,具有一定的參考價值,有興趣的可以了解一下2017-07-07Node.js包管理器代理工具Verdaccio輕松創(chuàng)建管理本地npm包倉庫
這篇文章主要為大家介紹了Node.js包管理器代理工具Verdaccio輕松創(chuàng)建管理本地npm包倉庫的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10