Skip to content

Commit 558265a

Browse files
Feature: Azure support & optional environment file (#4)
* Added Azure support and optional environment file input * Added details to README
1 parent f160871 commit 558265a

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,44 @@ Add to Claude Desktop or VSCode (including Cursor/Windsurf) config:
5353
}
5454
```
5555

56+
Also supports Azure deployments:
57+
58+
```json
59+
{
60+
"mcpServers": {
61+
"openai-gpt-image-mcp": {
62+
"command": "node",
63+
"args": ["/absolute/path/to/dist/index.js"],
64+
"env": {
65+
"AZURE_OPENAI_API_KEY": "sk-...",
66+
"AZURE_OPENAI_ENDPOINT": "my.endpoint.com",
67+
"OPENAI_API_VERSION": "2024-12-01-preview"
68+
}
69+
}
70+
}
71+
}
72+
```
73+
74+
Also supports supplying an environment files:
75+
76+
```json
77+
{
78+
"mcpServers": {
79+
"openai-gpt-image-mcp": {
80+
"command": "node",
81+
"args": ["/absolute/path/to/dist/index.js", "--env-file", "./deployment/.env"]
82+
}
83+
}
84+
}
85+
```
86+
5687
---
5788

5889
## ⚡ Advanced
5990

6091
- For `create-image`, set `n` to generate up to 10 images at once.
6192
- For `edit-image`, provide a mask image (file path or base64) to control where edits are applied.
93+
- Provide an environment file with `--env-file path/to/file/.env`
6294
- See `src/index.ts` for all options.
6395

6496
---
@@ -110,4 +142,5 @@ MIT
110142

111143
- Built with [@modelcontextprotocol/sdk](https://www.npmjs.com/package/@modelcontextprotocol/sdk)
112144
- Uses [openai](https://www.npmjs.com/package/openai) Node.js SDK
113-
- Built by [SureScale.ai](https://surescale.ai)
145+
- Built by [SureScale.ai](https://surescale.ai)
146+
- Contributions from [Axle Research and Technology](https://axleinfo.com/)

src/index.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,44 @@
44
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
55
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
66
import { z } from "zod";
7-
import { OpenAI, toFile } from "openai";
7+
import { OpenAI, AzureOpenAI, toFile } from "openai";
88
import fs from "fs";
99
import path from "path";
1010

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+
1145
(async () => {
1246
const server = new McpServer({
1347
name: "openai-gpt-image-mcp",
@@ -60,7 +94,9 @@ import path from "path";
6094
"create-image",
6195
(createImageSchema as any)._def.schema.shape,
6296
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+
64100
// Only allow gpt-image-1
65101
const {
66102
prompt,
@@ -217,7 +253,7 @@ import path from "path";
217253
throw new Error("Invalid 'mask' input: Must be an absolute path or a base64-encoded string.");
218254
}
219255

220-
const openai = new OpenAI();
256+
const openai = process.env.AZURE_OPENAI_API_KEY ? new AzureOpenAI() : new OpenAI();
221257
const {
222258
image: imageInput,
223259
prompt,

0 commit comments

Comments
 (0)