Skip to content

Commit 362645e

Browse files
committed
Simplify example
1 parent 3a71167 commit 362645e

File tree

1 file changed

+49
-76
lines changed

1 file changed

+49
-76
lines changed

examples/everything/main.go

+49-76
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,30 @@ const (
2828
COMPLEX PromptName = "complex_prompt"
2929
)
3030

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+
)
5139

52-
s.server.AddResource(mcp.NewResource("test://static/resource",
40+
mcpServer.AddResource(mcp.NewResource("test://static/resource",
5341
"Static Resource",
5442
mcp.WithMIMEType("text/plain"),
55-
), s.handleReadResource)
56-
s.server.AddResourceTemplate(
43+
), handleReadResource)
44+
mcpServer.AddResourceTemplate(
5745
mcp.NewResourceTemplate(
5846
"test://dynamic/resource/{id}",
5947
"Dynamic Resource",
6048
),
61-
s.handleResourceTemplate,
49+
handleResourceTemplate,
6250
)
63-
s.server.AddPrompt(mcp.NewPrompt(string(SIMPLE),
51+
mcpServer.AddPrompt(mcp.NewPrompt(string(SIMPLE),
6452
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),
6755
mcp.WithPromptDescription("A complex prompt"),
6856
mcp.WithArgument("temperature",
6957
mcp.ArgumentDescription("The temperature parameter for generation"),
@@ -73,21 +61,21 @@ func NewMCPServer() *MCPServer {
7361
mcp.ArgumentDescription("The style to use for the response"),
7462
mcp.RequiredArgument(),
7563
),
76-
), s.handleComplexPrompt)
77-
s.server.AddTool(mcp.NewTool(string(ECHO),
64+
), handleComplexPrompt)
65+
mcpServer.AddTool(mcp.NewTool(string(ECHO),
7866
mcp.WithDescription("Echoes back the input"),
7967
mcp.WithString("message",
8068
mcp.Description("Message to echo"),
8169
mcp.Required(),
8270
),
83-
), s.handleEchoTool)
71+
), handleEchoTool)
8472

85-
s.server.AddTool(
73+
mcpServer.AddTool(
8674
mcp.NewTool("notify"),
87-
s.handleSendNotification,
75+
handleSendNotification,
8876
)
8977

90-
s.server.AddTool(mcp.NewTool(string(ADD),
78+
mcpServer.AddTool(mcp.NewTool(string(ADD),
9179
mcp.WithDescription("Adds two numbers"),
9280
mcp.WithNumber("a",
9381
mcp.Description("First number"),
@@ -97,8 +85,8 @@ func NewMCPServer() *MCPServer {
9785
mcp.Description("Second number"),
9886
mcp.Required(),
9987
),
100-
), s.handleAddTool)
101-
s.server.AddTool(mcp.NewTool(
88+
), handleAddTool)
89+
mcpServer.AddTool(mcp.NewTool(
10290
string(LONG_RUNNING_OPERATION),
10391
mcp.WithDescription(
10492
"Demonstrates a long running operation with progress updates",
@@ -111,7 +99,7 @@ func NewMCPServer() *MCPServer {
11199
mcp.Description("Number of steps in the operation"),
112100
mcp.DefaultNumber(5),
113101
),
114-
), s.handleLongRunningOperationTool)
102+
), handleLongRunningOperationTool)
115103

116104
// s.server.AddTool(mcp.Tool{
117105
// Name: string(SAMPLE_LLM),
@@ -131,15 +119,13 @@ func NewMCPServer() *MCPServer {
131119
// },
132120
// },
133121
// }, s.handleSampleLLMTool)
134-
s.server.AddTool(mcp.NewTool(string(GET_TINY_IMAGE),
122+
mcpServer.AddTool(mcp.NewTool(string(GET_TINY_IMAGE),
135123
mcp.WithDescription("Returns the MCP_TINY_IMAGE"),
136-
), s.handleGetTinyImageTool)
137-
138-
s.server.AddNotificationHandler("notification", s.handleNotification)
124+
), handleGetTinyImageTool)
139125

140-
go s.runUpdateInterval()
126+
mcpServer.AddNotificationHandler("notification", handleNotification)
141127

142-
return s
128+
return mcpServer
143129
}
144130

145131
func generateResources() []mcp.Resource {
@@ -163,7 +149,7 @@ func generateResources() []mcp.Resource {
163149
return resources
164150
}
165151

166-
func (s *MCPServer) runUpdateInterval() {
152+
func runUpdateInterval() {
167153
// for range s.updateTicker.C {
168154
// for uri := range s.subscriptions {
169155
// s.server.HandleMessage(
@@ -184,7 +170,7 @@ func (s *MCPServer) runUpdateInterval() {
184170
// }
185171
}
186172

187-
func (s *MCPServer) handleReadResource(
173+
func handleReadResource(
188174
ctx context.Context,
189175
request mcp.ReadResourceRequest,
190176
) ([]interface{}, error) {
@@ -199,7 +185,7 @@ func (s *MCPServer) handleReadResource(
199185
}, nil
200186
}
201187

202-
func (s *MCPServer) handleResourceTemplate(
188+
func handleResourceTemplate(
203189
ctx context.Context,
204190
request mcp.ReadResourceRequest,
205191
) ([]interface{}, error) {
@@ -214,7 +200,7 @@ func (s *MCPServer) handleResourceTemplate(
214200
}, nil
215201
}
216202

217-
func (s *MCPServer) handleSimplePrompt(
203+
func handleSimplePrompt(
218204
ctx context.Context,
219205
request mcp.GetPromptRequest,
220206
) (*mcp.GetPromptResult, error) {
@@ -232,7 +218,7 @@ func (s *MCPServer) handleSimplePrompt(
232218
}, nil
233219
}
234220

235-
func (s *MCPServer) handleComplexPrompt(
221+
func handleComplexPrompt(
236222
ctx context.Context,
237223
request mcp.GetPromptRequest,
238224
) (*mcp.GetPromptResult, error) {
@@ -270,7 +256,7 @@ func (s *MCPServer) handleComplexPrompt(
270256
}, nil
271257
}
272258

273-
func (s *MCPServer) handleEchoTool(
259+
func handleEchoTool(
274260
ctx context.Context,
275261
request mcp.CallToolRequest,
276262
) (*mcp.CallToolResult, error) {
@@ -289,7 +275,7 @@ func (s *MCPServer) handleEchoTool(
289275
}, nil
290276
}
291277

292-
func (s *MCPServer) handleAddTool(
278+
func handleAddTool(
293279
ctx context.Context,
294280
request mcp.CallToolRequest,
295281
) (*mcp.CallToolResult, error) {
@@ -310,7 +296,7 @@ func (s *MCPServer) handleAddTool(
310296
}, nil
311297
}
312298

313-
func (s *MCPServer) handleSendNotification(
299+
func handleSendNotification(
314300
ctx context.Context,
315301
request mcp.CallToolRequest,
316302
) (*mcp.CallToolResult, error) {
@@ -339,15 +325,11 @@ func (s *MCPServer) handleSendNotification(
339325
}, nil
340326
}
341327

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))
348330
}
349331

350-
func (s *MCPServer) handleLongRunningOperationTool(
332+
func handleLongRunningOperationTool(
351333
ctx context.Context,
352334
request mcp.CallToolRequest,
353335
) (*mcp.CallToolResult, error) {
@@ -407,7 +389,7 @@ func (s *MCPServer) handleLongRunningOperationTool(
407389
// }, nil
408390
// }
409391

410-
func (s *MCPServer) handleGetTinyImageTool(
392+
func handleGetTinyImageTool(
411393
ctx context.Context,
412394
request mcp.CallToolRequest,
413395
) (*mcp.CallToolResult, error) {
@@ -430,17 +412,13 @@ func (s *MCPServer) handleGetTinyImageTool(
430412
}, nil
431413
}
432414

433-
func (s *MCPServer) handleNotification(
415+
func handleNotification(
434416
ctx context.Context,
435417
notification mcp.JSONRPCNotification,
436418
) {
437419
log.Printf("Received notification: %s", notification.Method)
438420
}
439421

440-
func (s *MCPServer) Serve() error {
441-
return server.ServeStdio(s.server)
442-
}
443-
444422
func main() {
445423
var transport string
446424
flag.StringVar(&transport, "t", "stdio", "Transport type (stdio or sse)")
@@ -452,24 +430,19 @@ func main() {
452430
)
453431
flag.Parse()
454432

455-
server := NewMCPServer()
433+
mcpServer := NewMCPServer()
456434

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")
464438
log.Printf("SSE server listening on :8080")
465439
if err := sseServer.Start(":8080"); err != nil {
466440
log.Fatalf("Server error: %v", err)
467441
}
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+
}
473446
}
474447
}
475448

0 commit comments

Comments
 (0)