Skip to content

Commit 5d6c47a

Browse files
committed
mcp: add prompt feature doc
1 parent 140b939 commit 5d6c47a

File tree

5 files changed

+109
-4
lines changed

5 files changed

+109
-4
lines changed

docs/client.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The SDK supports this as follows:
1313

1414
**Client-side**: The SDK client always has the `roots.listChanged` capability.
1515
To add roots to a client, use the
16-
[`Client.AddRoots`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#AddRoots)
16+
[`Client.AddRoots`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#Client.AddRoots)
1717
and
1818
[`Client.RemoveRoots`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#Client.RemoveRoots)
1919
methods. If any servers are already [connected](protocol.md#lifecycle) to the

docs/server.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,23 @@
1111

1212
## Prompts
1313

14-
<!-- TODO -->
14+
**Server-side**:
15+
MCP servers can provide LLM prompt templates (called simply _prompts_) to clients.
16+
Associated with each prompt is a handler that expands the template given a set of key-value pairs.
17+
Use [`Server.AddPrompt`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#Server.AddPrompt)
18+
to add a prompt along with its handler.
19+
If `AddPrompt` is called before a server is connected, the server will have the `prompts` capability.
20+
If all prompts are to be added after connection, set [`ServerOptions.HasPrompts`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.HasPrompts)
21+
to advertise the capability.
22+
23+
**Client-side**:
24+
To list the server's prompts, call
25+
Call [`ClientSession.Prompts`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.Prompts) to get an iterator.
26+
If needed, you can use the lower-level
27+
[`ClientSession.ListPrompts`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.ListPrompts) to list the server's prompts.
28+
Call [`ClientSession.GetPrompt`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.GetPrompt) to retrieve a prompt by name, providing
29+
arguments for expansion.
30+
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.
1531

1632
## Resources
1733

internal/docs/client.src.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The SDK supports this as follows:
1010

1111
**Client-side**: The SDK client always has the `roots.listChanged` capability.
1212
To add roots to a client, use the
13-
[`Client.AddRoots`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#AddRoots)
13+
[`Client.AddRoots`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#Client.AddRoots)
1414
and
1515
[`Client.RemoveRoots`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#Client.RemoveRoots)
1616
methods. If any servers are already [connected](protocol.md#lifecycle) to the

internal/docs/server.src.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,23 @@
44

55
## Prompts
66

7-
<!-- TODO -->
7+
**Server-side**:
8+
MCP servers can provide LLM prompt templates (called simply _prompts_) to clients.
9+
Associated with each prompt is a handler that expands the template given a set of key-value pairs.
10+
Use [`Server.AddPrompt`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#Server.AddPrompt)
11+
to add a prompt along with its handler.
12+
If `AddPrompt` is called before a server is connected, the server will have the `prompts` capability.
13+
If all prompts are to be added after connection, set [`ServerOptions.HasPrompts`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.HasPrompts)
14+
to advertise the capability.
15+
16+
**Client-side**:
17+
To list the server's prompts, call
18+
Call [`ClientSession.Prompts`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.Prompts) to get an iterator.
19+
If needed, you can use the lower-level
20+
[`ClientSession.ListPrompts`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.ListPrompts) to list the server's prompts.
21+
Call [`ClientSession.GetPrompt`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientSession.GetPrompt) to retrieve a prompt by name, providing
22+
arguments for expansion.
23+
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.
824

925
## Resources
1026

mcp/server_example_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2025 The Go MCP SDK Authors. All rights reserved.
2+
// Use of this source code is governed by an MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package mcp_test
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"log"
11+
12+
"github.com/modelcontextprotocol/go-sdk/mcp"
13+
)
14+
15+
// !+prompts
16+
17+
func Example_prompts() {
18+
ctx := context.Background()
19+
20+
promptHandler := func(ctx context.Context, req *mcp.GetPromptRequest) (*mcp.GetPromptResult, error) {
21+
return &mcp.GetPromptResult{
22+
Description: "Hi prompt",
23+
Messages: []*mcp.PromptMessage{
24+
{
25+
Role: "user",
26+
Content: &mcp.TextContent{Text: "Say hi to " + req.Params.Arguments["name"]},
27+
},
28+
},
29+
}, nil
30+
}
31+
32+
// Create a server with a single prompt.
33+
s := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil)
34+
s.AddPrompt(&mcp.Prompt{Name: "greet"}, promptHandler)
35+
36+
// Create a client.
37+
c := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, nil)
38+
39+
// Connect the server and client.
40+
t1, t2 := mcp.NewInMemoryTransports()
41+
if _, err := s.Connect(ctx, t1, nil); err != nil {
42+
log.Fatal(err)
43+
}
44+
cs, err := c.Connect(ctx, t2, nil)
45+
if err != nil {
46+
log.Fatal(err)
47+
}
48+
49+
// List the prompts.
50+
for p, err := range cs.Prompts(ctx, nil) {
51+
if err != nil {
52+
log.Fatal(err)
53+
}
54+
fmt.Println(p.Name)
55+
}
56+
57+
// Get the prompt.
58+
res, err := cs.GetPrompt(ctx, &mcp.GetPromptParams{
59+
Name: "greet",
60+
Arguments: map[string]string{"name": "Pat"},
61+
})
62+
if err != nil {
63+
log.Fatal(err)
64+
}
65+
for _, msg := range res.Messages {
66+
fmt.Println(msg.Role, msg.Content.(*mcp.TextContent).Text)
67+
}
68+
// Output:
69+
// greet
70+
// user Say hi to Pat
71+
}
72+
73+
// !-prompts

0 commit comments

Comments
 (0)