Initial commit: smartrun MVP
Garmin run classification and progression tracker. Go backend (MCP client to mcp-garmin, SQLite store, deterministic rule engine, REST API) and React/TS frontend (Dashboard, Review Queue, Workout Kinds). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
265
backend/internal/garmin/client.go
Normal file
265
backend/internal/garmin/client.go
Normal file
@@ -0,0 +1,265 @@
|
||||
// Package garmin wraps the mcp-garmin MCP server as a narrow Go client
|
||||
// interface, so the rest of smartrun never deals with MCP/JSON-RPC directly.
|
||||
package garmin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
mcpclient "github.com/mark3labs/mcp-go/client"
|
||||
"github.com/mark3labs/mcp-go/client/transport"
|
||||
"github.com/mark3labs/mcp-go/mcp"
|
||||
)
|
||||
|
||||
// Client is the interface the rest of smartrun depends on. The real
|
||||
// implementation drives mcp-garmin over stdio; internal/garmin/mock provides
|
||||
// a fake for tests and frontend-only development.
|
||||
type Client interface {
|
||||
// Authenticate triggers Garmin login using credentials the subprocess
|
||||
// was started with. Spawns the subprocess on first call.
|
||||
Authenticate(ctx context.Context) (AuthResult, error)
|
||||
// CompleteMFA submits an MFA code for a login started by Authenticate.
|
||||
CompleteMFA(ctx context.Context, code string) (AuthResult, error)
|
||||
// GetActivities lists activities between start and end (YYYY-MM-DD).
|
||||
GetActivities(ctx context.Context, startDate, endDate string, limit int) ([]Activity, error)
|
||||
// GetActivitySplits fetches lap/split summaries for one activity.
|
||||
GetActivitySplits(ctx context.Context, activityID int64) (ActivitySplits, error)
|
||||
// GetActivityDetails fetches raw per-second telemetry for one activity.
|
||||
GetActivityDetails(ctx context.Context, activityID int64) (ActivityDetails, error)
|
||||
// Close terminates the subprocess, if running.
|
||||
Close() error
|
||||
}
|
||||
|
||||
// Config configures how the mcp-garmin subprocess is spawned.
|
||||
type Config struct {
|
||||
PythonPath string // path to mcp-garmin's venv python executable
|
||||
ServerPath string // path to mcp-garmin's server.py
|
||||
GarminEmail string
|
||||
GarminPassword string
|
||||
// TokenStorePath, if set, is passed as GARMIN_TOKENSTORE so mcp-garmin
|
||||
// persists/resumes a Garmin session there instead of the default ~/.garth.
|
||||
TokenStorePath string
|
||||
}
|
||||
|
||||
// mcpClient is the real Client implementation, backed by an mcp-garmin
|
||||
// subprocess spoken to over stdio MCP.
|
||||
type mcpClient struct {
|
||||
cfg Config
|
||||
|
||||
mu sync.Mutex // serializes calls; mcp-garmin holds a single shared Garmin client
|
||||
inner *mcpclient.Client
|
||||
started bool
|
||||
}
|
||||
|
||||
// NewClient builds a Client. The subprocess is not spawned until the first
|
||||
// call that needs it (Authenticate, or any data call once authenticated).
|
||||
func NewClient(cfg Config) Client {
|
||||
return &mcpClient{cfg: cfg}
|
||||
}
|
||||
|
||||
func (c *mcpClient) ensureStarted(ctx context.Context) error {
|
||||
if c.started {
|
||||
return nil
|
||||
}
|
||||
|
||||
env := []string{
|
||||
"GARMIN_EMAIL=" + c.cfg.GarminEmail,
|
||||
"GARMIN_PASSWORD=" + c.cfg.GarminPassword,
|
||||
"PYTHONUNBUFFERED=1",
|
||||
}
|
||||
if c.cfg.TokenStorePath != "" {
|
||||
env = append(env, "GARMIN_TOKENSTORE="+c.cfg.TokenStorePath)
|
||||
}
|
||||
|
||||
inner, err := mcpclient.NewStdioMCPClient(c.cfg.PythonPath, env, c.cfg.ServerPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("spawn mcp-garmin subprocess: %w", err)
|
||||
}
|
||||
|
||||
if stdio, ok := inner.GetTransport().(*transport.Stdio); ok {
|
||||
go drainStderr(stdio)
|
||||
}
|
||||
|
||||
initReq := mcp.InitializeRequest{}
|
||||
initReq.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
|
||||
initReq.Params.ClientInfo = mcp.Implementation{Name: "smartrund", Version: "0.0.1"}
|
||||
if _, err := inner.Initialize(ctx, initReq); err != nil {
|
||||
inner.Close()
|
||||
return fmt.Errorf("mcp initialize handshake: %w", err)
|
||||
}
|
||||
|
||||
c.inner = inner
|
||||
c.started = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// drainStderr forwards the subprocess's debug/log output so it isn't
|
||||
// silently dropped (mcp-garmin logs auth/rate-limit diagnostics there).
|
||||
func drainStderr(stdio *transport.Stdio) {
|
||||
buf := make([]byte, 4096)
|
||||
for {
|
||||
n, err := stdio.Stderr().Read(buf)
|
||||
if n > 0 {
|
||||
fmt.Print(string(buf[:n]))
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *mcpClient) callTool(ctx context.Context, name string, args map[string]any) (string, error) {
|
||||
req := mcp.CallToolRequest{}
|
||||
req.Params.Name = name
|
||||
req.Params.Arguments = args
|
||||
|
||||
res, err := c.inner.CallTool(ctx, req)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("call tool %s: %w", name, err)
|
||||
}
|
||||
if res.IsError {
|
||||
return "", fmt.Errorf("tool %s returned an error result", name)
|
||||
}
|
||||
var out strings.Builder
|
||||
for _, content := range res.Content {
|
||||
if tc, ok := content.(mcp.TextContent); ok {
|
||||
out.WriteString(tc.Text)
|
||||
}
|
||||
}
|
||||
return out.String(), nil
|
||||
}
|
||||
|
||||
func (c *mcpClient) Authenticate(ctx context.Context) (AuthResult, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
if err := c.ensureStarted(ctx); err != nil {
|
||||
return AuthResult{}, err
|
||||
}
|
||||
msg, err := c.callTool(ctx, "authenticate", nil)
|
||||
if err != nil {
|
||||
return AuthResult{}, err
|
||||
}
|
||||
return parseAuthResult(msg), nil
|
||||
}
|
||||
|
||||
func (c *mcpClient) CompleteMFA(ctx context.Context, code string) (AuthResult, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
if err := c.ensureStarted(ctx); err != nil {
|
||||
return AuthResult{}, err
|
||||
}
|
||||
msg, err := c.callTool(ctx, "complete_mfa", map[string]any{"code": code})
|
||||
if err != nil {
|
||||
return AuthResult{}, err
|
||||
}
|
||||
return parseAuthResult(msg), nil
|
||||
}
|
||||
|
||||
func parseAuthResult(msg string) AuthResult {
|
||||
switch {
|
||||
case strings.Contains(msg, "Authenticated successfully") || strings.Contains(msg, "MFA accepted"):
|
||||
return AuthResult{Status: AuthSuccess, Message: msg}
|
||||
case strings.Contains(msg, "MFA required"):
|
||||
return AuthResult{Status: AuthMFARequired, Message: msg}
|
||||
default:
|
||||
return AuthResult{Status: AuthFailed, Message: msg}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *mcpClient) GetActivities(ctx context.Context, startDate, endDate string, limit int) ([]Activity, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
if err := c.ensureStarted(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
msg, err := c.callTool(ctx, "get_activities", map[string]any{
|
||||
"start_date": startDate,
|
||||
"end_date": endDate,
|
||||
"limit": limit,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var rawActivities []json.RawMessage
|
||||
if err := json.Unmarshal([]byte(msg), &rawActivities); err != nil {
|
||||
return nil, fmt.Errorf("get_activities did not return a JSON array (%q): %w", truncate(msg, 200), err)
|
||||
}
|
||||
|
||||
activities := make([]Activity, 0, len(rawActivities))
|
||||
for _, raw := range rawActivities {
|
||||
var a Activity
|
||||
if err := json.Unmarshal(raw, &a); err != nil {
|
||||
return nil, fmt.Errorf("parse activity: %w", err)
|
||||
}
|
||||
a.Raw = raw
|
||||
activities = append(activities, a)
|
||||
}
|
||||
return activities, nil
|
||||
}
|
||||
|
||||
func (c *mcpClient) GetActivitySplits(ctx context.Context, activityID int64) (ActivitySplits, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
if err := c.ensureStarted(ctx); err != nil {
|
||||
return ActivitySplits{}, err
|
||||
}
|
||||
msg, err := c.callTool(ctx, "get_activity_splits", map[string]any{
|
||||
"activity_id": strconv.FormatInt(activityID, 10),
|
||||
})
|
||||
if err != nil {
|
||||
return ActivitySplits{}, err
|
||||
}
|
||||
|
||||
var splits ActivitySplits
|
||||
if err := json.Unmarshal([]byte(msg), &splits); err != nil {
|
||||
return ActivitySplits{}, fmt.Errorf("get_activity_splits did not return valid JSON (%q): %w", truncate(msg, 200), err)
|
||||
}
|
||||
return splits, nil
|
||||
}
|
||||
|
||||
func (c *mcpClient) GetActivityDetails(ctx context.Context, activityID int64) (ActivityDetails, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
if err := c.ensureStarted(ctx); err != nil {
|
||||
return ActivityDetails{}, err
|
||||
}
|
||||
msg, err := c.callTool(ctx, "get_activity_details", map[string]any{
|
||||
"activity_id": strconv.FormatInt(activityID, 10),
|
||||
})
|
||||
if err != nil {
|
||||
return ActivityDetails{}, err
|
||||
}
|
||||
|
||||
var details ActivityDetails
|
||||
if err := json.Unmarshal([]byte(msg), &details); err != nil {
|
||||
return ActivityDetails{}, fmt.Errorf("get_activity_details did not return valid JSON (%q): %w", truncate(msg, 200), err)
|
||||
}
|
||||
return details, nil
|
||||
}
|
||||
|
||||
func (c *mcpClient) Close() error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
if !c.started {
|
||||
return nil
|
||||
}
|
||||
return c.inner.Close()
|
||||
}
|
||||
|
||||
func truncate(s string, n int) string {
|
||||
if len(s) <= n {
|
||||
return s
|
||||
}
|
||||
return s[:n] + "...(truncated)"
|
||||
}
|
||||
Reference in New Issue
Block a user