@teamnetwork-nz/xero-mcp-server 1.14.0 → 1.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -1
- package/dist/handlers/list-xero-profit-and-loss.handler.js +65 -0
- package/dist/handlers/search-xero-transactions.handler.js +5 -1
- package/dist/tools/list/batch-invoice-lookup.tool.js +1 -1
- package/dist/tools/list/index.js +2 -0
- package/dist/tools/list/list-profit-and-loss.tool.js +8 -3
- package/dist/tools/list/lookup-invoice.tool.js +9 -0
- package/dist/tools/list/search-transactions.tool.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -151,6 +151,7 @@ Date inputs to the Xero API (invoice dates, fromDate, toDate, etc.) are `YYYY-MM
|
|
|
151
151
|
| `list-invoices` | List invoices with optional filters: status, contact, invoice number, date range |
|
|
152
152
|
| `xero_search_transactions` | Structured search by invoice number, reference, supplier/contact, date, amount, status and type |
|
|
153
153
|
| `xero_batch_invoice_lookup` | Native exact lookup for up to 100 invoice numbers with per-input duplicate/unmatched results |
|
|
154
|
+
| `xero_lookup_invoice` | Scalar exact invoice-number lookup for clients affected by Hermes array coercion |
|
|
154
155
|
| `get-invoice` | Get full details of a single invoice including all line items |
|
|
155
156
|
| `xero_get_bill_settlement` | Trace payments, credits, prepayments, overpayments and the outstanding balance |
|
|
156
157
|
| `xero_get_invoice_history` | Retrieve Xero-recorded invoice/bill history events (not a complete field-level audit trail) |
|
|
@@ -231,6 +232,12 @@ Date inputs to the Xero API (invoice dates, fromDate, toDate, etc.) are `YYYY-MM
|
|
|
231
232
|
| `list-aged-receivables-by-contact` | Aged receivables for a contact |
|
|
232
233
|
| `list-aged-payables-by-contact` | Aged payables for a contact |
|
|
233
234
|
|
|
235
|
+
`list-profit-and-loss` enforces a single-calendar-month base range and returns
|
|
236
|
+
comparison-column metadata containing each label, actual date window, and MTD flag.
|
|
237
|
+
Multi-month base ranges are rejected because Xero can return misleading rolling totals.
|
|
238
|
+
For N completed monthly columns, request the current month with `periods=N` and ignore
|
|
239
|
+
the first MTD column, or make N separate calls with `periods=0`.
|
|
240
|
+
|
|
234
241
|
### Payroll *(requires `XERO_ENABLE_PAYROLL=true`)*
|
|
235
242
|
| Tool | Description |
|
|
236
243
|
|---|---|
|
|
@@ -281,7 +288,8 @@ separate `reference` search remains available for records that use that field.
|
|
|
281
288
|
looking identifiers are intentionally not coerced to numbers. The reported Hermes
|
|
282
289
|
`coerce_tool_args` defect is outside this repository and must also be corrected in
|
|
283
290
|
the Hermes runtime if that client still rewrites valid JSON arrays before sending
|
|
284
|
-
the MCP request.
|
|
291
|
+
the MCP request. Until then, use scalar `xero_lookup_invoice` for exact single-value
|
|
292
|
+
lookups or `xero_search_transactions` for broader matching.
|
|
285
293
|
|
|
286
294
|
## For Developers
|
|
287
295
|
|
|
@@ -1,6 +1,70 @@
|
|
|
1
1
|
import { xeroClient } from "../clients/xero-client.js";
|
|
2
2
|
import { formatError } from "../helpers/format-error.js";
|
|
3
3
|
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
4
|
+
import { ErrorCode, McpError } from "@modelcontextprotocol/sdk/types.js";
|
|
5
|
+
import { localDateString } from "../helpers/datetime.js";
|
|
6
|
+
export const PROFIT_AND_LOSS_RANGE_ERROR = "list-profit-and-loss requires fromDate and toDate to span a single calendar month; for N monthly columns, call N times with periods=0, or set fromDate=1st-of-current-month, toDate=last-day-of-current-month, periods=N and drop the first (MTD) column.";
|
|
7
|
+
function parseDateOnly(value) {
|
|
8
|
+
const date = new Date(`${value}T00:00:00Z`);
|
|
9
|
+
if (Number.isNaN(date.getTime()) || date.toISOString().slice(0, 10) !== value) {
|
|
10
|
+
throw new McpError(ErrorCode.InvalidParams, `Invalid date: ${value}. Expected YYYY-MM-DD.`);
|
|
11
|
+
}
|
|
12
|
+
return date;
|
|
13
|
+
}
|
|
14
|
+
export function validateProfitAndLossDateRange(fromDate, toDate) {
|
|
15
|
+
if (!fromDate || !toDate)
|
|
16
|
+
return;
|
|
17
|
+
const from = parseDateOnly(fromDate);
|
|
18
|
+
const to = parseDateOnly(toDate);
|
|
19
|
+
if (to < from || from.getUTCFullYear() !== to.getUTCFullYear() || from.getUTCMonth() !== to.getUTCMonth()) {
|
|
20
|
+
throw new McpError(ErrorCode.InvalidParams, PROFIT_AND_LOSS_RANGE_ERROR);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function shiftDate(value, months) {
|
|
24
|
+
const day = value.getUTCDate();
|
|
25
|
+
const shifted = new Date(Date.UTC(value.getUTCFullYear(), value.getUTCMonth() + months, 1));
|
|
26
|
+
const lastDay = new Date(Date.UTC(shifted.getUTCFullYear(), shifted.getUTCMonth() + 1, 0)).getUTCDate();
|
|
27
|
+
shifted.setUTCDate(Math.min(day, lastDay));
|
|
28
|
+
return shifted;
|
|
29
|
+
}
|
|
30
|
+
function isoDate(value) {
|
|
31
|
+
return value.toISOString().slice(0, 10);
|
|
32
|
+
}
|
|
33
|
+
function isLastDayOfMonth(value) {
|
|
34
|
+
return value.getUTCDate() === new Date(Date.UTC(value.getUTCFullYear(), value.getUTCMonth() + 1, 0)).getUTCDate();
|
|
35
|
+
}
|
|
36
|
+
export function buildComparisonColumnMetadata(report, fromDate, toDate, periods = 0, timeframe = "MONTH", today = localDateString()) {
|
|
37
|
+
if (!fromDate || !toDate)
|
|
38
|
+
return [];
|
|
39
|
+
validateProfitAndLossDateRange(fromDate, toDate);
|
|
40
|
+
const from = parseDateOnly(fromDate);
|
|
41
|
+
const to = parseDateOnly(toDate);
|
|
42
|
+
const todayDate = parseDateOnly(today);
|
|
43
|
+
const header = report.rows?.find((row) => String(row.rowType) === "Header" || Number(row.rowType) === 0);
|
|
44
|
+
const labels = header?.cells?.slice(1).map((cell) => cell.value ?? null) ?? [];
|
|
45
|
+
const stepMonths = timeframe === "QUARTER" ? 3 : timeframe === "YEAR" ? 12 : 1;
|
|
46
|
+
const fullMonthWindow = from.getUTCDate() === 1 && isLastDayOfMonth(to);
|
|
47
|
+
return Array.from({ length: periods + 1 }, (_, columnIndex) => {
|
|
48
|
+
const offset = -columnIndex * stepMonths;
|
|
49
|
+
const columnFrom = shiftDate(from, offset);
|
|
50
|
+
const shiftedTo = shiftDate(to, offset);
|
|
51
|
+
const requestedTo = fullMonthWindow
|
|
52
|
+
? new Date(Date.UTC(shiftedTo.getUTCFullYear(), shiftedTo.getUTCMonth() + 1, 0))
|
|
53
|
+
: shiftedTo;
|
|
54
|
+
const isMonthToDate = columnIndex === 0 &&
|
|
55
|
+
columnFrom.getUTCDate() === 1 &&
|
|
56
|
+
columnFrom.getUTCFullYear() === todayDate.getUTCFullYear() &&
|
|
57
|
+
columnFrom.getUTCMonth() === todayDate.getUTCMonth() &&
|
|
58
|
+
requestedTo >= todayDate;
|
|
59
|
+
return {
|
|
60
|
+
columnIndex,
|
|
61
|
+
label: labels[columnIndex] ?? null,
|
|
62
|
+
fromDate: isoDate(columnFrom),
|
|
63
|
+
toDate: isoDate(isMonthToDate ? todayDate : requestedTo),
|
|
64
|
+
isMonthToDate,
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
}
|
|
4
68
|
/**
|
|
5
69
|
* Internal function to fetch profit and loss data from Xero
|
|
6
70
|
*/
|
|
@@ -27,6 +91,7 @@ async function fetchProfitAndLoss(fromDate, toDate, periods, timeframe, standard
|
|
|
27
91
|
* @param paymentsOnly Optional boolean to include only accounts with payments
|
|
28
92
|
*/
|
|
29
93
|
export async function listXeroProfitAndLoss(fromDate, toDate, periods, timeframe, standardLayout, paymentsOnly) {
|
|
94
|
+
validateProfitAndLossDateRange(fromDate, toDate);
|
|
30
95
|
try {
|
|
31
96
|
const profitAndLoss = await fetchProfitAndLoss(fromDate, toDate, periods, timeframe, standardLayout, paymentsOnly);
|
|
32
97
|
if (!profitAndLoss) {
|
|
@@ -61,12 +61,16 @@ export function compactInvoice(invoice) {
|
|
|
61
61
|
hasAttachments: invoice.hasAttachments ?? false,
|
|
62
62
|
};
|
|
63
63
|
}
|
|
64
|
+
export function shouldUseInvoiceSummaryOnly(params) {
|
|
65
|
+
return params.amount === undefined && params.minAmount === undefined && params.maxAmount === undefined;
|
|
66
|
+
}
|
|
64
67
|
export async function searchXeroTransactions(params) {
|
|
65
68
|
try {
|
|
66
69
|
await xeroClient.authenticate();
|
|
67
70
|
const query = buildInvoiceSearchQuery(params);
|
|
68
71
|
const apiPageSize = Math.min(params.pageSize + 1, 1000);
|
|
69
|
-
const
|
|
72
|
+
const summaryOnly = shouldUseInvoiceSummaryOnly(params);
|
|
73
|
+
const response = await xeroClient.accountingApi.getInvoices(xeroClient.tenantId, undefined, query.where, "UpdatedDateUTC DESC", undefined, query.invoiceNumbers, query.contactIds, query.statuses, params.page, false, false, undefined, summaryOnly, apiPageSize, query.searchTerm, getClientHeaders());
|
|
70
74
|
const raw = response.body.invoices ?? [];
|
|
71
75
|
const filtered = filterInvoiceMatches(raw, params);
|
|
72
76
|
const truncated = raw.length > params.pageSize || filtered.length > params.pageSize;
|
|
@@ -2,7 +2,7 @@ import { z } from "zod";
|
|
|
2
2
|
import { batchXeroInvoiceLookup } from "../../handlers/batch-xero-invoice-lookup.handler.js";
|
|
3
3
|
import { CreateXeroTool } from "../../helpers/create-xero-tool.js";
|
|
4
4
|
import { jsonToolResponse } from "../../types/lookup-result.js";
|
|
5
|
-
const BatchInvoiceLookupTool = CreateXeroTool("xero_batch_invoice_lookup", "Look up multiple exact Xero InvoiceNumber values in one
|
|
5
|
+
const BatchInvoiceLookupTool = CreateXeroTool("xero_batch_invoice_lookup", "Look up multiple exact Xero InvoiceNumber values in one batch request. Accepts an array of strings, preserves numeric-looking invoice numbers as strings, and reports exact matches, duplicates, and unmatched values for each input.", {
|
|
6
6
|
invoiceNumbers: z.array(z.string().min(1)).min(1).max(100),
|
|
7
7
|
type: z.enum(["ACCREC", "ACCPAY"]).optional(),
|
|
8
8
|
}, async ({ invoiceNumbers, type }) => jsonToolResponse(await batchXeroInvoiceLookup(invoiceNumbers, type)));
|
package/dist/tools/list/index.js
CHANGED
|
@@ -32,6 +32,7 @@ import SearchTransactionsTool from "./search-transactions.tool.js";
|
|
|
32
32
|
import BatchInvoiceLookupTool from "./batch-invoice-lookup.tool.js";
|
|
33
33
|
import FindContactTool from "./find-contact.tool.js";
|
|
34
34
|
import ListBillAttachmentsTool from "./list-bill-attachments.tool.js";
|
|
35
|
+
import LookupInvoiceTool from "./lookup-invoice.tool.js";
|
|
35
36
|
export const CoreListTools = [
|
|
36
37
|
ListAccountsTool,
|
|
37
38
|
ListContactsTool,
|
|
@@ -54,6 +55,7 @@ export const CoreListTools = [
|
|
|
54
55
|
BatchInvoiceLookupTool,
|
|
55
56
|
FindContactTool,
|
|
56
57
|
ListBillAttachmentsTool,
|
|
58
|
+
LookupInvoiceTool,
|
|
57
59
|
];
|
|
58
60
|
export const ReportListTools = [
|
|
59
61
|
ListTrialBalanceTool,
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import { listXeroProfitAndLoss } from "../../handlers/list-xero-profit-and-loss.handler.js";
|
|
2
|
+
import { buildComparisonColumnMetadata, listXeroProfitAndLoss } from "../../handlers/list-xero-profit-and-loss.handler.js";
|
|
3
3
|
import { CreateXeroTool } from "../../helpers/create-xero-tool.js";
|
|
4
4
|
import { formatUtcDatetime } from "../../helpers/datetime.js";
|
|
5
5
|
const ListProfitAndLossTool = CreateXeroTool("list-profit-and-loss", "Lists profit and loss report in Xero. This provides a summary of revenue, expenses, and profit or loss over a specified period of time.", {
|
|
6
6
|
fromDate: z.string().optional().describe("Optional start date in YYYY-MM-DD format"),
|
|
7
7
|
toDate: z.string().optional().describe("Optional end date in YYYY-MM-DD format"),
|
|
8
|
-
periods: z.number().int().min(
|
|
8
|
+
periods: z.number().int().min(0).max(11).optional().describe("Optional number of prior comparison periods (0-11). Xero returns the requested base period plus this many comparisons, so periods=3 returns 4 columns. Use periods=0 for a single column."),
|
|
9
9
|
timeframe: z.enum(["MONTH", "QUARTER", "YEAR"]).optional().describe("Optional spacing between comparison periods (MONTH, QUARTER, YEAR). It does not split a multi-month fromDate/toDate range into individual columns — " +
|
|
10
10
|
"Xero repeats the whole fromDate/toDate span for each comparison. Example: for 4 separate monthly columns, set fromDate/toDate to a single month and periods=3, NOT a 4-month range with periods=3 (which instead returns 4 rolling 4-month totals). " +
|
|
11
|
-
"If fromDate/toDate covers the CURRENT, still-in-progress month, Xero returns month-to-date actuals
|
|
11
|
+
"If fromDate/toDate covers the CURRENT, still-in-progress month, Xero returns month-to-date actuals for the base column; prior comparison columns are completed periods. To obtain N reliable completed monthly columns, request the current calendar month with periods=N and ignore the first MTD column. Multi-month base ranges are rejected at runtime because Xero returns misleading rolling totals."),
|
|
12
12
|
standardLayout: z.boolean().optional().describe("Optional flag to use standard layout"),
|
|
13
13
|
paymentsOnly: z.boolean().optional().describe("Optional flag to include only accounts with payments"),
|
|
14
14
|
}, async (args) => {
|
|
@@ -24,6 +24,7 @@ const ListProfitAndLossTool = CreateXeroTool("list-profit-and-loss", "Lists prof
|
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
26
|
const profitAndLossReport = response.result;
|
|
27
|
+
const comparisonColumnMetadata = buildComparisonColumnMetadata(profitAndLossReport, args?.fromDate, args?.toDate, args?.periods, args?.timeframe);
|
|
27
28
|
return {
|
|
28
29
|
content: [
|
|
29
30
|
{
|
|
@@ -38,6 +39,10 @@ const ListProfitAndLossTool = CreateXeroTool("list-profit-and-loss", "Lists prof
|
|
|
38
39
|
type: "text",
|
|
39
40
|
text: `Updated At: ${profitAndLossReport?.updatedDateUTC ? formatUtcDatetime(profitAndLossReport.updatedDateUTC) : "Unknown"}`,
|
|
40
41
|
},
|
|
42
|
+
{
|
|
43
|
+
type: "text",
|
|
44
|
+
text: `Comparison Column Metadata: ${JSON.stringify(comparisonColumnMetadata)}`,
|
|
45
|
+
},
|
|
41
46
|
{
|
|
42
47
|
type: "text",
|
|
43
48
|
text: JSON.stringify(profitAndLossReport.rows, null, 2),
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { batchXeroInvoiceLookup } from "../../handlers/batch-xero-invoice-lookup.handler.js";
|
|
3
|
+
import { CreateXeroTool } from "../../helpers/create-xero-tool.js";
|
|
4
|
+
import { jsonToolResponse } from "../../types/lookup-result.js";
|
|
5
|
+
const LookupInvoiceTool = CreateXeroTool("xero_lookup_invoice", "Look up one Xero invoice or purchase bill by its exact InvoiceNumber value and report exact, duplicate, or unmatched results.", {
|
|
6
|
+
invoiceNumber: z.string().min(1),
|
|
7
|
+
type: z.enum(["ACCREC", "ACCPAY"]).optional(),
|
|
8
|
+
}, async ({ invoiceNumber, type }) => jsonToolResponse(await batchXeroInvoiceLookup([invoiceNumber], type)));
|
|
9
|
+
export default LookupInvoiceTool;
|
|
@@ -2,7 +2,7 @@ import { z } from "zod";
|
|
|
2
2
|
import { searchXeroTransactions } from "../../handlers/search-xero-transactions.handler.js";
|
|
3
3
|
import { CreateXeroTool } from "../../helpers/create-xero-tool.js";
|
|
4
4
|
import { jsonToolResponse } from "../../types/lookup-result.js";
|
|
5
|
-
const SearchTransactionsTool = CreateXeroTool("xero_search_transactions", "Search Xero sales invoices and purchase bills
|
|
5
|
+
const SearchTransactionsTool = CreateXeroTool("xero_search_transactions", "Search Xero sales invoices and purchase bills by invoice number, reference, contact, date, amount, status, or type. Uses Xero server-side filters where available, verifies the requested match mode, and returns compact match, ambiguity, pagination, and error metadata.", {
|
|
6
6
|
invoiceNumber: z.string().min(1).optional(),
|
|
7
7
|
reference: z.string().min(1).optional(),
|
|
8
8
|
contactId: z.string().uuid().optional(),
|