-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
executable file
·269 lines (233 loc) · 7.95 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env node
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import * as fs from "fs";
import * as fsPromises from "fs/promises";
import * as path from "path";
import * as os from "os";
import YAML from "yaml";
import { fileURLToPath } from "url";
import { dirname } from "path";
import { startServer } from "./server.js";
import readline from 'node:readline';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Function to prompt for ACCESS_TOKEN
async function promptForAccessToken(): Promise<string> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve) => {
rl.question('\n🔑 Enter your Square ACCESS_TOKEN: ', (token) => {
rl.close();
resolve(token.trim());
});
});
}
async function promptForSandboxMode(): Promise<boolean> {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve) => {
rl.question('\n🏝️ Use Square sandbox environment? (y/N): ', (answer) => {
rl.close();
resolve(answer.trim().toLowerCase() === 'y');
});
});
}
async function updateGooseConfig(accessToken: string, useSandbox: boolean): Promise<void> {
try {
console.log("\nUpdating Goose configuration...");
const homedir = os.homedir();
const gooseConfigPath = path.join(
homedir,
".config",
"goose",
"config.yaml"
);
// Check if Goose config directory exists
const gooseConfigDir = path.dirname(gooseConfigPath);
if (!fs.existsSync(gooseConfigDir)) {
console.warn("Warning: Goose directory not found at", gooseConfigDir);
console.warn("Goose does not appear to be installed on this system.");
console.warn("Skipping Goose configuration update.");
return;
}
// Read existing config or create new one
let config: any = { extensions: {} };
if (fs.existsSync(gooseConfigPath)) {
const configContent = await fsPromises.readFile(gooseConfigPath, "utf-8");
try {
config = YAML.parse(configContent) || { extensions: {} };
} catch (e) {
console.log("Error parsing Goose config, creating new one");
}
} else {
console.log("Goose config file not found, creating new one");
}
// Add or update Square MCP Server extension
if (!config.extensions) {
config.extensions = {};
}
config.extensions["square_mcp_server"] = {
name: "Square MCP Server",
cmd: "npx",
args: ["square-mcp-server", "start"],
enabled: true,
type: "stdio",
env: {
ACCESS_TOKEN: accessToken,
SANDBOX: useSandbox ? "true" : "false",
}
};
// Write updated config
await fsPromises.writeFile(
gooseConfigPath,
YAML.stringify(config, {
collectionStyle: "block",
})
);
console.log("Updated Goose configuration at", gooseConfigPath);
} catch (error) {
console.error("Failed to update Goose config:", error);
}
}
async function updateClaudeConfig(accessToken: string, useSandbox: boolean): Promise<void> {
try {
console.log("\nUpdating Claude configuration...");
const homedir = os.homedir();
const platform = os.platform();
let claudeConfigPath: string;
if (platform === "win32") {
claudeConfigPath = path.join(
homedir,
"AppData",
"Roaming",
"Claude",
"claude_desktop_config.json"
);
} else if (platform === "darwin") {
claudeConfigPath = path.join(
homedir,
"Library",
"Application Support",
"Claude",
"claude_desktop_config.json"
);
} else {
claudeConfigPath = path.join(homedir, ".config", "claude", "config.json");
}
// Check if Claude config directory exists
const claudeConfigDir = path.dirname(claudeConfigPath);
const claudeAppDir = path.dirname(claudeConfigDir);
if (!fs.existsSync(claudeAppDir)) {
console.warn("Warning: Claude app directory not found at", claudeAppDir);
console.warn("Claude does not appear to be installed on this system.");
console.warn("Skipping Claude configuration update.");
return;
}
if (!fs.existsSync(claudeConfigDir)) {
console.log("Claude config directory not found, creating it...");
await fsPromises.mkdir(claudeConfigDir, { recursive: true });
}
// Read existing config or create new one
let config: any = {};
if (fs.existsSync(claudeConfigPath)) {
const configContent = await fsPromises.readFile(claudeConfigPath, "utf-8");
try {
config = JSON.parse(configContent);
} catch (e) {
console.log("Error parsing Claude config, creating new one");
}
} else {
console.log("Claude config file not found, creating new one");
}
// Add or update Square MCP Server
if (!config.mcpServers) {
config.mcpServers = {};
}
config.mcpServers["mcp_square_api"] = {
command: "npx",
args: ["square-mcp-server", "start"],
env: {
ACCESS_TOKEN: accessToken,
SANDBOX: useSandbox ? "true" : "false",
}
};
// Write updated config
await fsPromises.writeFile(
claudeConfigPath,
JSON.stringify(config, null, 2)
);
console.log("Updated Claude configuration at", claudeConfigPath);
} catch (error) {
console.error("Failed to update Claude config:", error);
}
}
async function generateGooseUrl(): Promise<string> {
try {
const packageName = "square-mcp-server";
const gooseUrl = `goose://extension?cmd=npx&arg=${encodeURIComponent(
packageName
)}&arg=start&id=mcp_square_api&name=Square%20MCP%20Server&description=Square%20API%20MCP%20Server`;
return gooseUrl;
} catch (error) {
console.error("Failed to generate Goose URL:", error);
throw error;
}
}
async function main() {
const startCommand = async () => {
try {
await startServer();
} catch (error) {
console.error(" Failed to start server:", error);
process.exit(1);
}
};
const argv = await yargs(hideBin(process.argv))
.scriptName("mcp_square_api")
.usage("$0 [cmd] [args]")
.command("install", "Install Square MCP Server in Goose and Claude", {}, async () => {
console.log("\n🚀 Starting Square MCP Server installation...\n");
console.log("This will configure the Square MCP Server for use with Claude and Goose.");
// Prompt for Square access token
const accessToken = await promptForAccessToken();
if (!accessToken) {
console.warn("\nNo access token provided. Server will not be able to access Square API.");
console.warn("You can set ACCESS_TOKEN environment variable when starting the server manually.");
}
// Prompt for sandbox mode
const useSandbox = await promptForSandboxMode();
console.log(`\n${useSandbox ? '🏝️ Using sandbox environment' : '🌎 Using production environment'}`);
await updateClaudeConfig(accessToken, useSandbox);
await updateGooseConfig(accessToken, useSandbox);
const gooseUrl = await generateGooseUrl();
console.log("\n✨ Installation complete! ✨");
console.log(
"\n🦢 To add this extension to Goose, copy and paste the following URL into your browser:"
);
console.log(`\n\x1b[1m${gooseUrl}\x1b[0m\n`);
})
.command("start", "Start the Square MCP Server", {}, startCommand)
.command(
"get-goose-url",
"Get Goose URL for the server",
{},
async () => {
console.log("\n🔍 Getting Goose URL for Square MCP Server...");
const gooseUrl = await generateGooseUrl();
console.log("\n🦢 Goose URL for Square MCP Server:");
console.log(`\n\x1b[1m${gooseUrl}\x1b[0m\n`);
}
)
.command("$0", "Default command - starts the server", {}, startCommand)
.help()
.argv;
}
main().catch((error) => {
console.error("Error:", error);
process.exit(1);
});