| 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 | // The mcp package provides an SDK for writing model context protocol clients |
| 6 | // and servers. |
| 7 | // |
| 8 | // To get started, create either a [Client] or [Server], add features to it |
| 9 | // using `AddXXX` functions, and connect it to a peer using a [Transport]. |
| 10 | // |
| 11 | // For example, to run a simple server on the [StdioTransport]: |
| 12 | // |
| 13 | // server := mcp.NewServer(&mcp.Implementation{Name: "greeter"}, nil) |
| 14 | // |
| 15 | // // Using the generic AddTool automatically populates the the input and output |
| 16 | // // schema of the tool. |
| 17 | // type args struct { |
| 18 | // Name string `json:"name" jsonschema:"the person to greet"` |
| 19 | // } |
| 20 | // mcp.AddTool(server, &mcp.Tool{ |
| 21 | // Name: "greet", |
| 22 | // Description: "say hi", |
| 23 | // }, func(ctx context.Context, req *mcp.CallToolRequest, args args) (*mcp.CallToolResult, any, error) { |
| 24 | // return &mcp.CallToolResult{ |
| 25 | // Content: []mcp.Content{ |
| 26 | // &mcp.TextContent{Text: "Hi " + args.Name}, |
| 27 | // }, |
| 28 | // }, nil, nil |
| 29 | // }) |
| 30 | // |
| 31 | // // Run the server on the stdio transport. |
| 32 | // if err := server.Run(context.Background(), &mcp.StdioTransport{}); err != nil { |
| 33 | // log.Printf("Server failed: %v", err) |
| 34 | // } |
| 35 | // |
| 36 | // To connect to this server, use the [CommandTransport]: |
| 37 | // |
| 38 | // client := mcp.NewClient(&mcp.Implementation{Name: "mcp-client", Version: "v1.0.0"}, nil) |
| 39 | // transport := &mcp.CommandTransport{Command: exec.Command("myserver")} |
| 40 | // session, err := client.Connect(ctx, transport, nil) |
| 41 | // if err != nil { |
| 42 | // log.Fatal(err) |
| 43 | // } |
| 44 | // defer session.Close() |
| 45 | // |
| 46 | // params := &mcp.CallToolParams{ |
| 47 | // Name: "greet", |
| 48 | // Arguments: map[string]any{"name": "you"}, |
| 49 | // } |
| 50 | // res, err := session.CallTool(ctx, params) |
| 51 | // if err != nil { |
| 52 | // log.Fatalf("CallTool failed: %v", err) |
| 53 | // } |
| 54 | // |
| 55 | // # Clients, servers, and sessions |
| 56 | // |
| 57 | // In this SDK, both a [Client] and [Server] may handle many concurrent |
| 58 | // connections. Each time a client or server is connected to a peer using a |
| 59 | // [Transport], it creates a new session (either a [ClientSession] or |
| 60 | // [ServerSession]): |
| 61 | // |
| 62 | // Client Server |
| 63 | // ⇅ (jsonrpc2) ⇅ |
| 64 | // ClientSession ⇄ Client Transport ⇄ Server Transport ⇄ ServerSession |
| 65 | // |
| 66 | // The session types expose an API to interact with its peer. For example, |
| 67 | // [ClientSession.CallTool] or [ServerSession.ListRoots]. |
| 68 | // |
| 69 | // # Adding features |
| 70 | // |
| 71 | // Add MCP servers to your Client or Server using AddXXX methods (for example |
| 72 | // [Client.AddRoot] or [Server.AddPrompt]). If any peers are connected when |
| 73 | // AddXXX is called, they will receive a corresponding change notification |
| 74 | // (for example notifications/roots/list_changed). |
| 75 | // |
| 76 | // Adding tools is special: tools may be bound to ordinary Go functions by |
| 77 | // using the top-level generic [AddTool] function, which allows specifying an |
| 78 | // input and output type. When AddTool is used, the tool's input schema and |
| 79 | // output schema are automatically populated, and inputs are automatically |
| 80 | // validated. As a special case, if the output type is 'any', no output schema |
| 81 | // is generated. |
| 82 | // |
| 83 | // func double(_ context.Context, _ *mcp.CallToolRequest, in In) (*mcp.CallToolResult, Out, error) { |
| 84 | // return nil, Out{Answer: 2*in.Number}, nil |
| 85 | // } |
| 86 | // ... |
| 87 | // mcp.AddTool(server, &mcp.Tool{Name: "double"}, double) |
| 88 | package mcp |