@mindstone/mcp-server-xero 0.0.16 → 0.1.0
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 +48 -1
- package/dist/clients/xero-client.js +2 -3
- package/dist/handlers/create-xero-invoice.handler.js +2 -0
- package/dist/handlers/create-xero-purchase-order.handler.js +50 -0
- package/dist/handlers/email-xero-invoice.handler.js +27 -0
- package/dist/handlers/list-xero-bank-summary.handler.js +38 -0
- package/dist/handlers/list-xero-budget-summary.handler.js +45 -0
- package/dist/handlers/list-xero-currencies.handler.js +22 -0
- package/dist/handlers/list-xero-executive-summary.handler.js +37 -0
- package/dist/handlers/list-xero-purchase-orders.handler.js +29 -0
- package/dist/handlers/update-xero-invoice.handler.js +2 -0
- package/dist/helpers/create-xero-tool.js +32 -1
- package/dist/helpers/format-error.js +78 -9
- package/dist/helpers/get-deeplink.js +15 -4
- package/dist/helpers/xero-currencies.js +23 -0
- package/dist/server/xero-mcp-server.js +2 -1
- package/dist/tools/create/create-contact.tool.js +2 -3
- package/dist/tools/create/create-manual-journal.tool.js +2 -3
- package/dist/tools/create/create-purchase-order.tool.js +66 -0
- package/dist/tools/create/index.js +3 -1
- package/dist/tools/list/index.js +11 -1
- package/dist/tools/list/list-bank-summary.tool.js +41 -0
- package/dist/tools/list/list-budget-summary.tool.js +47 -0
- package/dist/tools/list/list-currencies.tool.js +34 -0
- package/dist/tools/list/list-executive-summary.tool.js +36 -0
- package/dist/tools/list/list-purchase-orders.tool.js +97 -0
- package/dist/tools/tool-factory.js +4 -1
- package/dist/tools/update/email-invoice.tool.js +29 -0
- package/dist/tools/update/index.js +3 -1
- package/dist/tools/update/update-contact.tool.js +2 -3
- package/dist/tools/update/update-manual-journal-tool.js +2 -3
- package/dist/untrusted-content.js +119 -0
- package/package.json +1 -1
- package/dist/helpers/ensure-error.js +0 -13
package/README.md
CHANGED
|
@@ -2,6 +2,44 @@
|
|
|
2
2
|
|
|
3
3
|
This is a Model Context Protocol (MCP) server implementation for Xero. It provides a bridge between the MCP protocol and Xero's API, allowing for standardized access to Xero's accounting and business features.
|
|
4
4
|
|
|
5
|
+
## Provenance
|
|
6
|
+
|
|
7
|
+
This package is Mindstone's maintained build of Xero's community Xero MCP server, announced on the [Xero Developer Blog](https://devblog.xero.com/xero-introduces-new-model-context-protocol-server-for-smarter-accounting-data-integration) and developed upstream at [XeroAPI/xero-mcp-server](https://github.com/XeroAPI/xero-mcp-server). It adds improvements and hardening by Mindstone on top of the upstream project.
|
|
8
|
+
|
|
9
|
+
<!-- BEGIN INSTALL_LINKS: do not edit by hand; regenerated by scripts/gen-install-links.mjs -->
|
|
10
|
+
## One-click install
|
|
11
|
+
|
|
12
|
+
[](cursor://anysphere.cursor-deeplink/mcp/install?name=Xero&config=eyJ0eXBlIjoic3RkaW8iLCJjb21tYW5kIjoibnB4IiwiYXJncyI6WyIteSIsIkBtaW5kc3RvbmUvbWNwLXNlcnZlci14ZXJvIl0sImVudiI6eyJYRVJPX0NMSUVOVF9JRCI6IiIsIlhFUk9fQ0xJRU5UX1NFQ1JFVCI6IiIsIlhFUk9fQ0xJRU5UX0JFQVJFUl9UT0tFTiI6IiJ9fQ)
|
|
13
|
+
[](vscode:mcp/install?%7B%22name%22%3A%22Xero%22%2C%22command%22%3A%22npx%22%2C%22args%22%3A%5B%22-y%22%2C%22%40mindstone%2Fmcp-server-xero%22%5D%2C%22env%22%3A%7B%22XERO_CLIENT_ID%22%3A%22%22%2C%22XERO_CLIENT_SECRET%22%3A%22%22%2C%22XERO_CLIENT_BEARER_TOKEN%22%3A%22%22%7D%7D)
|
|
14
|
+
[](vscode-insiders:mcp/install?%7B%22name%22%3A%22Xero%22%2C%22command%22%3A%22npx%22%2C%22args%22%3A%5B%22-y%22%2C%22%40mindstone%2Fmcp-server-xero%22%5D%2C%22env%22%3A%7B%22XERO_CLIENT_ID%22%3A%22%22%2C%22XERO_CLIENT_SECRET%22%3A%22%22%2C%22XERO_CLIENT_BEARER_TOKEN%22%3A%22%22%7D%7D)
|
|
15
|
+
|
|
16
|
+
After clicking the button, your host will prompt you to fill: `XERO_CLIENT_ID`, `XERO_CLIENT_SECRET`, `XERO_CLIENT_BEARER_TOKEN`.
|
|
17
|
+
|
|
18
|
+
<details>
|
|
19
|
+
<summary>Manual config for Claude Desktop / Claude Code / Goose / Continue.dev (Xero)</summary>
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
{
|
|
23
|
+
"mcpServers": {
|
|
24
|
+
"Xero": {
|
|
25
|
+
"command": "npx",
|
|
26
|
+
"args": [
|
|
27
|
+
"-y",
|
|
28
|
+
"@mindstone/mcp-server-xero"
|
|
29
|
+
],
|
|
30
|
+
"env": {
|
|
31
|
+
"XERO_CLIENT_ID": "",
|
|
32
|
+
"XERO_CLIENT_SECRET": "",
|
|
33
|
+
"XERO_CLIENT_BEARER_TOKEN": ""
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
</details>
|
|
41
|
+
<!-- END INSTALL_LINKS -->
|
|
42
|
+
|
|
5
43
|
## Features
|
|
6
44
|
|
|
7
45
|
- Xero OAuth2 authentication with custom connections
|
|
@@ -93,6 +131,8 @@ NOTE: The `XERO_CLIENT_BEARER_TOKEN` will take precedence over the `XERO_CLIENT_
|
|
|
93
131
|
|
|
94
132
|
### Available MCP Commands
|
|
95
133
|
|
|
134
|
+
> **Tool naming:** this connector's tools use unprefixed dash-case names (e.g. `list-invoices`, `create-contact`), inherited from its fork lineage. Sibling connectors use prefixed snake_case names (e.g. `list_quickbooks_invoices`). In multi-server hosts, unprefixed generic names can collide with other servers' tools. Renaming would be a breaking change for existing configurations, so the names are kept as-is; if your host supports namespacing/aliasing, prefer enabling it for this server.
|
|
135
|
+
|
|
96
136
|
- `list-accounts`: Retrieve a list of accounts
|
|
97
137
|
- `list-contacts`: Retrieve a list of contacts from Xero
|
|
98
138
|
- `list-credit-notes`: Retrieve a list of credit notes
|
|
@@ -100,6 +140,9 @@ NOTE: The `XERO_CLIENT_BEARER_TOKEN` will take precedence over the `XERO_CLIENT_
|
|
|
100
140
|
- `list-items`: Retrieve a list of items
|
|
101
141
|
- `list-organisation-details`: Retrieve details about an organisation
|
|
102
142
|
- `list-profit-and-loss`: Retrieve a profit and loss report
|
|
143
|
+
- `list-bank-summary`: Retrieve the bank summary report (per-account balances and movements)
|
|
144
|
+
- `list-budget-summary`: Retrieve the budget summary report (budget vs actuals)
|
|
145
|
+
- `list-executive-summary`: Retrieve the executive summary report (key financial metrics snapshot)
|
|
103
146
|
- `list-quotes`: Retrieve a list of quotes
|
|
104
147
|
- `list-tax-rates`: Retrieve a list of tax rates
|
|
105
148
|
- `list-payments`: Retrieve a list of payments
|
|
@@ -116,16 +159,19 @@ NOTE: The `XERO_CLIENT_BEARER_TOKEN` will take precedence over the `XERO_CLIENT_
|
|
|
116
159
|
- `list-aged-receivables-by-contact`: Retrieves aged receivables for a contact
|
|
117
160
|
- `list-aged-payables-by-contact`: Retrieves aged payables for a contact
|
|
118
161
|
- `list-contact-groups`: Retrieve a list of contact groups
|
|
162
|
+
- `list-currencies`: Retrieve currencies enabled in the connected Xero organisation
|
|
119
163
|
- `list-invoice-attachments`: Retrieve files attached to a Xero invoice
|
|
164
|
+
- `list-purchase-orders`: Retrieve a list of purchase orders
|
|
120
165
|
- `create-contact`: Create a new contact
|
|
121
166
|
- `create-credit-note`: Create a new credit note
|
|
122
167
|
- `create-invoice`: Create a new invoice. Supports optional `currencyCode`, such as `USD`, when that currency is enabled in the connected Xero organisation.
|
|
123
168
|
- `create-payment`: Create a new payment
|
|
124
169
|
- `create-quote`: Create a new quote
|
|
170
|
+
- `create-purchase-order`: Create a new purchase order
|
|
125
171
|
- `create-credit-note`: Create a new credit note
|
|
126
172
|
- `create-payroll-timesheet`: Create a new Payroll Timesheet
|
|
127
173
|
- `update-contact`: Update an existing contact
|
|
128
|
-
- `update-invoice`: Update an existing draft invoice. Supports optional `currencyCode`, such as `USD`,
|
|
174
|
+
- `update-invoice`: Update an existing draft invoice. Supports optional `currencyCode`, such as `USD`, when that currency is enabled in the connected Xero organisation.
|
|
129
175
|
- `update-quote`: Update an existing draft quote
|
|
130
176
|
- `update-credit-note`: Update an existing draft credit note
|
|
131
177
|
- `get-invoice-history`: Retrieve the history records for an invoice
|
|
@@ -144,6 +190,7 @@ NOTE: The `XERO_CLIENT_BEARER_TOKEN` will take precedence over the `XERO_CLIENT_
|
|
|
144
190
|
- `add-payroll-timesheet-line`: Add new line on an existing Payroll Timesheet
|
|
145
191
|
- `delete-payroll-timesheet`: Delete an existing Payroll Timesheet
|
|
146
192
|
- `get-payroll-timesheet`: Retrieve an existing Payroll Timesheet
|
|
193
|
+
- `email-invoice`: Email a copy of an AUTHORISED invoice to its related contact
|
|
147
194
|
|
|
148
195
|
For detailed API documentation, please refer to the [MCP Protocol Specification](https://modelcontextprotocol.io/).
|
|
149
196
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import axios from "axios";
|
|
2
2
|
import dotenv from "dotenv";
|
|
3
3
|
import { XeroClient, } from "xero-node";
|
|
4
|
-
import {
|
|
4
|
+
import { formatError } from "../helpers/format-error.js";
|
|
5
5
|
import { formatTokenRequestError } from "../helpers/format-token-error.js";
|
|
6
6
|
import { XERO_CUSTOM_CONNECTION_SCOPE } from "./xero-scopes.js";
|
|
7
7
|
dotenv.config();
|
|
@@ -44,8 +44,7 @@ class MCPXeroClient extends XeroClient {
|
|
|
44
44
|
this.shortCode = organisation.shortCode ?? "";
|
|
45
45
|
}
|
|
46
46
|
catch (error) {
|
|
47
|
-
|
|
48
|
-
throw new Error(`Failed to get Organisation short code: ${err.message}`);
|
|
47
|
+
throw new Error(`Failed to get Organisation short code: ${formatError(error)}`);
|
|
49
48
|
}
|
|
50
49
|
}
|
|
51
50
|
return this.shortCode;
|
|
@@ -2,8 +2,10 @@ import { xeroClient } from "../clients/xero-client.js";
|
|
|
2
2
|
import { formatError } from "../helpers/format-error.js";
|
|
3
3
|
import { Invoice } from "xero-node";
|
|
4
4
|
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
5
|
+
import { assertXeroCurrencyEnabled } from "../helpers/xero-currencies.js";
|
|
5
6
|
async function createInvoice(contactId, lineItems, type, reference, date, currencyCode) {
|
|
6
7
|
await xeroClient.authenticate();
|
|
8
|
+
await assertXeroCurrencyEnabled(currencyCode);
|
|
7
9
|
const invoice = {
|
|
8
10
|
type: type,
|
|
9
11
|
contact: {
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { formatError } from "../helpers/format-error.js";
|
|
3
|
+
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
4
|
+
import { assertXeroCurrencyEnabled } from "../helpers/xero-currencies.js";
|
|
5
|
+
import { PurchaseOrder } from "xero-node";
|
|
6
|
+
async function createPurchaseOrder(contactId, lineItems, reference, date, deliveryDate, purchaseOrderNumber, currencyCode, status) {
|
|
7
|
+
await xeroClient.authenticate();
|
|
8
|
+
await assertXeroCurrencyEnabled(currencyCode);
|
|
9
|
+
const purchaseOrder = {
|
|
10
|
+
contact: {
|
|
11
|
+
contactID: contactId,
|
|
12
|
+
},
|
|
13
|
+
lineItems: lineItems,
|
|
14
|
+
...(date ? { date } : {}),
|
|
15
|
+
...(deliveryDate ? { deliveryDate } : {}),
|
|
16
|
+
...(reference ? { reference } : {}),
|
|
17
|
+
...(purchaseOrderNumber ? { purchaseOrderNumber } : {}),
|
|
18
|
+
...(currencyCode ? { currencyCode } : {}),
|
|
19
|
+
status: status ?? PurchaseOrder.StatusEnum.DRAFT,
|
|
20
|
+
};
|
|
21
|
+
const response = await xeroClient.accountingApi.createPurchaseOrders(xeroClient.tenantId, {
|
|
22
|
+
purchaseOrders: [purchaseOrder],
|
|
23
|
+
}, true, // summarizeErrors
|
|
24
|
+
undefined, // idempotencyKey
|
|
25
|
+
getClientHeaders());
|
|
26
|
+
return response.body.purchaseOrders?.[0];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Create a new purchase order in Xero
|
|
30
|
+
*/
|
|
31
|
+
export async function createXeroPurchaseOrder(contactId, lineItems, reference, date, deliveryDate, purchaseOrderNumber, currencyCode, status) {
|
|
32
|
+
try {
|
|
33
|
+
const createdPurchaseOrder = await createPurchaseOrder(contactId, lineItems, reference, date, deliveryDate, purchaseOrderNumber, currencyCode, status);
|
|
34
|
+
if (!createdPurchaseOrder) {
|
|
35
|
+
throw new Error("Purchase order creation failed.");
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
result: createdPurchaseOrder,
|
|
39
|
+
isError: false,
|
|
40
|
+
error: null,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
return {
|
|
45
|
+
result: null,
|
|
46
|
+
isError: true,
|
|
47
|
+
error: formatError(error),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { formatError } from "../helpers/format-error.js";
|
|
3
|
+
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
4
|
+
/**
|
|
5
|
+
* Email a copy of an invoice to its related contact via Xero.
|
|
6
|
+
* The invoice must be AUTHORISED — Xero rejects emails for DRAFT or
|
|
7
|
+
* SUBMITTED invoices.
|
|
8
|
+
*/
|
|
9
|
+
export async function emailXeroInvoice(invoiceId) {
|
|
10
|
+
try {
|
|
11
|
+
await xeroClient.authenticate();
|
|
12
|
+
await xeroClient.accountingApi.emailInvoice(xeroClient.tenantId, invoiceId, {}, undefined, // idempotencyKey
|
|
13
|
+
getClientHeaders());
|
|
14
|
+
return {
|
|
15
|
+
result: true,
|
|
16
|
+
isError: false,
|
|
17
|
+
error: null,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
return {
|
|
22
|
+
result: null,
|
|
23
|
+
isError: true,
|
|
24
|
+
error: formatError(error),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { formatError } from "../helpers/format-error.js";
|
|
3
|
+
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
4
|
+
async function fetchBankSummary(fromDate, toDate) {
|
|
5
|
+
await xeroClient.authenticate();
|
|
6
|
+
const response = await xeroClient.accountingApi.getReportBankSummary(xeroClient.tenantId, fromDate, toDate, getClientHeaders());
|
|
7
|
+
return response.body.reports?.[0] ?? null;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Get the bank summary report from Xero (opening/closing balances and
|
|
11
|
+
* movements per bank account over a period).
|
|
12
|
+
* @param fromDate Optional start date for the report (YYYY-MM-DD)
|
|
13
|
+
* @param toDate Optional end date for the report (YYYY-MM-DD)
|
|
14
|
+
*/
|
|
15
|
+
export async function listXeroBankSummary(fromDate, toDate) {
|
|
16
|
+
try {
|
|
17
|
+
const report = await fetchBankSummary(fromDate, toDate);
|
|
18
|
+
if (!report) {
|
|
19
|
+
return {
|
|
20
|
+
result: null,
|
|
21
|
+
isError: true,
|
|
22
|
+
error: "Failed to fetch bank summary data from Xero.",
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
result: report,
|
|
27
|
+
isError: false,
|
|
28
|
+
error: null,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
return {
|
|
33
|
+
result: null,
|
|
34
|
+
isError: true,
|
|
35
|
+
error: formatError(error),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { formatError } from "../helpers/format-error.js";
|
|
3
|
+
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
4
|
+
// The SDK takes timeframe as a number of months (1=MONTH, 3=QUARTER, 12=YEAR).
|
|
5
|
+
const TIMEFRAME_MONTHS = {
|
|
6
|
+
MONTH: 1,
|
|
7
|
+
QUARTER: 3,
|
|
8
|
+
YEAR: 12,
|
|
9
|
+
};
|
|
10
|
+
async function fetchBudgetSummary(date, periods, timeframe) {
|
|
11
|
+
await xeroClient.authenticate();
|
|
12
|
+
const response = await xeroClient.accountingApi.getReportBudgetSummary(xeroClient.tenantId, date, periods, timeframe ? TIMEFRAME_MONTHS[timeframe] : undefined, getClientHeaders());
|
|
13
|
+
return response.body.reports?.[0] ?? null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Get the budget summary report from Xero (budgeted vs actual figures per
|
|
17
|
+
* account).
|
|
18
|
+
* @param date Optional date for the report (YYYY-MM-DD)
|
|
19
|
+
* @param periods Optional number of periods to compare (1-12)
|
|
20
|
+
* @param timeframe Optional period size to compare (MONTH, QUARTER, YEAR)
|
|
21
|
+
*/
|
|
22
|
+
export async function listXeroBudgetSummary(date, periods, timeframe) {
|
|
23
|
+
try {
|
|
24
|
+
const report = await fetchBudgetSummary(date, periods, timeframe);
|
|
25
|
+
if (!report) {
|
|
26
|
+
return {
|
|
27
|
+
result: null,
|
|
28
|
+
isError: true,
|
|
29
|
+
error: "Failed to fetch budget summary data from Xero.",
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
result: report,
|
|
34
|
+
isError: false,
|
|
35
|
+
error: null,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
return {
|
|
40
|
+
result: null,
|
|
41
|
+
isError: true,
|
|
42
|
+
error: formatError(error),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { formatError } from "../helpers/format-error.js";
|
|
2
|
+
import { listXeroCurrencies } from "../helpers/xero-currencies.js";
|
|
3
|
+
/**
|
|
4
|
+
* List currencies enabled in the connected Xero organisation.
|
|
5
|
+
*/
|
|
6
|
+
export async function listEnabledXeroCurrencies() {
|
|
7
|
+
try {
|
|
8
|
+
const currencies = await listXeroCurrencies();
|
|
9
|
+
return {
|
|
10
|
+
result: currencies,
|
|
11
|
+
isError: false,
|
|
12
|
+
error: null,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
return {
|
|
17
|
+
result: null,
|
|
18
|
+
isError: true,
|
|
19
|
+
error: formatError(error),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { formatError } from "../helpers/format-error.js";
|
|
3
|
+
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
4
|
+
async function fetchExecutiveSummary(date) {
|
|
5
|
+
await xeroClient.authenticate();
|
|
6
|
+
const response = await xeroClient.accountingApi.getReportExecutiveSummary(xeroClient.tenantId, date, getClientHeaders());
|
|
7
|
+
return response.body.reports?.[0] ?? null;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Get the executive summary report from Xero (a high-level snapshot of key
|
|
11
|
+
* financial metrics such as cash, debtors, creditors, and profit).
|
|
12
|
+
* @param date Optional date for the report (YYYY-MM-DD)
|
|
13
|
+
*/
|
|
14
|
+
export async function listXeroExecutiveSummary(date) {
|
|
15
|
+
try {
|
|
16
|
+
const report = await fetchExecutiveSummary(date);
|
|
17
|
+
if (!report) {
|
|
18
|
+
return {
|
|
19
|
+
result: null,
|
|
20
|
+
isError: true,
|
|
21
|
+
error: "Failed to fetch executive summary data from Xero.",
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
result: report,
|
|
26
|
+
isError: false,
|
|
27
|
+
error: null,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
return {
|
|
32
|
+
result: null,
|
|
33
|
+
isError: true,
|
|
34
|
+
error: formatError(error),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { formatError } from "../helpers/format-error.js";
|
|
3
|
+
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
4
|
+
async function getPurchaseOrders(status, dateFrom, dateTo, order, page, pageSize) {
|
|
5
|
+
await xeroClient.authenticate();
|
|
6
|
+
const response = await xeroClient.accountingApi.getPurchaseOrders(xeroClient.tenantId, undefined, // ifModifiedSince
|
|
7
|
+
status, dateFrom, dateTo, order, page, pageSize ?? 100, getClientHeaders());
|
|
8
|
+
return response.body.purchaseOrders ?? [];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* List purchase orders from Xero
|
|
12
|
+
*/
|
|
13
|
+
export async function listXeroPurchaseOrders(page = 1, status, dateFrom, dateTo, order, pageSize) {
|
|
14
|
+
try {
|
|
15
|
+
const purchaseOrders = await getPurchaseOrders(status, dateFrom, dateTo, order, page, pageSize);
|
|
16
|
+
return {
|
|
17
|
+
result: purchaseOrders,
|
|
18
|
+
isError: false,
|
|
19
|
+
error: null,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
return {
|
|
24
|
+
result: null,
|
|
25
|
+
isError: true,
|
|
26
|
+
error: formatError(error),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -2,6 +2,7 @@ import { xeroClient } from "../clients/xero-client.js";
|
|
|
2
2
|
import { formatError } from "../helpers/format-error.js";
|
|
3
3
|
import { Invoice } from "xero-node";
|
|
4
4
|
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
5
|
+
import { assertXeroCurrencyEnabled } from "../helpers/xero-currencies.js";
|
|
5
6
|
async function getInvoice(invoiceId) {
|
|
6
7
|
await xeroClient.authenticate();
|
|
7
8
|
// First, get the current invoice to check its status
|
|
@@ -43,6 +44,7 @@ export async function updateXeroInvoice(invoiceId, lineItems, reference, dueDate
|
|
|
43
44
|
error: `Cannot update invoice because it is not a draft. Current status: ${invoiceStatus}`,
|
|
44
45
|
};
|
|
45
46
|
}
|
|
47
|
+
await assertXeroCurrencyEnabled(currencyCode);
|
|
46
48
|
const updatedInvoice = await updateInvoice(invoiceId, lineItems, reference, dueDate, date, contactId, currencyCode);
|
|
47
49
|
if (!updatedInvoice) {
|
|
48
50
|
throw new Error("Invoice update failed.");
|
|
@@ -1,6 +1,37 @@
|
|
|
1
|
+
import { wrapUntrusted } from "../untrusted-content.js";
|
|
2
|
+
import { formatError } from "./format-error.js";
|
|
3
|
+
/**
|
|
4
|
+
* AGENTS.md security invariant #6: any text block a tool returns can carry
|
|
5
|
+
* Xero-authored (attacker-controllable) text — contact names, line item
|
|
6
|
+
* descriptions, references, history details/changes, validation messages.
|
|
7
|
+
* Envelope every text block here, at the single factory every tool is built
|
|
8
|
+
* with, so individual tools cannot forget to wrap their external text.
|
|
9
|
+
*/
|
|
10
|
+
function envelopToolResult(result, source) {
|
|
11
|
+
return {
|
|
12
|
+
...result,
|
|
13
|
+
content: result.content?.map((block) => block.type === "text"
|
|
14
|
+
? { ...block, text: wrapUntrusted(block.text, source) ?? block.text }
|
|
15
|
+
: block),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
1
18
|
export const CreateXeroTool = (name, description, schema, handler) => () => ({
|
|
2
19
|
name: name,
|
|
3
20
|
description: description,
|
|
4
21
|
schema: schema,
|
|
5
|
-
handler:
|
|
22
|
+
handler: (async (args, extra) => {
|
|
23
|
+
try {
|
|
24
|
+
return envelopToolResult(await handler(args, extra), `xero.${name}`);
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
// A rejection would otherwise bypass the envelope: the SDK serialises
|
|
28
|
+
// the raw error message into a plain, unwrapped text block. Format it
|
|
29
|
+
// (whitelisted fields only — SDK rejections can carry bearer tokens)
|
|
30
|
+
// and route it through the same choke point as any other result.
|
|
31
|
+
return envelopToolResult({
|
|
32
|
+
content: [{ type: "text", text: formatError(error) }],
|
|
33
|
+
isError: true,
|
|
34
|
+
}, `xero.${name}`);
|
|
35
|
+
}
|
|
36
|
+
}),
|
|
6
37
|
});
|
|
@@ -5,7 +5,8 @@ function isXeroSdkError(error) {
|
|
|
5
5
|
const response = error.response;
|
|
6
6
|
if (typeof response !== "object" || response === null)
|
|
7
7
|
return false;
|
|
8
|
-
return typeof response.statusCode === "number"
|
|
8
|
+
return (typeof response.statusCode === "number" ||
|
|
9
|
+
typeof response.status === "number");
|
|
9
10
|
}
|
|
10
11
|
function formatHttpStatus(status) {
|
|
11
12
|
switch (status) {
|
|
@@ -21,6 +22,69 @@ function formatHttpStatus(status) {
|
|
|
21
22
|
return "";
|
|
22
23
|
}
|
|
23
24
|
}
|
|
25
|
+
function isRecord(value) {
|
|
26
|
+
return typeof value === "object" && value !== null;
|
|
27
|
+
}
|
|
28
|
+
function getString(record, key) {
|
|
29
|
+
const value = record?.[key];
|
|
30
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
31
|
+
}
|
|
32
|
+
function getXeroBody(response) {
|
|
33
|
+
return response.body ?? response.data;
|
|
34
|
+
}
|
|
35
|
+
function getValidationMessages(body) {
|
|
36
|
+
if (!isRecord(body))
|
|
37
|
+
return [];
|
|
38
|
+
const messages = [];
|
|
39
|
+
const collectFromValidationErrors = (value) => {
|
|
40
|
+
if (!Array.isArray(value))
|
|
41
|
+
return;
|
|
42
|
+
for (const validationError of value) {
|
|
43
|
+
if (!isRecord(validationError))
|
|
44
|
+
continue;
|
|
45
|
+
const message = getString(validationError, "Message");
|
|
46
|
+
if (message)
|
|
47
|
+
messages.push(message);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
collectFromValidationErrors(body.ValidationErrors);
|
|
51
|
+
const elements = body.Elements;
|
|
52
|
+
if (Array.isArray(elements)) {
|
|
53
|
+
for (const element of elements) {
|
|
54
|
+
if (!isRecord(element))
|
|
55
|
+
continue;
|
|
56
|
+
collectFromValidationErrors(element.ValidationErrors);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return [...new Set(messages)];
|
|
60
|
+
}
|
|
61
|
+
function formatXeroResponseBody(status, body) {
|
|
62
|
+
if (!isRecord(body))
|
|
63
|
+
return `${status} HTTP error`;
|
|
64
|
+
const problem = isRecord(body.problem)
|
|
65
|
+
? body.problem
|
|
66
|
+
: undefined;
|
|
67
|
+
const title = problem?.title ??
|
|
68
|
+
getString(body, "Type") ??
|
|
69
|
+
getString(body, "httpStatusCode") ??
|
|
70
|
+
"HTTP error";
|
|
71
|
+
const validationMessages = getValidationMessages(body);
|
|
72
|
+
const detail = validationMessages.length > 0
|
|
73
|
+
? validationMessages.join("; ")
|
|
74
|
+
: problem?.detail ??
|
|
75
|
+
getString(body, "Detail") ??
|
|
76
|
+
getString(body, "Message");
|
|
77
|
+
return detail ? `${status} ${title}: ${detail}` : `${status} ${title}`;
|
|
78
|
+
}
|
|
79
|
+
function parseXeroSdkErrorString(error) {
|
|
80
|
+
try {
|
|
81
|
+
const parsed = JSON.parse(error);
|
|
82
|
+
return isRecord(parsed) ? parsed : null;
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
24
88
|
/**
|
|
25
89
|
* Format error messages for return to the LLM.
|
|
26
90
|
*
|
|
@@ -31,24 +95,29 @@ function formatHttpStatus(status) {
|
|
|
31
95
|
export function formatError(error) {
|
|
32
96
|
if (error instanceof AxiosError) {
|
|
33
97
|
const status = error.response?.status;
|
|
34
|
-
const detail = error.response?.data?.Detail;
|
|
35
98
|
if (status !== undefined) {
|
|
36
99
|
const mapped = formatHttpStatus(status);
|
|
37
100
|
if (mapped)
|
|
38
101
|
return mapped;
|
|
102
|
+
return formatXeroResponseBody(status, error.response?.data);
|
|
39
103
|
}
|
|
40
|
-
return
|
|
104
|
+
return "An error occurred while communicating with Xero.";
|
|
105
|
+
}
|
|
106
|
+
if (typeof error === "string") {
|
|
107
|
+
const parsed = parseXeroSdkErrorString(error);
|
|
108
|
+
return isXeroSdkError(parsed)
|
|
109
|
+
? formatError(parsed)
|
|
110
|
+
: "An unexpected error occurred while communicating with Xero.";
|
|
41
111
|
}
|
|
42
112
|
if (isXeroSdkError(error)) {
|
|
43
|
-
const status = error.response.statusCode;
|
|
113
|
+
const status = error.response.statusCode ?? error.response.status;
|
|
114
|
+
if (status === undefined) {
|
|
115
|
+
return "An unexpected error occurred while communicating with Xero.";
|
|
116
|
+
}
|
|
44
117
|
const mapped = formatHttpStatus(status);
|
|
45
118
|
if (mapped)
|
|
46
119
|
return mapped;
|
|
47
|
-
|
|
48
|
-
const problem = body?.problem;
|
|
49
|
-
const title = problem?.title ?? body?.httpStatusCode ?? "HTTP error";
|
|
50
|
-
const detail = problem?.detail ?? body?.Detail;
|
|
51
|
-
return detail ? `${status} ${title}: ${detail}` : `${status} ${title}`;
|
|
120
|
+
return formatXeroResponseBody(status, getXeroBody(error.response));
|
|
52
121
|
}
|
|
53
122
|
if (error instanceof Error) {
|
|
54
123
|
return error.message;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { formatError } from "./format-error.js";
|
|
2
3
|
import { contactDeepLink, creditNoteDeepLink, invoiceDeepLink, paymentDeepLink, manualJournalDeepLink, quoteDeepLink, billDeepLink, } from "../consts/deeplinks.js";
|
|
3
4
|
export var DeepLinkType;
|
|
4
5
|
(function (DeepLinkType) {
|
|
@@ -11,16 +12,26 @@ export var DeepLinkType;
|
|
|
11
12
|
DeepLinkType[DeepLinkType["BILL"] = 6] = "BILL";
|
|
12
13
|
})(DeepLinkType || (DeepLinkType = {}));
|
|
13
14
|
/**
|
|
14
|
-
* Gets a deep link for a specific type and item ID
|
|
15
|
-
*
|
|
15
|
+
* Gets a deep link for a specific type and item ID, or `null` when the link
|
|
16
|
+
* cannot be resolved. Best-effort by design: every caller has already
|
|
17
|
+
* committed a successful write in Xero, so a deeplink failure must never
|
|
18
|
+
* surface as a tool error — the model would retry and duplicate the record.
|
|
16
19
|
* @param type
|
|
17
20
|
* @param itemId
|
|
18
21
|
* @returns
|
|
19
22
|
*/
|
|
20
23
|
export const getDeepLink = async (type, itemId) => {
|
|
21
|
-
|
|
24
|
+
let orgShortCode;
|
|
25
|
+
try {
|
|
26
|
+
orgShortCode = await xeroClient.getShortCode();
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
console.warn(`Failed to resolve Xero deep link: ${formatError(error)}`);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
22
32
|
if (!orgShortCode) {
|
|
23
|
-
|
|
33
|
+
console.warn("Failed to resolve Xero deep link: no organisation short code");
|
|
34
|
+
return null;
|
|
24
35
|
}
|
|
25
36
|
switch (type) {
|
|
26
37
|
case DeepLinkType.CONTACT:
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { getClientHeaders } from "./get-client-headers.js";
|
|
3
|
+
export async function listXeroCurrencies() {
|
|
4
|
+
await xeroClient.authenticate();
|
|
5
|
+
const response = await xeroClient.accountingApi.getCurrencies(xeroClient.tenantId, undefined, // where
|
|
6
|
+
undefined, // order
|
|
7
|
+
getClientHeaders());
|
|
8
|
+
return response.body.currencies ?? [];
|
|
9
|
+
}
|
|
10
|
+
export async function assertXeroCurrencyEnabled(currencyCode) {
|
|
11
|
+
if (!currencyCode)
|
|
12
|
+
return;
|
|
13
|
+
const currencies = await listXeroCurrencies();
|
|
14
|
+
const enabledCurrencyCodes = currencies
|
|
15
|
+
.map((currency) => currency.code)
|
|
16
|
+
.filter((code) => Boolean(code));
|
|
17
|
+
if (enabledCurrencyCodes.includes(currencyCode))
|
|
18
|
+
return;
|
|
19
|
+
const enabledList = enabledCurrencyCodes.length
|
|
20
|
+
? enabledCurrencyCodes.join(", ")
|
|
21
|
+
: "none";
|
|
22
|
+
throw new Error(`Currency ${currencyCode} is not enabled in this Xero organisation. Enabled currencies: ${enabledList}. Add ${currencyCode} in Xero before creating or updating records in that currency.`);
|
|
23
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import { getPackageVersion } from "../helpers/get-package-version.js";
|
|
2
3
|
export class XeroMcpServer {
|
|
3
4
|
static instance = null;
|
|
4
5
|
constructor() { }
|
|
@@ -6,7 +7,7 @@ export class XeroMcpServer {
|
|
|
6
7
|
if (XeroMcpServer.instance === null) {
|
|
7
8
|
XeroMcpServer.instance = new McpServer({
|
|
8
9
|
name: "Xero MCP Server",
|
|
9
|
-
version:
|
|
10
|
+
version: getPackageVersion(),
|
|
10
11
|
});
|
|
11
12
|
}
|
|
12
13
|
return XeroMcpServer.instance;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createXeroContact } from "../../handlers/create-xero-contact.handler.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { DeepLinkType, getDeepLink } from "../../helpers/get-deeplink.js";
|
|
4
|
-
import {
|
|
4
|
+
import { formatError } from "../../helpers/format-error.js";
|
|
5
5
|
import { CreateXeroTool } from "../../helpers/create-xero-tool.js";
|
|
6
6
|
const CreateContactTool = CreateXeroTool("create-contact", "Create a contact in Xero.\
|
|
7
7
|
When a contact is created, a deep link to the contact in Xero is returned. \
|
|
@@ -42,12 +42,11 @@ const CreateContactTool = CreateXeroTool("create-contact", "Create a contact in
|
|
|
42
42
|
};
|
|
43
43
|
}
|
|
44
44
|
catch (error) {
|
|
45
|
-
const err = ensureError(error);
|
|
46
45
|
return {
|
|
47
46
|
content: [
|
|
48
47
|
{
|
|
49
48
|
type: "text",
|
|
50
|
-
text: `Error creating contact: ${
|
|
49
|
+
text: `Error creating contact: ${formatError(error)}`,
|
|
51
50
|
},
|
|
52
51
|
],
|
|
53
52
|
};
|