|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/google/jsonschema-go/jsonschema" |
| 12 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 13 | +) |
| 14 | + |
| 15 | +type DeleteCustomerParams struct { |
| 16 | + CustomerID string `json:"customerId" jsonschema:"Customer ID to delete"` |
| 17 | +} |
| 18 | + |
| 19 | +func deleteCustomer( |
| 20 | + ctx context.Context, |
| 21 | + req *mcp.CallToolRequest, |
| 22 | + params DeleteCustomerParams, |
| 23 | +) (*mcp.CallToolResult, any, error) { |
| 24 | + schema := &jsonschema.Schema{ |
| 25 | + Type: "object", |
| 26 | + Description: "Confirm the deletion of the data.", |
| 27 | + Properties: map[string]*jsonschema.Schema{ |
| 28 | + "confirm": {Type: "boolean", Description: "Confirm the deletion of the data."}, |
| 29 | + }, |
| 30 | + Required: []string{"confirm"}, |
| 31 | + } |
| 32 | + |
| 33 | + result, err := req.Session.Elicit(ctx, &mcp.ElicitParams{ |
| 34 | + // Intentionally blank to exercise client-side fallback to schema description. |
| 35 | + Message: "", |
| 36 | + RequestedSchema: schema, |
| 37 | + }) |
| 38 | + if err != nil { |
| 39 | + return nil, nil, fmt.Errorf("eliciting failed: %w", err) |
| 40 | + } |
| 41 | + |
| 42 | + confirmed := false |
| 43 | + if result != nil && result.Action == "accept" { |
| 44 | + if value, ok := result.Content["confirm"]; ok { |
| 45 | + switch typed := value.(type) { |
| 46 | + case bool: |
| 47 | + confirmed = typed |
| 48 | + case string: |
| 49 | + confirmed = strings.EqualFold(strings.TrimSpace(typed), "yes") |
| 50 | + default: |
| 51 | + confirmed = strings.EqualFold(strings.TrimSpace(fmt.Sprint(value)), "yes") |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + message := fmt.Sprintf("Deletion cancelled for %s.", params.CustomerID) |
| 57 | + if confirmed { |
| 58 | + message = fmt.Sprintf("Customer %s deleted.", params.CustomerID) |
| 59 | + } |
| 60 | + |
| 61 | + return &mcp.CallToolResult{ |
| 62 | + Content: []mcp.Content{ |
| 63 | + &mcp.TextContent{Text: message}, |
| 64 | + }, |
| 65 | + }, nil, nil |
| 66 | +} |
| 67 | + |
| 68 | +func main() { |
| 69 | + host := flag.String("host", "127.0.0.1", "host to listen on") |
| 70 | + port := flag.Int("port", 3142, "port to listen on") |
| 71 | + flag.Parse() |
| 72 | + |
| 73 | + addr := fmt.Sprintf("%s:%d", *host, *port) |
| 74 | + |
| 75 | + server := mcp.NewServer(&mcp.Implementation{ |
| 76 | + Name: "go-elicitation-server", |
| 77 | + Version: "0.1.0", |
| 78 | + }, nil) |
| 79 | + |
| 80 | + mcp.AddTool(server, &mcp.Tool{ |
| 81 | + Name: "customer_delete", |
| 82 | + Description: "Delete a customer after user confirmation.", |
| 83 | + }, deleteCustomer) |
| 84 | + |
| 85 | + handler := mcp.NewStreamableHTTPHandler(func(_ *http.Request) *mcp.Server { |
| 86 | + return server |
| 87 | + }, nil) |
| 88 | + |
| 89 | + mux := http.NewServeMux() |
| 90 | + mux.Handle("/mcp", handler) |
| 91 | + |
| 92 | + log.Printf("MCP server listening on http://%s/mcp", addr) |
| 93 | + if err := http.ListenAndServe(addr, mux); err != nil { |
| 94 | + log.Fatalf("server failed: %v", err) |
| 95 | + } |
| 96 | +} |
0 commit comments