@mindstone/mcp-server-xero 0.0.15 → 0.0.17

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 CHANGED
@@ -47,7 +47,7 @@ It is also the recommended approach if you are integrating this into 3rd party M
47
47
 
48
48
  Set up a Custom Connection following these instructions: https://developer.xero.com/documentation/guides/oauth2/custom-connections/
49
49
 
50
- Currently the following scopes are required for all sessions: [scopes](src/clients/xero-client.ts#L91-L92)
50
+ Currently the following scopes are required for Custom Connection sessions: [scopes](src/clients/xero-scopes.ts)
51
51
 
52
52
  ##### Integrating the MCP server with Claude Desktop
53
53
 
@@ -116,6 +116,7 @@ NOTE: The `XERO_CLIENT_BEARER_TOKEN` will take precedence over the `XERO_CLIENT_
116
116
  - `list-aged-receivables-by-contact`: Retrieves aged receivables for a contact
117
117
  - `list-aged-payables-by-contact`: Retrieves aged payables for a contact
118
118
  - `list-contact-groups`: Retrieve a list of contact groups
119
+ - `list-currencies`: Retrieve currencies enabled in the connected Xero organisation
119
120
  - `list-invoice-attachments`: Retrieve files attached to a Xero invoice
120
121
  - `create-contact`: Create a new contact
121
122
  - `create-credit-note`: Create a new credit note
@@ -125,7 +126,7 @@ NOTE: The `XERO_CLIENT_BEARER_TOKEN` will take precedence over the `XERO_CLIENT_
125
126
  - `create-credit-note`: Create a new credit note
126
127
  - `create-payroll-timesheet`: Create a new Payroll Timesheet
127
128
  - `update-contact`: Update an existing contact
128
- - `update-invoice`: Update an existing draft invoice. Supports optional `currencyCode`, such as `USD`, for draft invoices.
129
+ - `update-invoice`: Update an existing draft invoice. Supports optional `currencyCode`, such as `USD`, when that currency is enabled in the connected Xero organisation.
129
130
  - `update-quote`: Update an existing draft quote
130
131
  - `update-credit-note`: Update an existing draft credit note
131
132
  - `get-invoice-history`: Retrieve the history records for an invoice
@@ -3,6 +3,7 @@ import dotenv from "dotenv";
3
3
  import { XeroClient, } from "xero-node";
4
4
  import { ensureError } from "../helpers/ensure-error.js";
5
5
  import { formatTokenRequestError } from "../helpers/format-token-error.js";
6
+ import { XERO_CUSTOM_CONNECTION_SCOPE } from "./xero-scopes.js";
6
7
  dotenv.config();
7
8
  const client_id = process.env.XERO_CLIENT_ID;
8
9
  const client_secret = process.env.XERO_CLIENT_SECRET;
@@ -68,10 +69,9 @@ class CustomConnectionsXeroClient extends MCPXeroClient {
68
69
  return elapsed < this.tokenExpiresInMs - CustomConnectionsXeroClient.REFRESH_BUFFER_MS;
69
70
  }
70
71
  async getClientCredentialsToken() {
71
- const scope = "accounting.transactions accounting.contacts accounting.settings accounting.reports.read accounting.attachments.read payroll.settings payroll.employees payroll.timesheets";
72
72
  const credentials = Buffer.from(`${this.clientId}:${this.clientSecret}`).toString("base64");
73
73
  try {
74
- const response = await axios.post("https://identity.xero.com/connect/token", `grant_type=client_credentials&scope=${encodeURIComponent(scope)}`, {
74
+ const response = await axios.post("https://identity.xero.com/connect/token", `grant_type=client_credentials&scope=${encodeURIComponent(XERO_CUSTOM_CONNECTION_SCOPE)}`, {
75
75
  headers: {
76
76
  Authorization: `Basic ${credentials}`,
77
77
  "Content-Type": "application/x-www-form-urlencoded",
@@ -0,0 +1,23 @@
1
+ export const XERO_CUSTOM_CONNECTION_SCOPES = [
2
+ "accounting.attachments.read",
3
+ "accounting.banktransactions",
4
+ "accounting.contacts",
5
+ "accounting.invoices",
6
+ "accounting.manualjournals",
7
+ "accounting.payments",
8
+ "accounting.reports.aged.read",
9
+ "accounting.reports.balancesheet.read",
10
+ "accounting.reports.banksummary.read",
11
+ "accounting.reports.budgetsummary.read",
12
+ "accounting.reports.executivesummary.read",
13
+ "accounting.reports.profitandloss.read",
14
+ "accounting.reports.taxreports.read",
15
+ "accounting.reports.tenninetynine.read",
16
+ "accounting.reports.trialbalance.read",
17
+ "accounting.settings",
18
+ "payroll.employees",
19
+ "payroll.settings",
20
+ "payroll.timesheets",
21
+ ];
22
+ export const XERO_CUSTOM_CONNECTION_SCOPE = XERO_CUSTOM_CONNECTION_SCOPES.join(" ");
23
+ export const XERO_CUSTOM_CONNECTION_SCOPE_MESSAGE = XERO_CUSTOM_CONNECTION_SCOPES.join(", ");
@@ -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,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
+ }
@@ -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.");
@@ -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 detail || "An error occurred while communicating with Xero.";
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
- const body = error.response.body;
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,8 +1,8 @@
1
1
  import axios from "axios";
2
+ import { XERO_CUSTOM_CONNECTION_SCOPE_MESSAGE } from "../clients/xero-scopes.js";
2
3
  const REQUIRED_SCOPE_MESSAGE = "Xero authentication failed: your Custom Connection is missing required scopes. " +
3
4
  "Go to https://developer.xero.com/app/manage, select your app, and ensure these scopes are enabled: " +
4
- "accounting.transactions, accounting.contacts, accounting.settings, accounting.reports.read, " +
5
- "accounting.attachments.read, payroll.settings, payroll.employees, payroll.timesheets";
5
+ XERO_CUSTOM_CONNECTION_SCOPE_MESSAGE;
6
6
  function isRecord(value) {
7
7
  return typeof value === "object" && value !== null;
8
8
  }
@@ -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 invoices in that currency.`);
23
+ }
@@ -4,6 +4,7 @@ import ListAgedReceivablesByContact from "./list-aged-receivables-by-contact.too
4
4
  import ListBankTransactionsTool from "./list-bank-transactions.tool.js";
5
5
  import ListContactsTool from "./list-contacts.tool.js";
6
6
  import ListCreditNotesTool from "./list-credit-notes.tool.js";
7
+ import ListCurrenciesTool from "./list-currencies.tool.js";
7
8
  import ListInvoicesTool from "./list-invoices.tool.js";
8
9
  import ListItemsTool from "./list-items.tool.js";
9
10
  import ListManualJournalsTool from "./list-manual-journals.tool.js";
@@ -27,6 +28,7 @@ import ListInvoiceAttachmentsTool from "./list-invoice-attachments.tool.js";
27
28
  export const ListTools = [
28
29
  ListAccountsTool,
29
30
  ListContactsTool,
31
+ ListCurrenciesTool,
30
32
  ListCreditNotesTool,
31
33
  ListInvoicesTool,
32
34
  ListItemsTool,
@@ -0,0 +1,34 @@
1
+ import { listEnabledXeroCurrencies } from "../../handlers/list-xero-currencies.handler.js";
2
+ import { CreateXeroTool } from "../../helpers/create-xero-tool.js";
3
+ const ListCurrenciesTool = CreateXeroTool("list-currencies", "Lists currencies enabled in the connected Xero organisation. Use this before creating or updating invoices with a non-base currency.", {}, async () => {
4
+ const response = await listEnabledXeroCurrencies();
5
+ if (response.error !== null) {
6
+ return {
7
+ content: [
8
+ {
9
+ type: "text",
10
+ text: `Error listing currencies: ${response.error}`,
11
+ },
12
+ ],
13
+ };
14
+ }
15
+ const currencies = response.result;
16
+ return {
17
+ content: [
18
+ {
19
+ type: "text",
20
+ text: `Found ${currencies?.length || 0} enabled currencies:`,
21
+ },
22
+ ...(currencies?.map((currency) => ({
23
+ type: "text",
24
+ text: [
25
+ `Currency: ${currency.code || "No code"}`,
26
+ currency.description
27
+ ? `Description: ${currency.description}`
28
+ : null,
29
+ ].filter(Boolean).join("\n"),
30
+ })) || []),
31
+ ],
32
+ };
33
+ });
34
+ export default ListCurrenciesTool;
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@mindstone/mcp-server-xero",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "mcpName": "io.github.mindstone/mcp-server-xero",
5
5
  "description": "Xero MCP server for invoices, contacts, payments, quotes, and financial reports",
6
6
  "license": "MIT",
7
7
  "main": "dist/index.js",
8
8
  "repository": {
9
9
  "type": "git",
10
- "url": "https://github.com/mindstone/mcp-servers.git",
10
+ "url": "git+https://github.com/mindstone/mcp-servers.git",
11
11
  "directory": "connectors/xero"
12
12
  },
13
13
  "homepage": "https://github.com/mindstone/mcp-servers/tree/main/connectors/xero",