write-data
Write newline-delimited line protocol records into an InfluxDB bucket for time-series data ingestion with optional timestamp precision.
Instructions
Stream newline-delimited line protocol records into a bucket. Use this after composing measurements so the LLM can insert real telemetry, optionally controlling timestamp precision.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| org | Yes | Human-readable organization name that owns the destination bucket (the same value returned by the orgs resource). | |
| bucket | Yes | Bucket name to receive the points. Make sure it already exists or call create-bucket first. | |
| data | Yes | Payload containing one or more line protocol lines (measurements, tags, fields, timestamps) separated by newlines. | |
| precision | No | Optional timestamp precision. Provide it only when the line protocol omits unit suffix context; defaults to nanoseconds. |
Implementation Reference
- src/handlers/writeDataTool.js:5-58 (handler)The handler function for the 'write-data' tool. Accepts org, bucket, data, and precision parameters. Makes a POST request to the InfluxDB /api/v2/write endpoint with the line protocol data as the body.
export async function writeData({ org, bucket, data, precision }) { // Add extremely clear logging console.log(`=== WRITE-DATA TOOL CALLED ===`); console.log( `Writing to org: ${org}, bucket: ${bucket}, data length: ${data.length}`, ); try { // Simplified approach focusing on core functionality let endpoint = `/api/v2/write?org=${encodeURIComponent(org)}&bucket=${encodeURIComponent(bucket) }`; if (precision) { endpoint += `&precision=${precision}`; } console.log(`Write URL: ${INFLUXDB_URL}${endpoint}`); // Use fetch directly instead of our wrapper to eliminate any potential issues const response = await fetch(`${INFLUXDB_URL}${endpoint}`, { method: "POST", headers: { "Content-Type": "text/plain; charset=utf-8", "Authorization": `Token ${INFLUXDB_TOKEN}`, }, body: data, }); console.log(`Write response status: ${response.status}`); if (!response.ok) { const errorText = await response.text(); throw new Error( `Failed to write data: ${response.status} ${errorText}`, ); } console.log(`=== WRITE-DATA TOOL COMPLETED SUCCESSFULLY ===`); return { content: [{ type: "text", text: "Data written successfully", }], }; } catch (error) { console.error(`=== WRITE-DATA TOOL ERROR: ${error.message} ===`); return { content: [{ type: "text", text: `Error writing data: ${error.message}`, }], isError: true, }; } } - src/index.js:78-100 (schema)Zod schema definitions for the 'write-data' tool parameters: org (string), bucket (string), data (string), and precision (optional enum: ns, us, ms, s).
{ org: z .string() .describe( "Human-readable organization name that owns the destination bucket (the same value returned by the orgs resource).", ), bucket: z .string() .describe( "Bucket name to receive the points. Make sure it already exists or call create-bucket first.", ), data: z .string() .describe( "Payload containing one or more line protocol lines (measurements, tags, fields, timestamps) separated by newlines.", ), precision: z .enum(["ns", "us", "ms", "s"]) .optional() .describe( "Optional timestamp precision. Provide it only when the line protocol omits unit suffix context; defaults to nanoseconds.", ), }, - src/index.js:74-102 (registration)Registration of the 'write-data' tool with the MCP server using server.tool(), including its description, schema, and mapping to the writeData handler.
// Register tools server.tool( "write-data", "Stream newline-delimited line protocol records into a bucket. Use this after composing measurements so the LLM can insert real telemetry, optionally controlling timestamp precision.", { org: z .string() .describe( "Human-readable organization name that owns the destination bucket (the same value returned by the orgs resource).", ), bucket: z .string() .describe( "Bucket name to receive the points. Make sure it already exists or call create-bucket first.", ), data: z .string() .describe( "Payload containing one or more line protocol lines (measurements, tags, fields, timestamps) separated by newlines.", ), precision: z .enum(["ns", "us", "ms", "s"]) .optional() .describe( "Optional timestamp precision. Provide it only when the line protocol omits unit suffix context; defaults to nanoseconds.", ), }, writeData, ); - src/handlers/writeDataTool.js:1-2 (helper)Imports: 'fetch' from node-fetch for HTTP calls, and INFLUXDB_TOKEN/INFLUXDB_URL from the environment config.
import fetch from "node-fetch"; import { INFLUXDB_TOKEN, INFLUXDB_URL } from "../config/env.js";