Go MCP Server Generator

Go MCP Server Generator

Generates a complete Go Model Context Protocol server project using the official SDK, with module layout, typed tools, error handling, README and basic tests.

Skill files1 file
SKILL.md335 lines
1
---
2
name: go-mcp-server-generator
3
description: 'Generate a complete Go MCP server project with proper structure, dependencies, and implementation using the official github.com/modelcontextprotocol/go-sdk.'
4
---
5
6
# Go MCP Server Project Generator
7
8
Generate a complete, production-ready Model Context Protocol (MCP) server project in Go.
9
10
## Project Requirements
11
12
You will create a Go MCP server with:
13
14
1. **Project Structure**: Proper Go module layout
15
2. **Dependencies**: Official MCP SDK and necessary packages
16
3. **Server Setup**: Configured MCP server with transports
17
4. **Tools**: At least 2-3 useful tools with typed inputs/outputs
18
5. **Error Handling**: Proper error handling and context usage
19
6. **Documentation**: README with setup and usage instructions
20
7. **Testing**: Basic test structure
21
22
## Template Structure
23
24
```
25
myserver/
26
├── go.mod
27
├── go.sum
28
├── main.go
29
├── tools/
30
│ ├── tool1.go
31
│ └── tool2.go
32
├── resources/
33
│ └── resource1.go
34
├── config/
35
│ └── config.go
36
├── README.md
37
└── main_test.go
38
```
39
40
## go.mod Template
41
42
```go
43
module github.com/yourusername/{{PROJECT_NAME}}
44
45
go 1.23
46
47
require (
48
github.com/modelcontextprotocol/go-sdk v1.0.0
49
)
50
```
51
52
## main.go Template
53
54
```go
55
package main
56
57
import (
58
"context"
59
"log"
60
"os"
61
"os/signal"
62
"syscall"
63
64
"github.com/modelcontextprotocol/go-sdk/mcp"
65
"github.com/yourusername/{{PROJECT_NAME}}/config"
66
"github.com/yourusername/{{PROJECT_NAME}}/tools"
67
)
68
69
func main() {
70
cfg := config.Load()
71
72
ctx, cancel := context.WithCancel(context.Background())
73
defer cancel()
74
75
// Handle graceful shutdown
76
sigCh := make(chan os.Signal, 1)
77
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
78
go func() {
79
<-sigCh
80
log.Println("Shutting down...")
81
cancel()
82
}()
83
84
// Create server
85
server := mcp.NewServer(
86
&mcp.Implementation{
87
Name: cfg.ServerName,
88
Version: cfg.Version,
89
},
90
&mcp.Options{
91
Capabilities: &mcp.ServerCapabilities{
92
Tools: &mcp.ToolsCapability{},
93
Resources: &mcp.ResourcesCapability{},
94
Prompts: &mcp.PromptsCapability{},
95
},
96
},
97
)
98
99
// Register tools
100
tools.RegisterTools(server)
101
102
// Run server
103
transport := &mcp.StdioTransport{}
104
if err := server.Run(ctx, transport); err != nil {
105
log.Fatalf("Server error: %v", err)
106
}
107
}
108
```
109
110
## tools/tool1.go Template
111
112
```go
113
package tools
114
115
import (
116
"context"
117
"fmt"
118
119
"github.com/modelcontextprotocol/go-sdk/mcp"
120
)
121
122
type Tool1Input struct {
123
Param1 string `json:"param1" jsonschema:"required,description=First parameter"`
124
Param2 int `json:"param2,omitempty" jsonschema:"description=Optional second parameter"`
125
}
126
127
type Tool1Output struct {
128
Result string `json:"result" jsonschema:"description=The result of the operation"`
129
Status string `json:"status" jsonschema:"description=Operation status"`
130
}
131
132
func Tool1Handler(ctx context.Context, req *mcp.CallToolRequest, input Tool1Input) (
133
*mcp.CallToolResult,
134
Tool1Output,
135
error,
136
) {
137
// Validate input
138
if input.Param1 == "" {
139
return nil, Tool1Output{}, fmt.Errorf("param1 is required")
140
}
141
142
// Check context
143
if ctx.Err() != nil {
144
return nil, Tool1Output{}, ctx.Err()
145
}
146
147
// Perform operation
148
result := fmt.Sprintf("Processed: %s", input.Param1)
149
150
return nil, Tool1Output{
151
Result: result,
152
Status: "success",
153
}, nil
154
}
155
156
func RegisterTool1(server *mcp.Server) {
157
mcp.AddTool(server,
158
&mcp.Tool{
159
Name: "tool1",
160
Description: "Description of what tool1 does",
161
},
162
Tool1Handler,
163
)
164
}
165
```
166
167
## tools/registry.go Template
168
169
```go
170
package tools
171
172
import "github.com/modelcontextprotocol/go-sdk/mcp"
173
174
func RegisterTools(server *mcp.Server) {
175
RegisterTool1(server)
176
RegisterTool2(server)
177
// Register additional tools here
178
}
179
```
180
181
## config/config.go Template
182
183
```go
184
package config
185
186
import "os"
187
188
type Config struct {
189
ServerName string
190
Version string
191
LogLevel string
192
}
193
194
func Load() *Config {
195
return &Config{
196
ServerName: getEnv("SERVER_NAME", "{{PROJECT_NAME}}"),
197
Version: getEnv("VERSION", "v1.0.0"),
198
LogLevel: getEnv("LOG_LEVEL", "info"),
199
}
200
}
201
202
func getEnv(key, defaultValue string) string {
203
if value := os.Getenv(key); value != "" {
204
return value
205
}
206
return defaultValue
207
}
208
```
209
210
## main_test.go Template
211
212
```go
213
package main
214
215
import (
216
"context"
217
"testing"
218
219
"github.com/yourusername/{{PROJECT_NAME}}/tools"
220
)
221
222
func TestTool1Handler(t *testing.T) {
223
ctx := context.Background()
224
input := tools.Tool1Input{
225
Param1: "test",
226
Param2: 42,
227
}
228
229
result, output, err := tools.Tool1Handler(ctx, nil, input)
230
if err != nil {
231
t.Fatalf("Tool1Handler failed: %v", err)
232
}
233
234
if output.Status != "success" {
235
t.Errorf("Expected status 'success', got '%s'", output.Status)
236
}
237
238
if result != nil {
239
t.Error("Expected result to be nil")
240
}
241
}
242
```
243
244
## README.md Template
245
246
```markdown
247
# {{PROJECT_NAME}}
248
249
A Model Context Protocol (MCP) server built with Go.
250
251
## Description
252
253
{{PROJECT_DESCRIPTION}}
254
255
## Installation
256
257
\`\`\`bash
258
go mod download
259
go build -o {{PROJECT_NAME}}
260
\`\`\`
261
262
## Usage
263
264
Run the server with stdio transport:
265
266
\`\`\`bash
267
./{{PROJECT_NAME}}
268
\`\`\`
269
270
## Configuration
271
272
Configure via environment variables:
273
274
- `SERVER_NAME`: Server name (default: "{{PROJECT_NAME}}")
275
- `VERSION`: Server version (default: "v1.0.0")
276
- `LOG_LEVEL`: Logging level (default: "info")
277
278
## Available Tools
279
280
### tool1
281
{{TOOL1_DESCRIPTION}}
282
283
**Input:**
284
- `param1` (string, required): First parameter
285
- `param2` (int, optional): Second parameter
286
287
**Output:**
288
- `result` (string): Operation result
289
- `status` (string): Status of the operation
290
291
## Development
292
293
Run tests:
294
295
\`\`\`bash
296
go test ./...
297
\`\`\`
298
299
Build:
300
301
\`\`\`bash
302
go build -o {{PROJECT_NAME}}
303
\`\`\`
304
305
## License
306
307
MIT
308
```
309
310
## Generation Instructions
311
312
When generating a Go MCP server:
313
314
1. **Initialize Module**: Create `go.mod` with proper module path
315
2. **Structure**: Follow the template directory structure
316
3. **Type Safety**: Use structs with JSON schema tags for all inputs/outputs
317
4. **Error Handling**: Validate inputs, check context, wrap errors
318
5. **Documentation**: Add clear descriptions and examples
319
6. **Testing**: Include at least one test per tool
320
7. **Configuration**: Use environment variables for config
321
8. **Logging**: Use structured logging (log/slog)
322
9. **Graceful Shutdown**: Handle signals properly
323
10. **Transport**: Default to stdio, document alternatives
324
325
## Best Practices
326
327
- Keep tools focused and single-purpose
328
- Use descriptive names for types and functions
329
- Include JSON schema documentation in struct tags
330
- Always respect context cancellation
331
- Return descriptive errors
332
- Keep main.go minimal, logic in packages
333
- Write tests for tool handlers
334
- Document all exported functions
335

© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…