MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / Example_prompts

Function Example_prompts

mcp/server_example_test.go:19–84  ·  view source on GitHub ↗

!+prompts

()

Source from the content-addressed store, hash-verified

17// !+prompts
18
19func Example_prompts() {
20 ctx := context.Background()
21
22 promptHandler := func(ctx context.Context, req *mcp.GetPromptRequest) (*mcp.GetPromptResult, error) {
23 return &mcp.GetPromptResult{
24 Description: "Hi prompt",
25 Messages: []*mcp.PromptMessage{
26 {
27 Role: "user",
28 Content: &mcp.TextContent{Text: "Say hi to " + req.Params.Arguments["name"]},
29 },
30 },
31 }, nil
32 }
33
34 // Create a server with a single prompt.
35 s := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil)
36 prompt := &mcp.Prompt{
37 Name: "greet",
38 Arguments: []*mcp.PromptArgument{
39 {
40 Name: "name",
41 Description: "the name of the person to greet",
42 Required: true,
43 },
44 },
45 }
46 s.AddPrompt(prompt, promptHandler)
47
48 // Create a client.
49 c := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, nil)
50
51 // Connect the server and client.
52 t1, t2 := mcp.NewInMemoryTransports()
53 if _, err := s.Connect(ctx, t1, nil); err != nil {
54 log.Fatal(err)
55 }
56 cs, err := c.Connect(ctx, t2, nil)
57 if err != nil {
58 log.Fatal(err)
59 }
60 defer cs.Close()
61
62 // List the prompts.
63 for p, err := range cs.Prompts(ctx, nil) {
64 if err != nil {
65 log.Fatal(err)
66 }
67 fmt.Println(p.Name)
68 }
69
70 // Get the prompt.
71 res, err := cs.GetPrompt(ctx, &mcp.GetPromptParams{
72 Name: "greet",
73 Arguments: map[string]string{"name": "Pat"},
74 })
75 if err != nil {
76 log.Fatal(err)

Callers

nothing calls this directly

Calls 9

AddPromptMethod · 0.95
ConnectMethod · 0.95
ConnectMethod · 0.95
NewServerFunction · 0.92
NewClientFunction · 0.92
NewInMemoryTransportsFunction · 0.92
PromptsMethod · 0.80
GetPromptMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…