@mindstone/mcp-server-xero 0.0.15
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/LICENSE +21 -0
- package/README.md +199 -0
- package/dist/clients/xero-client.js +134 -0
- package/dist/consts/deeplinks.js +24 -0
- package/dist/handlers/approve-xero-payroll-timesheet.handler.js +28 -0
- package/dist/handlers/create-xero-bank-transaction.handler.js +50 -0
- package/dist/handlers/create-xero-contact.handler.js +49 -0
- package/dist/handlers/create-xero-credit-note.handler.js +49 -0
- package/dist/handlers/create-xero-invoice.handler.js +55 -0
- package/dist/handlers/create-xero-item.handler.js +59 -0
- package/dist/handlers/create-xero-manual-journal.handler.js +57 -0
- package/dist/handlers/create-xero-payment.handler.js +49 -0
- package/dist/handlers/create-xero-payroll-timesheet.handler.js +28 -0
- package/dist/handlers/create-xero-quote.handler.js +54 -0
- package/dist/handlers/create-xero-tracking-category.handler.js +35 -0
- package/dist/handlers/create-xero-tracking-option.handler.js +31 -0
- package/dist/handlers/delete-xero-payroll-timesheet.handler.js +28 -0
- package/dist/handlers/download-xero-invoice-attachment.handler.js +26 -0
- package/dist/handlers/get-xero-payroll-timesheet.handler.js +28 -0
- package/dist/handlers/history-handler-factory.js +46 -0
- package/dist/handlers/list-aged-payables-by-contact.handler.js +37 -0
- package/dist/handlers/list-aged-receivables-by-contact.handler.js +37 -0
- package/dist/handlers/list-xero-accounts.handler.js +32 -0
- package/dist/handlers/list-xero-bank-transactions.handler.js +31 -0
- package/dist/handlers/list-xero-contact-groups.handler.js +34 -0
- package/dist/handlers/list-xero-contacts.handler.js +37 -0
- package/dist/handlers/list-xero-credit-notes.handler.js +34 -0
- package/dist/handlers/list-xero-invoice-attachments.handler.js +20 -0
- package/dist/handlers/list-xero-invoices.handler.js +59 -0
- package/dist/handlers/list-xero-items.handler.js +32 -0
- package/dist/handlers/list-xero-manual-journals.handler.js +33 -0
- package/dist/handlers/list-xero-organisation-details.handler.js +32 -0
- package/dist/handlers/list-xero-payments.handler.js +53 -0
- package/dist/handlers/list-xero-payroll-employee-leave-balances.handler.js +42 -0
- package/dist/handlers/list-xero-payroll-employee-leave-types.handler.js +42 -0
- package/dist/handlers/list-xero-payroll-employee-leave.handler.js +44 -0
- package/dist/handlers/list-xero-payroll-employees.handler.js +31 -0
- package/dist/handlers/list-xero-payroll-leave-periods.handler.js +44 -0
- package/dist/handlers/list-xero-payroll-leave-types.handler.js +40 -0
- package/dist/handlers/list-xero-profit-and-loss.handler.js +52 -0
- package/dist/handlers/list-xero-quotes.handler.js +37 -0
- package/dist/handlers/list-xero-report-balance-sheet.handler.js +34 -0
- package/dist/handlers/list-xero-tax-rates.handler.js +30 -0
- package/dist/handlers/list-xero-timesheets.handler.js +29 -0
- package/dist/handlers/list-xero-tracking-categories.handler.js +29 -0
- package/dist/handlers/list-xero-trial-balance.handler.js +42 -0
- package/dist/handlers/revert-xero-payroll-timesheet.handler.js +28 -0
- package/dist/handlers/update-xero-bank-transaction.handler.js +56 -0
- package/dist/handlers/update-xero-contact.handler.js +66 -0
- package/dist/handlers/update-xero-credit-note.handler.js +61 -0
- package/dist/handlers/update-xero-invoice.handler.js +63 -0
- package/dist/handlers/update-xero-item.handler.js +46 -0
- package/dist/handlers/update-xero-manual-journal.handler.js +46 -0
- package/dist/handlers/update-xero-payroll-timesheet-add-line.handler.js +28 -0
- package/dist/handlers/update-xero-payroll-timesheet-update-line.handler.js +28 -0
- package/dist/handlers/update-xero-quote.handler.js +78 -0
- package/dist/handlers/update-xero-tracking-category.handler.js +43 -0
- package/dist/handlers/update-xero-tracking-options.handler.js +51 -0
- package/dist/helpers/create-xero-tool.js +6 -0
- package/dist/helpers/ensure-error.js +13 -0
- package/dist/helpers/format-aged-report-filter.js +8 -0
- package/dist/helpers/format-error.js +57 -0
- package/dist/helpers/format-line-item.js +13 -0
- package/dist/helpers/format-token-error.js +34 -0
- package/dist/helpers/format-tracking-option.js +7 -0
- package/dist/helpers/get-client-headers.js +8 -0
- package/dist/helpers/get-deeplink.js +41 -0
- package/dist/helpers/get-external-link.js +8 -0
- package/dist/helpers/get-package-version.js +20 -0
- package/dist/index.js +16 -0
- package/dist/server/xero-mcp-server.js +14 -0
- package/dist/tools/create/create-bank-transaction.tool.js +57 -0
- package/dist/tools/create/create-contact.tool.js +56 -0
- package/dist/tools/create/create-credit-note.tool.js +53 -0
- package/dist/tools/create/create-invoice.tool.js +76 -0
- package/dist/tools/create/create-item.tool.js +61 -0
- package/dist/tools/create/create-manual-journal.tool.js +116 -0
- package/dist/tools/create/create-payment.tool.js +71 -0
- package/dist/tools/create/create-payroll-timesheet.tool.js +40 -0
- package/dist/tools/create/create-quote.tool.js +57 -0
- package/dist/tools/create/create-tracking-category.tool.js +28 -0
- package/dist/tools/create/create-tracking-options.tool.js +30 -0
- package/dist/tools/create/index.js +24 -0
- package/dist/tools/delete/delete-payroll-timesheet.tool.js +28 -0
- package/dist/tools/delete/index.js +4 -0
- package/dist/tools/get/get-invoice-attachment.tool.js +45 -0
- package/dist/tools/get/get-payroll-timesheet.tool.js +49 -0
- package/dist/tools/get/index.js +6 -0
- package/dist/tools/history/index.js +141 -0
- package/dist/tools/list/index.js +54 -0
- package/dist/tools/list/list-accounts.tool.js +39 -0
- package/dist/tools/list/list-aged-payables-by-contact.tool.js +49 -0
- package/dist/tools/list/list-aged-receivables-by-contact.tool.js +49 -0
- package/dist/tools/list/list-bank-transactions.tool.js +63 -0
- package/dist/tools/list/list-contact-groups.tool.js +48 -0
- package/dist/tools/list/list-contacts.tool.js +70 -0
- package/dist/tools/list/list-credit-notes.tool.js +70 -0
- package/dist/tools/list/list-invoice-attachments.tool.js +42 -0
- package/dist/tools/list/list-invoices.tool.js +141 -0
- package/dist/tools/list/list-items.tool.js +53 -0
- package/dist/tools/list/list-manual-journals.tool.js +83 -0
- package/dist/tools/list/list-organisation-details.tool.js +90 -0
- package/dist/tools/list/list-payments.tool.js +84 -0
- package/dist/tools/list/list-payroll-employee-leave-balances.tool.js +39 -0
- package/dist/tools/list/list-payroll-employee-leave-types.tool.js +57 -0
- package/dist/tools/list/list-payroll-employee-leave.tool.js +42 -0
- package/dist/tools/list/list-payroll-employees.tool.js +48 -0
- package/dist/tools/list/list-payroll-leave-periods.tool.js +45 -0
- package/dist/tools/list/list-payroll-leave-types.tool.js +39 -0
- package/dist/tools/list/list-payroll-timesheets.tool.js +39 -0
- package/dist/tools/list/list-profit-and-loss.tool.js +45 -0
- package/dist/tools/list/list-quotes.tool.js +72 -0
- package/dist/tools/list/list-report-balance-sheet.tool.js +39 -0
- package/dist/tools/list/list-tax-rates.tool.js +49 -0
- package/dist/tools/list/list-tracking-categories.tool.js +39 -0
- package/dist/tools/list/list-trial-balance.tool.js +41 -0
- package/dist/tools/tool-factory.js +24 -0
- package/dist/tools/update/approve-payroll-timesheet.tool.js +29 -0
- package/dist/tools/update/index.js +28 -0
- package/dist/tools/update/revert-payroll-timesheet.tool.js +29 -0
- package/dist/tools/update/update-bank-transaction.tool.js +56 -0
- package/dist/tools/update/update-contact.tool.js +69 -0
- package/dist/tools/update/update-credit-note.tool.js +56 -0
- package/dist/tools/update/update-invoice.tool.js +74 -0
- package/dist/tools/update/update-item.tool.js +62 -0
- package/dist/tools/update/update-manual-journal-tool.js +89 -0
- package/dist/tools/update/update-payroll-timesheet-add-line.tool.js +34 -0
- package/dist/tools/update/update-payroll-timesheet-update-line.tool.js +35 -0
- package/dist/tools/update/update-quote.tool.js +61 -0
- package/dist/tools/update/update-tracking-category.tool.js +30 -0
- package/dist/tools/update/update-tracking-options.tool.js +35 -0
- package/dist/types/list-report-balance-sheet-params.js +1 -0
- package/dist/types/timeframeType.js +1 -0
- package/dist/types/tool-definition.js +1 -0
- package/dist/types/tool-list.js +1 -0
- package/dist/types/tool-response.js +1 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Xero Limited
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# Xero MCP Server
|
|
2
|
+
|
|
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
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Xero OAuth2 authentication with custom connections
|
|
8
|
+
- Contact management
|
|
9
|
+
- Chart of Accounts management
|
|
10
|
+
- Invoice creation and management
|
|
11
|
+
- MCP protocol compliance
|
|
12
|
+
|
|
13
|
+
## Prerequisites
|
|
14
|
+
|
|
15
|
+
- Node.js (v20 or higher)
|
|
16
|
+
- npm or pnpm
|
|
17
|
+
- A Xero developer account with API credentials
|
|
18
|
+
|
|
19
|
+
## Docs and Links
|
|
20
|
+
|
|
21
|
+
- [Xero Public API Documentation](https://developer.xero.com/documentation/api/)
|
|
22
|
+
- [Xero API Explorer](https://api-explorer.xero.com/)
|
|
23
|
+
- [Xero OpenAPI Specs](https://github.com/XeroAPI/Xero-OpenAPI)
|
|
24
|
+
- [Xero-Node Public API SDK Docs](https://xeroapi.github.io/xero-node/accounting)
|
|
25
|
+
- [Developer Documentation](https://developer.xero.com/)
|
|
26
|
+
|
|
27
|
+
## Setup
|
|
28
|
+
|
|
29
|
+
### Create a Xero Account
|
|
30
|
+
|
|
31
|
+
If you don't already have a Xero account and organisation already, can create one by signing up [here](https://www.xero.com/au/signup/) using the free trial.
|
|
32
|
+
|
|
33
|
+
We recommend using a Demo Company to start with because it comes with some pre-loaded sample data. Once you are logged in, switch to it by using the top left-hand dropdown and selecting "Demo Company". You can reset the data on a Demo Company, or change the country, at any time by using the top left-hand dropdown and navigating to [My Xero](https://my.xero.com).
|
|
34
|
+
|
|
35
|
+
NOTE: To use Payroll-specific queries, the region should be either NZ or UK.
|
|
36
|
+
|
|
37
|
+
### Authentication
|
|
38
|
+
|
|
39
|
+
There are 2 modes of authentication supported in the Xero MCP server:
|
|
40
|
+
|
|
41
|
+
#### 1. Custom Connections
|
|
42
|
+
|
|
43
|
+
This is a better choice for testing and development which allows you to specify client id and secrets for a specific organisation.
|
|
44
|
+
It is also the recommended approach if you are integrating this into 3rd party MCP clients such as Claude Desktop.
|
|
45
|
+
|
|
46
|
+
##### Configuring your Xero Developer account
|
|
47
|
+
|
|
48
|
+
Set up a Custom Connection following these instructions: https://developer.xero.com/documentation/guides/oauth2/custom-connections/
|
|
49
|
+
|
|
50
|
+
Currently the following scopes are required for all sessions: [scopes](src/clients/xero-client.ts#L91-L92)
|
|
51
|
+
|
|
52
|
+
##### Integrating the MCP server with Claude Desktop
|
|
53
|
+
|
|
54
|
+
To add the MCP server to Claude go to Settings > Developer > Edit config and add the following to your claude_desktop_config.json file:
|
|
55
|
+
|
|
56
|
+
```json
|
|
57
|
+
{
|
|
58
|
+
"mcpServers": {
|
|
59
|
+
"xero": {
|
|
60
|
+
"command": "npx",
|
|
61
|
+
"args": ["-y", "@mindstone/mcp-server-xero@latest"],
|
|
62
|
+
"env": {
|
|
63
|
+
"XERO_CLIENT_ID": "your_client_id_here",
|
|
64
|
+
"XERO_CLIENT_SECRET": "your_client_secret_here"
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
NOTE: If you are using [Node Version Manager](https://github.com/nvm-sh/nvm) `"command": "npx"` section change it to be the full path to the executable, ie: `your_home_directory/.nvm/versions/node/v22.14.0/bin/npx` on Mac / Linux or `"your_home_directory\\.nvm\\versions\\node\\v22.14.0\\bin\\npx"` on Windows
|
|
72
|
+
|
|
73
|
+
#### 2. Bearer Token
|
|
74
|
+
|
|
75
|
+
This is a better choice if you are to support multiple Xero accounts at runtime and allow the MCP client to execute an auth flow (such as PKCE) as required.
|
|
76
|
+
In this case, use the following configuration:
|
|
77
|
+
|
|
78
|
+
```json
|
|
79
|
+
{
|
|
80
|
+
"mcpServers": {
|
|
81
|
+
"xero": {
|
|
82
|
+
"command": "npx",
|
|
83
|
+
"args": ["-y", "@mindstone/mcp-server-xero@latest"],
|
|
84
|
+
"env": {
|
|
85
|
+
"XERO_CLIENT_BEARER_TOKEN": "your_bearer_token"
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
NOTE: The `XERO_CLIENT_BEARER_TOKEN` will take precedence over the `XERO_CLIENT_ID` if defined.
|
|
93
|
+
|
|
94
|
+
### Available MCP Commands
|
|
95
|
+
|
|
96
|
+
- `list-accounts`: Retrieve a list of accounts
|
|
97
|
+
- `list-contacts`: Retrieve a list of contacts from Xero
|
|
98
|
+
- `list-credit-notes`: Retrieve a list of credit notes
|
|
99
|
+
- `list-invoices`: Retrieve a list of invoices
|
|
100
|
+
- `list-items`: Retrieve a list of items
|
|
101
|
+
- `list-organisation-details`: Retrieve details about an organisation
|
|
102
|
+
- `list-profit-and-loss`: Retrieve a profit and loss report
|
|
103
|
+
- `list-quotes`: Retrieve a list of quotes
|
|
104
|
+
- `list-tax-rates`: Retrieve a list of tax rates
|
|
105
|
+
- `list-payments`: Retrieve a list of payments
|
|
106
|
+
- `list-trial-balance`: Retrieve a trial balance report
|
|
107
|
+
- `list-profit-and-loss`: Retrieve a profit and loss report
|
|
108
|
+
- `list-bank-transactions`: Retrieve a list of bank account transactions
|
|
109
|
+
- `list-payroll-employees`: Retrieve a list of Payroll Employees
|
|
110
|
+
- `list-report-balance-sheet`: Retrieve a balance sheet report
|
|
111
|
+
- `list-payroll-employee-leave`: Retrieve a Payroll Employee's leave records
|
|
112
|
+
- `list-payroll-employee-leave-balances`: Retrieve a Payroll Employee's leave balances
|
|
113
|
+
- `list-payroll-employee-leave-types`: Retrieve a list of Payroll leave types
|
|
114
|
+
- `list-payroll-leave-periods`: Retrieve a list of a Payroll Employee's leave periods
|
|
115
|
+
- `list-payroll-leave-types`: Retrieve a list of all avaliable leave types in Xero Payroll
|
|
116
|
+
- `list-aged-receivables-by-contact`: Retrieves aged receivables for a contact
|
|
117
|
+
- `list-aged-payables-by-contact`: Retrieves aged payables for a contact
|
|
118
|
+
- `list-contact-groups`: Retrieve a list of contact groups
|
|
119
|
+
- `list-invoice-attachments`: Retrieve files attached to a Xero invoice
|
|
120
|
+
- `create-contact`: Create a new contact
|
|
121
|
+
- `create-credit-note`: Create a new credit note
|
|
122
|
+
- `create-invoice`: Create a new invoice. Supports optional `currencyCode`, such as `USD`, when that currency is enabled in the connected Xero organisation.
|
|
123
|
+
- `create-payment`: Create a new payment
|
|
124
|
+
- `create-quote`: Create a new quote
|
|
125
|
+
- `create-credit-note`: Create a new credit note
|
|
126
|
+
- `create-payroll-timesheet`: Create a new Payroll Timesheet
|
|
127
|
+
- `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-quote`: Update an existing draft quote
|
|
130
|
+
- `update-credit-note`: Update an existing draft credit note
|
|
131
|
+
- `get-invoice-history`: Retrieve the history records for an invoice
|
|
132
|
+
- `add-invoice-note`: Add an immutable note to an invoice history
|
|
133
|
+
- `get-contact-history`: Retrieve the history records for a contact
|
|
134
|
+
- `add-contact-note`: Add an immutable note to a contact history
|
|
135
|
+
- `get-credit-note-history`: Retrieve the history records for a credit note
|
|
136
|
+
- `add-credit-note-note`: Add an immutable note to a credit note history
|
|
137
|
+
- `get-bank-transaction-history`: Retrieve the history records for a bank transaction
|
|
138
|
+
- `add-bank-transaction-note`: Add an immutable note to a bank transaction history
|
|
139
|
+
- `get-quote-history`: Retrieve the history records for a quote
|
|
140
|
+
- `add-quote-note`: Add an immutable note to a quote history
|
|
141
|
+
- `update-payroll-timesheet-line`: Update a line on an existing Payroll Timesheet
|
|
142
|
+
- `approve-payroll-timesheet`: Approve a Payroll Timesheet
|
|
143
|
+
- `revert-payroll-timesheet`: Revert an approved Payroll Timesheet
|
|
144
|
+
- `add-payroll-timesheet-line`: Add new line on an existing Payroll Timesheet
|
|
145
|
+
- `delete-payroll-timesheet`: Delete an existing Payroll Timesheet
|
|
146
|
+
- `get-payroll-timesheet`: Retrieve an existing Payroll Timesheet
|
|
147
|
+
|
|
148
|
+
For detailed API documentation, please refer to the [MCP Protocol Specification](https://modelcontextprotocol.io/).
|
|
149
|
+
|
|
150
|
+
## For Developers
|
|
151
|
+
|
|
152
|
+
### Installation
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
# Using npm
|
|
156
|
+
npm install
|
|
157
|
+
|
|
158
|
+
# Using pnpm
|
|
159
|
+
pnpm install
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Run a build
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
# Using npm
|
|
166
|
+
npm run build
|
|
167
|
+
|
|
168
|
+
# Using pnpm
|
|
169
|
+
pnpm build
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Integrating with Claude Desktop
|
|
173
|
+
|
|
174
|
+
To link your Xero MCP server in development to Claude Desktop go to Settings > Developer > Edit config and add the following to your `claude_desktop_config.json` file:
|
|
175
|
+
|
|
176
|
+
NOTE: For Windows ensure the `args` path escapes the `\` between folders ie. `"C:\\projects\xero-mcp-server\\dist\\index.js"`
|
|
177
|
+
|
|
178
|
+
```json
|
|
179
|
+
{
|
|
180
|
+
"mcpServers": {
|
|
181
|
+
"xero": {
|
|
182
|
+
"command": "node",
|
|
183
|
+
"args": ["insert-your-file-path-here/xero-mcp-server/dist/index.js"],
|
|
184
|
+
"env": {
|
|
185
|
+
"XERO_CLIENT_ID": "your_client_id_here",
|
|
186
|
+
"XERO_CLIENT_SECRET": "your_client_secret_here"
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## License
|
|
194
|
+
|
|
195
|
+
MIT
|
|
196
|
+
|
|
197
|
+
## Security
|
|
198
|
+
|
|
199
|
+
Please do not commit your `.env` file or any sensitive credentials to version control (it is included in `.gitignore` as a safe default.)
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import dotenv from "dotenv";
|
|
3
|
+
import { XeroClient, } from "xero-node";
|
|
4
|
+
import { ensureError } from "../helpers/ensure-error.js";
|
|
5
|
+
import { formatTokenRequestError } from "../helpers/format-token-error.js";
|
|
6
|
+
dotenv.config();
|
|
7
|
+
const client_id = process.env.XERO_CLIENT_ID;
|
|
8
|
+
const client_secret = process.env.XERO_CLIENT_SECRET;
|
|
9
|
+
const bearer_token = process.env.XERO_CLIENT_BEARER_TOKEN;
|
|
10
|
+
const grant_type = "client_credentials";
|
|
11
|
+
if (!bearer_token && (!client_id || !client_secret)) {
|
|
12
|
+
throw Error("Environment Variables not set - please check your .env file");
|
|
13
|
+
}
|
|
14
|
+
class MCPXeroClient extends XeroClient {
|
|
15
|
+
tenantId;
|
|
16
|
+
shortCode;
|
|
17
|
+
constructor(config) {
|
|
18
|
+
super(config);
|
|
19
|
+
this.tenantId = "";
|
|
20
|
+
this.shortCode = "";
|
|
21
|
+
}
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
23
|
+
async updateTenants(fullOrgDetails) {
|
|
24
|
+
await super.updateTenants(fullOrgDetails);
|
|
25
|
+
if (this.tenants && this.tenants.length > 0) {
|
|
26
|
+
this.tenantId = this.tenants[0].tenantId;
|
|
27
|
+
}
|
|
28
|
+
return this.tenants;
|
|
29
|
+
}
|
|
30
|
+
async getOrganisation() {
|
|
31
|
+
await this.authenticate();
|
|
32
|
+
const organisationResponse = await this.accountingApi.getOrganisations(this.tenantId || "");
|
|
33
|
+
const organisation = organisationResponse.body.organisations?.[0];
|
|
34
|
+
if (!organisation) {
|
|
35
|
+
throw new Error("Failed to retrieve organisation");
|
|
36
|
+
}
|
|
37
|
+
return organisation;
|
|
38
|
+
}
|
|
39
|
+
async getShortCode() {
|
|
40
|
+
if (!this.shortCode) {
|
|
41
|
+
try {
|
|
42
|
+
const organisation = await this.getOrganisation();
|
|
43
|
+
this.shortCode = organisation.shortCode ?? "";
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
const err = ensureError(error);
|
|
47
|
+
throw new Error(`Failed to get Organisation short code: ${err.message}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return this.shortCode;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
class CustomConnectionsXeroClient extends MCPXeroClient {
|
|
54
|
+
clientId;
|
|
55
|
+
clientSecret;
|
|
56
|
+
tokenObtainedAt = 0;
|
|
57
|
+
tokenExpiresInMs = 0;
|
|
58
|
+
static REFRESH_BUFFER_MS = 5 * 60 * 1000; // refresh 5 min before expiry
|
|
59
|
+
constructor(config) {
|
|
60
|
+
super(config);
|
|
61
|
+
this.clientId = config.clientId;
|
|
62
|
+
this.clientSecret = config.clientSecret;
|
|
63
|
+
}
|
|
64
|
+
isTokenValid() {
|
|
65
|
+
if (this.tokenExpiresInMs === 0)
|
|
66
|
+
return false;
|
|
67
|
+
const elapsed = Date.now() - this.tokenObtainedAt;
|
|
68
|
+
return elapsed < this.tokenExpiresInMs - CustomConnectionsXeroClient.REFRESH_BUFFER_MS;
|
|
69
|
+
}
|
|
70
|
+
async getClientCredentialsToken() {
|
|
71
|
+
const scope = "accounting.transactions accounting.contacts accounting.settings accounting.reports.read accounting.attachments.read payroll.settings payroll.employees payroll.timesheets";
|
|
72
|
+
const credentials = Buffer.from(`${this.clientId}:${this.clientSecret}`).toString("base64");
|
|
73
|
+
try {
|
|
74
|
+
const response = await axios.post("https://identity.xero.com/connect/token", `grant_type=client_credentials&scope=${encodeURIComponent(scope)}`, {
|
|
75
|
+
headers: {
|
|
76
|
+
Authorization: `Basic ${credentials}`,
|
|
77
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
78
|
+
Accept: "application/json",
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
// Get the tenant ID from the connections endpoint
|
|
82
|
+
const token = response.data.access_token;
|
|
83
|
+
const connectionsResponse = await axios.get("https://api.xero.com/connections", {
|
|
84
|
+
headers: {
|
|
85
|
+
Authorization: `Bearer ${token}`,
|
|
86
|
+
Accept: "application/json",
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
if (connectionsResponse.data && connectionsResponse.data.length > 0) {
|
|
90
|
+
this.tenantId = connectionsResponse.data[0].tenantId;
|
|
91
|
+
}
|
|
92
|
+
return response.data;
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
throw new Error(formatTokenRequestError(error));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async authenticate() {
|
|
99
|
+
if (this.isTokenValid())
|
|
100
|
+
return;
|
|
101
|
+
const tokenResponse = await this.getClientCredentialsToken();
|
|
102
|
+
this.tokenObtainedAt = Date.now();
|
|
103
|
+
if (tokenResponse.expires_in) {
|
|
104
|
+
this.tokenExpiresInMs = tokenResponse.expires_in * 1000;
|
|
105
|
+
}
|
|
106
|
+
this.setTokenSet({
|
|
107
|
+
access_token: tokenResponse.access_token,
|
|
108
|
+
expires_in: tokenResponse.expires_in,
|
|
109
|
+
token_type: tokenResponse.token_type,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
class BearerTokenXeroClient extends MCPXeroClient {
|
|
114
|
+
bearerToken;
|
|
115
|
+
constructor(config) {
|
|
116
|
+
super();
|
|
117
|
+
this.bearerToken = config.bearerToken;
|
|
118
|
+
}
|
|
119
|
+
async authenticate() {
|
|
120
|
+
this.setTokenSet({
|
|
121
|
+
access_token: this.bearerToken,
|
|
122
|
+
});
|
|
123
|
+
await this.updateTenants();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
export const xeroClient = bearer_token
|
|
127
|
+
? new BearerTokenXeroClient({
|
|
128
|
+
bearerToken: bearer_token,
|
|
129
|
+
})
|
|
130
|
+
: new CustomConnectionsXeroClient({
|
|
131
|
+
clientId: client_id,
|
|
132
|
+
clientSecret: client_secret,
|
|
133
|
+
grantType: grant_type,
|
|
134
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const invoiceDeepLink = (orgShortCode, invoiceId) => {
|
|
2
|
+
return ` https://go.xero.com/app/${orgShortCode}/invoicing/view/${invoiceId}`;
|
|
3
|
+
};
|
|
4
|
+
export const contactDeepLink = (orgShortCode, contactId) => {
|
|
5
|
+
return ` https://go.xero.com/app/${orgShortCode}/contacts/contact/${contactId}`;
|
|
6
|
+
};
|
|
7
|
+
export const creditNoteDeepLink = (orgShortCode, creditNoteId) => {
|
|
8
|
+
return `https://go.xero.com/organisationlogin/default.aspx?shortcode=${orgShortCode}&redirecturl=/AccountsPayable/ViewCreditNote.aspx?creditNoteID=${creditNoteId}`;
|
|
9
|
+
};
|
|
10
|
+
export const quoteDeepLink = (orgShortCode, quoteId) => {
|
|
11
|
+
return `https://go.xero.com/app/${orgShortCode}/quotes/view/${quoteId}`;
|
|
12
|
+
};
|
|
13
|
+
export const paymentDeepLink = (orgShortCode, paymentId) => {
|
|
14
|
+
return `https://go.xero.com/organisationlogin/default.aspx?shortcode=${orgShortCode}&redirecturl=/Bank/ViewTransaction.aspx?bankTransactionID=${paymentId}`;
|
|
15
|
+
};
|
|
16
|
+
export const bankTransactionDeepLink = (accountId, bankTransactionId) => {
|
|
17
|
+
return `https://go.xero.com/Bank/ViewTransaction.aspx?bankTransactionID=${bankTransactionId}&accountID=${accountId}`;
|
|
18
|
+
};
|
|
19
|
+
export const manualJournalDeepLink = (journalId) => {
|
|
20
|
+
return `https://go.xero.com/Journal/View.aspx?invoiceID=${journalId}`;
|
|
21
|
+
};
|
|
22
|
+
export const billDeepLink = (orgShortCode, billId) => {
|
|
23
|
+
return `https://go.xero.com/organisationlogin/default.aspx?shortcode=${orgShortCode}&redirecturl=/AccountsPayable/Edit.aspx?InvoiceID=${billId}`;
|
|
24
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { formatError } from "../helpers/format-error.js";
|
|
3
|
+
async function approveTimesheet(timesheetID) {
|
|
4
|
+
await xeroClient.authenticate();
|
|
5
|
+
// Call the approveTimesheet endpoint from the PayrollNZApi
|
|
6
|
+
const approvedTimesheet = await xeroClient.payrollNZApi.approveTimesheet(xeroClient.tenantId, timesheetID);
|
|
7
|
+
return approvedTimesheet.body.timesheet ?? null;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Approve a payroll timesheet in Xero
|
|
11
|
+
*/
|
|
12
|
+
export async function approveXeroPayrollTimesheet(timesheetID) {
|
|
13
|
+
try {
|
|
14
|
+
const approvedTimesheet = await approveTimesheet(timesheetID);
|
|
15
|
+
return {
|
|
16
|
+
result: approvedTimesheet,
|
|
17
|
+
isError: false,
|
|
18
|
+
error: null,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
return {
|
|
23
|
+
result: null,
|
|
24
|
+
isError: true,
|
|
25
|
+
error: formatError(error),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { BankTransaction } from "xero-node";
|
|
3
|
+
import { formatError } from "../helpers/format-error.js";
|
|
4
|
+
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
5
|
+
async function createBankTransaction(type, bankAccountId, contactId, lineItems, reference, date) {
|
|
6
|
+
await xeroClient.authenticate();
|
|
7
|
+
const bankTransaction = {
|
|
8
|
+
type: BankTransaction.TypeEnum[type],
|
|
9
|
+
bankAccount: {
|
|
10
|
+
accountID: bankAccountId
|
|
11
|
+
},
|
|
12
|
+
contact: {
|
|
13
|
+
contactID: contactId
|
|
14
|
+
},
|
|
15
|
+
lineItems: lineItems,
|
|
16
|
+
date: date ?? new Date().toISOString().split("T")[0],
|
|
17
|
+
reference: reference,
|
|
18
|
+
status: BankTransaction.StatusEnum.AUTHORISED
|
|
19
|
+
};
|
|
20
|
+
const response = await xeroClient.accountingApi.createBankTransactions(xeroClient.tenantId, // xeroTenantId
|
|
21
|
+
{
|
|
22
|
+
bankTransactions: [bankTransaction]
|
|
23
|
+
}, // bankTransactions
|
|
24
|
+
true, // summarizeErrors
|
|
25
|
+
undefined, // unitdp
|
|
26
|
+
undefined, // idempotencyKey
|
|
27
|
+
getClientHeaders());
|
|
28
|
+
const createdBankTransaction = response.body.bankTransactions?.[0];
|
|
29
|
+
return createdBankTransaction;
|
|
30
|
+
}
|
|
31
|
+
export async function createXeroBankTransaction(type, bankAccountId, contactId, lineItems, reference, date) {
|
|
32
|
+
try {
|
|
33
|
+
const createdTransaction = await createBankTransaction(type, bankAccountId, contactId, lineItems, reference, date);
|
|
34
|
+
if (!createdTransaction) {
|
|
35
|
+
throw new Error("Bank transaction creation failed.");
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
result: createdTransaction,
|
|
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,49 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { formatError } from "../helpers/format-error.js";
|
|
3
|
+
import { Phone } from "xero-node";
|
|
4
|
+
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
5
|
+
async function createContact(name, email, phone) {
|
|
6
|
+
await xeroClient.authenticate();
|
|
7
|
+
const contact = {
|
|
8
|
+
name,
|
|
9
|
+
emailAddress: email,
|
|
10
|
+
phones: phone
|
|
11
|
+
? [
|
|
12
|
+
{
|
|
13
|
+
phoneNumber: phone,
|
|
14
|
+
phoneType: Phone.PhoneTypeEnum.MOBILE,
|
|
15
|
+
},
|
|
16
|
+
]
|
|
17
|
+
: undefined,
|
|
18
|
+
};
|
|
19
|
+
const response = await xeroClient.accountingApi.createContacts(xeroClient.tenantId, {
|
|
20
|
+
contacts: [contact],
|
|
21
|
+
}, //contacts
|
|
22
|
+
true, //summarizeErrors
|
|
23
|
+
undefined, //idempotencyKey
|
|
24
|
+
getClientHeaders());
|
|
25
|
+
return response.body.contacts?.[0];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Create a new invoice in Xero
|
|
29
|
+
*/
|
|
30
|
+
export async function createXeroContact(name, email, phone) {
|
|
31
|
+
try {
|
|
32
|
+
const createdContact = await createContact(name, email, phone);
|
|
33
|
+
if (!createdContact) {
|
|
34
|
+
throw new Error("Contact creation failed.");
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
result: createdContact,
|
|
38
|
+
isError: false,
|
|
39
|
+
error: null,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
return {
|
|
44
|
+
result: null,
|
|
45
|
+
isError: true,
|
|
46
|
+
error: formatError(error),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { formatError } from "../helpers/format-error.js";
|
|
3
|
+
import { CreditNote } from "xero-node";
|
|
4
|
+
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
5
|
+
async function createCreditNote(contactId, lineItems, reference) {
|
|
6
|
+
await xeroClient.authenticate();
|
|
7
|
+
const creditNote = {
|
|
8
|
+
type: CreditNote.TypeEnum.ACCRECCREDIT,
|
|
9
|
+
contact: {
|
|
10
|
+
contactID: contactId,
|
|
11
|
+
},
|
|
12
|
+
lineItems: lineItems,
|
|
13
|
+
date: new Date().toISOString().split("T")[0], // Today's date
|
|
14
|
+
reference: reference,
|
|
15
|
+
status: CreditNote.StatusEnum.DRAFT,
|
|
16
|
+
};
|
|
17
|
+
const response = await xeroClient.accountingApi.createCreditNotes(xeroClient.tenantId, {
|
|
18
|
+
creditNotes: [creditNote],
|
|
19
|
+
}, // creditNotes
|
|
20
|
+
true, // summarizeErrors
|
|
21
|
+
undefined, // unitdp
|
|
22
|
+
undefined, // idempotencyKey
|
|
23
|
+
getClientHeaders());
|
|
24
|
+
const createdCreditNote = response.body.creditNotes?.[0];
|
|
25
|
+
return createdCreditNote;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Create a new credit note in Xero
|
|
29
|
+
*/
|
|
30
|
+
export async function createXeroCreditNote(contactId, lineItems, reference) {
|
|
31
|
+
try {
|
|
32
|
+
const createdCreditNote = await createCreditNote(contactId, lineItems, reference);
|
|
33
|
+
if (!createdCreditNote) {
|
|
34
|
+
throw new Error("Credit note creation failed.");
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
result: createdCreditNote,
|
|
38
|
+
isError: false,
|
|
39
|
+
error: null,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
return {
|
|
44
|
+
result: null,
|
|
45
|
+
isError: true,
|
|
46
|
+
error: formatError(error),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { xeroClient } from "../clients/xero-client.js";
|
|
2
|
+
import { formatError } from "../helpers/format-error.js";
|
|
3
|
+
import { Invoice } from "xero-node";
|
|
4
|
+
import { getClientHeaders } from "../helpers/get-client-headers.js";
|
|
5
|
+
async function createInvoice(contactId, lineItems, type, reference, date, currencyCode) {
|
|
6
|
+
await xeroClient.authenticate();
|
|
7
|
+
const invoice = {
|
|
8
|
+
type: type,
|
|
9
|
+
contact: {
|
|
10
|
+
contactID: contactId,
|
|
11
|
+
},
|
|
12
|
+
lineItems: lineItems,
|
|
13
|
+
date: date || new Date().toISOString().split("T")[0], // Use provided date or today's date
|
|
14
|
+
dueDate: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000)
|
|
15
|
+
.toISOString()
|
|
16
|
+
.split("T")[0], // 30 days from now
|
|
17
|
+
...(type === Invoice.TypeEnum.ACCPAY
|
|
18
|
+
? { invoiceNumber: reference }
|
|
19
|
+
: { reference: reference }),
|
|
20
|
+
...(currencyCode ? { currencyCode } : {}),
|
|
21
|
+
status: Invoice.StatusEnum.DRAFT,
|
|
22
|
+
};
|
|
23
|
+
const response = await xeroClient.accountingApi.createInvoices(xeroClient.tenantId, {
|
|
24
|
+
invoices: [invoice],
|
|
25
|
+
}, // invoices
|
|
26
|
+
true, //summarizeErrors
|
|
27
|
+
undefined, //unitdp
|
|
28
|
+
undefined, //idempotencyKey
|
|
29
|
+
getClientHeaders());
|
|
30
|
+
const createdInvoice = response.body.invoices?.[0];
|
|
31
|
+
return createdInvoice;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Create a new invoice in Xero
|
|
35
|
+
*/
|
|
36
|
+
export async function createXeroInvoice(contactId, lineItems, type = Invoice.TypeEnum.ACCREC, reference, date, currencyCode) {
|
|
37
|
+
try {
|
|
38
|
+
const createdInvoice = await createInvoice(contactId, lineItems, type, reference, date, currencyCode);
|
|
39
|
+
if (!createdInvoice) {
|
|
40
|
+
throw new Error("Invoice creation failed.");
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
result: createdInvoice,
|
|
44
|
+
isError: false,
|
|
45
|
+
error: null,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
return {
|
|
50
|
+
result: null,
|
|
51
|
+
isError: true,
|
|
52
|
+
error: formatError(error),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
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 createItem(itemDetails) {
|
|
5
|
+
await xeroClient.authenticate();
|
|
6
|
+
const item = {
|
|
7
|
+
code: itemDetails.code,
|
|
8
|
+
name: itemDetails.name,
|
|
9
|
+
description: itemDetails.description,
|
|
10
|
+
purchaseDescription: itemDetails.purchaseDescription,
|
|
11
|
+
isPurchased: !!itemDetails.purchaseDetails,
|
|
12
|
+
isSold: !!itemDetails.salesDetails,
|
|
13
|
+
purchaseDetails: itemDetails.purchaseDetails
|
|
14
|
+
? {
|
|
15
|
+
unitPrice: itemDetails.purchaseDetails.unitPrice,
|
|
16
|
+
taxType: itemDetails.purchaseDetails.taxType,
|
|
17
|
+
accountCode: itemDetails.purchaseDetails.accountCode,
|
|
18
|
+
}
|
|
19
|
+
: undefined,
|
|
20
|
+
salesDetails: itemDetails.salesDetails
|
|
21
|
+
? {
|
|
22
|
+
unitPrice: itemDetails.salesDetails.unitPrice,
|
|
23
|
+
taxType: itemDetails.salesDetails.taxType,
|
|
24
|
+
accountCode: itemDetails.salesDetails.accountCode,
|
|
25
|
+
}
|
|
26
|
+
: undefined,
|
|
27
|
+
isTrackedAsInventory: itemDetails.isTrackedAsInventory,
|
|
28
|
+
inventoryAssetAccountCode: itemDetails.inventoryAssetAccountCode,
|
|
29
|
+
};
|
|
30
|
+
const items = {
|
|
31
|
+
items: [item],
|
|
32
|
+
};
|
|
33
|
+
const response = await xeroClient.accountingApi.createItems(xeroClient.tenantId, items, // items
|
|
34
|
+
true, // summarizeErrors
|
|
35
|
+
undefined, // unitdp
|
|
36
|
+
undefined, // idempotencyKey
|
|
37
|
+
getClientHeaders());
|
|
38
|
+
return response.body.items?.[0] ?? null;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Create an item in Xero
|
|
42
|
+
*/
|
|
43
|
+
export async function createXeroItem(itemDetails) {
|
|
44
|
+
try {
|
|
45
|
+
const item = await createItem(itemDetails);
|
|
46
|
+
return {
|
|
47
|
+
result: item,
|
|
48
|
+
isError: false,
|
|
49
|
+
error: null,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
return {
|
|
54
|
+
result: null,
|
|
55
|
+
isError: true,
|
|
56
|
+
error: formatError(error),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|