OpenAI Integration for GoMCP
This example demonstrates how to integrate OpenAI's API with GoMCP, allowing you to expose OpenAI capabilities as MCP tools.
Features
- Chat Completion: Generate text responses using OpenAI's GPT models
- Image Generation: Create images using DALL-E models
- Embeddings: Generate embeddings for text using OpenAI's embedding models
- Real-time Streaming: Get responses in real-time via WebSockets
Requirements
- Go 1.18 or later
- An OpenAI API key
Setup
- Install the OpenAI Go client:
go get github.com/sashabaranov/go-openai
- Install WebSocket library:
go get github.com/gobwas/ws
- Set your OpenAI API key as an environment variable:
export OPENAI_API_KEY="your-api-key-here"
Running the Example
This example provides both a command-line client and a web-based client with WebSocket streaming.
Server
The MCP server needs to be running before you can use either client:
# Start the server in a separate terminal window
cd examples/openai/server
go run main.go
Command-line Client
To use the command-line client:
# Run the client in command-line mode
cd examples/openai/client
go run main.go
Web Client with WebSockets
The web client provides a real-time chat interface using WebSockets for streaming responses:
# Run the web client
cd examples/openai/client
go run main.go -web
Then open your browser to http://localhost:3366 to interact with the chat interface.
Technical Details
WebSocket Implementation
The web client uses the gobwas/ws package to handle WebSocket connections. This provides several advantages:
- No External Dependencies: unlike other WebSocket libraries, gobwas/ws has no external dependencies
- Low-level Control: provides direct access to the underlying connection for better performance
- Streaming Capability: allows for real-time streaming of responses
Progress Notifications
The integration uses MCP's progress notifications to stream responses in real-time:
- The server processes each chunk from OpenAI's streaming API
- The server sends progress updates via the
ReportProgress method
- The client registers a progress handler to process these updates
- The web UI displays the streaming response in real-time
Protocol Flow
- User sends a message via the WebSocket
- Client calls the OpenAI MCP tool with streaming enabled
- Server streams responses back to the client via progress notifications
- Client forwards these chunks to the WebSocket client
- WebSocket client displays the response in real-time
Extending the Example
You can add more OpenAI features by:
- Adding new tool handlers in
server/main.go
- Exposing these tools in the client interfaces
- Updating the web UI to make use of the new capabilities
For example, you could add support for:
- Audio transcription with Whisper API
- Function calling with GPT models
- Fine-tuning API integration