@@ -28,42 +28,30 @@ const (
28
28
COMPLEX PromptName = "complex_prompt"
29
29
)
30
30
31
- type MCPServer struct {
32
- server * server.MCPServer
33
- subscriptions map [string ]bool
34
- updateTicker * time.Ticker
35
- allResources []mcp.Resource
36
- }
37
-
38
- func NewMCPServer () * MCPServer {
39
- s := & MCPServer {
40
- server : server .NewMCPServer (
41
- "example-servers/everything" ,
42
- "1.0.0" ,
43
- server .WithResourceCapabilities (true , true ),
44
- server .WithPromptCapabilities (true ),
45
- server .WithLogging (),
46
- ),
47
- subscriptions : make (map [string ]bool ),
48
- updateTicker : time .NewTicker (5 * time .Second ),
49
- allResources : generateResources (),
50
- }
31
+ func NewMCPServer () * server.MCPServer {
32
+ mcpServer := server .NewMCPServer (
33
+ "example-servers/everything" ,
34
+ "1.0.0" ,
35
+ server .WithResourceCapabilities (true , true ),
36
+ server .WithPromptCapabilities (true ),
37
+ server .WithLogging (),
38
+ )
51
39
52
- s . server .AddResource (mcp .NewResource ("test://static/resource" ,
40
+ mcpServer .AddResource (mcp .NewResource ("test://static/resource" ,
53
41
"Static Resource" ,
54
42
mcp .WithMIMEType ("text/plain" ),
55
- ), s . handleReadResource )
56
- s . server .AddResourceTemplate (
43
+ ), handleReadResource )
44
+ mcpServer .AddResourceTemplate (
57
45
mcp .NewResourceTemplate (
58
46
"test://dynamic/resource/{id}" ,
59
47
"Dynamic Resource" ,
60
48
),
61
- s . handleResourceTemplate ,
49
+ handleResourceTemplate ,
62
50
)
63
- s . server .AddPrompt (mcp .NewPrompt (string (SIMPLE ),
51
+ mcpServer .AddPrompt (mcp .NewPrompt (string (SIMPLE ),
64
52
mcp .WithPromptDescription ("A simple prompt" ),
65
- ), s . handleSimplePrompt )
66
- s . server .AddPrompt (mcp .NewPrompt (string (COMPLEX ),
53
+ ), handleSimplePrompt )
54
+ mcpServer .AddPrompt (mcp .NewPrompt (string (COMPLEX ),
67
55
mcp .WithPromptDescription ("A complex prompt" ),
68
56
mcp .WithArgument ("temperature" ,
69
57
mcp .ArgumentDescription ("The temperature parameter for generation" ),
@@ -73,21 +61,21 @@ func NewMCPServer() *MCPServer {
73
61
mcp .ArgumentDescription ("The style to use for the response" ),
74
62
mcp .RequiredArgument (),
75
63
),
76
- ), s . handleComplexPrompt )
77
- s . server .AddTool (mcp .NewTool (string (ECHO ),
64
+ ), handleComplexPrompt )
65
+ mcpServer .AddTool (mcp .NewTool (string (ECHO ),
78
66
mcp .WithDescription ("Echoes back the input" ),
79
67
mcp .WithString ("message" ,
80
68
mcp .Description ("Message to echo" ),
81
69
mcp .Required (),
82
70
),
83
- ), s . handleEchoTool )
71
+ ), handleEchoTool )
84
72
85
- s . server .AddTool (
73
+ mcpServer .AddTool (
86
74
mcp .NewTool ("notify" ),
87
- s . handleSendNotification ,
75
+ handleSendNotification ,
88
76
)
89
77
90
- s . server .AddTool (mcp .NewTool (string (ADD ),
78
+ mcpServer .AddTool (mcp .NewTool (string (ADD ),
91
79
mcp .WithDescription ("Adds two numbers" ),
92
80
mcp .WithNumber ("a" ,
93
81
mcp .Description ("First number" ),
@@ -97,8 +85,8 @@ func NewMCPServer() *MCPServer {
97
85
mcp .Description ("Second number" ),
98
86
mcp .Required (),
99
87
),
100
- ), s . handleAddTool )
101
- s . server .AddTool (mcp .NewTool (
88
+ ), handleAddTool )
89
+ mcpServer .AddTool (mcp .NewTool (
102
90
string (LONG_RUNNING_OPERATION ),
103
91
mcp .WithDescription (
104
92
"Demonstrates a long running operation with progress updates" ,
@@ -111,7 +99,7 @@ func NewMCPServer() *MCPServer {
111
99
mcp .Description ("Number of steps in the operation" ),
112
100
mcp .DefaultNumber (5 ),
113
101
),
114
- ), s . handleLongRunningOperationTool )
102
+ ), handleLongRunningOperationTool )
115
103
116
104
// s.server.AddTool(mcp.Tool{
117
105
// Name: string(SAMPLE_LLM),
@@ -131,15 +119,13 @@ func NewMCPServer() *MCPServer {
131
119
// },
132
120
// },
133
121
// }, s.handleSampleLLMTool)
134
- s . server .AddTool (mcp .NewTool (string (GET_TINY_IMAGE ),
122
+ mcpServer .AddTool (mcp .NewTool (string (GET_TINY_IMAGE ),
135
123
mcp .WithDescription ("Returns the MCP_TINY_IMAGE" ),
136
- ), s .handleGetTinyImageTool )
137
-
138
- s .server .AddNotificationHandler ("notification" , s .handleNotification )
124
+ ), handleGetTinyImageTool )
139
125
140
- go s . runUpdateInterval ( )
126
+ mcpServer . AddNotificationHandler ( "notification" , handleNotification )
141
127
142
- return s
128
+ return mcpServer
143
129
}
144
130
145
131
func generateResources () []mcp.Resource {
@@ -163,7 +149,7 @@ func generateResources() []mcp.Resource {
163
149
return resources
164
150
}
165
151
166
- func ( s * MCPServer ) runUpdateInterval () {
152
+ func runUpdateInterval () {
167
153
// for range s.updateTicker.C {
168
154
// for uri := range s.subscriptions {
169
155
// s.server.HandleMessage(
@@ -184,7 +170,7 @@ func (s *MCPServer) runUpdateInterval() {
184
170
// }
185
171
}
186
172
187
- func ( s * MCPServer ) handleReadResource (
173
+ func handleReadResource (
188
174
ctx context.Context ,
189
175
request mcp.ReadResourceRequest ,
190
176
) ([]interface {}, error ) {
@@ -199,7 +185,7 @@ func (s *MCPServer) handleReadResource(
199
185
}, nil
200
186
}
201
187
202
- func ( s * MCPServer ) handleResourceTemplate (
188
+ func handleResourceTemplate (
203
189
ctx context.Context ,
204
190
request mcp.ReadResourceRequest ,
205
191
) ([]interface {}, error ) {
@@ -214,7 +200,7 @@ func (s *MCPServer) handleResourceTemplate(
214
200
}, nil
215
201
}
216
202
217
- func ( s * MCPServer ) handleSimplePrompt (
203
+ func handleSimplePrompt (
218
204
ctx context.Context ,
219
205
request mcp.GetPromptRequest ,
220
206
) (* mcp.GetPromptResult , error ) {
@@ -232,7 +218,7 @@ func (s *MCPServer) handleSimplePrompt(
232
218
}, nil
233
219
}
234
220
235
- func ( s * MCPServer ) handleComplexPrompt (
221
+ func handleComplexPrompt (
236
222
ctx context.Context ,
237
223
request mcp.GetPromptRequest ,
238
224
) (* mcp.GetPromptResult , error ) {
@@ -270,7 +256,7 @@ func (s *MCPServer) handleComplexPrompt(
270
256
}, nil
271
257
}
272
258
273
- func ( s * MCPServer ) handleEchoTool (
259
+ func handleEchoTool (
274
260
ctx context.Context ,
275
261
request mcp.CallToolRequest ,
276
262
) (* mcp.CallToolResult , error ) {
@@ -289,7 +275,7 @@ func (s *MCPServer) handleEchoTool(
289
275
}, nil
290
276
}
291
277
292
- func ( s * MCPServer ) handleAddTool (
278
+ func handleAddTool (
293
279
ctx context.Context ,
294
280
request mcp.CallToolRequest ,
295
281
) (* mcp.CallToolResult , error ) {
@@ -310,7 +296,7 @@ func (s *MCPServer) handleAddTool(
310
296
}, nil
311
297
}
312
298
313
- func ( s * MCPServer ) handleSendNotification (
299
+ func handleSendNotification (
314
300
ctx context.Context ,
315
301
request mcp.CallToolRequest ,
316
302
) (* mcp.CallToolResult , error ) {
@@ -339,15 +325,11 @@ func (s *MCPServer) handleSendNotification(
339
325
}, nil
340
326
}
341
327
342
- func (s * MCPServer ) ServeSSE (addr string ) * server.SSEServer {
343
- return server .NewSSEServer (s .server , fmt .Sprintf ("http://%s" , addr ))
344
- }
345
-
346
- func (s * MCPServer ) ServeStdio () error {
347
- return server .ServeStdio (s .server )
328
+ func ServeSSE (mcpServer * server.MCPServer , addr string ) * server.SSEServer {
329
+ return server .NewSSEServer (mcpServer , fmt .Sprintf ("http://%s" , addr ))
348
330
}
349
331
350
- func ( s * MCPServer ) handleLongRunningOperationTool (
332
+ func handleLongRunningOperationTool (
351
333
ctx context.Context ,
352
334
request mcp.CallToolRequest ,
353
335
) (* mcp.CallToolResult , error ) {
@@ -407,7 +389,7 @@ func (s *MCPServer) handleLongRunningOperationTool(
407
389
// }, nil
408
390
// }
409
391
410
- func ( s * MCPServer ) handleGetTinyImageTool (
392
+ func handleGetTinyImageTool (
411
393
ctx context.Context ,
412
394
request mcp.CallToolRequest ,
413
395
) (* mcp.CallToolResult , error ) {
@@ -430,17 +412,13 @@ func (s *MCPServer) handleGetTinyImageTool(
430
412
}, nil
431
413
}
432
414
433
- func ( s * MCPServer ) handleNotification (
415
+ func handleNotification (
434
416
ctx context.Context ,
435
417
notification mcp.JSONRPCNotification ,
436
418
) {
437
419
log .Printf ("Received notification: %s" , notification .Method )
438
420
}
439
421
440
- func (s * MCPServer ) Serve () error {
441
- return server .ServeStdio (s .server )
442
- }
443
-
444
422
func main () {
445
423
var transport string
446
424
flag .StringVar (& transport , "t" , "stdio" , "Transport type (stdio or sse)" )
@@ -452,24 +430,19 @@ func main() {
452
430
)
453
431
flag .Parse ()
454
432
455
- server := NewMCPServer ()
433
+ mcpServer := NewMCPServer ()
456
434
457
- switch transport {
458
- case "stdio" :
459
- if err := server .ServeStdio (); err != nil {
460
- log .Fatalf ("Server error: %v" , err )
461
- }
462
- case "sse" :
463
- sseServer := server .ServeSSE ("localhost:8080" )
435
+ // Only check for "sse" since stdio is the default
436
+ if transport == "sse" {
437
+ sseServer := ServeSSE (mcpServer , "localhost:8080" )
464
438
log .Printf ("SSE server listening on :8080" )
465
439
if err := sseServer .Start (":8080" ); err != nil {
466
440
log .Fatalf ("Server error: %v" , err )
467
441
}
468
- default :
469
- log .Fatalf (
470
- "Invalid transport type: %s. Must be 'stdio' or 'sse'" ,
471
- transport ,
472
- )
442
+ } else {
443
+ if err := server .ServeStdio (mcpServer ); err != nil {
444
+ log .Fatalf ("Server error: %v" , err )
445
+ }
473
446
}
474
447
}
475
448
0 commit comments