CWCloud MCP Public API
This document describes the public API provided by the pkg/mcp
package, which allows other Go projects to integrate with CWCloud AI services and create their own MCP servers.
Installationβ
go get github.com/cwloudtech/cwcloud-mcp/pkg/mcp
Quick Startβ
package main
import (
"log"
"cwcloud-mcp/pkg/mcp"
)
func main() {
// Create a CWCloud client
client := mcp.NewClient(
mcp.DefaultAPIURL,
"your-access-key",
"your-secret-key",
mcp.LogLevelInfo,
)
// Generate AI content
response, err := client.GeneratePrompt(&mcp.PromptRequest{
Adapter: mcp.AdapterGPT4o,
Message: "Hello, World!",
EnableHistory: true,
})
if err != nil {
log.Fatal(err)
}
log.Printf("AI Response: %s", response.Response)
}
Core Componentsβ
Client Interfaceβ
The Client
interface provides access to all CWCloud AI services:
type Client interface {
GeneratePrompt(req *PromptRequest) (*PromptResponse, error)
ListConversations() (*ConversationList, error)
GetConversation(id string) (*ConversationDetail, error)
DeleteConversation(id string) error
ListAdapters() (*AdapterList, error)
GetUsageStats(params map[string]string) (*UsageStats, error)
ValidateAuth() error
}
Factory Functionβ
func NewClient(baseURL, accessKey, secretKey, logLevel string) Client
Creates a new CWCloud API client. Use mcp.DefaultAPIURL
for production.
Available AI Adaptersβ
Use these constants when specifying which AI model to use:
const (
AdapterGPT4o = "gpt4o"
AdapterGPT4oMini = "gpt4o-mini"
AdapterClaude3Sonnet = "claude3sonnet"
AdapterClaude3Haiku = "claude3haiku"
AdapterGemini = "gemini"
AdapterGeminiPro = "gemini-pro"
)
MCP Tool Creationβ
The library provides helpers for creating MCP tools:
Simple AI Toolβ
tool := mcp.NewSimpleAITool(
"summarize",
"Summarize text using AI",
client,
mcp.AdapterGPT4o,
)
Conversation Management Toolβ
tool := mcp.NewConversationTool(
"list_conversations",
"List all AI conversations",
client,
)
Custom Toolsβ
Implement the MCPTool
interface:
type MCPTool interface {
GetName() string
GetDescription() string
GetInputSchema() map[string]interface{}
Execute(ctx context.Context, params map[string]interface{}) (*MCPToolResult, error)
}
Data Typesβ
PromptRequestβ
type PromptRequest struct {
Adapter string // AI model to use
Message string // Prompt text
Settings *PromptSettings // Optional parameters
ListID string // Conversation ID (optional)
EnableHistory bool // Save to conversation history
}
PromptSettingsβ
type PromptSettings struct {
MaxTokens *int // Maximum tokens to generate
Temperature *float64 // Creativity (0.0 to 1.0)
TopP *int // Nucleus sampling
Model *string // Specific model variant
}
PromptResponseβ
type PromptResponse struct {
Status string // Response status
Response string // AI-generated text
ListID string // Conversation ID
Usage TokenUsage // Token usage statistics
}
Authenticationβ
Before making API calls, validate your credentials:
if err := client.ValidateAuth(); err != nil {
log.Fatal("Authentication failed:", err)
}
Error Handlingβ
All methods return standard Go errors. Check for authentication failures, network issues, and API errors:
response, err := client.GeneratePrompt(req)
if err != nil {
// Handle error appropriately
log.Printf("API call failed: %v", err)
return
}
Conversation Managementβ
Start a Conversationβ
response, err := client.GeneratePrompt(&mcp.PromptRequest{
Adapter: mcp.AdapterGPT4o,
Message: "Hello, let's chat!",
EnableHistory: true, // This creates a conversation
})
conversationID := response.ListID
Continue a Conversationβ
response, err := client.GeneratePrompt(&mcp.PromptRequest{
Adapter: mcp.AdapterGPT4o,
Message: "Tell me more about that.",
ListID: conversationID, // Continue existing conversation
})
List All Conversationsβ
conversations, err := client.ListConversations()
if err != nil {
return err
}
for _, conv := range conversations.Conversations {
fmt.Printf("ID: %s, Title: %s\n", conv.ID, conv.Title)
}
Usage Statisticsβ
Track your AI usage and costs:
stats, err := client.GetUsageStats(nil)
if err != nil {
return err
}
fmt.Printf("Total tokens: %d\n", stats.Usage.TotalTokens)
fmt.Printf("Total cost: $%.4f\n", stats.Usage.TotalCost)
// Usage by adapter
for adapter, usage := range stats.Usage.ByAdapter {
fmt.Printf("%s: %d tokens, $%.4f\n",
adapter, usage.Tokens, usage.Cost)
}
Examplesβ
See the examples/
directory for complete working examples:
examples/usage.go
- Comprehensive usage examples- Basic client operations
- Custom MCP tool creation
- Conversation management
- Usage statistics
Thread Safetyβ
The client is safe for concurrent use across multiple goroutines.
Best Practicesβ
- Reuse Clients: Create one client instance and reuse it across your application
- Handle Errors: Always check for errors, especially authentication failures
- Set Limits: Use
MaxTokens
to control costs and response length - Save Conversations: Use
EnableHistory: true
for multi-turn conversations - Monitor Usage: Regularly check usage statistics to track costs
Integration with MCP Serversβ
This library is designed to be used within MCP servers. The tools created with this library can be easily integrated into MCP protocol implementations.
For a complete example of an MCP server using this library, see the main cwcloud-mcp
server implementation in the project root.