@heroku/mcp-server 0.0.1-alpha.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 (42) hide show
  1. package/LICENSE.txt +206 -0
  2. package/README.md +279 -0
  3. package/dist/index.js +86 -0
  4. package/dist/repl/heroku-cli-repl.js +210 -0
  5. package/dist/repl/heroku-cli-repl.spec.js +106 -0
  6. package/dist/services/app-service.js +42 -0
  7. package/dist/services/app-setup-service.js +72 -0
  8. package/dist/services/build-service.js +149 -0
  9. package/dist/services/source-service.js +42 -0
  10. package/dist/tools/addons.js +241 -0
  11. package/dist/tools/addons.spec.js +335 -0
  12. package/dist/tools/apps.js +222 -0
  13. package/dist/tools/apps.spec.js +332 -0
  14. package/dist/tools/data.js +345 -0
  15. package/dist/tools/data.spec.js +300 -0
  16. package/dist/tools/deploy-to-heroku.js +449 -0
  17. package/dist/tools/deploy-to-heroku.spec.js +166 -0
  18. package/dist/tools/logs.js +61 -0
  19. package/dist/tools/logs.spec.js +112 -0
  20. package/dist/tools/maintenance.js +55 -0
  21. package/dist/tools/maintenance.spec.js +115 -0
  22. package/dist/tools/pipelines.js +146 -0
  23. package/dist/tools/pipelines.spec.js +183 -0
  24. package/dist/tools/ps.js +99 -0
  25. package/dist/tools/ps.spec.js +127 -0
  26. package/dist/tools/spaces.js +34 -0
  27. package/dist/tools/spaces.spec.js +71 -0
  28. package/dist/tools/teams.js +37 -0
  29. package/dist/tools/teams.spec.js +69 -0
  30. package/dist/utils/app-json.schema.json +232 -0
  31. package/dist/utils/command-builder.js +61 -0
  32. package/dist/utils/command-builder.spec.js +95 -0
  33. package/dist/utils/generate-request-init.js +18 -0
  34. package/dist/utils/handle-cli-output.js +26 -0
  35. package/dist/utils/handle-cli-output.spec.js +54 -0
  36. package/dist/utils/mcp-tool-response.js +1 -0
  37. package/dist/utils/read-app-json.js +39 -0
  38. package/dist/utils/read-app-json.spec.js +106 -0
  39. package/dist/utils/tarball.js +114 -0
  40. package/dist/utils/tarball.spec.js +98 -0
  41. package/dist/utils/tool-commands.js +47 -0
  42. package/package.json +85 -0
