read_email
Retrieve and read emails from Hiworks Mail by specifying the message ID using the Hiworks Mail MCP server for efficient email management and integration.
Instructions
하이웍스 이메일을 읽어옵니다.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| messageId | Yes | ||
| password | No | ||
| username | No |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"messageId": {
"type": "string"
},
"password": {
"default": "",
"type": "string"
},
"username": {
"default": "",
"type": "string"
}
},
"required": [
"messageId"
],
"type": "object"
}
Implementation Reference
- src/index.ts:245-304 (handler)The core handler function for the 'read_email' tool. It connects to the Hiworks POP3 server, iterates through messages to find the one matching the provided messageId, parses the email content using simpleParser from mailparser, converts dates to KST, and returns the structured email data or error.async ({ username, password, messageId }) => { try { const client = await connectPOP3(username, password); const stat = await client.STAT(); const totalMessages = stat[0]; let email: Email | undefined; for (let i = totalMessages; i >= 1; i--) { try { const rawEmail = await client.RETR(i); const parsed = await simpleParser(rawEmail); if (parsed.messageId === messageId || String(i) === messageId) { // KST로 변환된 날짜 사용 const date = parsed.date ? formatDate(parsed.date) : formatDate(new Date()); email = { id: parsed.messageId || String(i), subject: parsed.subject || '(제목 없음)', from: Array.isArray(parsed.from) ? parsed.from[0]?.text || '' : parsed.from?.text || '', to: Array.isArray(parsed.to) ? parsed.to[0]?.text || '' : parsed.to?.text || '', date, content: parsed.text || '', html: parsed.html || undefined }; break; } } catch (err) { log(`Error processing email ${i}:`, err); continue; } } await client.QUIT(); return { content: [ { type: "text", text: JSON.stringify({ success: true, email } as ReadEmailResponse) } ] }; } catch (error: any) { return { content: [ { type: "text", text: JSON.stringify({ success: false, error: error.message } as ReadEmailResponse) } ] }; } }
- src/index.ts:241-305 (registration)Registers the 'read_email' tool with the MCP server, providing name, description, input schema, and handler function.server.tool( 'read_email', '하이웍스 이메일을 읽어옵니다.', readEmailSchema, async ({ username, password, messageId }) => { try { const client = await connectPOP3(username, password); const stat = await client.STAT(); const totalMessages = stat[0]; let email: Email | undefined; for (let i = totalMessages; i >= 1; i--) { try { const rawEmail = await client.RETR(i); const parsed = await simpleParser(rawEmail); if (parsed.messageId === messageId || String(i) === messageId) { // KST로 변환된 날짜 사용 const date = parsed.date ? formatDate(parsed.date) : formatDate(new Date()); email = { id: parsed.messageId || String(i), subject: parsed.subject || '(제목 없음)', from: Array.isArray(parsed.from) ? parsed.from[0]?.text || '' : parsed.from?.text || '', to: Array.isArray(parsed.to) ? parsed.to[0]?.text || '' : parsed.to?.text || '', date, content: parsed.text || '', html: parsed.html || undefined }; break; } } catch (err) { log(`Error processing email ${i}:`, err); continue; } } await client.QUIT(); return { content: [ { type: "text", text: JSON.stringify({ success: true, email } as ReadEmailResponse) } ] }; } catch (error: any) { return { content: [ { type: "text", text: JSON.stringify({ success: false, error: error.message } as ReadEmailResponse) } ] }; } } );
- src/index.ts:130-133 (schema)Zod schema for validating 'read_email' tool inputs: username, password (from emailSchema), and required messageId.const readEmailSchema = { ...emailSchema, messageId: z.string() };
- src/types/mail.ts:1-5 (schema)TypeScript interface defining the input parameters for the read_email tool.export interface ReadEmailParams { username?: string; password?: string; messageId: string; }
- src/types/mail.ts:33-37 (schema)TypeScript interface defining the output response for the read_email tool.export interface ReadEmailResponse { success: boolean; email?: Email; error?: string; }
- src/index.ts:44-56 (helper)Helper function to establish POP3 connection to Hiworks server, used by the read_email handler.async function connectPOP3(username: string, password: string): Promise<Pop3Command> { const pop3Config = { user: username, password: password, host: config.pop3.host, port: config.pop3.port, tls: config.pop3.ssl, timeout: 60000 }; const client = new Pop3Command(pop3Config); return client; }