Skip to content

Commit f37e549

Browse files
committed
mcp: polish package doc for v0.3.0
Do a pass through the package doc for v0.3.0.
1 parent fb69971 commit f37e549

File tree

4 files changed

+95
-33
lines changed

4 files changed

+95
-33
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<!-- Autogenerated by weave; DO NOT EDIT -->
2-
# MCP Go SDK v0.2.0
2+
# MCP Go SDK v0.3.0
33

44
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/modelcontextprotocol/go-sdk)
55

66
***BREAKING CHANGES***
77

88
This version contains breaking changes.
99
See the [release notes](
10-
https://github.com/modelcontextprotocol/go-sdk/releases/tag/v0.2.0) for details.
10+
https://github.com/modelcontextprotocol/go-sdk/releases/tag/v0.3.0) for details.
1111

1212
[![PkgGoDev](https://pkg.go.dev/badge/github.com/modelcontextprotocol/go-sdk)](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk)
1313

internal/readme/README.src.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# MCP Go SDK v0.2.0
1+
# MCP Go SDK v0.3.0
22

33
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/modelcontextprotocol/go-sdk)
44

55
***BREAKING CHANGES***
66

77
This version contains breaking changes.
88
See the [release notes](
9-
https://github.com/modelcontextprotocol/go-sdk/releases/tag/v0.2.0) for details.
9+
https://github.com/modelcontextprotocol/go-sdk/releases/tag/v0.3.0) for details.
1010

1111
[![PkgGoDev](https://pkg.go.dev/badge/github.com/modelcontextprotocol/go-sdk)](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk)
1212

mcp/mcp.go

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,84 @@
77
// The mcp package provides an SDK for writing model context protocol clients
88
// and servers.
99
//
10-
// To get started, create either a [Client] or [Server], and connect it to a
11-
// peer using a [Transport]. The diagram below illustrates how this works:
10+
// To get started, create either a [Client] or [Server], add features to it
11+
// using `AddXXX` functions, and connect it to a peer using a [Transport].
12+
//
13+
// For example, to run a simple server on the [StdioTransport]:
14+
//
15+
// server := mcp.NewServer(&mcp.Implementation{Name: "greeter"}, nil)
16+
//
17+
// // Using the generic AddTool automatically populates the the input and output
18+
// // schema of the tool.
19+
// type args struct {
20+
// Name string `json:"name" jsonschema:"the person to greet"`
21+
// }
22+
// mcp.AddTool(server, &mcp.Tool{
23+
// Name: "greet",
24+
// Description: "say hi",
25+
// }, func(ctx context.Context, req *mcp.CallToolRequest, args args) (*mcp.CallToolResult, any, error) {
26+
// return &mcp.CallToolResult{
27+
// Content: []mcp.Content{
28+
// &mcp.TextContent{Text: "Hi " + args.Name},
29+
// },
30+
// }, nil, nil
31+
// })
32+
//
33+
// // Run the server on the stdio transport.
34+
// if err := server.Run(context.Background(), &mcp.StdioTransport{}); err != nil {
35+
// log.Printf("Server failed: %v", err)
36+
// }
37+
//
38+
// To connect to this server, use the [CommandTransport]:
39+
//
40+
// client := mcp.NewClient(&mcp.Implementation{Name: "mcp-client", Version: "v1.0.0"}, nil)
41+
// transport := &mcp.CommandTransport{Command: exec.Command("myserver")}
42+
// session, err := client.Connect(ctx, transport, nil)
43+
// if err != nil {
44+
// log.Fatal(err)
45+
// }
46+
// defer session.Close()
47+
//
48+
// params := &mcp.CallToolParams{
49+
// Name: "greet",
50+
// Arguments: map[string]any{"name": "you"},
51+
// }
52+
// res, err := session.CallTool(ctx, params)
53+
// if err != nil {
54+
// log.Fatalf("CallTool failed: %v", err)
55+
// }
56+
//
57+
// # Clients, servers, and sessions
58+
//
59+
// In this SDK, both a [Client] and [Server] may handle many concurrent
60+
// connections. Each time a client or server is connected to a peer using a
61+
// [Transport], it creates a new session (either a [ClientSession] or
62+
// [ServerSession]):
1263
//
1364
// Client Server
1465
// ⇅ (jsonrpc2) ⇅
1566
// ClientSession ⇄ Client Transport ⇄ Server Transport ⇄ ServerSession
1667
//
17-
// A [Client] is an MCP client, which can be configured with various client
18-
// capabilities. Clients may be connected to a [Server] instance
19-
// using the [Client.Connect] method.
20-
//
21-
// Similarly, a [Server] is an MCP server, which can be configured with various
22-
// server capabilities. Servers may be connected to one or more [Client]
23-
// instances using the [Server.Connect] method, which creates a
24-
// [ServerSession].
25-
//
26-
// A [Transport] connects a bidirectional [Connection] of jsonrpc2 messages. In
27-
// practice, transports in the MCP spec are are either client transports or
28-
// server transports. For example, the [StdioTransport] is a server transport
29-
// that communicates over stdin/stdout, and its counterpart is a
30-
// [CommandTransport] that communicates with a subprocess over its
31-
// stdin/stdout.
32-
//
33-
// Some transports may hide more complicated details, such as an
34-
// [SSEClientTransport], which reads messages via server-sent events on a
35-
// hanging GET request, and writes them to a POST endpoint. Users of this SDK
36-
// may define their own custom Transports by implementing the [Transport]
37-
// interface.
68+
// The session types expose an API to interact with its peer. For example,
69+
// [ClientSession.CallTool] or [ServerSession.ListRoots].
70+
//
71+
// # Adding features
72+
//
73+
// Add MCP servers to your Client or Server using AddXXX methods (for example
74+
// [Client.AddRoot] or [Server.AddPrompt]). If any peers are connected when
75+
// AddXXX is called, they will receive a corresponding change notification
76+
// (for example notifications/roots/list_changed).
77+
//
78+
// Adding tools is special: tools may be bound to ordinary Go functions by
79+
// using the top-level generic [AddTool] function, which allows specifying an
80+
// input and output type. When AddTool is used, the tool's input schema and
81+
// output schema are automatically populated, and inputs are automatically
82+
// validated. As a special case, if the output type is 'any', no output schema
83+
// is generated.
84+
//
85+
// func double(_ context.Context, _ *mcp.CallToolRequest, in In) (*mcp.CallToolResponse, Out, error) {
86+
// return nil, Out{Answer: 2*in.Number}, nil
87+
// }
88+
// ...
89+
// mcp.AddTool(&mcp.Tool{Name: "double", Description: "double a number"}, double)
3890
package mcp

mcp/server.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,17 @@ func (s *Server) AddTool(t *Tool, h ToolHandler) {
182182
}
183183

184184
// ToolFor returns a shallow copy of t and a [ToolHandler] that wraps h.
185+
//
185186
// If the tool's input schema is nil, it is set to the schema inferred from the In
186187
// type parameter, using [jsonschema.For]. The In type parameter must be a map
187188
// or a struct, so that its inferred JSON Schema has type "object".
188189
//
189-
// If the tool's output schema is nil and the Out type parameter is not the empty
190-
// interface, then the output schema is set to the schema inferred from Out, which
191-
// must be a map or a struct.
190+
// For tools that don't return structured output, Out should be 'any'.
191+
// Otherwise, if the tool's output schema is nil the output schema is set to
192+
// the schema inferred from Out, which must be a map or a struct.
192193
//
193-
// Most users will call [AddTool]. Use [ToolFor] if you wish to modify the tool's
194-
// schemas or wrap the ToolHandler before calling [Server.AddTool].
194+
// Most users will call [AddTool]. Use [ToolFor] if you wish to modify the
195+
// tool's schemas or wrap the ToolHandler before calling [Server.AddTool].
195196
func ToolFor[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHandler) {
196197
tt, hh, err := toolForErr(t, h)
197198
if err != nil {
@@ -317,7 +318,16 @@ func setSchema[T any](sfield **jsonschema.Schema, rfield **jsonschema.Resolved)
317318
return zero, err
318319
}
319320

320-
// AddTool adds a tool and handler to the server.
321+
// AddTool adds a tool and typed tool handler to the server.
322+
//
323+
// If the tool's input schema is nil, it is set to the schema inferred from the
324+
// In type parameter, using [jsonschema.For]. The In type parameter must be a
325+
// map or a struct, so that its inferred JSON Schema has type "object".
326+
//
327+
// For tools that don't return structured output, Out should be 'any'.
328+
// Otherwise, if the tool's output schema is nil the output schema is set to
329+
// the schema inferred from Out, which must be a map or a struct.
330+
//
321331
// It is a convenience for s.AddTool(ToolFor(t, h)).
322332
func AddTool[In, Out any](s *Server, t *Tool, h ToolHandlerFor[In, Out]) {
323333
s.AddTool(ToolFor(t, h))

0 commit comments

Comments
 (0)