财务数据访问SDK

一个TypeScript库,用于访问Monarch Money的财务数据,包括账户、交易、预算等信息。

  • by martinamps
  • TypeScript
  • 本地部署
  • 3 星标
  • 更新于
在 GitHub 查看 martinamps/monarch-mcp

使用说明

Monarch Money TypeScript SDK

用于访问的TypeScript库 君主货币 数据。

安装

bun add monarchmoney

或

npm install monarchmoney

用法

浏览器令牌快速入门

最简单的入门方法是直接从浏览器会话中使用令牌:

import MonarchMoney from 'monarchmoney';

// You can get this token from your browser's localStorage or Cookies after logging in
// to Monarch Money on the web
const token = process.env.MONARCH_TOKEN;

// Create a client with the token
const mm = new MonarchMoney(token);

// Now you can use the API without logging in
const accounts = await mm.getAccounts();

标准认证

如果您没有令牌,可以使用凭据登录:

import MonarchMoney, { RequireMFAError } from 'monarchmoney';

// Create a new client
const mm = new MonarchMoney();

// Login using environment variables
const email = process.env.MONARCH_USER;
const password = process.env.MONARCH_PASSWORD;

if (!email || !password) {
  throw new Error('MONARCH_USER and MONARCH_PASSWORD environment variables must be set');
}

// Login
try {
  await mm.login({
    email,
    password
  });
} catch (error) {
  if (error instanceof RequireMFAError) {
    // Get MFA code from user
    const mfaCode = process.env.MONARCH_MFA || prompt('Enter MFA code:');
    
    await mm.authenticateWithMFA({
      email,
      password,
      mfaCode
    });
  } else {
    throw error;
  }
}

使用已保存的会话

您可以保存会话以供以后使用:

// After logging in, save the session
mm.saveSession();

// Later, load the session
const mm = new MonarchMoney();
mm.loadSession();

// Now you can access data without logging in again
const accounts = await mm.getAccounts();

访问数据

// Get accounts
const accounts = await mm.getAccounts();
console.log(accounts);

// Get transactions for current month
const today = new Date();
const firstDayOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
const lastDayOfMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0);

const transactions = await mm.getTransactions({
  limit: 10,
  filters: {
    startDate: firstDayOfMonth.toISOString().split('T')[0],
    endDate: lastDayOfMonth.toISOString().split('T')[0]
  }
});
console.log(transactions);

// Get budgets
const budgets = await mm.getBudgets();
console.log(budgets);

// Get account holdings
const holdings = await mm.getAccountHoldings('account-id-here');
console.log(holdings);

运行示例

要运行包含的示例,您有两个选项:

选项1:使用浏览器令牌(最简单)

# Get the token from your browser after logging in to Monarch Money
# In Chrome: DevTools > Application > Storage > Local Storage > monarchmoney.com > find the auth token
export MONARCH_TOKEN="your-token-from-browser"

# Run the example
bun run example

选项2:使用用户名/密码

# Set your credentials as environment variables
export MONARCH_USER="your.email@example.com"
export MONARCH_PASSWORD="your-password"
# Optional: Set MFA code if required
export MONARCH_MFA="123456"

# Run the example
bun run example

可用方法

此SDK提供对Monarch Money数据的只读访问:

  • getAccounts() -获取所有帐户
  • getAccountTypeOptions() -获取所有帐户类型和子类型
  • getAccountHoldings(accountId) -在经纪账户中获取证券
  • getAccountHistory(accountId) -获取每日帐户历史记录
  • getInstitutions() -获取链接机构
  • getBudgets(startDate, endDate) -获取预算和实际金额
  • getSubscriptionDetails() -获取帐户订阅状态
  • getTransactionsSummary() -获取交易摘要数据
  • getTransactions(options) -获取交易数据
  • getTransactionCategories() -获取交易类别
  • getTransactionCategoryGroups() -获取交易类别组
  • getTransactionDetails(transactionId) -获取交易的详细信息
  • getTransactionSplits(transactionId) -获取交易拆分
  • getTransactionTags() -获取交易标签
  • getCashflow(options) -获取现金流数据
  • getCashflowSummary(options) -获取现金流摘要
  • getRecurringTransactions(startDate, endDate) -获取定期交易

用于AI代理的MCP服务器

该SDK包括一个模型上下文协议(MCP)服务器,允许AI代理与Monarch Money进行交互。MCP服务器提供用于查询帐户、交易、预算等的工具。

要启动MCP服务器,请执行以下操作:

# Set up authentication via environment variables
export MONARCH_TOKEN="your-token-from-browser"

# Start the MCP server
bun run mcp

MCP服务器将使用MONARCH_TOKEN环境变量自动进行身份验证,并提供以下工具:

  • check_authentication -检查是否已通过身份验证
  • get_accounts -获取所有帐户
  • get_account_holdings -获取账户持有量
  • get_account_history -获取帐户的历史记录
  • get_transactions -使用筛选器获取交易
  • get_transaction_categories -获取交易类别
  • get_budgets -获取日期范围的预算
  • get_cashflow -使用过滤器获取现金流数据

Claude桌面配置

要将其与Claude Desktop一起使用,请将以下内容添加到您的Claude Desktop配置文件中(位于 ~/Library/Application Support/Claude/claude_desktop_config.json):

{
  "mcpServers": {
    "monarchMoney": {
      "command": "bun",
      "args": ["run", "mcp"],
      "env": {
        "MONARCH_TOKEN": "your-monarch-token-here"
      },
      "cwd": "/path/to/your/monarch-ts"
    }
  }
}

替换 /path/to/your/monarch-ts 带有项目目录的实际路径,以及 your-monarch-token-here 从浏览器中使用您的Monarch Money代币。

许可证

麻省理工学院