@teamnetwork-nz/xero-mcp-server 1.15.0 → 1.15.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 CHANGED
@@ -106,9 +106,12 @@ Scopes and tools are controlled by environment variables. This means you only pr
106
106
  | *(core — always on)* | — | `accounting.transactions` `accounting.contacts` `accounting.settings` `accounting.attachments` `accounting.journals.read` | All invoicing, contact, payment, attachment, PDF & journal tools |
107
107
  | `XERO_ENABLE_REPORTS` | `true` | `accounting.reports.read` | Trial Balance, P&L, Balance Sheet, Aged Payables/Receivables |
108
108
  | `XERO_ENABLE_PAYROLL` | `false` | `payroll.settings` `payroll.employees` `payroll.timesheets` | All payroll tools |
109
+ | `XERO_READWRITE` | `false` | No additional scopes | Create, update, and delete tools |
109
110
 
110
111
  > **Note:** To use payroll tools, your Xero region must be **NZ** or **UK**, and your Xero app must have the payroll scopes provisioned.
111
112
 
113
+ `XERO_ENABLE_READWRITE=true` remains a deprecated compatibility alias for one release. `XERO_READWRITE` takes precedence when both are set.
114
+
112
115
  ### Full configuration example
113
116
 
114
117
  ```json
@@ -122,6 +125,7 @@ Scopes and tools are controlled by environment variables. This means you only pr
122
125
  "XERO_CLIENT_SECRET": "your_client_secret_here",
123
126
  "XERO_ENABLE_REPORTS": "true",
124
127
  "XERO_ENABLE_PAYROLL": "false",
128
+ "XERO_READWRITE": "false",
125
129
  "XERO_DOWNLOAD_PATH": "C:/Users/you/Downloads/Xero"
126
130
  }
127
131
  }
@@ -2,14 +2,14 @@ import axios from "axios";
2
2
  import dotenv from "dotenv";
3
3
  import { XeroClient, } from "xero-node";
4
4
  import { ensureError } from "../helpers/ensure-error.js";
5
- dotenv.config();
6
- const client_id = process.env.XERO_CLIENT_ID;
7
- const client_secret = process.env.XERO_CLIENT_SECRET;
8
- const bearer_token = process.env.XERO_CLIENT_BEARER_TOKEN;
5
+ import { loadXeroCredentials } from "../helpers/xero-config.js";
6
+ import { readWriteEnabled } from "../helpers/write-gate.js";
7
+ dotenv.config({ quiet: true });
8
+ const credentials = loadXeroCredentials();
9
+ const client_id = credentials.clientId;
10
+ const client_secret = credentials.clientSecret;
11
+ const bearer_token = credentials.bearerToken;
9
12
  const grant_type = "client_credentials";
10
- if (!bearer_token && (!client_id || !client_secret)) {
11
- throw Error("Environment Variables not set - please check your .env file");
12
- }
13
13
  /**
14
14
  * Builds the OAuth2 scope string based on enabled feature groups.
15
15
  *
@@ -131,6 +131,24 @@ class BearerTokenXeroClient extends MCPXeroClient {
131
131
  await this.updateTenants();
132
132
  }
133
133
  }
134
+ const writeEnabled = readWriteEnabled("XERO_READWRITE", { name: "XERO_ENABLE_READWRITE" });
135
+ export function assertXeroWritable() {
136
+ if (!writeEnabled)
137
+ throw new Error("Write operations are disabled. Set XERO_READWRITE=true to enable them.");
138
+ }
139
+ function writeProtectedApi(api) {
140
+ if (writeEnabled)
141
+ return api;
142
+ return new Proxy(api, {
143
+ get(target, property, receiver) {
144
+ const value = Reflect.get(target, property, receiver);
145
+ if (typeof property === "string" && typeof value === "function" && /^(create|update|delete|archive|void|approve|revert)/i.test(property)) {
146
+ return () => Promise.reject(new Error("Write operations are disabled. Set XERO_READWRITE=true to enable them."));
147
+ }
148
+ return typeof value === "function" ? value.bind(target) : value;
149
+ },
150
+ });
151
+ }
134
152
  export const xeroClient = bearer_token
135
153
  ? new BearerTokenXeroClient({
136
154
  bearerToken: bearer_token,
@@ -140,3 +158,8 @@ export const xeroClient = bearer_token
140
158
  clientSecret: client_secret,
141
159
  grantType: grant_type,
142
160
  });
161
+ if (!writeEnabled) {
162
+ const mutable = xeroClient;
163
+ mutable.accountingApi = writeProtectedApi(mutable.accountingApi);
164
+ mutable.payrollNZApi = writeProtectedApi(mutable.payrollNZApi);
165
+ }
package/dist/env.js ADDED
@@ -0,0 +1,12 @@
1
+ export function parsePositiveInt(name, raw, fallback, maximum) {
2
+ if (raw === undefined || raw.trim() === '')
3
+ return fallback;
4
+ if (!/^\d+$/.test(raw.trim())) {
5
+ throw new Error(`${name} must be an integer from 1 to ${maximum}`);
6
+ }
7
+ const value = Number(raw);
8
+ if (!Number.isSafeInteger(value) || value < 1 || value > maximum) {
9
+ throw new Error(`${name} must be an integer from 1 to ${maximum}`);
10
+ }
11
+ return value;
12
+ }
@@ -1,5 +1,5 @@
1
1
  import axios from "axios";
2
- import { xeroClient } from "../clients/xero-client.js";
2
+ import { assertXeroWritable, xeroClient } from "../clients/xero-client.js";
3
3
  import { formatError } from "../helpers/format-error.js";
4
4
  /**
5
5
  * Delete an attachment from an invoice by its attachment ID.
@@ -9,6 +9,7 @@ import { formatError } from "../helpers/format-error.js";
9
9
  */
10
10
  export async function deleteXeroInvoiceAttachment(invoiceId, attachmentId) {
11
11
  try {
12
+ assertXeroWritable();
12
13
  await xeroClient.authenticate();
13
14
  const tokenSet = xeroClient.readTokenSet();
14
15
  const accessToken = tokenSet.access_token;
@@ -18,3 +18,17 @@ export function getPackageVersion() {
18
18
  return '0.0.0'; // Fallback version
19
19
  }
20
20
  }
21
+ /** Gets the published package name from package.json. */
22
+ export function getPackageName() {
23
+ const __filename = fileURLToPath(import.meta.url);
24
+ const __dirname = dirname(__filename);
25
+ const packagePath = join(__dirname, '../../package.json');
26
+ try {
27
+ const packageJson = JSON.parse(readFileSync(packagePath, 'utf-8'));
28
+ return packageJson.name ?? 'xero-mcp-server';
29
+ }
30
+ catch (error) {
31
+ console.warn('Failed to read package name:', error);
32
+ return 'xero-mcp-server';
33
+ }
34
+ }
@@ -0,0 +1,10 @@
1
+ export function readWriteEnabled(canonicalName, legacy, env = process.env) {
2
+ const canonical = env[canonicalName];
3
+ if (canonical !== undefined)
4
+ return canonical.toLowerCase() === 'true';
5
+ if (!legacy || env[legacy.name] === undefined)
6
+ return false;
7
+ process.stderr.write(`[deprecated] ${legacy.name} is deprecated; use ${canonicalName}=true to enable writes.\n`);
8
+ const value = env[legacy.name].toLowerCase();
9
+ return legacy.readonlySemantics ? value === 'false' : value === 'true';
10
+ }
@@ -0,0 +1,21 @@
1
+ import { z } from 'zod';
2
+ export const XeroCredentialSchema = z.object({
3
+ clientId: z.string().min(1).optional(),
4
+ clientSecret: z.string().min(1).optional(),
5
+ bearerToken: z.string().min(1).optional(),
6
+ }).strict().superRefine((value, ctx) => {
7
+ const hasPair = Boolean(value.clientId && value.clientSecret);
8
+ if (!value.bearerToken && !hasPair) {
9
+ ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'Set XERO_CLIENT_BEARER_TOKEN, or set both XERO_CLIENT_ID and XERO_CLIENT_SECRET' });
10
+ }
11
+ if ((value.clientId && !value.clientSecret) || (!value.clientId && value.clientSecret)) {
12
+ ctx.addIssue({ code: z.ZodIssueCode.custom, message: 'XERO_CLIENT_ID and XERO_CLIENT_SECRET must be set together' });
13
+ }
14
+ });
15
+ export function loadXeroCredentials() {
16
+ return XeroCredentialSchema.parse({
17
+ clientId: process.env.XERO_CLIENT_ID?.trim() || undefined,
18
+ clientSecret: process.env.XERO_CLIENT_SECRET || undefined,
19
+ bearerToken: process.env.XERO_CLIENT_BEARER_TOKEN || undefined,
20
+ });
21
+ }
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ import { XeroMcpServer } from "./server/xero-mcp-server.js";
4
4
  import { ToolFactory } from "./tools/tool-factory.js";
5
5
  import { registerResources } from "./resources/register-resources.js";
6
6
  import { createHttpServer } from "./http-server.js";
7
+ import { parsePositiveInt } from "./env.js";
7
8
  const main = async () => {
8
9
  const createConfiguredServer = () => {
9
10
  const server = XeroMcpServer.GetServer(true);
@@ -14,8 +15,8 @@ const main = async () => {
14
15
  // Start receiving messages on stdin and sending messages on stdout
15
16
  if ((process.env["XERO_TRANSPORT"] ?? "stdio") === "http") {
16
17
  const host = process.env["XERO_HTTP_HOST"] ?? "127.0.0.1";
17
- const port = Number.parseInt(process.env["XERO_HTTP_PORT"] ?? "3000", 10);
18
- createHttpServer(createConfiguredServer, { host, port, serverName: "@teamnetwork-nz/xero-mcp-server", bearerToken: process.env["XERO_HTTP_BEARER_TOKEN"], allowedHosts: process.env["XERO_HTTP_ALLOWED_HOSTS"]?.split(",").map(v => v.trim()).filter(Boolean), allowedOrigins: process.env["XERO_HTTP_ALLOWED_ORIGINS"]?.split(",").map(v => v.trim()).filter(Boolean), maxBodyBytes: Number.parseInt(process.env["XERO_HTTP_MAX_BODY_BYTES"] ?? "1048576", 10) }).listen(port, host, () => console.error(`[xero-mcp] HTTP listening on ${host}:${port}/mcp`));
18
+ const port = parsePositiveInt("XERO_HTTP_PORT", process.env["XERO_HTTP_PORT"], 3000, 65535);
19
+ createHttpServer(createConfiguredServer, { host, port, serverName: "@teamnetwork-nz/xero-mcp-server", bearerToken: process.env["XERO_HTTP_BEARER_TOKEN"], allowedHosts: process.env["XERO_HTTP_ALLOWED_HOSTS"]?.split(",").map(v => v.trim()).filter(Boolean), allowedOrigins: process.env["XERO_HTTP_ALLOWED_ORIGINS"]?.split(",").map(v => v.trim()).filter(Boolean), maxBodyBytes: parsePositiveInt("XERO_HTTP_MAX_BODY_BYTES", process.env["XERO_HTTP_MAX_BODY_BYTES"], 1_048_576, 16_777_216) }).listen(port, host, () => process.stderr.write(`[xero-mcp] HTTP listening on ${host}:${port}/mcp\n`));
19
20
  }
20
21
  else {
21
22
  await createConfiguredServer().connect(new StdioServerTransport());
@@ -1,12 +1,13 @@
1
1
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
- import { getPackageVersion } from "../helpers/get-package-version.js";
2
+ import { getPackageName, getPackageVersion } from "../helpers/get-package-version.js";
3
+ import { enforceStrictToolInputs } from "../strict-tools.js";
3
4
  export class XeroMcpServer {
4
5
  static instance = null;
5
6
  constructor() { }
6
7
  static GetServer(fresh = false) {
7
8
  if (fresh || XeroMcpServer.instance === null) {
8
9
  XeroMcpServer.instance = new McpServer({
9
- name: "Xero MCP Server",
10
+ name: getPackageName(),
10
11
  version: getPackageVersion(),
11
12
  }, {
12
13
  instructions: [
@@ -43,6 +44,7 @@ export class XeroMcpServer {
43
44
  "For a full query playbook read the MCP resource: xero://guidance/agent-playbook",
44
45
  ].join("\n"),
45
46
  });
47
+ enforceStrictToolInputs(XeroMcpServer.instance);
46
48
  }
47
49
  return XeroMcpServer.instance;
48
50
  }
@@ -0,0 +1,29 @@
1
+ import { z } from "zod";
2
+ function isSchema(value) {
3
+ return typeof value === "object" && value !== null && "_def" in value;
4
+ }
5
+ function isRawShape(value) {
6
+ if (typeof value !== "object" || value === null || isSchema(value))
7
+ return false;
8
+ const values = Object.values(value);
9
+ return values.length === 0 || values.some(isSchema);
10
+ }
11
+ function strictSchema(schema) {
12
+ if (schema === undefined)
13
+ return z.object({}).strict();
14
+ if (isRawShape(schema))
15
+ return z.object(schema).strict();
16
+ if (isSchema(schema) && typeof schema.strict === "function")
17
+ return schema.strict();
18
+ return schema;
19
+ }
20
+ /** Make every tool object reject undeclared arguments, including no-argument tools. */
21
+ export function enforceStrictToolInputs(server) {
22
+ const mutable = server;
23
+ const createRegisteredTool = mutable._createRegisteredTool.bind(server);
24
+ mutable._createRegisteredTool = (...args) => {
25
+ // SDK 1.29 funnels both tool() and registerTool() through this method.
26
+ args[3] = strictSchema(args[3]);
27
+ return createRegisteredTool(...args);
28
+ };
29
+ }
@@ -3,10 +3,11 @@ import { CoreDeleteTools, PayrollDeleteTools } from "./delete/index.js";
3
3
  import { CoreGetTools, PayrollGetTools } from "./get/index.js";
4
4
  import { CoreListTools, ReportListTools, PayrollListTools } from "./list/index.js";
5
5
  import { CoreUpdateTools, PayrollUpdateTools } from "./update/index.js";
6
+ import { readWriteEnabled } from "../helpers/write-gate.js";
6
7
  export function ToolFactory(server) {
7
8
  const enableReports = process.env.XERO_ENABLE_REPORTS !== "false";
8
9
  const enablePayroll = process.env.XERO_ENABLE_PAYROLL === "true";
9
- const enableReadWrite = process.env.XERO_ENABLE_READWRITE === "true";
10
+ const enableReadWrite = readWriteEnabled("XERO_READWRITE", { name: "XERO_ENABLE_READWRITE" });
10
11
  const allTools = [
11
12
  ...CoreListTools,
12
13
  ...CoreGetTools,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teamnetwork-nz/xero-mcp-server",
3
- "version": "1.15.0",
3
+ "version": "1.15.1",
4
4
  "description": "MCP server implementation for Xero integration - TEAMnetwork",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -19,11 +19,11 @@
19
19
  "lint": "eslint ."
20
20
  },
21
21
  "dependencies": {
22
- "@modelcontextprotocol/sdk": "^1.23.4",
22
+ "@modelcontextprotocol/sdk": "^1.29.0",
23
23
  "dotenv": "^16.4.7",
24
24
  "openid-client": "^6.8.1",
25
25
  "xero-node": "^13.3.0",
26
- "zod": "3.25"
26
+ "zod": "^3.25.0"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@eslint/js": "^9.39.1",