msexchange-mcp
Version:
MCP server for Microsoft Exchange/Outlook email operations via Microsoft Graph API
77 lines • 3.28 kB
JavaScript
import { z } from 'zod';
import { getGraphClient } from '../graph/graphClient.js';
import accountsConfig from '../config/accountsLoader.js';
import * as fs from 'fs/promises';
export const getAttachmentTool = {
name: 'get_attachment',
description: 'Download an attachment from an email',
parameters: z.object({
user_id: z.string().describe('The user ID or email address'),
message_id: z.string().describe('The email message ID'),
attachment_id: z.string().describe('The attachment ID'),
save_path: z.string().optional().describe('Path to save the attachment (optional)'),
}),
execute: async (params) => {
try {
const account = accountsConfig.accounts.find(acc => acc.id === params.user_id || acc.email === params.user_id);
if (!account) {
throw new Error(`User not found: ${params.user_id}`);
}
const client = await getGraphClient(account.email);
// Get attachment metadata
const attachment = await client
.api(`/users/${account.email}/messages/${params.message_id}/attachments/${params.attachment_id}`)
.get();
// Get attachment content
const content = await client
.api(`/users/${account.email}/messages/${params.message_id}/attachments/${params.attachment_id}/$value`)
.get();
// Save to file if path provided
if (params.save_path) {
const buffer = Buffer.from(content.contentBytes || content, 'base64');
await fs.writeFile(params.save_path, buffer);
return {
content: [
{
type: 'text',
text: JSON.stringify({
message: 'Attachment saved successfully',
path: params.save_path,
name: attachment.name,
size: attachment.size,
contentType: attachment.contentType,
}, null, 2),
},
],
};
}
// Return attachment info and content
return {
content: [
{
type: 'text',
text: JSON.stringify({
id: attachment.id,
name: attachment.name,
contentType: attachment.contentType,
size: attachment.size,
isInline: attachment.isInline,
contentBytes: content.contentBytes || content,
}, null, 2),
},
],
};
}
catch (error) {
return {
content: [
{
type: 'text',
text: `Error getting attachment: ${error instanceof Error ? error.message : 'Unknown error'}`,
},
],
};
}
},
};
//# sourceMappingURL=getAttachment.js.map