Skip to content

Commit 0f0a134

Browse files
committed
docs: add feature and troubleshooting documentation
Add a framework for feature documentation, and start populating it with our SDK documentation. This framework is as follows: - internal/docs/**.src.md is the markdown source for the docs/ directory. - The x/example/internal/cmd/weave tool is used to compile these docs to the top-level docs, supporting both linked code samples and generated tables of contents. - The readme-check workflow is updated to check these docs as well. - The structure of these docs follows the MCP spec. - Wherever possible, example code is linked from actual Go documentation examples, so that it is testable. Some minor modifications to the weave tool were made to support this framework. Additionally, partially fill out this documentation with content on base protocol and client features, as well as troubleshooting help. Along the way, a bug was encountered that our LoggingTransport was not concurrency safe. This is fixed with a mutex. Fixes #466 Fixes #409 Updates #442
1 parent c2c810b commit 0f0a134

22 files changed

+1451
-30
lines changed

.github/workflows/readme-check.yml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
name: README Check
22
on:
3-
workflow_dispatch:
3+
workflow_dispatch:
44
pull_request:
55
paths:
66
- 'internal/readme/**'
77
- 'README.md'
8-
8+
- 'internal/docs/**'
9+
- 'docs/**'
10+
911
permissions:
1012
contents: read
1113

@@ -17,15 +19,15 @@ jobs:
1719
uses: actions/setup-go@v5
1820
- name: Check out code
1921
uses: actions/checkout@v4
20-
- name: Check README is up-to-date
22+
- name: Check docs is up-to-date
2123
run: |
22-
go generate ./internal/readme
24+
go generate ./...
2325
if [ -n "$(git status --porcelain)" ]; then
24-
echo "ERROR: README.md is not up-to-date!"
26+
echo "ERROR: docs are not up-to-date!"
2527
echo ""
26-
echo "The README.md file differs from what would be generated by `go generate ./internal/readme`."
27-
echo "Please update internal/readme/README.src.md instead of README.md directly,"
28-
echo "then run `go generate ./internal/readme` to regenerate README.md."
28+
echo "The docs differ from what would be generated by `go generate ./...`."
29+
echo "Please update internal/**/*.src.md instead of directly editing README.md or docs/ files,"
30+
echo "then run `go generate ./...` to regenerate docs."
2931
echo ""
3032
echo "Changes:"
3133
git status --porcelain
@@ -34,4 +36,4 @@ jobs:
3436
git diff
3537
exit 1
3638
fi
37-
echo "README.md is up-to-date"
39+
echo "Docs are up-to-date."

