examples: add a 'listeverything' client example, and reorganize
Add a client example that lists all features on a stdio server. Since this is our first client example, organize examples by client / server -- otherwise it is too difficult to find client examples. For #33
This commit is contained in:
parent
c132621eb0
commit
e00c485969
1 changed files with 65 additions and 0 deletions
70
examples/server/sse/main.go
Normal file
70
examples/server/sse/main.go
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
)
|
||||
|
||||
var (
|
||||
host = flag.String("host", "localhost", "host to listen on")
|
||||
port = flag.String("port", "8080", "port to listen on")
|
||||
)
|
||||
|
||||
type SayHiParams struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func SayHi(ctx context.Context, cc *mcp.ServerSession, params *mcp.CallToolParamsFor[SayHiParams]) (*mcp.CallToolResultFor[any], error) {
|
||||
return &mcp.CallToolResultFor[any]{
|
||||
Content: []mcp.Content{
|
||||
&mcp.TextContent{Text: "Hi " + params.Arguments.Name},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s [options]\n\n", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, "This program runs MCP servers over SSE HTTP.\n\n")
|
||||
fmt.Fprintf(os.Stderr, "Options:\n")
|
||||
flag.PrintDefaults()
|
||||
fmt.Fprintf(os.Stderr, "\nEndpoints:\n")
|
||||
fmt.Fprintf(os.Stderr, " /greeter1 - Greeter 1 service\n")
|
||||
fmt.Fprintf(os.Stderr, " /greeter2 - Greeter 2 service\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
flag.Parse()
|
||||
|
||||
addr := fmt.Sprintf("%s:%s", *host, *port)
|
||||
|
||||
server1 := mcp.NewServer(&mcp.Implementation{Name: "greeter1"}, nil)
|
||||
mcp.AddTool(server1, &mcp.Tool{Name: "greet1", Description: "say hi"}, SayHi)
|
||||
|
||||
server2 := mcp.NewServer(&mcp.Implementation{Name: "greeter2"}, nil)
|
||||
mcp.AddTool(server2, &mcp.Tool{Name: "greet2", Description: "say hello"}, SayHi)
|
||||
|
||||
log.Printf("MCP servers serving at %s", addr)
|
||||
handler := mcp.NewSSEHandler(func(request *http.Request) *mcp.Server {
|
||||
url := request.URL.Path
|
||||
log.Printf("Handling request for URL %s\n", url)
|
||||
switch url {
|
||||
case "/greeter1":
|
||||
return server1
|
||||
case "/greeter2":
|
||||
return server2
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
})
|
||||
log.Fatal(http.ListenAndServe(addr, handler))
|
||||
}
|
||||
Loading…
Reference in a new issue