Skip to main content
Glama

get_weight

Retrieve weight data from Fitbit for a specified time period. Use this tool to access historical weight entries by providing a period parameter like '7d' or '1y'.

Instructions

Get the raw JSON response for weight entries from Fitbit for a specified period ending today. Requires a 'period' parameter such as '1d', '7d', '30d', '3m', '6m', '1y'

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
periodYesThe time period for which to retrieve data

Implementation Reference

  • The handler function that executes the get_weight tool logic: constructs the Fitbit API endpoint for body weight data over a given period and invokes handleFitbitApiCall to fetch, extract, and format the response.
    handler: async ({ period }: WeightParams) => {
      const endpoint = `/body/weight/date/today/${period}.json`;
      
      return handleFitbitApiCall<WeightTimeSeriesResponse, WeightParams>(
        endpoint,
        { period },
        getAccessTokenFn,
        {
          successDataExtractor: (data) => data['body-weight'] || [],
          noDataMessage: `the period '${period}'`,
          errorContext: `period '${period}'`
        }
      );
    }
  • src/weight.ts:33-53 (registration)
    Registration of the get_weight tool via registerTool, specifying name, description, input schema (period parameter), and handler function.
    registerTool(server, {
      name: 'get_weight',
      description: "Get the raw JSON response for weight entries from Fitbit for a specified period ending today. Requires a 'period' parameter such as '1d', '7d', '30d', '3m', '6m', '1y'",
      parametersSchema: {
        period: CommonSchemas.period,
      },
      handler: async ({ period }: WeightParams) => {
        const endpoint = `/body/weight/date/today/${period}.json`;
        
        return handleFitbitApiCall<WeightTimeSeriesResponse, WeightParams>(
          endpoint,
          { period },
          getAccessTokenFn,
          {
            successDataExtractor: (data) => data['body-weight'] || [],
            noDataMessage: `the period '${period}'`,
            errorContext: `period '${period}'`
          }
        );
      }
    });
  • src/index.ts:77-77 (registration)
    Top-level call to registerWeightTool, which sets up the get_weight tool on the MCP server instance.
    registerWeightTool(server, getAccessToken);
  • TypeScript interfaces defining the structure of Fitbit weight time series data used in the tool's handler.
    interface WeightTimeSeriesEntry {
      dateTime: string; // Date (and potentially time) of the entry
      value: string; // Weight value as a string
    }
    
    // Represents the structure of the response from the Fitbit Time Series API for weight
    interface WeightTimeSeriesResponse {
      'body-weight': WeightTimeSeriesEntry[]; // Array of weight entries
    }
  • Shared helper function handleFitbitApiCall used by the get_weight handler to perform the API request, handle authentication, errors, no-data cases, and format the MCP tool response.
    export async function handleFitbitApiCall<TResponse, TParams>(
      endpoint: string,
      params: TParams,
      getAccessTokenFn: () => Promise<string | null>,
      options: {
        apiBase?: string;
        successDataExtractor?: (data: TResponse) => unknown[] | null;
        noDataMessage?: string;
        errorContext?: string;
      } = {}
    ): Promise<ToolResponseStructure> {
      const {
        apiBase = FITBIT_API_VERSIONS.V1,
        successDataExtractor,
        noDataMessage,
        errorContext = JSON.stringify(params)
      } = options;
    
      const responseData = await makeFitbitRequest<TResponse>(
        endpoint,
        getAccessTokenFn,
        apiBase
      );
    
      if (!responseData) {
        return createErrorResponse(
          `${ERROR_MESSAGES.API_REQUEST_FAILED} for ${errorContext}. ${ERROR_MESSAGES.CHECK_TOKEN_PERMISSIONS}.`
        );
      }
    
      // Check for empty data if extractor provided
      if (successDataExtractor) {
        const extractedData = successDataExtractor(responseData);
        if (!extractedData || extractedData.length === 0) {
          return createNoDataResponse(noDataMessage || errorContext);
        }
      }
    
      return createSuccessResponse(responseData);
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/TheDigitalNinja/mcp-fitbit'

If you have feedback or need assistance with the MCP directory API, please join our Discord server