write-data
Write time-series data into InfluxDB using line protocol format, specifying organization, bucket, and timestamp precision for efficient data storage.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| bucket | Yes | The bucket name | |
| data | Yes | Data in InfluxDB line protocol format | |
| org | Yes | The organization name | |
| precision | No | Timestamp precision (ns, us, ms, s) |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"bucket": {
"description": "The bucket name",
"type": "string"
},
"data": {
"description": "Data in InfluxDB line protocol format",
"type": "string"
},
"org": {
"description": "The organization name",
"type": "string"
},
"precision": {
"description": "Timestamp precision (ns, us, ms, s)",
"enum": [
"ns",
"us",
"ms",
"s"
],
"type": "string"
}
},
"required": [
"org",
"bucket",
"data"
],
"type": "object"
}
Implementation Reference
- src/handlers/writeDataTool.js:5-58 (handler)The primary handler function that executes the 'write-data' tool. It sends the provided line protocol data to the InfluxDB write API endpoint using fetch, handles errors, and returns success or error responses.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:76-102 (registration)Registers the 'write-data' tool with the MCP server, including the tool name, description, input schema using Zod, and reference to the handler function."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/index.js:79-102 (schema)The Zod schema defining the input parameters for the 'write-data' tool: org (string), bucket (string), data (string), precision (optional enum).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, );