Skip to main content
Glama

get_food_log

Retrieve detailed nutrition data from Fitbit food logs for a specific date, including calories, protein, carbs, fat, fiber, sodium, daily totals, and individual food entries.

Instructions

Get comprehensive nutrition data (calories, protein, carbs, fat, fiber, sodium) from Fitbit food log for a specific date. Returns daily summary totals and individual food entries with nutritional values.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
dateNoThe date for which to retrieve food log data (YYYY-MM-DD or 'today'). Defaults to 'today'.

Implementation Reference

  • The async handler function for the 'get_food_log' tool. It constructs the Fitbit API endpoint for food log on the specified date, fetches the data using makeFitbitRequest, handles errors, and returns the JSON response.
    async ({ date = 'today', }: FoodLogParams): Promise<ToolResponseStructure> => { // Construct the endpoint const endpoint = `foods/log/date/${date}.json`; const foodLogData = await makeFitbitRequest<FoodLogResponse>( endpoint, getAccessTokenFn, FITBIT_API_BASE ); // Handle API call failure if (!foodLogData) { return { content: [ { type: 'text', text: `Failed to retrieve food log data from Fitbit API for date '${date}'. Check token and permissions.`, }, ], isError: true, }; } // Return successful response with raw JSON const rawJsonResponse = JSON.stringify(foodLogData, null, 2); return { content: [{ type: 'text', text: rawJsonResponse }], }; }
  • Zod schema defining the input parameters for the 'get_food_log' tool, specifically the optional 'date' parameter.
    const foodLogParametersSchemaShape = { date: z .string() .regex( /^\d{4}-\d{2}-\d{2}$|^today$/, "Date must be in YYYY-MM-DD format or 'today'." ) .optional() .describe( "The date for which to retrieve food log data (YYYY-MM-DD or 'today'). Defaults to 'today'." ), };
  • Registers the 'get_food_log' tool on the MCP server using server.tool(), providing name, description, input schema, and handler function.
    server.tool( foodLogToolName, foodLogDescription, foodLogParametersSchemaShape, async ({ date = 'today', }: FoodLogParams): Promise<ToolResponseStructure> => { // Construct the endpoint const endpoint = `foods/log/date/${date}.json`; const foodLogData = await makeFitbitRequest<FoodLogResponse>( endpoint, getAccessTokenFn, FITBIT_API_BASE ); // Handle API call failure if (!foodLogData) { return { content: [ { type: 'text', text: `Failed to retrieve food log data from Fitbit API for date '${date}'. Check token and permissions.`, }, ], isError: true, }; } // Return successful response with raw JSON const rawJsonResponse = JSON.stringify(foodLogData, null, 2); return { content: [{ type: 'text', text: rawJsonResponse }], }; } );
  • Shared helper function used by the handler to make authenticated requests to the Fitbit API.
    export async function makeFitbitRequest<T>( endpoint: string, getAccessTokenFn: () => Promise<string | null>, apiBase: string = FITBIT_API_VERSIONS.V1 ): Promise<T | null> { const currentAccessToken = await getAccessTokenFn(); if (!currentAccessToken) { console.error(`Error: ${ERROR_MESSAGES.NO_ACCESS_TOKEN}`); return null; } // Ensure endpoint starts correctly relative to the user path const cleanEndpoint = endpoint.startsWith('/') ? endpoint.substring(1) : endpoint; // Construct the full URL including the user scope '-'. const url = `${apiBase}/user/-/${cleanEndpoint}`; console.error(`Attempting Fitbit API request to: ${url}`); const headers = { 'User-Agent': HTTP_CONFIG.USER_AGENT, Authorization: `Bearer ${currentAccessToken}`, Accept: 'application/json', }; try { const response = await fetch(url, { headers }); if (!response.ok) { const errorBody = await response.text(); console.error( `Fitbit API Error! Status: ${response.status}, URL: ${url}, Body: ${errorBody}` ); if (response.status === 401) { console.error(ERROR_MESSAGES.TOKEN_EXPIRED); } return null; } // Handle potential empty response body for certain success statuses (e.g., 204 No Content) if (response.status === 204) { return {} as T; // Return an empty object or appropriate type for no content } return (await response.json()) as T; } catch (error) { console.error(`Error making Fitbit request to ${url}:`, error); return null; } }
  • src/index.ts:82-82 (registration)
    Top-level call to registerNutritionTools, which includes the registration of 'get_food_log'.
    registerNutritionTools(server, getAccessToken);

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