@@ -0,0 +1,345 @@
1
+ import { z } from 'zod';
2
+ import { handleCliOutput } from '../utils/handle-cli-output.js';
3
+ import { CommandBuilder } from '../utils/command-builder.js';
4
+ import { TOOL_COMMAND_MAP } from '../utils/tool-commands.js';
5
+ /**
6
+ * Schema for executing PostgreSQL queries.
7
+ * This schema defines the structure and validation rules for running SQL queries.
8
+ */
9
+ export const pgPsqlOptionsSchema = z.object({
10
+ command: z
11
+ .string()
12
+ .optional()
13
+ .describe('SQL command to run; file is ignored if provided; must be single line; must supply either command or file'),
14
+ file: z
15
+ .string()
16
+ .optional()
17
+ .describe('SQL file to run; command is ignored if provided; must be an absolute path; must supply either command or file'),
18
+ credential: z.string().optional().describe('credential to use'),
19
+ app: z.string().describe('app to run command against'),
20
+ database: z
21
+ .string()
22
+ .optional()
23
+ .describe("Config var containing the connection string, unique name, ID, or alias of the database. To access another app's database, prepend the app name to the config var or alias with `APP_NAME::`. If omitted, DATABASE_URL is used.")
24
+ });
25
+ /**
26
+ * Schema for getting database information.
27
+ */
28
+ export const pgInfoOptionsSchema = z.object({
29
+ app: z.string().describe('The name of the Heroku app whose database to inspect.'),
30
+ database: z
31
+ .string()
32
+ .optional()
33
+ .describe("Config var containing the connection string, unique name, ID, or alias of the database. To access another app's database, prepend the app name to the config var or alias with `APP_NAME::`. If omitted, all databases are shown.")
34
+ });
35
+ /**
36
+ * Schema for viewing active queries.
37
+ */
38
+ export const pgPsOptionsSchema = z.object({
39
+ app: z.string().describe('The name of the Heroku app whose database processes to view.'),
40
+ verbose: z
41
+ .boolean()
42
+ .optional()
43
+ .describe('When true, shows additional query details including query plan and memory usage.'),
44
+ database: z
45
+ .string()
46
+ .optional()
47
+ .describe("Config var containing the connection string, unique name, ID, or alias of the database. To access another app's database, prepend the app name to the config var or alias with `APP_NAME::`. If omitted, DATABASE_URL is used.")
48
+ });
49
+ /**
50
+ * Schema for viewing database locks.
51
+ */
52
+ export const pgLocksOptionsSchema = z.object({
53
+ app: z.string().describe('The name of the Heroku app whose database locks to view.'),
54
+ truncate: z.boolean().optional().describe('When true, truncates queries to 40 characters.'),
55
+ database: z
56
+ .string()
57
+ .optional()
58
+ .describe("Config var containing the connection string, unique name, ID, or alias of the database. To access another app's database, prepend the app name to the config var or alias with `APP_NAME::`. If omitted, DATABASE_URL is used.")
59
+ });
60
+ /**
61
+ * Schema for viewing query statistics.
62
+ */
63
+ export const pgOutliersOptionsSchema = z.object({
64
+ app: z.string().describe('The name of the Heroku app whose query statistics to analyze.'),
65
+ num: z.number().optional().describe('The number of queries to display. Defaults to 10.'),
66
+ reset: z.boolean().optional().describe('When true, resets statistics gathered by pg_stat_statements.'),
67
+ truncate: z.boolean().optional().describe('When true, truncates queries to 40 characters.'),
68
+ database: z
69
+ .string()
70
+ .optional()
71
+ .describe("Config var containing the connection string, unique name, ID, or alias of the database. To access another app's database, prepend the app name to the config var or alias with `APP_NAME::`. If omitted, DATABASE_URL is used.")
72
+ });
73
+ /**
74
+ * Schema for viewing database credentials.
75
+ */
76
+ export const pgCredentialsOptionsSchema = z.object({
77
+ app: z.string().describe('The name of the Heroku app whose database credentials to view.'),
78
+ database: z
79
+ .string()
80
+ .optional()
81
+ .describe("Config var containing the connection string, unique name, ID, or alias of the database. To access another app's database, prepend the app name to the config var or alias with `APP_NAME::`. If omitted, DATABASE_URL is used.")
82
+ });
83
+ /**
84
+ * Schema for terminating database processes.
85
+ */
86
+ export const pgKillOptionsSchema = z.object({
87
+ app: z.string().describe('The name of the Heroku app whose database process to terminate.'),
88
+ pid: z.number().describe('The process ID to terminate, as shown by pg_ps.'),
89
+ force: z.boolean().optional().describe('When true, forces immediate termination instead of graceful shutdown.'),
90
+ database: z
91
+ .string()
92
+ .optional()
93
+ .describe("Config var containing the connection string, unique name, ID, or alias of the database. To access another app's database, prepend the app name to the config var or alias with `APP_NAME::`. If omitted, DATABASE_URL is used.")
94
+ });
95
+ /**
96
+ * Schema for managing database maintenance.
97
+ */
98
+ export const pgMaintenanceOptionsSchema = z.object({
99
+ app: z.string().describe('Show current maintenance information for the app.'),
100
+ database: z
101
+ .string()
102
+ .optional()
103
+ .describe("Config var containing the connection string, unique name, ID, or alias of the database. To access another app's database, prepend the app name to the config var or alias with `APP_NAME::`. If omitted, DATABASE_URL is used.")
104
+ });
105
+ /**
106
+ * Schema for managing database backups.
107
+ */
108
+ export const pgBackupsOptionsSchema = z.object({
109
+ app: z.string().describe('The name of the Heroku app whose backups to manage.')
110
+ });
111
+ /**
112
+ * Schema for upgrading database version.
113
+ */
114
+ export const pgUpgradeOptionsSchema = z.object({
115
+ app: z.string().describe('The name of the Heroku app whose database to upgrade.'),
116
+ version: z.string().optional().describe('PostgreSQL version to upgrade to'),
117
+ confirm: z.string().optional().describe('Confirmation string required for this potentially destructive operation.'),
118
+ database: z
119
+ .string()
120
+ .optional()
121
+ .describe("Config var containing the connection string, unique name, ID, or alias of the database. To access another app's database, prepend the app name to the config var or alias with `APP_NAME::`. If omitted, DATABASE_URL is used.")
122
+ });
123
+ /**
124
+ * Registers the pg:psql tool with the MCP server.
125
+ *
126
+ * @param server - The MCP server instance to register the tool with
127
+ * @param herokuRepl - The Heroku REPL instance for executing commands
128
+ */
129
+ export const registerPgPsqlTool = (server, herokuRepl) => {
130
+ server.tool('pg_psql', 'Execute SQL queries against Heroku PostgreSQL databases. Use this tool when you need to: ' +
131
+ '1) Run SQL queries for database analysis, 2) Investigate database locks and performance, ' +
132
+ '3) Make schema modifications or updates, 4) Execute complex database operations. ' +
133
+ 'The tool provides direct SQL access with support for file-based queries and credential management.', pgPsqlOptionsSchema.shape, async (options) => {
134
+ const command = new CommandBuilder(TOOL_COMMAND_MAP.PG_PSQL)
135
+ .addFlags({
136
+ app: options.app,
137
+ command: `"${options.command?.replaceAll('\n', ' ') ?? ''}"`,
138
+ file: options.file,
139
+ credential: options.credential
140
+ })
141
+ .addPositionalArguments({ database: options.database })
142
+ .build();
143
+ const output = await herokuRepl.executeCommand(command);
144
+ return handleCliOutput(output);
145
+ });
146
+ };
147
+ /**
148
+ * Registers the pg:info tool with the MCP server.
149
+ *
150
+ * @param server - The MCP server instance to register the tool with
151
+ * @param herokuRepl - The Heroku REPL instance for executing commands
152
+ */
153
+ export const registerPgInfoTool = (server, herokuRepl) => {
154
+ server.tool('pg_info', 'Display detailed information about Heroku PostgreSQL databases. Use this tool when you need to: ' +
155
+ '1) View comprehensive database configuration and status, 2) Monitor database performance metrics, ' +
156
+ '3) Check connection and resource utilization, 4) Assess database health and capacity. ' +
157
+ 'The tool provides detailed insights into database operations and configuration.', pgInfoOptionsSchema.shape, async (options) => {
158
+ const command = new CommandBuilder(TOOL_COMMAND_MAP.PG_INFO)
159
+ .addFlags({
160
+ app: options.app
161
+ })
162
+ .addPositionalArguments({ database: options.database })
163
+ .build();
164
+ const output = await herokuRepl.executeCommand(command);
165
+ return handleCliOutput(output);
166
+ });
167
+ };
168
+ /**
169
+ * Registers the pg:ps tool with the MCP server.
170
+ *
171
+ * @param server - The MCP server instance to register the tool with
172
+ * @param herokuRepl - The Heroku REPL instance for executing commands
173
+ */
174
+ export const registerPgPsTool = (server, herokuRepl) => {
175
+ server.tool('pg_ps', 'Monitor active database queries and processes. Use this tool when you need to: ' +
176
+ '1) View currently executing queries, 2) Track query progress and resource usage, ' +
177
+ '3) Identify long-running or blocked queries, 4) Debug performance issues in real-time. ' +
178
+ 'The tool provides detailed visibility into database activity with optional verbose output.', pgPsOptionsSchema.shape, async (options) => {
179
+ const command = new CommandBuilder(TOOL_COMMAND_MAP.PG_PS)
180
+ .addFlags({
181
+ app: options.app,
182
+ verbose: options.verbose
183
+ })
184
+ .addPositionalArguments({ database: options.database })
185
+ .build();
186
+ const output = await herokuRepl.executeCommand(command);
187
+ return handleCliOutput(output);
188
+ });
189
+ };
190
+ /**
191
+ * Registers the pg:locks tool with the MCP server.
192
+ *
193
+ * @param server - The MCP server instance to register the tool with
194
+ * @param herokuRepl - The Heroku REPL instance for executing commands
195
+ */
196
+ export const registerPgLocksTool = (server, herokuRepl) => {
197
+ server.tool('pg_locks', 'Analyze database locks and blocking transactions. Use this tool when you need to: ' +
198
+ '1) Identify blocked queries and lock chains, 2) Investigate deadlock situations, ' +
199
+ '3) Monitor transaction lock states, 4) Resolve blocking issues affecting performance. ' +
200
+ 'The tool helps diagnose and resolve database concurrency problems.', pgLocksOptionsSchema.shape, async (options) => {
201
+ const command = new CommandBuilder(TOOL_COMMAND_MAP.PG_LOCKS)
202
+ .addFlags({
203
+ app: options.app,
204
+ truncate: options.truncate
205
+ })
206
+ .addPositionalArguments({ database: options.database })
207
+ .build();
208
+ const output = await herokuRepl.executeCommand(command);
209
+ return handleCliOutput(output);
210
+ });
211
+ };
212
+ /**
213
+ * Registers the pg:outliers tool with the MCP server.
214
+ *
215
+ * @param server - The MCP server instance to register the tool with
216
+ * @param herokuRepl - The Heroku REPL instance for executing commands
217
+ */
218
+ export const registerPgOutliersTool = (server, herokuRepl) => {
219
+ server.tool('pg_outliers', 'Identify resource-intensive database operations. Use this tool when you need to: ' +
220
+ '1) Find slow or expensive queries, 2) Analyze query performance patterns, ' +
221
+ '3) Optimize database workload, 4) Track query statistics over time. ' +
222
+ 'The tool helps identify opportunities for performance optimization.', pgOutliersOptionsSchema.shape, async (options) => {
223
+ const command = new CommandBuilder(TOOL_COMMAND_MAP.PG_OUTLIERS)
224
+ .addFlags({
225
+ app: options.app,
226
+ num: options.num?.toString(),
227
+ reset: options.reset,
228
+ truncate: options.truncate
229
+ })
230
+ .addPositionalArguments({ database: options.database })
231
+ .build();
232
+ const output = await herokuRepl.executeCommand(command);
233
+ return handleCliOutput(output);
234
+ });
235
+ };
236
+ /**
237
+ * Registers the pg:credentials tool with the MCP server.
238
+ *
239
+ * @param server - The MCP server instance to register the tool with
240
+ * @param herokuRepl - The Heroku REPL instance for executing commands
241
+ */
242
+ export const registerPgCredentialsTool = (server, herokuRepl) => {
243
+ server.tool('pg_credentials', 'Manage database access credentials and security. Use this tool when you need to: ' +
244
+ '1) View current database credentials, 2) Configure database access permissions, ' +
245
+ '3) Rotate credentials for security compliance, 4) Set up monitoring access. ' +
246
+ 'The tool helps maintain secure database access and credential management.', pgCredentialsOptionsSchema.shape, async (options) => {
247
+ const command = new CommandBuilder(TOOL_COMMAND_MAP.PG_CREDENTIALS)
248
+ .addFlags({
249
+ app: options.app
250
+ })
251
+ .addPositionalArguments({ database: options.database })
252
+ .build();
253
+ const output = await herokuRepl.executeCommand(command);
254
+ return handleCliOutput(output);
255
+ });
256
+ };
257
+ /**
258
+ * Registers the pg:kill tool with the MCP server.
259
+ *
260
+ * @param server - The MCP server instance to register the tool with
261
+ * @param herokuRepl - The Heroku REPL instance for executing commands
262
+ */
263
+ export const registerPgKillTool = (server, herokuRepl) => {
264
+ server.tool('pg_kill', 'Terminate specific database processes. Use this tool when you need to: ' +
265
+ '1) Stop problematic or stuck queries, 2) Clear blocking transactions, ' +
266
+ '3) Manage resource-intensive operations, 4) Handle runaway processes safely. ' +
267
+ 'The tool provides controlled process termination with optional force mode.', pgKillOptionsSchema.shape, async (options) => {
268
+ const command = new CommandBuilder(TOOL_COMMAND_MAP.PG_KILL)
269
+ .addFlags({
270
+ app: options.app,
271
+ force: options.force
272
+ })
273
+ .addPositionalArguments({
274
+ pid: options.pid.toString(),
275
+ database: options.database
276
+ })
277
+ .build();
278
+ const output = await herokuRepl.executeCommand(command);
279
+ return handleCliOutput(output);
280
+ });
281
+ };
282
+ /**
283
+ * Registers the pg:maintenance tool with the MCP server.
284
+ *
285
+ * @param server - The MCP server instance to register the tool with
286
+ * @param herokuRepl - The Heroku REPL instance for executing commands
287
+ */
288
+ export const registerPgMaintenanceTool = (server, herokuRepl) => {
289
+ server.tool('pg_maintenance', 'Monitor database maintenance status and operations. Use this tool when you need to: ' +
290
+ '1) Check current maintenance windows, 2) View scheduled maintenance activities, ' +
291
+ '3) Track maintenance operation progress, 4) Plan database maintenance tasks. ' +
292
+ 'The tool provides visibility into database maintenance state and scheduling.', pgMaintenanceOptionsSchema.shape, async (options) => {
293
+ const command = new CommandBuilder(TOOL_COMMAND_MAP.PG_MAINTENANCE)
294
+ .addFlags({
295
+ app: options.app
296
+ })
297
+ .addPositionalArguments({ database: options.database })
298
+ .build();
299
+ const output = await herokuRepl.executeCommand(command);
300
+ return handleCliOutput(output);
301
+ });
302
+ };
303
+ /**
304
+ * Registers the pg:backups tool with the MCP server.
305
+ *
306
+ * @param server - The MCP server instance to register the tool with
307
+ * @param herokuRepl - The Heroku REPL instance for executing commands
308
+ */
309
+ export const registerPgBackupsTool = (server, herokuRepl) => {
310
+ server.tool('pg_backups', 'Manage database backup operations and schedules. Use this tool when you need to: ' +
311
+ '1) View existing database backups, 2) Monitor backup schedules and status, ' +
312
+ '3) Track backup operation progress, 4) Verify backup availability. ' +
313
+ 'The tool helps maintain database backup operations and disaster recovery readiness.', pgBackupsOptionsSchema.shape, async (options) => {
314
+ const command = new CommandBuilder(TOOL_COMMAND_MAP.PG_BACKUPS)
315
+ .addFlags({
316
+ app: options.app
317
+ })
318
+ .build();
319
+ const output = await herokuRepl.executeCommand(command);
320
+ return handleCliOutput(output);
321
+ });
322
+ };
323
+ /**
324
+ * Registers the pg:upgrade tool with the MCP server.
325
+ *
326
+ * @param server - The MCP server instance to register the tool with
327
+ * @param herokuRepl - The Heroku REPL instance for executing commands
328
+ */
329
+ export const registerPgUpgradeTool = (server, herokuRepl) => {
330
+ server.tool('pg_upgrade', 'Upgrade PostgreSQL database version. Use this tool when you need to: ' +
331
+ '1) Migrate to a newer PostgreSQL version, 2) Plan version upgrade paths, ' +
332
+ '3) Execute controlled version migrations, 4) Verify upgrade compatibility. ' +
333
+ 'The tool manages safe database version upgrades with confirmation protection.', pgUpgradeOptionsSchema.shape, async (options) => {
334
+ const command = new CommandBuilder(TOOL_COMMAND_MAP.PG_UPGRADE)
335
+ .addFlags({
336
+ app: options.app,
337
+ version: options.version,
338
+ confirm: options.confirm
339
+ })
340
+ .addPositionalArguments({ database: options.database })
341
+ .build();
342
+ const output = await herokuRepl.executeCommand(command);
343
+ return handleCliOutput(output);
344
+ });
345
+ };
@@ -0,0 +1,300 @@
1
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
+ import { HerokuREPL } from '../repl/heroku-cli-repl.js';
3
+ import { TOOL_COMMAND_MAP } from '../utils/tool-commands.js';
4
+ import { registerPgPsqlTool, registerPgInfoTool, registerPgPsTool, registerPgKillTool, registerPgBackupsTool, registerPgUpgradeTool } from './data.js';
5
+ import { expect } from 'chai';
6
+ import sinon from 'sinon';
7
+ describe('PostgreSQL Tools', () => {
8
+ let server;
9
+ let herokuRepl;
10
+ let toolCallback;
11
+ beforeEach(() => {
12
+ server = sinon.createStubInstance(McpServer);
13
+ herokuRepl = sinon.createStubInstance(HerokuREPL);
14
+ server.tool.callsFake((_name, _description, _schema, callback) => {
15
+ toolCallback = callback;
16
+ return server;
17
+ });
18
+ });
19
+ afterEach(() => {
20
+ sinon.restore();
21
+ });
22
+ describe('pg:psql', () => {
23
+ beforeEach(() => {
24
+ registerPgPsqlTool(server, herokuRepl);
25
+ });
26
+ it('should register the tool with correct parameters', () => {
27
+ expect(server.tool.calledOnce).to.be.true;
28
+ expect(server.tool.firstCall.args[0]).to.equal('pg_psql');
29
+ expect(server.tool.firstCall.args[1]).to.be.a('string');
30
+ expect(server.tool.firstCall.args[2]).to.be.an('object');
31
+ expect(server.tool.firstCall.args[3]).to.be.a('function');
32
+ });
33
+ it('should build correct command with required parameters', async () => {
34
+ herokuRepl.executeCommand.resolves('psql (14.9)\nType "help" for help.\n\nmyapp::DATABASE=> ');
35
+ await toolCallback({ app: 'myapp' });
36
+ expect(herokuRepl.executeCommand.calledOnce).to.be.true;
37
+ expect(herokuRepl.executeCommand.firstCall.args[0]).to.equal(`${TOOL_COMMAND_MAP.PG_PSQL} --app=myapp --command=""`);
38
+ });
39
+ it('should build correct command with all parameters', async () => {
40
+ herokuRepl.executeCommand.resolves('Query executed successfully\n');
41
+ await toolCallback({
42
+ app: 'myapp',
43
+ command: 'SELECT * FROM users',
44
+ file: 'query.sql',
45
+ credential: 'mycred',
46
+ database: 'HEROKU_POSTGRESQL_RED'
47
+ });
48
+ expect(herokuRepl.executeCommand.calledOnce).to.be.true;
49
+ expect(herokuRepl.executeCommand.firstCall.args[0]).to.equal(`${TOOL_COMMAND_MAP.PG_PSQL} --app=myapp --command="SELECT * FROM users" --file=query.sql --credential=mycred -- HEROKU_POSTGRESQL_RED`);
50
+ });
51
+ it('should handle error response', async () => {
52
+ const errorResponse = '<<<ERROR>>>\nError: Database not found\n<<<END ERROR>>>\n';
53
+ herokuRepl.executeCommand.resolves(errorResponse);
54
+ const result = await toolCallback({ app: 'myapp' });
55
+ expect(result).to.deep.equal({
56
+ content: [
57
+ {
58
+ type: 'text',
59
+ text: '[Heroku MCP Server Error] Please use available tools to resolve this issue. Ignore any Heroku CLI command suggestions that may be provided in the command output or error details. Details:\n<<<ERROR>>>\nError: Database not found\n<<<END ERROR>>>\n'
60
+ }
61
+ ],
62
+ isError: true
63
+ });
64
+ });
65
+ });
66
+ describe('pg:info', () => {
67
+ beforeEach(() => {
68
+ registerPgInfoTool(server, herokuRepl);
69
+ });
70
+ it('should register the tool with correct parameters', () => {
71
+ expect(server.tool.calledOnce).to.be.true;
72
+ expect(server.tool.firstCall.args[0]).to.equal('pg_info');
73
+ expect(server.tool.firstCall.args[1]).to.be.a('string');
74
+ expect(server.tool.firstCall.args[2]).to.be.an('object');
75
+ expect(server.tool.firstCall.args[3]).to.be.a('function');
76
+ });
77
+ it('should build correct command with required parameters', async () => {
78
+ const successResponse = '=== DATABASE_URL\nPlan: Hobby-dev\nStatus: Available\nConnections: 0/20\nPG Version: 14.9\n';
79
+ herokuRepl.executeCommand.resolves(successResponse);
80
+ await toolCallback({ app: 'myapp' });
81
+ expect(herokuRepl.executeCommand.calledOnce).to.be.true;
82
+ expect(herokuRepl.executeCommand.firstCall.args[0]).to.equal(`${TOOL_COMMAND_MAP.PG_INFO} --app=myapp`);
83
+ });
84
+ it('should build correct command with database parameter', async () => {
85
+ const successResponse = '=== HEROKU_POSTGRESQL_RED\nPlan: Standard-0\nStatus: Available\n';
86
+ herokuRepl.executeCommand.resolves(successResponse);
87
+ await toolCallback({ app: 'myapp', database: 'HEROKU_POSTGRESQL_RED' });
88
+ expect(herokuRepl.executeCommand.calledOnce).to.be.true;
89
+ expect(herokuRepl.executeCommand.firstCall.args[0]).to.equal(`${TOOL_COMMAND_MAP.PG_INFO} --app=myapp -- HEROKU_POSTGRESQL_RED`);
90
+ });
91
+ it('should handle error response', async () => {
92
+ const errorResponse = '<<<ERROR>>>\nError: Database not found\n<<<END ERROR>>>\n';
93
+ herokuRepl.executeCommand.resolves(errorResponse);
94
+ const result = await toolCallback({ app: 'myapp' });
95
+ expect(result).to.deep.equal({
96
+ content: [
97
+ {
98
+ type: 'text',
99
+ text: '[Heroku MCP Server Error] Please use available tools to resolve this issue. Ignore any Heroku CLI command suggestions that may be provided in the command output or error details. Details:\n<<<ERROR>>>\nError: Database not found\n<<<END ERROR>>>\n'
100
+ }
101
+ ],
102
+ isError: true
103
+ });
104
+ });
105
+ });
106
+ describe('pg:ps', () => {
107
+ beforeEach(() => {
108
+ registerPgPsTool(server, herokuRepl);
109
+ });
110
+ it('should register the tool with correct parameters', () => {
111
+ expect(server.tool.calledOnce).to.be.true;
112
+ expect(server.tool.firstCall.args[0]).to.equal('pg_ps');
113
+ expect(server.tool.firstCall.args[1]).to.be.a('string');
114
+ expect(server.tool.firstCall.args[2]).to.be.an('object');
115
+ expect(server.tool.firstCall.args[3]).to.be.a('function');
116
+ });
117
+ it('should build correct command with required parameters', async () => {
118
+ const successResponse = ' pid | state | source | running_for | waiting | query\n-----+-------+--------+-------------+---------+-------\n';
119
+ herokuRepl.executeCommand.resolves(successResponse);
120
+ await toolCallback({ app: 'myapp' });
121
+ expect(herokuRepl.executeCommand.calledOnce).to.be.true;
122
+ expect(herokuRepl.executeCommand.firstCall.args[0]).to.equal(`${TOOL_COMMAND_MAP.PG_PS} --app=myapp`);
123
+ });
124
+ it('should build correct command with all parameters', async () => {
125
+ const successResponse = ' pid | state | source | running_for | waiting | query | memory\n-----+-------+--------+-------------+---------+-------+--------\n';
126
+ herokuRepl.executeCommand.resolves(successResponse);
127
+ await toolCallback({
128
+ app: 'myapp',
129
+ verbose: true,
130
+ database: 'HEROKU_POSTGRESQL_RED'
131
+ });
132
+ expect(herokuRepl.executeCommand.calledOnce).to.be.true;
133
+ expect(herokuRepl.executeCommand.firstCall.args[0]).to.equal(`${TOOL_COMMAND_MAP.PG_PS} --app=myapp --verbose -- HEROKU_POSTGRESQL_RED`);
134
+ });
135
+ it('should handle error response', async () => {
136
+ const errorResponse = '<<<ERROR>>>\nError: Database not found\n<<<END ERROR>>>\n';
137
+ herokuRepl.executeCommand.resolves(errorResponse);
138
+ const result = await toolCallback({ app: 'myapp' });
139
+ expect(result).to.deep.equal({
140
+ content: [
141
+ {
142
+ type: 'text',
143
+ text: '[Heroku MCP Server Error] Please use available tools to resolve this issue. Ignore any Heroku CLI command suggestions that may be provided in the command output or error details. Details:\n<<<ERROR>>>\nError: Database not found\n<<<END ERROR>>>\n'
144
+ }
145
+ ],
146
+ isError: true
147
+ });
148
+ });
149
+ });
150
+ describe('pg:kill', () => {
151
+ beforeEach(() => {
152
+ registerPgKillTool(server, herokuRepl);
153
+ });
154
+ it('should register the tool with correct parameters', () => {
155
+ expect(server.tool.calledOnce).to.be.true;
156
+ expect(server.tool.firstCall.args[0]).to.equal('pg_kill');
157
+ expect(server.tool.firstCall.args[1]).to.be.a('string');
158
+ expect(server.tool.firstCall.args[2]).to.be.an('object');
159
+ expect(server.tool.firstCall.args[3]).to.be.a('function');
160
+ });
161
+ it('should build correct command with required parameters', async () => {
162
+ const successResponse = 'Terminating process 12345... done\n';
163
+ herokuRepl.executeCommand.resolves(successResponse);
164
+ await toolCallback({ app: 'myapp', pid: 12345 });
165
+ expect(herokuRepl.executeCommand.calledOnce).to.be.true;
166
+ expect(herokuRepl.executeCommand.firstCall.args[0]).to.equal(`${TOOL_COMMAND_MAP.PG_KILL} --app=myapp -- 12345`);
167
+ });
168
+ it('should build correct command with all parameters', async () => {
169
+ const successResponse = 'Terminating process 12345... done\n';
170
+ herokuRepl.executeCommand.resolves(successResponse);
171
+ await toolCallback({
172
+ app: 'myapp',
173
+ pid: 12345,
174
+ force: true,
175
+ database: 'HEROKU_POSTGRESQL_RED'
176
+ });
177
+ expect(herokuRepl.executeCommand.calledOnce).to.be.true;
178
+ expect(herokuRepl.executeCommand.firstCall.args[0]).to.equal(`${TOOL_COMMAND_MAP.PG_KILL} --app=myapp --force -- 12345 HEROKU_POSTGRESQL_RED`);
179
+ });
180
+ it('should handle error response', async () => {
181
+ const errorResponse = '<<<ERROR>>>\nError: Process not found\n<<<END ERROR>>>\n';
182
+ herokuRepl.executeCommand.resolves(errorResponse);
183
+ const result = await toolCallback({ app: 'myapp', pid: 12345 });
184
+ expect(result).to.deep.equal({
185
+ content: [
186
+ {
187
+ type: 'text',
188
+ text: '[Heroku MCP Server Error] Please use available tools to resolve this issue. Ignore any Heroku CLI command suggestions that may be provided in the command output or error details. Details:\n<<<ERROR>>>\nError: Process not found\n<<<END ERROR>>>\n'
189
+ }
190
+ ],
191
+ isError: true
192
+ });
193
+ });
194
+ });
195
+ describe('pg:backups', () => {
196
+ beforeEach(() => {
197
+ registerPgBackupsTool(server, herokuRepl);
198
+ });
199
+ it('should register the tool with correct parameters', () => {
200
+ expect(server.tool.calledOnce).to.be.true;
201
+ expect(server.tool.firstCall.args[0]).to.equal('pg_backups');
202
+ expect(server.tool.firstCall.args[1]).to.be.a('string');
203
+ expect(server.tool.firstCall.args[2]).to.be.an('object');
204
+ expect(server.tool.firstCall.args[3]).to.be.a('function');
205
+ });
206
+ it('should build correct command with required parameters', async () => {
207
+ const successResponse = '=== Backups\nID Created at Status Size Database\n──── ───────────────────────── ─────────────────────────────────── ─────── ────────\nb001 2024-01-01 00:00:00 +0000 Completed 2024-01-01 00:01:00 +0000 20.3 MB DATABASE_URL\n';
208
+ herokuRepl.executeCommand.resolves(successResponse);
209
+ await toolCallback({ app: 'myapp' });
210
+ expect(herokuRepl.executeCommand.calledOnce).to.be.true;
211
+ expect(herokuRepl.executeCommand.firstCall.args[0]).to.equal(`${TOOL_COMMAND_MAP.PG_BACKUPS} --app=myapp`);
212
+ });
213
+ it('should build correct command with all parameters', async () => {
214
+ const successResponse = 'Scheduling automatic daily backups at 00:00 America/New_York... done\n';
215
+ herokuRepl.executeCommand.resolves(successResponse);
216
+ await toolCallback({
217
+ app: 'myapp',
218
+ verbose: true,
219
+ at: '00:00 America/New_York',
220
+ output: 'json',
221
+ 'wait-interval': '5',
222
+ quiet: true
223
+ });
224
+ expect(herokuRepl.executeCommand.calledOnce).to.be.true;
225
+ expect(herokuRepl.executeCommand.firstCall.args[0]).to.equal(`${TOOL_COMMAND_MAP.PG_BACKUPS} --app=myapp`);
226
+ });
227
+ it('should handle error response', async () => {
228
+ const errorResponse = '<<<ERROR>>>\nError: No backups found\n<<<END ERROR>>>\n';
229
+ herokuRepl.executeCommand.resolves(errorResponse);
230
+ const result = await toolCallback({ app: 'myapp' });
231
+ expect(result).to.deep.equal({
232
+ content: [
233
+ {
234
+ type: 'text',
235
+ text: '[Heroku MCP Server Error] Please use available tools to resolve this issue. Ignore any Heroku CLI command suggestions that may be provided in the command output or error details. Details:\n<<<ERROR>>>\nError: No backups found\n<<<END ERROR>>>\n'
236
+ }
237
+ ],
238
+ isError: true
239
+ });
240
+ });
241
+ });
242
+ describe('pg:upgrade', () => {
243
+ beforeEach(() => {
244
+ registerPgUpgradeTool(server, herokuRepl);
245
+ });
246
+ it('should register the tool with correct parameters', () => {
247
+ expect(server.tool.calledOnce).to.be.true;
248
+ expect(server.tool.firstCall.args[0]).to.equal('pg_upgrade');
249
+ expect(server.tool.firstCall.args[1]).to.be.a('string');
250
+ expect(server.tool.firstCall.args[2]).to.be.an('object');
251
+ expect(server.tool.firstCall.args[3]).to.be.a('function');
252
+ });
253
+ it('should build correct command with required parameters', async () => {
254
+ const successResponse = 'Starting upgrade of DATABASE_URL to PostgreSQL 14... done\n';
255
+ herokuRepl.executeCommand.resolves(successResponse);
256
+ await toolCallback({ app: 'myapp' });
257
+ expect(herokuRepl.executeCommand.calledOnce).to.be.true;
258
+ expect(herokuRepl.executeCommand.firstCall.args[0]).to.equal(`${TOOL_COMMAND_MAP.PG_UPGRADE} --app=myapp`);
259
+ });
260
+ it('should build correct command with all parameters', async () => {
261
+ const successResponse = 'Starting upgrade of HEROKU_POSTGRESQL_RED to PostgreSQL 14... done\n';
262
+ herokuRepl.executeCommand.resolves(successResponse);
263
+ await toolCallback({
264
+ app: 'myapp',
265
+ version: '14',
266
+ confirm: 'myapp',
267
+ database: 'HEROKU_POSTGRESQL_RED'
268
+ });
269
+ expect(herokuRepl.executeCommand.calledOnce).to.be.true;
270
+ expect(herokuRepl.executeCommand.firstCall.args[0]).to.equal(`${TOOL_COMMAND_MAP.PG_UPGRADE} --app=myapp --version=14 --confirm=myapp -- HEROKU_POSTGRESQL_RED`);
271
+ });
272
+ it('should handle error response', async () => {
273
+ const errorResponse = '<<<ERROR>>>\nError: Database is already at version 14\n<<<END ERROR>>>\n';
274
+ herokuRepl.executeCommand.resolves(errorResponse);
275
+ const result = await toolCallback({ app: 'myapp' });
276
+ expect(result).to.deep.equal({
277
+ content: [
278
+ {
279
+ type: 'text',
280
+ text: '[Heroku MCP Server Error] Please use available tools to resolve this issue. Ignore any Heroku CLI command suggestions that may be provided in the command output or error details. Details:\n<<<ERROR>>>\nError: Database is already at version 14\n<<<END ERROR>>>\n'
281
+ }
282
+ ],
283
+ isError: true
284
+ });
285
+ });
286
+ it('should handle undefined response', async () => {
287
+ herokuRepl.executeCommand.resolves(undefined);
288
+ const result = await toolCallback({ app: 'myapp' });
289
+ expect(result).to.deep.equal({
290
+ content: [
291
+ {
292
+ type: 'text',
293
+ text: '[Heroku MCP Server Error] Please use available tools to resolve this issue. Ignore any Heroku CLI command suggestions that may be provided in the command output or error details. Details:\nNo response from command'
294
+ }
295
+ ],
296
+ isError: true
297
+ });
298
+ });
299
+ });
300
+ });