Skip to main content
Glama

get_nutrition_by_date_range

Retrieve nutrition data like calories, water, protein, carbs, fat, fiber, or sodium from Fitbit for a specific date range to analyze dietary patterns and track nutritional intake over time.

Instructions

Get the raw JSON response for nutrition data from Fitbit for a specific resource and date range. Requires 'resource' parameter (caloriesIn, water), 'startDate' and 'endDate' parameters in 'YYYY-MM-DD' format. Note: The API enforces a maximum range of 1,095 days.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
resourceYesThe nutrition resource to retrieve data for.
startDateYesThe start date for which to retrieve nutrition data (YYYY-MM-DD).
endDateYesThe end date for which to retrieve nutrition data (YYYY-MM-DD).

Implementation Reference

  • The core handler function for the 'get_nutrition_by_date_range' tool. It constructs the Fitbit API endpoint for the specified nutrition resource and date range, fetches the data using makeFitbitRequest, handles errors and empty results, and returns the raw JSON response.
    async ({
      resource,
      startDate,
      endDate,
    }: NutritionRangeParams): Promise<ToolResponseStructure> => {
      // Construct the endpoint dynamically
      const endpoint = `foods/log/${resource}/date/${startDate}/${endDate}.json`;
    
      // Make the request
      const nutritionData =
        await makeFitbitRequest<NutritionTimeSeriesResponse>(
          endpoint,
          getAccessTokenFn,
          FITBIT_API_BASE
        );
    
      // Handle API call failure
      if (!nutritionData) {
        return {
          content: [
            {
              type: 'text',
              text: `Failed to retrieve nutrition data from Fitbit API for resource '${resource}' and the date range '${startDate}' to '${endDate}'. Check token, permissions, date format, and ensure the range is 1,095 days or less.`,
            },
          ],
          isError: true,
        };
      }
    
      // Handle no data found
      const resourceKey = `foods-log-${resource}`;
      const nutritionEntries = nutritionData[resourceKey] || [];
      if (nutritionEntries.length === 0) {
        return {
          content: [
            {
              type: 'text',
              text: `No nutrition data found for resource '${resource}' and the date range '${startDate}' to '${endDate}'.`,
            },
          ],
        };
      }
    
      // Return successful response
      const rawJsonResponse = JSON.stringify(nutritionData, null, 2);
      return {
        content: [{ type: 'text', text: rawJsonResponse }],
      };
    }
  • Zod input schema for the tool parameters: resource (enum of nutrition types), startDate and endDate (YYYY-MM-DD format strings).
    const rangeParametersSchemaShape = {
      resource: z
        .enum([
          'caloriesIn',
          'water',
          'protein',
          'carbs',
          'fat',
          'fiber',
          'sodium',
        ])
        .describe('The nutrition resource to retrieve data for.'),
      startDate: z
        .string()
        .regex(/^\d{4}-\d{2}-\d{2}$/, 'Start date must be in YYYY-MM-DD format.')
        .describe(
          'The start date for which to retrieve nutrition data (YYYY-MM-DD).'
        ),
      endDate: z
        .string()
        .regex(/^\d{4}-\d{2}-\d{2}$/, 'End date must be in YYYY-MM-DD format.')
        .describe(
          'The end date for which to retrieve nutrition data (YYYY-MM-DD).'
        ),
    };
  • The server.tool registration call for 'get_nutrition_by_date_range', including name, description, schema reference, and inline handler function. This occurs within the registerNutritionTools function.
    server.tool(
      rangeToolName,
      rangeDescription,
      rangeParametersSchemaShape,
      async ({
        resource,
        startDate,
        endDate,
      }: NutritionRangeParams): Promise<ToolResponseStructure> => {
        // Construct the endpoint dynamically
        const endpoint = `foods/log/${resource}/date/${startDate}/${endDate}.json`;
    
        // Make the request
        const nutritionData =
          await makeFitbitRequest<NutritionTimeSeriesResponse>(
            endpoint,
            getAccessTokenFn,
            FITBIT_API_BASE
          );
    
        // Handle API call failure
        if (!nutritionData) {
          return {
            content: [
              {
                type: 'text',
                text: `Failed to retrieve nutrition data from Fitbit API for resource '${resource}' and the date range '${startDate}' to '${endDate}'. Check token, permissions, date format, and ensure the range is 1,095 days or less.`,
              },
            ],
            isError: true,
          };
        }
    
        // Handle no data found
        const resourceKey = `foods-log-${resource}`;
        const nutritionEntries = nutritionData[resourceKey] || [];
        if (nutritionEntries.length === 0) {
          return {
            content: [
              {
                type: 'text',
                text: `No nutrition data found for resource '${resource}' and the date range '${startDate}' to '${endDate}'.`,
              },
            ],
          };
        }
    
        // Return successful response
        const rawJsonResponse = JSON.stringify(nutritionData, null, 2);
        return {
          content: [{ type: 'text', text: rawJsonResponse }],
        };
      }
    );
  • src/index.ts:82-82 (registration)
    Invocation of registerNutritionTools in the main index.ts, which registers the nutrition tools including get_nutrition_by_date_range.
    registerNutritionTools(server, getAccessToken);
  • TypeScript type definition for the NutritionRangeParams used in the tool handler and schema.
    type NutritionRangeParams = {
      resource:
        | 'caloriesIn'
        | 'water'
        | 'protein'
        | 'carbs'
        | 'fat'
        | 'fiber'
        | 'sodium';
      startDate: string;
      endDate: string;
    };

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