Skip to content

Commit 870e39e

Browse files
committed
doc name
1 parent 5d6c47a commit 870e39e

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

docs/server.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,65 @@ Call [`ClientSession.GetPrompt`](https://pkg.go.dev/github.com/modelcontextproto
2929
arguments for expansion.
3030
Set [`ClientOptions.PromptListChangedHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.PromptListChangedHandler) to be notified of changes in the list of prompts.
3131

32+
```go
33+
func Example_prompts() {
34+
ctx := context.Background()
35+
36+
promptHandler := func(ctx context.Context, req *mcp.GetPromptRequest) (*mcp.GetPromptResult, error) {
37+
return &mcp.GetPromptResult{
38+
Description: "Hi prompt",
39+
Messages: []*mcp.PromptMessage{
40+
{
41+
Role: "user",
42+
Content: &mcp.TextContent{Text: "Say hi to " + req.Params.Arguments["name"]},
43+
},
44+
},
45+
}, nil
46+
}
47+
48+
// Create a server with a single prompt.
49+
s := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil)
50+
// The name is required: it uniquely identifies the prompt.
51+
s.AddPrompt(&mcp.Prompt{Name: "greet"}, promptHandler)
52+
53+
// Create a client.
54+
c := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, nil)
55+
56+
// Connect the server and client.
57+
t1, t2 := mcp.NewInMemoryTransports()
58+
if _, err := s.Connect(ctx, t1, nil); err != nil {
59+
log.Fatal(err)
60+
}
61+
cs, err := c.Connect(ctx, t2, nil)
62+
if err != nil {
63+
log.Fatal(err)
64+
}
65+
66+
// List the prompts.
67+
for p, err := range cs.Prompts(ctx, nil) {
68+
if err != nil {
69+
log.Fatal(err)
70+
}
71+
fmt.Println(p.Name)
72+
}
73+
74+
// Get the prompt.
75+
res, err := cs.GetPrompt(ctx, &mcp.GetPromptParams{
76+
Name: "greet",
77+
Arguments: map[string]string{"name": "Pat"},
78+
})
79+
if err != nil {
80+
log.Fatal(err)
81+
}
82+
for _, msg := range res.Messages {
83+
fmt.Println(msg.Role, msg.Content.(*mcp.TextContent).Text)
84+
}
85+
// Output:
86+
// greet
87+
// user Say hi to Pat
88+
}
89+
```
90+
3291
## Resources
3392

3493
<!-- TODO -->

internal/docs/server.src.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Call [`ClientSession.GetPrompt`](https://pkg.go.dev/github.com/modelcontextproto
2222
arguments for expansion.
2323
Set [`ClientOptions.PromptListChangedHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.PromptListChangedHandler) to be notified of changes in the list of prompts.
2424

25+
%include ../../mcp/server_example_test.go prompts -
26+
2527
## Resources
2628

2729
<!-- TODO -->

mcp/server_example_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func Example_prompts() {
3131

3232
// Create a server with a single prompt.
3333
s := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil)
34+
// The name is required: it uniquely identifies the prompt.
3435
s.AddPrompt(&mcp.Prompt{Name: "greet"}, promptHandler)
3536

3637
// Create a client.

0 commit comments

Comments
 (0)