| 17 | ) |
| 18 | |
| 19 | func ExampleServer_AddTool_rawSchema() { |
| 20 | // In some scenarios, you may want your server to be a pass-through, with |
| 21 | // JSON schema coming from another source. Or perhaps you want to implement |
| 22 | // tool validation using a different JSON schema library. |
| 23 | // |
| 24 | // For these cases, you can use [mcp.Server.AddTool], which is the "raw" form |
| 25 | // of the API. Note that it is the caller's responsibility to validate inputs |
| 26 | // and outputs. |
| 27 | server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, nil) |
| 28 | server.AddTool(&mcp.Tool{ |
| 29 | Name: "greet", |
| 30 | InputSchema: json.RawMessage(`{"type":"object","properties":{"user":{"type":"string"}}}`), |
| 31 | }, func(_ context.Context, req *mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| 32 | // Note: no validation! |
| 33 | var args struct{ User string } |
| 34 | if err := json.Unmarshal(req.Params.Arguments, &args); err != nil { |
| 35 | // TODO: we should use a jsonrpc error here, to be consistent with other |
| 36 | // SDKs. |
| 37 | return nil, err |
| 38 | } |
| 39 | return &mcp.CallToolResult{ |
| 40 | Content: []mcp.Content{&mcp.TextContent{Text: "Hi " + args.User}}, |
| 41 | }, nil |
| 42 | }) |
| 43 | |
| 44 | ctx := context.Background() |
| 45 | session, err := connect(ctx, server) |
| 46 | if err != nil { |
| 47 | log.Fatal(err) |
| 48 | } |
| 49 | defer session.Close() |
| 50 | |
| 51 | res, err := session.CallTool(ctx, &mcp.CallToolParams{ |
| 52 | Name: "greet", |
| 53 | Arguments: map[string]any{"user": "you"}, |
| 54 | }) |
| 55 | if err != nil { |
| 56 | log.Fatal(err) |
| 57 | } |
| 58 | fmt.Println(res.Content[0].(*mcp.TextContent).Text) |
| 59 | // Output: Hi you |
| 60 | } |
| 61 | |
| 62 | func ExampleAddTool_customMarshalling() { |
| 63 | // Sometimes when you want to customize the input or output schema for a |