|
4 | 4 | import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
5 | 5 | import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
6 | 6 | import { z } from "zod";
|
7 |
| -import { OpenAI, toFile } from "openai"; |
| 7 | +import { OpenAI, AzureOpenAI, toFile } from "openai"; |
8 | 8 | import fs from "fs";
|
9 | 9 | import path from "path";
|
10 | 10 |
|
| 11 | +// Function to load environment variables from a file |
| 12 | +const loadEnvFile = (filePath: string) => { |
| 13 | + try { |
| 14 | + const envConfig = fs.readFileSync(filePath, "utf8"); |
| 15 | + envConfig.split("\n").forEach((line) => { |
| 16 | + const trimmedLine = line.trim(); |
| 17 | + if (trimmedLine && !trimmedLine.startsWith("#")) { |
| 18 | + const [key, ...valueParts] = trimmedLine.split("="); |
| 19 | + const value = valueParts.join("=").trim(); |
| 20 | + if (key) { |
| 21 | + // Remove surrounding quotes if present |
| 22 | + process.env[key.trim()] = value.startsWith("'") && value.endsWith("'") || value.startsWith("\"") && value.endsWith("\"") |
| 23 | + ? value.slice(1, -1) |
| 24 | + : value; |
| 25 | + } |
| 26 | + } |
| 27 | + }); |
| 28 | + console.log(`Loaded environment variables from ${filePath}`); |
| 29 | + } catch (error) { |
| 30 | + console.warn(`Warning: Could not read environment file at ${filePath}:`, error); |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +// Parse command line arguments for --env-file |
| 35 | +const cmdArgs = process.argv.slice(2); |
| 36 | +const envFileArgIndex = cmdArgs.findIndex(arg => arg === "--env-file"); |
| 37 | +if (envFileArgIndex !== -1 && cmdArgs[envFileArgIndex + 1]) { |
| 38 | + console.log("Loading environment variables from file:", cmdArgs[envFileArgIndex + 1]); |
| 39 | + const envFilePath = cmdArgs[envFileArgIndex + 1]; |
| 40 | + loadEnvFile(envFilePath); |
| 41 | +} else { |
| 42 | + console.log("No environment file provided"); |
| 43 | +} |
| 44 | + |
11 | 45 | (async () => {
|
12 | 46 | const server = new McpServer({
|
13 | 47 | name: "openai-gpt-image-mcp",
|
@@ -60,7 +94,9 @@ import path from "path";
|
60 | 94 | "create-image",
|
61 | 95 | (createImageSchema as any)._def.schema.shape,
|
62 | 96 | async (args, _extra) => {
|
63 |
| - const openai = new OpenAI(); |
| 97 | + // If AZURE_OPENAI_API_KEY is defined, use the AzureOpenAI class |
| 98 | + const openai = process.env.AZURE_OPENAI_API_KEY ? new AzureOpenAI() : new OpenAI(); |
| 99 | + |
64 | 100 | // Only allow gpt-image-1
|
65 | 101 | const {
|
66 | 102 | prompt,
|
@@ -217,7 +253,7 @@ import path from "path";
|
217 | 253 | throw new Error("Invalid 'mask' input: Must be an absolute path or a base64-encoded string.");
|
218 | 254 | }
|
219 | 255 |
|
220 |
| - const openai = new OpenAI(); |
| 256 | + const openai = process.env.AZURE_OPENAI_API_KEY ? new AzureOpenAI() : new OpenAI(); |
221 | 257 | const {
|
222 | 258 | image: imageInput,
|
223 | 259 | prompt,
|
|
0 commit comments