@teamnetwork-nz/xero-mcp-server 1.14.2 → 1.15.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 +4 -0
- package/dist/http-server.js +83 -0
- package/dist/index.js +15 -6
- package/dist/server/xero-mcp-server.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# TEAMnetwork Xero MCP Server
|
|
2
2
|
|
|
3
|
+
## Streamable HTTP
|
|
4
|
+
|
|
5
|
+
Set `XERO_TRANSPORT=http` to serve stateless MCP at `/mcp`. It binds to `127.0.0.1` by default; remote binding requires `XERO_HTTP_BEARER_TOKEN` and `XERO_HTTP_ALLOWED_HOSTS`. Optional controls include `XERO_HTTP_PORT`, `XERO_HTTP_ALLOWED_ORIGINS`, and `XERO_HTTP_MAX_BODY_BYTES`. A health endpoint is available at `/health`.
|
|
6
|
+
|
|
3
7
|
A [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) server for Xero, maintained by [TEAMnetwork](https://teamnetwork.co.nz). Forked and extended from the [official Xero MCP Server](https://github.com/XeroAPI/xero-mcp-server).
|
|
4
8
|
|
|
5
9
|
Provides AI agents and MCP-compatible clients (e.g. Claude Desktop) with tools to read and write Xero accounting data, manage invoice attachments, and download invoice PDFs.
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { timingSafeEqual } from "node:crypto";
|
|
2
|
+
import http from "node:http";
|
|
3
|
+
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
4
|
+
const loopback = new Set(["127.0.0.1", "::1", "localhost"]);
|
|
5
|
+
const send = (res, status, body) => {
|
|
6
|
+
res.writeHead(status, { "Content-Type": "application/json" }).end(JSON.stringify(body));
|
|
7
|
+
};
|
|
8
|
+
const error = (res, status, code, message) => send(res, status, { jsonrpc: "2.0", error: { code, message }, id: null });
|
|
9
|
+
const matches = (value, expected) => {
|
|
10
|
+
if (!value?.startsWith("Bearer "))
|
|
11
|
+
return false;
|
|
12
|
+
const supplied = Buffer.from(value.slice(7));
|
|
13
|
+
const wanted = Buffer.from(expected);
|
|
14
|
+
return supplied.length === wanted.length && timingSafeEqual(supplied, wanted);
|
|
15
|
+
};
|
|
16
|
+
const readBody = (req, limit) => new Promise((resolve, reject) => {
|
|
17
|
+
const chunks = [];
|
|
18
|
+
let size = 0;
|
|
19
|
+
let oversized = false;
|
|
20
|
+
req.on("data", (chunk) => { size += chunk.length; oversized ||= size > limit; if (!oversized)
|
|
21
|
+
chunks.push(chunk); });
|
|
22
|
+
req.on("end", () => {
|
|
23
|
+
if (oversized)
|
|
24
|
+
return reject(Object.assign(new Error("Request body too large"), { statusCode: 413 }));
|
|
25
|
+
try {
|
|
26
|
+
resolve(JSON.parse(Buffer.concat(chunks).toString("utf8")));
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
reject(Object.assign(new Error("Invalid JSON body"), { statusCode: 400 }));
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
req.on("error", reject);
|
|
33
|
+
});
|
|
34
|
+
export function validateHttpOptions(o) {
|
|
35
|
+
if (!Number.isInteger(o.port) || o.port < 1 || o.port > 65535)
|
|
36
|
+
throw new Error(`Invalid HTTP port: ${o.port}`);
|
|
37
|
+
if (o.maxBodyBytes !== undefined && (!Number.isInteger(o.maxBodyBytes) || o.maxBodyBytes < 1))
|
|
38
|
+
throw new Error(`Invalid HTTP body limit: ${o.maxBodyBytes}`);
|
|
39
|
+
if (!loopback.has(o.host) && !o.bearerToken)
|
|
40
|
+
throw new Error("A bearer token is required when binding beyond loopback");
|
|
41
|
+
if (!loopback.has(o.host) && !o.allowedHosts?.length)
|
|
42
|
+
throw new Error("HTTP_ALLOWED_HOSTS is required when binding beyond loopback");
|
|
43
|
+
}
|
|
44
|
+
export function createHttpServer(factory, o) {
|
|
45
|
+
validateHttpOptions(o);
|
|
46
|
+
const hosts = new Set(o.allowedHosts?.map(v => v.toLowerCase()));
|
|
47
|
+
const origins = new Set(o.allowedOrigins?.map(v => v.toLowerCase()));
|
|
48
|
+
return http.createServer(async (req, res) => {
|
|
49
|
+
const path = new URL(req.url ?? "/", "http://localhost").pathname;
|
|
50
|
+
if (path === "/health")
|
|
51
|
+
return send(res, 200, { status: "ok", server: o.serverName });
|
|
52
|
+
if (path !== "/mcp")
|
|
53
|
+
return void res.writeHead(404).end();
|
|
54
|
+
if (hosts.size && !hosts.has((req.headers.host ?? "").toLowerCase()))
|
|
55
|
+
return error(res, 403, -32000, "Host not allowed");
|
|
56
|
+
if (req.headers.origin && !origins.has(req.headers.origin.toLowerCase()))
|
|
57
|
+
return error(res, 403, -32000, "Origin not allowed");
|
|
58
|
+
if (o.bearerToken && !matches(req.headers.authorization, o.bearerToken)) {
|
|
59
|
+
res.setHeader("WWW-Authenticate", "Bearer");
|
|
60
|
+
return error(res, 401, -32001, "Unauthorized");
|
|
61
|
+
}
|
|
62
|
+
if (req.method !== "POST") {
|
|
63
|
+
res.setHeader("Allow", "POST");
|
|
64
|
+
return error(res, 405, -32000, "Method not allowed");
|
|
65
|
+
}
|
|
66
|
+
const server = factory();
|
|
67
|
+
const transport = new StreamableHTTPServerTransport({ sessionIdGenerator: undefined });
|
|
68
|
+
try {
|
|
69
|
+
const body = await readBody(req, o.maxBodyBytes ?? 1048576);
|
|
70
|
+
await server.connect(transport);
|
|
71
|
+
await transport.handleRequest(req, res, body);
|
|
72
|
+
res.once("close", () => { void transport.close(); void server.close(); });
|
|
73
|
+
}
|
|
74
|
+
catch (cause) {
|
|
75
|
+
void transport.close();
|
|
76
|
+
void server.close();
|
|
77
|
+
if (!res.headersSent) {
|
|
78
|
+
const status = cause.statusCode ?? 500;
|
|
79
|
+
error(res, status, status === 400 ? -32700 : -32603, status < 500 ? cause.message : "Internal server error");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -3,14 +3,23 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
|
|
|
3
3
|
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
|
+
import { createHttpServer } from "./http-server.js";
|
|
6
7
|
const main = async () => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
const createConfiguredServer = () => {
|
|
9
|
+
const server = XeroMcpServer.GetServer(true);
|
|
10
|
+
ToolFactory(server);
|
|
11
|
+
registerResources(server);
|
|
12
|
+
return server;
|
|
13
|
+
};
|
|
11
14
|
// Start receiving messages on stdin and sending messages on stdout
|
|
12
|
-
|
|
13
|
-
|
|
15
|
+
if ((process.env["XERO_TRANSPORT"] ?? "stdio") === "http") {
|
|
16
|
+
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`));
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
await createConfiguredServer().connect(new StdioServerTransport());
|
|
22
|
+
}
|
|
14
23
|
};
|
|
15
24
|
main().catch((error) => {
|
|
16
25
|
console.error("Error:", error);
|
|
@@ -3,8 +3,8 @@ import { getPackageVersion } from "../helpers/get-package-version.js";
|
|
|
3
3
|
export class XeroMcpServer {
|
|
4
4
|
static instance = null;
|
|
5
5
|
constructor() { }
|
|
6
|
-
static GetServer() {
|
|
7
|
-
if (XeroMcpServer.instance === null) {
|
|
6
|
+
static GetServer(fresh = false) {
|
|
7
|
+
if (fresh || XeroMcpServer.instance === null) {
|
|
8
8
|
XeroMcpServer.instance = new McpServer({
|
|
9
9
|
name: "Xero MCP Server",
|
|
10
10
|
version: getPackageVersion(),
|