gaia-vault-mcp
Version:
Azure Blob Storage MCP server for AI assistants
86 lines (85 loc) • 3.44 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const storage_blob_1 = require("@azure/storage-blob");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const env_1 = require("../config/env");
exports.default = async (containerName, blobName, options) => {
const { connectionString } = (0, env_1.getAzureConfig)();
// Create BlobServiceClient
const blobServiceClient = storage_blob_1.BlobServiceClient.fromConnectionString(connectionString);
// Get container client
const containerClient = blobServiceClient.getContainerClient(containerName);
// Get block blob client
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
// Check if blob exists
const exists = await blockBlobClient.exists();
if (!exists) {
throw new Error(`Blob ${blobName} does not exist in container ${containerName}`);
}
if (!options.pathFile && !options.asText) {
throw new Error("Either pathFile or asText must be provided");
}
if (options.pathFile) {
// Ensure directory exists
const dir = path.dirname(options.pathFile);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
// Download to file
await blockBlobClient.downloadToFile(options.pathFile);
return `File downloaded successfully to: ${options.pathFile}`;
}
else {
// Download as text
const downloadResponse = await blockBlobClient.download();
const downloadedContent = await streamToString(downloadResponse.readableStreamBody);
return downloadedContent;
}
};
// Helper function to convert stream to string
async function streamToString(readableStream) {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on("data", (data) => {
chunks.push(data.toString());
});
readableStream.on("end", () => {
resolve(chunks.join(""));
});
readableStream.on("error", reject);
});
}