docs/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!-- Autogenerated by weave; DO NOT EDIT -->
2+
These docs are a work-in-progress.
3+
4+
# Features
5+
6+
This doc mirrors the official MCP spec hosted at
7+
https://modelcontextprotocol.io/specification/2025-06-18
8+
9+
## Base Protocol
10+
11+
1. [Lifecycle (Clients, Servers, and Sessions)](protocol.md#lifecycle).
12+
1. [Transports](protocol.md#transports)
13+
1. [Stdio transport](protocol.md#stdio-transport)
14+
1. [Streamable transport](protocol.md#streamable-transport)
15+
1. [Custom transports](protocol.md#stateless-mode)
16+
1. [Authorization](protocol.md#authorization)
17+
1. [Security](protocol.md#security)
18+
1. [Utilities](protocol.md#utilities)
19+
1. [Cancellation](utilities.md#cancellation)
20+
1. [Ping](utilities.md#ping)
21+
1. [Progress](utilities.md#progress)
22+
23+
## Client Features
24+
25+
1. [Roots](client.md#roots)
26+
1. [Sampling](client.md#sampling)
27+
1. [Elicitation](clients.md#elicitation)
28+
29+
## Server Features
30+
31+
1. [Prompts](server.md#prompts)
32+
1. [Resources](server.md#resources)
33+
1. [Tools](tools.md)
34+
1. [Utilities](server.md#utilities)
35+
1. [Completion](server.md#completion)
36+
1. [Logging](server.md#logging)
37+
1. [Pagination](server.md#pagination)
38+
39+
# TroubleShooting
40+
41+
See [troubleshooting.md](troubleshooting.md) for a troubleshooting guide.

docs/client.md

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<!-- Autogenerated by weave; DO NOT EDIT -->
2+
# Support for MCP client features
3+
4+
1. [Roots](#roots)
5+
1. [Sampling](#sampling)
6+
1. [Elicitation](#elicitation)
7+
8+
## Roots
9+
10+
MCP allows clients to specify a set of filesystem
11+
["roots"](https://modelcontextprotocol.io/specification/2025-06-18/client/roots).
12+
The SDK supports this as follows:
13+
14+
**Client-side**: The SDK client always has the `roots.listChanged` capability.
15+
To add roots to a client, use the
16+
[`Client.AddRoots`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#AddRoots)
17+
and
18+
[`Client.RemoveRoots`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#Client.RemoveRoots)
19+
methods. If any servers are already [connected](protocol.md#lifecycle) to the
20+
client, a call to `AddRoot` or `RemoveRoots` will result in a
21+
`notifications/roots/list_changed` notification to each connected server.
22+
23+
**Server-side**: To query roots from the server, use the
24+
[`ServerSession.ListRoots`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerSession.ListRoots)
25+
method. To receive notifications about root changes, set
26+
[`ServerOptions.RootsListChangedHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.RootsListChangedHandler).
27+
28+
```go
29+
func Example_roots() {
30+
ctx := context.Background()
31+
32+
// Create a client with a single root.
33+
c := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, nil)
34+
c.AddRoots(&mcp.Root{URI: "file://a"})
35+
36+
// Now create a server with a handler to receive notifications about roots.
37+
rootsChanged := make(chan struct{})
38+
handleRootsChanged := func(ctx context.Context, req *mcp.RootsListChangedRequest) {
39+
rootList, err := req.Session.ListRoots(ctx, nil)
40+
if err != nil {
41+
log.Fatal(err)
42+
}
43+
var roots []string
44+
for _, root := range rootList.Roots {
45+
roots = append(roots, root.URI)
46+
}
47+
fmt.Println(roots)
48+
close(rootsChanged)
49+
}
50+
s := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, &mcp.ServerOptions{
51+
RootsListChangedHandler: handleRootsChanged,
52+
})
53+
54+
// Connect the server and client...
55+
t1, t2 := mcp.NewInMemoryTransports()
56+
if _, err := s.Connect(ctx, t1, nil); err != nil {
57+
log.Fatal(err)
58+
}
59+
if _, err := c.Connect(ctx, t2, nil); err != nil {
60+
log.Fatal(err)
61+
}
62+
63+
// ...and add a root. The server is notified about the change.
64+
c.AddRoots(&mcp.Root{URI: "file://b"})
65+
<-rootsChanged
66+
// Output: [file://a file://b]
67+
}
68+
```
69+
70+
## Sampling
71+
72+
[Sampling](https://modelcontextprotocol.io/specification/2025-06-18/client/sampling)
73+
is a way for server's to leverage the client's AI capabilities. It is
74+
implemented in the SDK as follows:
75+
76+
**Client-side**: To add the `sampling` capability to a client, set
77+
[`ClientOptions.CreateMessageHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.CreateMessageHandler).
78+
This function is invoked whenever the server requests sampling.
79+
80+
**Server-side**: To use sampling from the server, call
81+
[`ServerSession.CreateMessage`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerSession.CreateMessage).
82+
83+
```go
84+
func Example_sampling() {
85+
ctx := context.Background()
86+
87+
// Create a client with a single root.
88+
c := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, &mcp.ClientOptions{
89+
CreateMessageHandler: func(_ context.Context, req *mcp.CreateMessageRequest) (*mcp.CreateMessageResult, error) {
90+
return &mcp.CreateMessageResult{
91+
Content: &mcp.TextContent{
92+
Text: "would have created a message",
93+
},
94+
}, nil
95+
},
96+
})
97+
98+
// Connect the server and client...
99+
ct, st := mcp.NewInMemoryTransports()
100+
s := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil)
101+
session, err := s.Connect(ctx, st, nil)
102+
if err != nil {
103+
log.Fatal(err)
104+
}
105+
defer session.Close()
106+
107+
if _, err := c.Connect(ctx, ct, nil); err != nil {
108+
log.Fatal(err)
109+
}
110+
111+
msg, err := session.CreateMessage(ctx, &mcp.CreateMessageParams{})
112+
if err != nil {
113+
log.Fatal(err)
114+
}
115+
fmt.Println(msg.Content.(*mcp.TextContent).Text)
116+
// Output: would have created a message
117+
}
118+
```
119+
120+
## Elicitation
121+
122+
[Elicitation](https://modelcontextprotocol.io/specification/2025-06-18/client/elicitation)
123+
allows servers to request user inputs. It is implemented in the SDK as follows:
124+
125+
**Client-side**: To add the `elicitation` capability to a client, set
126+
[`ClientOptions.ElicitationHandler`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ClientOptions.ElicitationHandler).
127+
The elicitation handler must return a result that matches the requested schema;
128+
otherwise, elicitation returns an error.
129+
130+
**Server-side**: To use eliciation from the server, call
131+
[`ServerSession.Elicit`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerSession.Elicit).
132+
133+
```go
134+
func Example_elicitation() {
135+
ctx := context.Background()
136+
ct, st := mcp.NewInMemoryTransports()
137+
138+
s := mcp.NewServer(testImpl, nil)
139+
ss, err := s.Connect(ctx, st, nil)
140+
if err != nil {
141+
log.Fatal(err)
142+
}
143+
defer ss.Close()
144+
145+
c := mcp.NewClient(testImpl, &mcp.ClientOptions{
146+
ElicitationHandler: func(context.Context, *mcp.ElicitRequest) (*mcp.ElicitResult, error) {
147+
return &mcp.ElicitResult{Action: "accept", Content: map[string]any{"test": "value"}}, nil
148+
},
149+
})
150+
if _, err := c.Connect(ctx, ct, nil); err != nil {
151+
log.Fatal(err)
152+
}
153+
res, err := ss.Elicit(ctx, &mcp.ElicitParams{
154+
Message: "This should fail",
155+
RequestedSchema: &jsonschema.Schema{
156+
Type: "object",
157+
Properties: map[string]*jsonschema.Schema{
158+
"test": {Type: "string"},
159+
},
160+
},
161+
})
162+
if err != nil {
163+
log.Fatal(err)
164+
}
165+
fmt.Println(res.Content["test"])
166+
// Output: value
167+
}
168+
```

0 commit comments

Comments
 (0)