Skip to content

Commit 5f5303c

Browse files
committed
Add minimal server and client
1 parent d82bf78 commit 5f5303c

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

examples/minimal_client/main.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"os"
8+
"time"
9+
10+
"github.com/mark3labs/mcp-go/client/transport"
11+
)
12+
13+
func main() {
14+
// Create a new Streamable HTTP transport with a longer timeout
15+
trans, err := transport.NewStreamableHTTP("http://localhost:8080/mcp",
16+
transport.WithHTTPTimeout(30*time.Second))
17+
if err != nil {
18+
fmt.Printf("Failed to create transport: %v\n", err)
19+
os.Exit(1)
20+
}
21+
defer trans.Close()
22+
23+
// Create a context with timeout
24+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
25+
defer cancel()
26+
27+
// Initialize the connection
28+
fmt.Println("Initializing connection...")
29+
initRequest := transport.JSONRPCRequest{
30+
JSONRPC: "2.0",
31+
ID: 1,
32+
Method: "initialize",
33+
}
34+
35+
initResponse, err := trans.SendRequest(ctx, initRequest)
36+
if err != nil {
37+
fmt.Printf("Failed to initialize: %v\n", err)
38+
os.Exit(1)
39+
}
40+
41+
// Print the initialization response
42+
initResponseJSON, _ := json.MarshalIndent(initResponse, "", " ")
43+
fmt.Printf("Initialization response: %s\n", initResponseJSON)
44+
fmt.Printf("Session ID: %s\n", trans.GetSessionId())
45+
46+
// Call the echo tool
47+
fmt.Println("\nCalling echo tool...")
48+
echoRequest := transport.JSONRPCRequest{
49+
JSONRPC: "2.0",
50+
ID: 2,
51+
Method: "tools/call",
52+
Params: map[string]interface{}{
53+
"name": "echo",
54+
"arguments": map[string]interface{}{
55+
"message": "Hello from minimal client!",
56+
},
57+
},
58+
}
59+
60+
echoResponse, err := trans.SendRequest(ctx, echoRequest)
61+
if err != nil {
62+
fmt.Printf("Failed to call echo tool: %v\n", err)
63+
os.Exit(1)
64+
}
65+
66+
// Print the echo response
67+
echoResponseJSON, _ := json.MarshalIndent(echoResponse, "", " ")
68+
fmt.Printf("Echo response: %s\n", echoResponseJSON)
69+
70+
fmt.Println("\nTest completed successfully!")
71+
}

examples/minimal_server/main.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
"os/signal"
9+
"syscall"
10+
"time"
11+
12+
"github.com/mark3labs/mcp-go/mcp"
13+
"github.com/mark3labs/mcp-go/server"
14+
)
15+
16+
func main() {
17+
// Create a new MCP server
18+
mcpServer := server.NewMCPServer("minimal-server", "1.0.0")
19+
20+
// Add a simple echo tool
21+
mcpServer.AddTool(
22+
mcp.Tool{
23+
Name: "echo",
24+
Description: "Echoes back the input",
25+
InputSchema: mcp.ToolInputSchema{
26+
Type: "object",
27+
Properties: map[string]interface{}{
28+
"message": map[string]interface{}{
29+
"type": "string",
30+
"description": "The message to echo",
31+
},
32+
},
33+
Required: []string{"message"},
34+
},
35+
},
36+
func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
37+
// Extract the message from the request
38+
message, ok := request.Params.Arguments["message"].(string)
39+
if !ok {
40+
return nil, fmt.Errorf("message must be a string")
41+
}
42+
43+
// Create the result
44+
result := &mcp.CallToolResult{
45+
Result: mcp.Result{},
46+
Content: []mcp.Content{
47+
mcp.TextContent{
48+
Type: "text",
49+
Text: fmt.Sprintf("Echo: %s", message),
50+
},
51+
},
52+
}
53+
54+
return result, nil
55+
},
56+
)
57+
58+
// Create a new Streamable HTTP server with direct JSON responses
59+
streamableServer := server.NewStreamableHTTPServer(mcpServer,
60+
server.WithEnableJSONResponse(true),
61+
)
62+
63+
// Start the server in a goroutine
64+
go func() {
65+
log.Println("Starting Minimal Streamable HTTP server on :8080...")
66+
if err := streamableServer.Start(":8080"); err != nil {
67+
log.Fatalf("Failed to start server: %v", err)
68+
}
69+
}()
70+
71+
// Wait for interrupt signal to gracefully shutdown the server
72+
quit := make(chan os.Signal, 1)
73+
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
74+
<-quit
75+
76+
log.Println("Shutting down server...")
77+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
78+
defer cancel()
79+
if err := streamableServer.Shutdown(ctx); err != nil {
80+
log.Fatalf("Server shutdown failed: %v", err)
81+
}
82+
log.Println("Server exited properly")
83+
}

0 commit comments

Comments
 (0)