图像生成服务

一个使用Grok Imagine生成图像的MCP服务器,通过自动化无头Chromium浏览器提交提示、等待生成并将结果图像保存到磁盘。

  • by laruss
  • TypeScript
  • 本地部署
  • 5 星标
  • 更新于
在 GitHub 查看 laruss/grok-imagine-mcp

使用说明

grok想象一下mcp

MCP(模型上下文协议)服务器,使用 Grok想象一下。它使无头Chromium浏览器自动提交提示、等待生成并将生成的图像保存到磁盘。

特性

  • 图像生成 通过具有可配置宽高比的文本提示
  • 基于Cookie的身份验证 --从浏览器粘贴cURL命令以登录
  • 并发生成 --最多4个并行选项卡,可自动排队处理其他请求
  • 自动关闭浏览器 --在1分钟不活动后关闭Chromium,在下次请求时重新打开
  • 错误处理 --检测速率限制、过期会话和带有清除消息的登录重定向
  • 隐身模式 --用途 playwright-extra 使用隐形插件避免机器人检测

先决条件

  • 包子 v1.3+
  • Grok帐户(免费版有效)

安装

bun install

MCP配置

将服务器添加到MCP客户端配置中(例如。 .cursor/mcp.json):

{
  "mcpServers": {
    "grok-imagine-mcp": {
      "command": "bun",
      "args": ["run", "/path/to/grok-imagine-mcp/src/index.ts"]
    }
  }
}

集 "env": { "DEV": "true" } 以可见(非无头)模式运行浏览器进行调试。

工具

auth

通过在浏览器会话中提供Cookie与Grok进行身份验证。

  1. 打开https://grok.com在浏览器中(确保您已登录)
  2. 打开开发人员工具(F12或Cmd+选项+I)
  3. 转到“网络”选项卡
  4. 刷新页面
  5. 点击grok.com的任何请求
  6. 右键单击->“复制为cURL”
  7. 将整个curl命令传递给 auth 工具

该工具提取并保存必要的Cookie(sso, sso-rw, cf_clearance, x-userid).Cookie持续存在 data/storage-state.json.

generate_images

使用Grok Imagine生成图像。

参数 类型 必填 说明
prompt string yes 描述要生成的图像的文本提示
quantity number no 要保存的图像数量(1-6,默认值:1)。每批生成约3张图像。
saveFolderFullPath string yes 保存图像的文件夹的绝对路径
aspectRatio string 否 2:3, 3:2, 1:1, 9:16,或 16:9 (默认值: 1:1)

图像另存为 grok-{timestamp}-{index}.jpg (或 .png).

发展

# Run in dev mode (visible browser, file watching)
bun run dev

# Type check
bun run typecheck

# Lint & format
bun run fix

项目结构

src/
  index.ts        MCP server entry point and tool registration
  browser.ts      Browser lifecycle, tab pool, and idle timeout
  generate.ts     Image generation logic (prompt submission, polling, saving)
  chromium.ts     Playwright + stealth plugin setup
  cookies.ts      Cookie parsing, saving, and loading
  console.ts      Browser console log capture
  constants.ts    Configuration constants
data/
  profile/        Chromium persistent user data
  storage-state.json   Saved cookies and storage state