Skip to main content
Glama
therealsachin

Langfuse MCP Server

get_daily_metrics

Retrieve daily usage trends and patterns from Langfuse analytics for specified date ranges, with optional environment filtering and missing data handling.

Instructions

Daily usage trends and patterns.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
fromYesStart timestamp (ISO 8601)
toYesEnd timestamp (ISO 8601)
environmentNoOptional environment filter
fillMissingDaysNoFill missing days with zero values (default: true)

Implementation Reference

  • The main handler function that fetches daily metrics from Langfuse client, processes and filters data by date range, calculates aggregates like total tokens, costs, averages, fills missing days if requested, and returns formatted JSON response.
    export async function getDailyMetrics(
      client: LangfuseAnalyticsClient,
      args: z.infer<typeof getDailyMetricsSchema>
    ) {
      try {
        // Use the working getDailyMetrics API directly (same approach as cost_analysis)
        const dailyResponse = await client.getDailyMetrics({
          tags: args.environment ? [`environment:${args.environment}`] : undefined,
        });
    
        const dailyData: any[] = [];
    
        if (dailyResponse.data && Array.isArray(dailyResponse.data)) {
          // Filter by date range
          const fromDate = new Date(args.from);
          const toDate = new Date(args.to);
    
          const filteredData = dailyResponse.data.filter((day: any) => {
            const dayDate = new Date(day.date);
            return dayDate >= fromDate && dayDate <= toDate;
          });
    
          // Process each day's data
          filteredData.forEach((day: any) => {
            // Calculate total tokens from usage breakdown
            let totalTokens = 0;
            let totalObservations = 0;
            if (day.usage && Array.isArray(day.usage)) {
              totalTokens = day.usage.reduce((sum: number, usage: any) => {
                return sum + (usage.totalUsage || usage.inputUsage + usage.outputUsage || 0);
              }, 0);
              totalObservations = day.usage.reduce((sum: number, usage: any) => {
                return sum + (usage.countObservations || 0);
              }, 0);
            }
    
            dailyData.push({
              date: day.date,
              totalCost: day.totalCost || 0,
              totalTokens: totalTokens,
              totalTraces: day.countTraces || 0,
              totalObservations: totalObservations || day.countObservations || 0,
              avgCostPerTrace: (day.countTraces || 0) > 0
                ? Math.round(((day.totalCost || 0) / (day.countTraces || 0)) * 10000) / 10000
                : 0,
              avgTokensPerTrace: (day.countTraces || 0) > 0
                ? Math.round((totalTokens / (day.countTraces || 0)) * 100) / 100
                : 0,
            });
          });
    
          // Fill in missing days if requested
          if (args.fillMissingDays) {
            const startDate = new Date(args.from);
            const endDate = new Date(args.to);
            const dataMap = new Map(dailyData.map(d => [d.date, d]));
    
            dailyData.length = 0; // Clear array
    
            for (let date = new Date(startDate); date <= endDate; date.setDate(date.getDate() + 1)) {
              const dateStr = date.toISOString().split('T')[0];
              const existingData = dataMap.get(dateStr);
    
              if (existingData) {
                dailyData.push(existingData);
              } else {
                // Fill missing day with zeros
                dailyData.push({
                  date: dateStr,
                  totalCost: 0,
                  totalTokens: 0,
                  totalTraces: 0,
                  totalObservations: 0,
                  avgCostPerTrace: 0,
                  avgTokensPerTrace: 0,
                });
              }
            }
          }
    
          // Sort by date
          dailyData.sort((a, b) => a.date.localeCompare(b.date));
        }
    
        // Return the successful result
        const result: DailyMetrics = {
          projectId: client.getProjectId(),
          from: args.from,
          to: args.to,
          dailyData,
        };
    
        return {
          content: [
            {
              type: 'text' as const,
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      } catch (error) {
        return {
          content: [
            {
              type: 'text' as const,
              text: JSON.stringify({
                error: 'Failed to get daily metrics',
                message: error instanceof Error ? error.message : 'Unknown error',
                projectId: client.getProjectId(),
                from: args.from,
                to: args.to,
              }, null, 2),
            },
          ],
          isError: true,
        };
      }
    }
  • Zod schema defining input parameters: from/to datetimes (required), optional environment filter, and fillMissingDays boolean (default true).
    export const getDailyMetricsSchema = z.object({
      from: z.string().datetime(),
      to: z.string().datetime(),
      environment: z.string().optional(),
      fillMissingDays: z.boolean().default(true),
    });
  • src/index.ts:1056-1059 (registration)
    Registration in the CallToolRequestSchema handler switch statement: parses arguments with schema and calls the getDailyMetrics handler function.
    case 'get_daily_metrics': {
      const args = getDailyMetricsSchema.parse(request.params.arguments);
      return await getDailyMetrics(this.client, args);
    }
  • src/index.ts:497-524 (registration)
    Tool registration in ListToolsRequestSchema handler: defines name, description, and inputSchema for discovery.
    {
      name: 'get_daily_metrics',
      description: 'Daily usage trends and patterns.',
      inputSchema: {
        type: 'object',
        properties: {
          from: {
            type: 'string',
            format: 'date-time',
            description: 'Start timestamp (ISO 8601)',
          },
          to: {
            type: 'string',
            format: 'date-time',
            description: 'End timestamp (ISO 8601)',
          },
          environment: {
            type: 'string',
            description: 'Optional environment filter',
          },
          fillMissingDays: {
            type: 'boolean',
            description: 'Fill missing days with zero values (default: true)',
          },
        },
        required: ['from', 'to'],
      },
    },
  • src/index.ts:63-63 (registration)
    Import of the handler function and schema from the tools module.
    import { getDailyMetrics, getDailyMetricsSchema } from './tools/get-daily-metrics.js';
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure but offers minimal information. It mentions 'daily' trends but doesn't specify time granularity, data format, pagination, rate limits, authentication requirements, or whether the operation is read-only. The description doesn't contradict annotations (none exist), but it fails to provide essential behavioral context for a tool that presumably returns time-series data.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is extremely concise at just five words, which is efficient for a simple concept. However, it's arguably too brief given the tool's complexity (4 parameters, no annotations, no output schema). While there's no wasted language, the description may be under-specified rather than optimally concise.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a tool with 4 parameters, no annotations, no output schema, and multiple similar sibling tools, the description is insufficiently complete. It doesn't explain what metrics are returned, in what format, with what time granularity, or how it differs from related tools. The agent would need to guess about the tool's behavior and appropriate use cases based on this minimal description.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The description adds no parameter-specific information beyond what's already documented in the schema (which has 100% coverage). While the description implies date-range functionality through 'daily trends,' it doesn't explain parameter relationships, default behaviors, or provide examples. With complete schema documentation, the baseline score of 3 is appropriate since the schema already provides adequate parameter information.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose3/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description 'Daily usage trends and patterns' indicates the tool provides trend data but is vague about what specific metrics it returns. It mentions 'usage' but doesn't specify whether this refers to API calls, compute resources, user activity, or other metrics. It doesn't clearly distinguish from siblings like 'get_metrics', 'usage_by_model', or 'usage_by_service' which also appear to provide usage-related data.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. With multiple sibling tools that appear related to metrics and usage (get_metrics, usage_by_model, usage_by_service, top_expensive_traces), there's no indication of what makes this tool unique or when it should be preferred over those alternatives. The description offers no context about appropriate use cases or exclusions.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/therealsachin/langfuse-mcp'

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