parse_rakuten_csv
Parse a Rakuten Securities transaction CSV to extract cash flows, automatically handling buy/sell transactions. Output is ready for XIRR calculation.
Instructions
Parse a Rakuten Securities transaction CSV (取引履歴) and convert it into a normalized cash flow list. Handles 買付/売却 transaction types automatically. The output can be fed directly into calculate_xirr.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| csvContent | Yes | Raw CSV text exported from Rakuten Securities. Note: Rakuten exports are typically Shift_JIS encoded; decode to UTF-8 before passing. |
Implementation Reference
- src/index.ts:84-117 (handler)Handler function for parse_rakuten_csv that validates input via Zod schema, calls mapRakutenCsv to parse the CSV, and returns the result as JSON.
function handleParseRakutenCsv(args: unknown): string { const parsed = ParseRakutenCsvInputSchema.parse(args); const result = mapRakutenCsv(parsed.csvContent); if (!result.ok) { return JSON.stringify( { ok: false, error: result.error, }, null, 2, ); } const totalAmount = result.rows.reduce((sum, r) => sum + r.amount, 0); return JSON.stringify( { ok: true, rowCount: result.rows.length, totalAmount, cashFlows: result.rows.map((r) => ({ date: r.date, amount: r.amount, transactionType: r.transactionType, fundName: r.fundName, })), warnings: result.warnings, }, null, 2, ); } - src/index.ts:177-192 (registration)Tool registration in ListToolsRequestSchema handler with name 'parse_rakuten_csv', description, and inputSchema (csvContent string).
{ name: 'parse_rakuten_csv', description: 'Parse a Rakuten Securities transaction CSV (取引履歴) and convert it into a normalized cash flow list. Handles 買付/売却 transaction types automatically. The output can be fed directly into calculate_xirr.', inputSchema: { type: 'object', properties: { csvContent: { type: 'string', description: 'Raw CSV text exported from Rakuten Securities. Note: Rakuten exports are typically Shift_JIS encoded; decode to UTF-8 before passing.', }, }, required: ['csvContent'], }, }, - src/index.ts:205-207 (registration)Switch-case dispatch mapping the tool name 'parse_rakuten_csv' to the handler function handleParseRakutenCsv.
case 'parse_rakuten_csv': text = handleParseRakutenCsv(args); break; - src/index.ts:38-45 (schema)Zod input validation schema for parse_rakuten_csv requiring a non-empty csvContent string.
const ParseRakutenCsvInputSchema = z.object({ csvContent: z .string() .min(1, 'csvContent must not be empty') .describe( 'Raw CSV text exported from Rakuten Securities. Headers must include 約定日, 取引, ファンド名, 受渡金額/(ポイント利用)[円].', ), }); - src/parsers/rakuten.ts:39-95 (helper)Core CSV parsing logic for Rakuten Securities CSV. Validates headers, normalizes dates, parses amounts, and converts buy/sell transactions to signed cash flows.
export function mapRakutenCsv(csvText: string): MapResult { const parsed = parseCsv(csvText); if (!parsed.ok) { return { ok: false, error: parsed.error }; } const { headers, rows: dataRows } = parsed; const indices: Record<string, number> = {}; for (const name of REQUIRED_HEADERS) { const idx = findColumnIndex(headers, name); if (idx === -1) { return { ok: false, error: `必要なカラム「${name}」が見つかりません`, }; } indices[name] = idx; } const results: RakutenCsvRow[] = []; const warnings: string[] = []; for (let i = 0; i < dataRows.length; i++) { const row = dataRows[i]; const rawDate = row[indices['約定日']] ?? ''; const rawTransaction = row[indices['取引']] ?? ''; const rawFundName = row[indices['ファンド名']] ?? ''; const rawAmount = row[indices['受渡金額/(ポイント利用)[円]']] ?? ''; const date = normalizeDate(rawDate); if (!date) { warnings.push(`${i + 2}行目: 日付「${rawDate}」を解析できません`); continue; } const amount = parseAmount(rawAmount); if (amount === null) { warnings.push(`${i + 2}行目: 金額「${rawAmount}」を解析できません`); continue; } const isBuy = rawTransaction === '買付'; results.push({ date, amount: isBuy ? amount : -amount, transactionType: rawTransaction, fundName: rawFundName, }); } if (results.length === 0) { return { ok: false, error: '有効な取引データがありません' }; } return { ok: true, rows: results, warnings }; }