@monsoft/mcp-generate-image-openai
v0.1.0
Published
MCP for generating images using OpenAI's API
Readme
MCP Generate Image OpenAI
A Model Context Protocol (MCP) server for generating images using OpenAI's image generation API.
Features
- Implements the Model Context Protocol for image generation
- Supports both stdio and Server-Sent Events (SSE) transports
- Easy integration with OpenAI's DALL-E API
- Configurable through environment variables
- Extensible architecture with tools, resources, and prompts
Installation
npm install mcp-generate-image-openaiPrerequisites
- Node.js 16 or later
- An OpenAI API key with access to image generation models
Usage
Basic Usage
import { startServer } from 'mcp-generate-image-openai';
// Start the server with configuration from environment variables
startServer()
.then(() => console.log('Server started successfully'))
.catch((err) => console.error('Failed to start server:', err));Manual Configuration
import { startMcpServer, startMcpServerSSE } from 'mcp-generate-image-openai';
// Start with stdio transport
startMcpServer('your-openai-api-key')
.then(() => console.log('Stdio server started'))
.catch((err) => console.error('Error:', err));
// Or start with SSE transport
startMcpServerSSE('your-openai-api-key', 3000)
.then(() => console.log('SSE server started on port 3000'))
.catch((err) => console.error('Error:', err));Configuration
Environment Variables
The server can be configured using the following environment variables:
| Variable | Description | Default |
| ---------------- | ---------------------------- | -------- |
| OPENAI_API_KEY | Your OpenAI API key | Required |
| PORT | Port for SSE server | 3000 |
| RUN_SSE | Whether to use SSE transport | false |
Command Line Arguments
You can also provide configuration through command-line arguments, which will override environment variables:
node your-script.js --openai-api-key=your-key --port=8080 --run-sseAPI
Server Functions
startServer()
The main entry point that automatically configures and starts the server based on environment variables.
startMcpServer(apiKey: string)
Starts an MCP server with stdio transport.
- Parameters:
apiKey: OpenAI API key
startMcpServerSSE(apiKey: string, port: number)
Starts an MCP server with SSE transport.
- Parameters:
apiKey: OpenAI API keyport: HTTP server port
SSE Endpoints
When using SSE transport:
GET /sse: Establishes an SSE connection- Query parameter:
sessionId(optional, defaults to 'default')
- Query parameter:
POST /messages: Sends messages to an established session- Body parameter:
sessionId(optional, defaults to 'default')
- Body parameter:
Examples
Integrating with a Client Application
// Client code example
import { McpClient } from '@modelcontextprotocol/sdk/client';
const client = new McpClient({
transport: 'stdio', // or 'sse' for web applications
serverUrl: 'http://localhost:3000', // Only needed for SSE
});
await client.connect();
// Generate an image
const result = await client.execute({
toolName: 'generate_image',
params: {
prompt: 'A futuristic city with flying cars',
size: '1024x1024',
},
});
console.log('Generated image URL:', result.imageUrl);Development
Project Structure
src/
├── config/ # Environment and configuration handling
├── prompts/ # Prompt templates and definitions
├── resources/ # Resource implementations
├── tools/ # Tool implementations
└── server.ts # Main server implementationBuilding
npm run buildTesting
npm testLicense
MIT © [Your Organization]
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Acknowledgements
- Built with Model Context Protocol SDK
- Uses OpenAI's API for image generation
