Also validate that requests observe the lifecycle rules defined in the spec. Change-Id: I302282f81e053e76926e0bb295f7dbf8d526f02d Reviewed-on: https://go-review.googlesource.com/c/tools/+/667575 Reviewed-by: Jonathan Amsterdam <jba@google.com> Commit-Queue: Robert Findley <rfindley@google.com> Auto-Submit: Robert Findley <rfindley@google.com> TryBot-Bypass: Robert Findley <rfindley@google.com>
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
// 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"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
)
|
|
|
|
var httpAddr = flag.String("http", "", "if set, use SSE HTTP at this address, instead of stdin/stdout")
|
|
|
|
type SayHiParams struct {
|
|
Name string `json:"name" mcp:"the name to say hi to"`
|
|
}
|
|
|
|
func SayHi(ctx context.Context, cc *mcp.ClientConnection, params *SayHiParams) ([]mcp.Content, error) {
|
|
return []mcp.Content{
|
|
mcp.TextContent{Text: "Hi " + params.Name},
|
|
}, nil
|
|
}
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
server := mcp.NewServer("greeter", "v0.0.1", nil)
|
|
server.AddTools(mcp.MakeTool("greet", "say hi", SayHi))
|
|
|
|
if *httpAddr != "" {
|
|
handler := mcp.NewSSEHandler(func() *mcp.Server {
|
|
return server
|
|
})
|
|
http.ListenAndServe(*httpAddr, handler)
|
|
} else {
|
|
opts := &mcp.ConnectionOptions{Logger: os.Stderr}
|
|
if err := server.Run(context.Background(), mcp.NewStdIOTransport(), opts); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Server failed: %v", err)
|
|
}
|
|
}
|
|
}
|