$npx -y skills add github/awesome-copilot --skill go-mcp-server-generatorGenerate a complete Go MCP server project with proper structure, dependencies, and implementation using the official github.com/modelcontextprotocol/go-sdk.
| 1 | # Go MCP Server Project Generator |
| 2 | |
| 3 | Generate a complete, production-ready Model Context Protocol (MCP) server project in Go. |
| 4 | |
| 5 | ## Project Requirements |
| 6 | |
| 7 | You will create a Go MCP server with: |
| 8 | |
| 9 | 1. **Project Structure**: Proper Go module layout |
| 10 | 2. **Dependencies**: Official MCP SDK and necessary packages |
| 11 | 3. **Server Setup**: Configured MCP server with transports |
| 12 | 4. **Tools**: At least 2-3 useful tools with typed inputs/outputs |
| 13 | 5. **Error Handling**: Proper error handling and context usage |
| 14 | 6. **Documentation**: README with setup and usage instructions |
| 15 | 7. **Testing**: Basic test structure |
| 16 | |
| 17 | ## Template Structure |
| 18 | |
| 19 | ``` |
| 20 | myserver/ |
| 21 | ├── go.mod |
| 22 | ├── go.sum |
| 23 | ├── main.go |
| 24 | ├── tools/ |
| 25 | │ ├── tool1.go |
| 26 | │ └── tool2.go |
| 27 | ├── resources/ |
| 28 | │ └── resource1.go |
| 29 | ├── config/ |
| 30 | │ └── config.go |
| 31 | ├── README.md |
| 32 | └── main_test.go |
| 33 | ``` |
| 34 | |
| 35 | ## go.mod Template |
| 36 | |
| 37 | ```go |
| 38 | module github.com/yourusername/{{PROJECT_NAME}} |
| 39 | |
| 40 | go 1.23 |
| 41 | |
| 42 | require ( |
| 43 | github.com/modelcontextprotocol/go-sdk v1.0.0 |
| 44 | ) |
| 45 | ``` |
| 46 | |
| 47 | ## main.go Template |
| 48 | |
| 49 | ```go |
| 50 | package main |
| 51 | |
| 52 | import ( |
| 53 | "context" |
| 54 | "log" |
| 55 | "os" |
| 56 | "os/signal" |
| 57 | "syscall" |
| 58 | |
| 59 | "github.com/modelcontextprotocol/go-sdk/mcp" |
| 60 | "github.com/yourusername/{{PROJECT_NAME}}/config" |
| 61 | "github.com/yourusername/{{PROJECT_NAME}}/tools" |
| 62 | ) |
| 63 | |
| 64 | func main() { |
| 65 | cfg := config.Load() |
| 66 | |
| 67 | ctx, cancel := context.WithCancel(context.Background()) |
| 68 | defer cancel() |
| 69 | |
| 70 | // Handle graceful shutdown |
| 71 | sigCh := make(chan os.Signal, 1) |
| 72 | signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM) |
| 73 | go func() { |
| 74 | <-sigCh |
| 75 | log.Println("Shutting down...") |
| 76 | cancel() |
| 77 | }() |
| 78 | |
| 79 | // Create server |
| 80 | server := mcp.NewServer( |
| 81 | &mcp.Implementation{ |
| 82 | Name: cfg.ServerName, |
| 83 | Version: cfg.Version, |
| 84 | }, |
| 85 | &mcp.Options{ |
| 86 | Capabilities: &mcp.ServerCapabilities{ |
| 87 | Tools: &mcp.ToolsCapability{}, |
| 88 | Resources: &mcp.ResourcesCapability{}, |
| 89 | Prompts: &mcp.PromptsCapability{}, |
| 90 | }, |
| 91 | }, |
| 92 | ) |
| 93 | |
| 94 | // Register tools |
| 95 | tools.RegisterTools(server) |
| 96 | |
| 97 | // Run server |
| 98 | transport := &mcp.StdioTransport{} |
| 99 | if err := server.Run(ctx, transport); err != nil { |
| 100 | log.Fatalf("Server error: %v", err) |
| 101 | } |
| 102 | } |
| 103 | ``` |
| 104 | |
| 105 | ## tools/tool1.go Template |
| 106 | |
| 107 | ```go |
| 108 | package tools |
| 109 | |
| 110 | import ( |
| 111 | "context" |
| 112 | "fmt" |
| 113 | |
| 114 | "github.com/modelcontextprotocol/go-sdk/mcp" |
| 115 | ) |
| 116 | |
| 117 | type Tool1Input struct { |
| 118 | Param1 string `json:"param1" jsonschema:"required,description=First parameter"` |
| 119 | Param2 int `json:"param2,omitempty" jsonschema:"description=Optional second parameter"` |
| 120 | } |
| 121 | |
| 122 | type Tool1Output struct { |
| 123 | Result string `json:"result" jsonschema:"description=The result of the operation"` |
| 124 | Status string `json:"status" jsonschema:"description=Operation status"` |
| 125 | } |
| 126 | |
| 127 | func Tool1Handler(ctx context.Context, req *mcp.CallToolRequest, input Tool1Input) ( |
| 128 | *mcp.CallToolResult, |
| 129 | Tool1Output, |
| 130 | error, |
| 131 | ) { |
| 132 | // Validate input |
| 133 | if input.Param1 == "" { |
| 134 | return nil, Tool1Output{}, fmt.Errorf("param1 is required") |
| 135 | } |
| 136 | |
| 137 | // Check context |
| 138 | if ctx.Err() != nil { |
| 139 | return nil, Tool1Output{}, ctx.Err() |
| 140 | } |
| 141 | |
| 142 | // Perform operation |
| 143 | result := fmt.Sprintf("Processed: %s", input.Param1) |
| 144 | |
| 145 | return nil, Tool1Output{ |
| 146 | Result: result, |
| 147 | Status: "success", |
| 148 | }, nil |
| 149 | } |
| 150 | |
| 151 | func RegisterTool1(server *mcp.Server) { |
| 152 | mcp.AddTool(server, |
| 153 | &mcp.Tool{ |
| 154 | Name: "tool1", |
| 155 | Description: "Description of what tool1 does", |
| 156 | }, |
| 157 | Tool1Handler, |
| 158 | ) |
| 159 | } |
| 160 | ``` |
| 161 | |
| 162 | ## tools/registry.go Template |
| 163 | |
| 164 | ```go |
| 165 | package tools |
| 166 | |
| 167 | import "github.com/modelcontextprotocol/go-sdk/mcp" |
| 168 | |
| 169 | func RegisterTools(server *mcp.Server) { |
| 170 | RegisterTool1(server) |
| 171 | RegisterTool2(server) |
| 172 | // Register additional tools here |
| 173 | } |
| 174 | ``` |
| 175 | |
| 176 | ## config/config.go Template |
| 177 | |
| 178 | ```go |
| 179 | package config |
| 180 | |
| 181 | import "os" |
| 182 | |
| 183 | type Config struct { |
| 184 | ServerName string |
| 185 | Version string |
| 186 | LogLevel string |
| 187 | } |
| 188 | |
| 189 | func Load() *Config { |
| 190 | return &Config{ |
| 191 | ServerName: getEnv("SERVER_NAME", "{{PROJECT_NAME}}"), |
| 192 | Version: getEnv("VERSION", "v1.0.0"), |
| 193 | LogLevel: getEnv("LOG_LEVEL", "info"), |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | func getEnv(key, defaultValue string) string { |
| 198 | if value := os.Getenv(key); value != "" { |
| 199 | return value |
| 200 | } |
| 201 | return defaultValue |
| 202 | } |
| 203 | ``` |
| 204 | |
| 205 | ## main_test.go Template |
| 206 | |
| 207 | ```go |
| 208 | package main |
| 209 | |
| 210 | import ( |
| 211 | "context" |
| 212 | "testing" |
| 213 | |
| 214 | "github.com/yourusername/{{PROJECT_NAME}}/tools" |
| 215 | ) |
| 216 | |
| 217 | func TestTool1Handler(t *testing.T) { |
| 218 | ctx := context.Background() |
| 219 | input := tools.Tool1Input{ |
| 220 | Param1: "test", |
| 221 | Param2: 42, |
| 222 | } |
| 223 | |
| 224 | result, output, err := tools.Tool1Handler(ctx, nil, input) |
| 225 | if err != nil { |
| 226 | t.Fatalf("Tool1Handler failed: %v", err) |
| 227 | } |
| 228 | |
| 229 | if output.Status != "success" { |
| 230 | t.Errorf("Expected status |