internal/mcp: support prompts
Add support for 'prompts/list' and 'prompts/get', and registering prompts on the server using AddPrompts. Add a 'MakePrompt' helper to construct prompts from a prompt handler using reflection. Change-Id: I479cb9c9b99313cca99640c1f5ea1939363759e6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/669015 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Jonathan Amsterdam <jba@google.com>
This commit is contained in:
parent
e082a340be
commit
af4d138732
1 changed files with 531 additions and 98 deletions
|
|
@ -6,26 +6,46 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
"github.com/modelcontextprotocol/go-sdk/internal/protocol"
|
||||
)
|
||||
|
||||
var httpAddr = flag.String("http", "", "if set, use SSE HTTP at this address, instead of stdin/stdout")
|
||||
|
||||
type SayHiParams struct {
|
||||
type HiParams struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func SayHi(ctx context.Context, cc *mcp.ClientConnection, params *SayHiParams) ([]mcp.Content, error) {
|
||||
func SayHi(ctx context.Context, cc *mcp.ClientConnection, params *HiParams) ([]mcp.Content, error) {
|
||||
return []mcp.Content{
|
||||
mcp.TextContent{Text: "Hi " + params.Name},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func PromptHi(ctx context.Context, cc *mcp.ClientConnection, params *HiParams) (*protocol.GetPromptResult, error) {
|
||||
// (see related TODOs about cleaning up content construction)
|
||||
content, err := json.Marshal(protocol.TextContent{
|
||||
Type: "text",
|
||||
Text: "Say hi to " + params.Name,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &protocol.GetPromptResult{
|
||||
Description: "Code review prompt",
|
||||
Messages: []protocol.PromptMessage{
|
||||
// TODO: move 'Content' to the protocol package.
|
||||
{Role: "user", Content: json.RawMessage(content)},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
|
|
@ -33,6 +53,7 @@ func main() {
|
|||
server.AddTools(mcp.MakeTool("greet", "say hi", SayHi, mcp.Input(
|
||||
mcp.Property("name", mcp.Description("the name to say hi to")),
|
||||
)))
|
||||
server.AddPrompts(mcp.MakePrompt("greet", "", PromptHi))
|
||||
|
||||
if *httpAddr != "" {
|
||||
handler := mcp.NewSSEHandler(func(*http.Request) *mcp.Server {
|
||||
|
|
|
|||
Loading…
Reference in a new issue