README
ΒΆ
OpenAI Compatibility Example
This example demonstrates how to use the MCP openai package to create an OpenAI-compatible API server that exposes MCP tools. It forwards LLM requests to LM Studio running locally.
Prerequisites
- LM Studio running on
127.0.0.1:1234 - Model:
qwen/qwen3-1.7bloaded in LM Studio (or configure a different model)
What It Does
- Creates an MCP server with a simple
greettool - Exposes an OpenAI-compatible
/v1/chat/completionsendpoint - Automatically converts MCP tools to OpenAI function format
- Forwards requests to LM Studio for inference
- Executes tool calls and returns results to the LLM
Running the Example
With LM Studio (Default)
- Start LM Studio and load the
qwen/qwen3-1.7bmodel - Enable the local server (should be on
127.0.0.1:1234) - Run the example:
go run main.go
With a Different LLM Provider
You can override the defaults with environment variables:
# Use OpenAI
export OPENAI_BASE_URL=https://api.openai.com/v1
export OPENAI_API_KEY=your-api-key
export DEFAULT_MODEL=gpt-4o-mini
go run main.go
# Use Anthropic (via compatible endpoint)
export OPENAI_BASE_URL=https://api.anthropic.com/v1
export OPENAI_API_KEY=your-anthropic-key
export DEFAULT_MODEL=claude-3-haiku-20240307
go run main.go
Testing
List Models
curl http://localhost:8080/v1/models
Chat Completion with Tool
curl -X POST http://localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "qwen/qwen3-1.7b",
"messages": [
{"role": "user", "content": "Please greet Paul"}
]
}'
Expected Behavior
The LLM will:
- Recognize the request to greet someone
- Call the
greetMCP tool with{"name": "Paul"} - Receive the response "Hi Paul! Greetings from MCP!"
- Return a final response to the user
Code Walkthrough
1. Create the MCP Server
mcpServer := mcp.NewServer("greeting-server", "1.0.0")
mcpServer.RegisterTool(
mcp.NewTool("greet", "Greet someone with a friendly message from MCP",
mcp.String("name", "The name of the person to greet", mcp.Required()),
),
func(ctx context.Context, req *mcp.ToolRequest) (*mcp.ToolResponse, error) {
name, _ := req.String("name")
return mcp.NewToolResponseText(fmt.Sprintf("Hi %s! Greetings from MCP! π", name)), nil
},
)
2. Convert MCP Tools to OpenAI Format
mcpTools := mcpServer.ListTools()
req.Tools = append(req.Tools, openai.MCPToolsToOpenAI(mcpTools)...)
3. Execute Tool Calls
for _, toolCall := range response.Choices[0].Message.ToolCalls {
// Call the MCP tool
mcpResponse, err := mcpServer.CallTool(ctx, toolCall.Function.Name, toolCall.Function.Arguments)
// Extract result for OpenAI
result, _ := openai.ExtractToolResult(mcpResponse)
// Add tool result message
toolResultMsg := openai.Message{
Role: "tool",
ToolCallID: toolCall.ID,
}
toolResultMsg.SetContentAsString(result)
messages = append(messages, toolResultMsg)
}
Architecture
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β β β β β β
β Client/User ββββββΆβ This Server ββββββΆβ Upstream LLM β
β β β β β (OpenAI, etc) β
βββββββββββββββββββ ββββββββββ¬βββββββββ βββββββββββββββββββ
β
β Tool Calls
βΌ
βββββββββββββββββββ
β β
β MCP Server β
β (greet tool) β
β β
βββββββββββββββββββ
The server acts as a proxy that:
- Receives OpenAI-format requests
- Adds MCP tools as OpenAI functions
- Forwards to upstream LLM
- Intercepts tool calls and executes them via MCP
- Returns tool results to LLM
- Repeats until LLM gives final response
Documentation
ΒΆ
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.