最近在集成Gemini 3的API,发现还没有一个可以在各个 Coding 客户端配置的 MCP的画图工具,支持一边写代码,一边画流程图,于是把Gemini 3 Nano Banana
的API继承了进来,干脆自己写了一个,下面分享一下技术方案。有兴趣的兄弟可以去Github提交issue哈。
Github地址: https://github.com/aiagenta2z/gemini_mcp_onekey
下面会介绍这个工具架构设计和链接方案,成功之后部署在OneKey Agent Router 上试用地址:Website-Playground Gemini 3 Nano Banana MCP
Agent的信息和API也托管到网站上了 Nano Banana MCP Server,可以设置了API来方便 Agent MCP Router 来调用分发。
设计概念:主要是它是基于Python写的,支持最新的Gemini 2.5 Flash,还有 Gemini 3 Pro 的图片API调用功能
1. Tool工具设计
封装的一个生成图片的函数,支持参数:prompt 生成指令,model_name 模型名 ,还有其他属性作为图片指定的作为入参。这里 prompt 是把用户输入短的要求,给扩写为精确的 prompt,可以调用Qwen/Gemini等模型来使用,比如把 "火鸡逃跑" 扩写成为 例如 "Turkey Escape, White House Scene, Two man, Chaos," 等长并且具体的。
@server.tool() def generate_image_nano_banana( model: Annotated[str, "The image generation model to use. Defaults to 'gemini-2.5-flash-image'. Supported models include 'gemini-3-pro-image-preview', 'gemini-2.5-flash-image' "] = "gemini-2.5-flash-image", prompt: Annotated[str, "A detailed text description for the image to be generated."] = "A detailed, cinematic image of a futuristic city.", image_name: Annotated[str, "The filename for the output image. Defaults to 'gemini_output_images.png'."] = "gemini_output_images.png", output_folder: Annotated[Optional[str], "The optional folder path where the image will be saved. If None, uses a default location."] = None, aspect_ratio: Annotated[str, "The aspect ratio of the generated image (e.g., '16:9', '1:1', '4:3')."] = "16:9", image_size: Annotated[str, "The size/resolution of the generated image (e.g., '1K', '2K')."] = "1K" ) -> Dict:
2. Gemini 3 Nano Banana 工具调用
对应链接Google Gemini客户端方法, 这里有一个细节处理:超时设置,因为有一些图片比如4K生成过慢,
所以要设置好超时时间,方便agent loop继续,需要外部设置 http_options,然后初始化传递给客户端。
http_options = types.HttpOptions(timeout=120000) client = genai.Client(api_key=GOOGLE_API_KEY, http_options=http_options) ## more code response = client.models.generate_content( model=model, contents=prompt, config=types.GenerateContentConfig( tools=[{"google_search": {}}], image_config=types.ImageConfig( aspect_ratio=aspect_ratio, image_size=image_size ## 4K 2K 1K ), # thinking_config=types.ThinkingConfig(thinking_level="low") ) )
3. 国内直连方案
Gemini客户端直接从国内IP是无法请求通的,这里利用了MCP Router的路由转发能力,支持 Cursor等客户端 链接 Router服务器,再转发给Gemini 请求
这里接住了 DeepNLP OneKey MCP Router转发部署,就可以请求到了哈,使用方法配置,需要做key验证,目前通过测试KEY,但是限流。
自己注册的KEY(https://www.deepnlp.org/workspace/keys) 和对应 API Endpoint 开发文档 (https://www.deepnlp.org/doc/onekey_mcp_router) 就不会限流了。
{ "mcpServers": { "deepnlp-onekey-gemini": { "url": "https://agent.deepnlp.org/mcp?server_name=gemini&onekey=BETA_TEST_KEY_OCT_2025" }, "deepnlp-onekey-nano-banana": { "url": "https://agent.deepnlp.org/mcp?server_name=gemini-nano-banana&onekey=BETA_TEST_KEY_OCT_2025" } } }
如果有什么问题随时欢迎提交Issue给我