|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/kkjdaniel/gogeek/v2" |
| 10 | + "github.com/kkjdaniel/gogeek/v2/collection" |
| 11 | + "github.com/mark3labs/mcp-go/mcp" |
| 12 | + "github.com/mark3labs/mcp-go/server" |
| 13 | +) |
| 14 | + |
| 15 | +func MyCollectionResource(client *gogeek.Client) (mcp.Resource, server.ResourceHandlerFunc) { |
| 16 | + username := os.Getenv("BGG_USERNAME") |
| 17 | + |
| 18 | + var description string |
| 19 | + if username != "" { |
| 20 | + description = fmt.Sprintf("Your BoardGameGeek collection (user: %s). Shows all owned games with their ratings, play counts, and status.", username) |
| 21 | + } else { |
| 22 | + description = "Your BoardGameGeek collection. Requires BGG_USERNAME environment variable to be set." |
| 23 | + } |
| 24 | + |
| 25 | + resource := mcp.NewResource( |
| 26 | + "bgg://my-collection", |
| 27 | + "My BGG Collection", |
| 28 | + mcp.WithResourceDescription(description), |
| 29 | + mcp.WithMIMEType("application/json"), |
| 30 | + ) |
| 31 | + |
| 32 | + handler := func(ctx context.Context, request mcp.ReadResourceRequest) ([]mcp.ResourceContents, error) { |
| 33 | + if username == "" { |
| 34 | + return nil, fmt.Errorf("BGG_USERNAME environment variable not set") |
| 35 | + } |
| 36 | + |
| 37 | + result, err := collection.Query(client, username, collection.WithOwned(true)) |
| 38 | + if err != nil { |
| 39 | + return nil, fmt.Errorf("error fetching collection: %v", err) |
| 40 | + } |
| 41 | + |
| 42 | + if len(result.Items) == 0 { |
| 43 | + return []mcp.ResourceContents{ |
| 44 | + &mcp.TextResourceContents{ |
| 45 | + URI: "bgg://my-collection", |
| 46 | + MIMEType: "application/json", |
| 47 | + Text: "[]", |
| 48 | + }, |
| 49 | + }, nil |
| 50 | + } |
| 51 | + |
| 52 | + out, err := json.Marshal(result.Items) |
| 53 | + if err != nil { |
| 54 | + return nil, fmt.Errorf("error formatting results: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + return []mcp.ResourceContents{ |
| 58 | + &mcp.TextResourceContents{ |
| 59 | + URI: "bgg://my-collection", |
| 60 | + MIMEType: "application/json", |
| 61 | + Text: string(out), |
| 62 | + }, |
| 63 | + }, nil |
| 64 | + } |
| 65 | + |
| 66 | + return resource, handler |
| 67 | +} |
0 commit comments