如果,我是说如果,我们不让大模型走文生图,而是让他们直接吐坐标画像素画,你觉得会怎么样。
MCP is an open protocol that standardizes how applications provide context to large language models (LLMs). Think of MCP like a USB-C port for AI applications. Just as USB-C provides a standardized way to connect your devices to various peripherals and accessories, MCP provides a standardized way to connect AI models to different data sources and tools. MCP enables you to build agents and complex workflows on top of LLMs and connects your models with the world. [1]
信息
所有需要你输入的指令,都将以 $
开头,其余行均是命令输出,输入命令时,不要带 $
符号
$ mkdir pix-llm
$ cd pix-llm
$ mkdir -p packages/mcp-server
信息
此后,除非特殊指定,所有命令块与代码文件的工作目录均为 pix-llm
$ cd packages/mcp-server
$ bun init
$ bun init
✓ Select a project template: Blank
$ cd packages
$ bun create vue
┌ Vue.js - The Progressive JavaScript Framework
│
◇ 请输入项目名称:
│ preview-client
│
◇ 请选择要包含的功能: (↑/↓ 切换,空格选择,a 全选,回车确认)
│ TypeScript
│
◇ 选择要包含的试验特性: (↑/↓ 切换,空格选择,a 全选,回车确认)
│ none
│
◇ 跳过所有示例代码,创建一个空白的 Vue 项目?
│ Yes
$ cd packages/preview-client
$ bun install
除了在 mcp-server 中实现一个画布和 MCP 协议,我们顺便启动一个 WebSocket 服务器,给我们的前端做实时通讯
$ cd packages/mcp-server
$ bun add @modelcontextprotocol/sdk
$ bun add zod@^3
$ bun add canvas # 这个用于生成像素画的预览
警告
你可能注意到这里使用了 Zod 3 而非最新版本的 Zod 4,截至 @modelcontextprotocol/sdk,inputSchema
仍然使用 Zod 3,我们需要使用匹配的版本,或者干脆不要安装 Zod 依赖而直接使用来自 @modelcontextprotocol/sdk 的 Zod 版本
一个粗糙的画布实现
然后是 MCP 和 WebSocket 部分
稍作解释
const transport = new StdioServerTransport(); mcpServer.connect(transport)
会创建一个基于标准输入输出的传输通道,并将 MCP 服务器连接到这个通道上。这样,MCP 服务器就可以通过标准输入输出与外部系统进行通信(虽然名为 MCP 服务器,但是我们的使用方法并不需要依赖于网络请求)。稍作解释
Bun.serve()
默认启动在 localhost:3000
。server.proxy
将 /ws
代理到 localhost:3000/ws
,同时更改 origin,这使得我们无需处理烦人的 CORS 问题。当然,你也可以修改 Bun.serve() 的代码添加 CORS 头部。bunx --bun vite
启动预览客户端,server.proxy
无法正常工作,你将会看到 [vite] ws proxy error: undefined
。如果你也在用 Cline:
或者使用 Copilot:
然后启动预览
$ cd packages/preview-client
$ bun dev
如果你恰好只打开了一个 VSCode 窗口,那么恭喜你,你已经可以使用这个小玩具了!
事实上,如果你同时打开了两个 VSCode 窗口,或者同时使用了 Cline 和 Copilot,或者各种差不多的情况,你很有可能遇到一个小问题:
error: Failed to start server. Is port 3000 in use? syscall: "listen", errno: 0, code: "EADDRINUSE" at [REDACTED]/pix-llm/packages/mcp-server/index.ts:10:27 at loadAndEvaluateModule (2:1)
除非特殊配置,我们在 MCP 服务器里使用的 Bun.serve()
将始终尝试使用 3000 端口。很不幸的,我们没法控制各种 agent tool 只启动一次我们的程序,所以我们要进行亿些小修改
此处采用的修改方法:
类似建立 mcp-server 的过程,建立一个 db-server
把 packages/mcp-server/canvasDb.ts
移动到 packages/db-server/canvasDb.ts
处理 db-server
修改 mcp-server
启动这个中间商服务器
$ cd packages/db-server
$ bun index.ts
Server started on http://localhost:3000
打开预览前端
$ cd packages/preview-client
$ bun dev
# 然后戳一下给出的网址
最后给大模型喂一段 prompt
create a pixel art with the tool you have, as complex as possible, then describe it for me
坐等结果
Created a complex 32x32 pixel art featuring:
- Central diamond structure in orange-red with nested smaller diamonds
- Four concentric square borders in alternating blue and green
- Radiating diagonal lines in purple creating star-like patterns
- Symmetrical geometric designs in all four quadrants
- Color gradients achieved through strategic pixel placement
- Multiple layers of visual complexity with overlapping shapes The artwork combines precise symmetry with vibrant color contrasts to create an intricate, eye-catching composition.
Created a 32x32 pixel art landscape featuring multiple elements:
- A yellow sun (#FFFF00) in the top-left quadrant with radiating edges
- Light blue sky (#87CEEB) covering the upper portion of the canvas
- Two white clouds (#FFFFFF) floating in the sky
- A green bush/tree (#008000) with darker green details (#006400) in the mid-left area
- A red house (#FF0000) with a blue window (#0000FF) and black door (#000000) in the center
- A gray mountain range (#808080) on the right side
- A brown tree trunk (#8B4513) with white cloud-like foliage in the lower-left area
The composition balances natural elements (sky, clouds, mountain, tree) with a man-made structure (house), creating a cohesive outdoor scene.
这只是一个及其粗糙的 MCP 实现,我们还有很多可以改进的地方
SDK 中还有例如 registerPrompt()
registerResource()
这样的方法完全没有使用
但是无论如何,这都是一个非常有趣的小玩具,读者也可尝试扩充这个小玩具的各种功能
完整代码见 GitHub 仓库,完成这篇文章时对本仓库的代码进行了小范围的修改,因此并不完全一致
我测我博客排版怎么又炸了,修一下