|
| 1 | +#!/usr/bin/env node |
| 2 | +import EverArt from "everart"; |
| 3 | +import { Server } from "@modelcontextprotocol/sdk/server/index.js"; |
| 4 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
| 5 | +import { |
| 6 | + CallToolRequestSchema, |
| 7 | + ListToolsRequestSchema, |
| 8 | + ListResourcesRequestSchema, |
| 9 | + ReadResourceRequestSchema, |
| 10 | +} from "@modelcontextprotocol/sdk/types.js"; |
| 11 | +import fetch from "node-fetch"; |
| 12 | +import open from "open"; |
| 13 | + |
| 14 | +const server = new Server( |
| 15 | + { |
| 16 | + name: "example-servers/everart", |
| 17 | + version: "0.1.0", |
| 18 | + }, |
| 19 | + { |
| 20 | + capabilities: { |
| 21 | + tools: {}, |
| 22 | + resources: {}, // Required for image resources |
| 23 | + }, |
| 24 | + }, |
| 25 | +); |
| 26 | + |
| 27 | +if (!process.env.EVERART_API_KEY) { |
| 28 | + console.error("EVERART_API_KEY environment variable is not set"); |
| 29 | + process.exit(1); |
| 30 | +} |
| 31 | + |
| 32 | +const client = new EverArt.default(process.env.EVERART_API_KEY); |
| 33 | + |
| 34 | +server.setRequestHandler(ListToolsRequestSchema, async () => ({ |
| 35 | + tools: [ |
| 36 | + { |
| 37 | + name: "generate_image", |
| 38 | + description: |
| 39 | + "Generate images using EverArt Models and returns a clickable link to view the generated image. " + |
| 40 | + "The tool will return a URL that can be clicked to view the image in a browser. " + |
| 41 | + "Available models:\n" + |
| 42 | + "- 5000:FLUX1.1: Standard quality\n" + |
| 43 | + "- 9000:FLUX1.1-ultra: Ultra high quality\n" + |
| 44 | + "- 6000:SD3.5: Stable Diffusion 3.5\n" + |
| 45 | + "- 7000:Recraft-Real: Photorealistic style\n" + |
| 46 | + "- 8000:Recraft-Vector: Vector art style\n" + |
| 47 | + "\nThe response will contain a direct link to view the generated image.", |
| 48 | + inputSchema: { |
| 49 | + type: "object", |
| 50 | + properties: { |
| 51 | + prompt: { |
| 52 | + type: "string", |
| 53 | + description: "Text description of desired image", |
| 54 | + }, |
| 55 | + model: { |
| 56 | + type: "string", |
| 57 | + description: |
| 58 | + "Model ID (5000:FLUX1.1, 9000:FLUX1.1-ultra, 6000:SD3.5, 7000:Recraft-Real, 8000:Recraft-Vector)", |
| 59 | + default: "5000", |
| 60 | + }, |
| 61 | + image_count: { |
| 62 | + type: "number", |
| 63 | + description: "Number of images to generate", |
| 64 | + default: 1, |
| 65 | + }, |
| 66 | + }, |
| 67 | + required: ["prompt"], |
| 68 | + }, |
| 69 | + }, |
| 70 | + ], |
| 71 | +})); |
| 72 | + |
| 73 | +server.setRequestHandler(ListResourcesRequestSchema, async () => { |
| 74 | + return { |
| 75 | + resources: [ |
| 76 | + { |
| 77 | + uri: "everart://images", |
| 78 | + mimeType: "image/png", |
| 79 | + name: "Generated Images", |
| 80 | + }, |
| 81 | + ], |
| 82 | + }; |
| 83 | +}); |
| 84 | + |
| 85 | +server.setRequestHandler(ReadResourceRequestSchema, async (request) => { |
| 86 | + if (request.params.uri === "everart://images") { |
| 87 | + return { |
| 88 | + contents: [ |
| 89 | + { |
| 90 | + uri: "everart://images", |
| 91 | + mimeType: "image/png", |
| 92 | + blob: "", // Empty since this is just for listing |
| 93 | + }, |
| 94 | + ], |
| 95 | + }; |
| 96 | + } |
| 97 | + throw new Error("Resource not found"); |
| 98 | +}); |
| 99 | + |
| 100 | +server.setRequestHandler(CallToolRequestSchema, async (request) => { |
| 101 | + if (request.params.name === "generate_image") { |
| 102 | + try { |
| 103 | + const { |
| 104 | + prompt, |
| 105 | + model = "207910310772879360", |
| 106 | + image_count = 1, |
| 107 | + } = request.params.arguments as any; |
| 108 | + |
| 109 | + // Use correct EverArt API method |
| 110 | + const generation = await client.v1.generations.create( |
| 111 | + model, |
| 112 | + prompt, |
| 113 | + "txt2img", |
| 114 | + { |
| 115 | + imageCount: image_count, |
| 116 | + height: 1024, |
| 117 | + width: 1024, |
| 118 | + }, |
| 119 | + ); |
| 120 | + |
| 121 | + // Wait for generation to complete |
| 122 | + const completedGen = await client.v1.generations.fetchWithPolling( |
| 123 | + generation[0].id, |
| 124 | + ); |
| 125 | + |
| 126 | + const imgUrl = completedGen.image_url; |
| 127 | + if (!imgUrl) throw new Error("No image URL"); |
| 128 | + |
| 129 | + // Automatically open the image URL in the default browser |
| 130 | + await open(imgUrl); |
| 131 | + |
| 132 | + // Return a formatted message with the clickable link |
| 133 | + return { |
| 134 | + content: [ |
| 135 | + { |
| 136 | + type: "text", |
| 137 | + text: `Image generated successfully!\nThe image has been opened in your default browser.\n\nGeneration details:\n- Model: ${model}\n- Prompt: "${prompt}"\n- Image URL: ${imgUrl}\n\nYou can also click the URL above to view the image again.`, |
| 138 | + }, |
| 139 | + ], |
| 140 | + }; |
| 141 | + } catch (error: unknown) { |
| 142 | + console.error("Detailed error:", error); |
| 143 | + const errorMessage = |
| 144 | + error instanceof Error ? error.message : "Unknown error"; |
| 145 | + return { |
| 146 | + content: [{ type: "text", text: `Error: ${errorMessage}` }], |
| 147 | + isError: true, |
| 148 | + }; |
| 149 | + } |
| 150 | + } |
| 151 | + throw new Error(`Unknown tool: ${request.params.name}`); |
| 152 | +}); |
| 153 | + |
| 154 | +async function runServer() { |
| 155 | + const transport = new StdioServerTransport(); |
| 156 | + await server.connect(transport); |
| 157 | + console.error("EverArt MCP Server running on stdio"); |
| 158 | +} |
| 159 | + |
| 160 | +runServer().catch(console.error); |
0 commit comments