@commerce.io/mcp 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.
Files changed (2) hide show
  1. package/package.json +15 -0
  2. package/src/cli.mjs +105 -0
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@commerce.io/mcp",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "bin": {
6
+ "commerce-mcp": "./src/cli.mjs"
7
+ },
8
+ "scripts": {
9
+ "lint": "eslint src",
10
+ "clean": "rm -rf dist"
11
+ },
12
+ "devDependencies": {
13
+ "typescript": "^5.6.0"
14
+ }
15
+ }
package/src/cli.mjs ADDED
@@ -0,0 +1,105 @@
1
+ #!/usr/bin/env node
2
+ import { randomUUID } from 'node:crypto';
3
+ import { createInterface } from 'node:readline';
4
+
5
+ function parseArgs() {
6
+ const args = process.argv.slice(2);
7
+ const urlIdx = args.indexOf('--url');
8
+ if (urlIdx === -1 || urlIdx + 1 >= args.length) {
9
+ process.stderr.write('Usage: npx @commerce/mcp --url <mcp-endpoint-url>\n');
10
+ process.exit(1);
11
+ }
12
+ return { remoteUrl: args[urlIdx + 1] };
13
+ }
14
+
15
+ const { remoteUrl } = parseArgs();
16
+
17
+ const rl = createInterface({ input: process.stdin, crlfDelay: Infinity });
18
+ let sessionId = null;
19
+ let bearerToken = null;
20
+ let authPollTimer = null;
21
+
22
+ function send(msg) {
23
+ process.stdout.write(JSON.stringify(msg) + '\n');
24
+ }
25
+
26
+ async function mcpFetch(reqBody) {
27
+ const headers = { 'content-type': 'application/json' };
28
+ if (bearerToken !== null) headers['authorization'] = `Bearer ${bearerToken}`;
29
+ if (sessionId !== null && reqBody.method !== 'initialize') headers['mcp-session-id'] = sessionId;
30
+ const res = await fetch(remoteUrl, { method: 'POST', headers, body: JSON.stringify(reqBody) });
31
+ const text = await res.text();
32
+ if (reqBody.method === 'initialize') {
33
+ const newSession = res.headers.get('mcp-session-id');
34
+ if (newSession !== null) sessionId = newSession;
35
+ }
36
+ if (res.status === 404) {
37
+ try { const b = JSON.parse(text); if (b?.error?.message === 'Session not found') sessionId = null; } catch {}
38
+ }
39
+ return { text, status: res.status };
40
+ }
41
+
42
+ function captureToken(text) {
43
+ try {
44
+ const body = JSON.parse(text);
45
+ const sc = body?.result?.structuredContent ?? body?.result ?? body?.ucp;
46
+ if (sc?.access_token) bearerToken = sc.access_token;
47
+ } catch {}
48
+ }
49
+
50
+ function startAuthPolling(deviceCode, interval) {
51
+ if (authPollTimer) clearInterval(authPollTimer);
52
+ const ms = Math.max(3000, (interval || 5) * 1000);
53
+ authPollTimer = setInterval(async () => {
54
+ const { text } = await mcpFetch({
55
+ jsonrpc: '2.0',
56
+ id: 'poll-' + Date.now(),
57
+ method: 'tools/call',
58
+ params: { name: 'customer.auth_status', arguments: { device_code: deviceCode } },
59
+ });
60
+ try {
61
+ const body = JSON.parse(text);
62
+ const sc = body?.result?.structuredContent ?? body?.result;
63
+ if (sc?.status === 'completed' && sc?.access_token) {
64
+ bearerToken = sc.access_token;
65
+ clearInterval(authPollTimer);
66
+ authPollTimer = null;
67
+ }
68
+ } catch {}
69
+ }, ms);
70
+ }
71
+
72
+ for await (const line of rl) {
73
+ if (!line.trim()) continue;
74
+
75
+ let req;
76
+ try { req = JSON.parse(line); } catch {
77
+ send({ jsonrpc: '2.0', id: null, error: { code: -32700, message: 'Parse error' } });
78
+ continue;
79
+ }
80
+
81
+ if (req.method === 'tools/call' && req.params) {
82
+ req.params.meta ??= {};
83
+ req.params.meta['idempotency-key'] ??= randomUUID();
84
+ }
85
+
86
+ try {
87
+ const { text } = await mcpFetch(req);
88
+ captureToken(text);
89
+
90
+ if (req.method === 'tools/call' && req.params?.name === 'customer.authenticate') {
91
+ try {
92
+ const body = JSON.parse(text);
93
+ const sc = body?.result?.structuredContent ?? body?.result;
94
+ if (sc?.device_code && sc?.interval) {
95
+ startAuthPolling(sc.device_code, sc.interval);
96
+ }
97
+ } catch {}
98
+ }
99
+
100
+ process.stdout.write(text + '\n');
101
+ } catch (err) {
102
+ if (req.method !== 'initialize') sessionId = null;
103
+ send({ jsonrpc: '2.0', id: req.id ?? null, error: { code: -32603, message: err.message } });
104
+ }
105
+ }